Skip to content

Commit

Permalink
Chores (swc-project#54)
Browse files Browse the repository at this point in the history
 - improve readme
 - fix lints
 - Enable lto
 - better codegen (newline after stmts)
 - add some docs

 - implement `**=` transformer

 - use `quote_ident!`
 - use `.as_arg()` instead of constructing ExprOrSpread
 - use `as_callee()`
 - inject helpers

 - fix imports

 - split libswc

 - don't mark! useless span
  • Loading branch information
kdy1 authored Nov 15, 2018
1 parent 5cf15cb commit fae7bfc
Show file tree
Hide file tree
Showing 29 changed files with 338 additions and 182 deletions.
13 changes: 5 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,8 @@ authors = ["강동윤 <[email protected]>"]
[[bin]]
name = "swc"

[lib]
name = "swc"

[dependencies]
swc_atoms = { path = "./atoms" }
swc_common = { path = "./common" }
swc_ecmascript = { path = "./ecmascript" }
swc_macros = { path = "./macros" }
libswc = { path = "./libswc" }
rayon = "1.0.3"
slog = "2"
slog-envlogger = "2.1"
Expand All @@ -28,6 +22,9 @@ features = [ "suggestions", "color" ]


[profile.bench]
debug = true
lto = true

[profile.release]
lto = true

[patch.crates-io]
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,29 @@ swc is rust port of [babel][] and [closure compiler][].
Requires nightly version of [rust][].

```sh
cargo install --git https://github.com/swc-project/swc.git
RUSTFLAGS='--cfg procmacro2_semver_exempt' cargo install --git https://github.com/swc-project/swc.git
```

# Features

## Transforms
New generation javascript to old-days javascript.

- es3
- [x] member expression literals
- [x] peroperty literals
- [ ] reserved words

- es2015
- [x] classes
- [x] shorthand property
- [x] spread element
- [x] sticky regex (`y` flag)

- es2016
- [x] operator `**`


# Usage

`test.js`:
Expand Down
9 changes: 5 additions & 4 deletions ecmascript/codegen/src/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,11 @@ impl<'a> Emitter<'a> {
// include `//`
ext_sp = ext_sp.with_lo(ext_sp.lo() - BytePos(3));

let src = self
.cm
.span_to_snippet(ext_sp)
.expect("failed to get snippet for span");
let src = match self.cm.span_to_snippet(ext_sp) {
Ok(src) => src,
// TODO: Handle error
Err(e) => return Ok(()),
};
// if src == "" {
// return Ok(());
// }
Expand Down
4 changes: 3 additions & 1 deletion ecmascript/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,7 +1157,7 @@ impl<'a> Emitter<'a> {
match *node {
Stmt::Expr(ref e) => {
emit!(e);
semi!()
semi!();
}
Stmt::Block(ref e) => emit!(e),
Stmt::Empty(ref e) => emit!(e),
Expand All @@ -1178,6 +1178,7 @@ impl<'a> Emitter<'a> {
Stmt::ForOf(ref e) => emit!(e),
Stmt::Decl(ref e) => emit!(e),
}
self.wr.write_line()?;
}

#[emitter]
Expand Down Expand Up @@ -1448,6 +1449,7 @@ fn get_text_of_node<T: Spanned>(
node: &T,
_include_travia: bool,
) -> Option<String> {
return None;
let span = node.span();
if span.is_dummy() || span.ctxt() != SyntaxContext::empty() {
// This node is transformed so we shoukld not use original source code.
Expand Down
2 changes: 1 addition & 1 deletion ecmascript/parser/src/parser/stmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<'a, I: Input> Parser<'a, I> {
Ok(stmts)
}

fn parse_stmt(&mut self, top_level: bool) -> PResult<'a, Stmt> {
pub fn parse_stmt(&mut self, top_level: bool) -> PResult<'a, Stmt> {
self.parse_stmt_internal(false, top_level)
}

Expand Down
4 changes: 2 additions & 2 deletions ecmascript/transforms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ authors = ["강동윤 <[email protected]>"]
swc_atoms = { path = "../../atoms" }
swc_common = { path = "../../common" }
swc_ecma_ast = { path = "../ast" }
swc_ecma_parser = { path = "../parser" }
slog = "2"

[dev-dependencies]
testing = { path = "../../testing" }
swc_ecma_codegen = { path = "../codegen" }
swc_ecma_parser = { path = "../parser" }
pretty_assertions = "0.5"
slog = "2"
15 changes: 5 additions & 10 deletions ecmascript/transforms/src/compat/es2015/classes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::Helpers;
use crate::util::ExprFactory;
use crate::{compat::helpers::Helpers, util::ExprFactory};
use std::{
iter,
sync::{atomic::Ordering, Arc},
Expand Down Expand Up @@ -140,7 +139,7 @@ impl Classes {

Expr::Call(CallExpr {
span: DUMMY_SP,
callee: ExprOrSuper::Expr(box Expr::Fn(FnExpr {
callee: Expr::Fn(FnExpr {
ident: None,
function: Function {
span: DUMMY_SP,
Expand All @@ -149,7 +148,8 @@ impl Classes {
params,
body,
},
})),
})
.as_callee(),
args,
})
}
Expand Down Expand Up @@ -208,10 +208,7 @@ impl Classes {
// inject _classCallCheck(this, Bar);
function.body.stmts = iter::once(Stmt::Expr(box Expr::Call(CallExpr {
span: DUMMY_SP,
callee: ExprOrSuper::Expr(box Expr::Ident(Ident::new(
"_classCallCheck".into(),
DUMMY_SP,
))),
callee: Expr::Ident(quote_ident!("_classCallCheck")).as_callee(),
args: vec![
Expr::This(ThisExpr { span: DUMMY_SP }).as_arg(),
Expr::Ident(class_name.clone()).as_arg(),
Expand Down Expand Up @@ -382,8 +379,6 @@ impl Classes {
let (mut props, mut static_props) = (vec![], vec![]);

for m in methods {
let span = mark!(m.span());

let prop_name = match m.key {
PropName::Ident(ref i) => i,
_ => unimplemented!("non-ident prop name: {:?}", m.key),
Expand Down
7 changes: 2 additions & 5 deletions ecmascript/transforms/src/compat/es2015/shorthand_property.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,8 @@ impl Fold<Prop> for Shorthand {
let span = mark!(span);

Prop::KeyValue(KeyValueProp {
key: PropName::Ident(Ident {
sym: sym.clone(),
span,
}),
value: box Ident { sym, span }.into(),
key: PropName::Ident(quote_ident!(span, sym.clone())),
value: box quote_ident!(span, sym).into(),
})
}
Prop::Method(MethodProp { key, function }) => Prop::KeyValue(KeyValueProp {
Expand Down
32 changes: 20 additions & 12 deletions ecmascript/transforms/src/compat/es2015/spread.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::compat::helpers::Helpers;
use crate::{compat::helpers::Helpers, util::ExprFactory};
use std::{
mem,
sync::{atomic::Ordering, Arc},
};
use swc_common::{Fold, FoldWith, Span};
use swc_common::{Fold, FoldWith, Span, DUMMY_SP};
use swc_ecma_ast::*;
use util::ExprFactory;

/// es2015 - `SpreadElement`
#[derive(Debug, Clone, Default)]
Expand All @@ -18,7 +17,17 @@ impl Fold<Expr> for SpreadElement {
let e = e.fold_children(self);

match e {
Expr::Array(ArrayLit { .. }) => unimplemented!("Rest element"),
Expr::Array(ArrayLit { ref elems, .. }) => {
if elems.iter().any(|e| match e {
Some(ExprOrSpread {
spread: Some(_), ..
}) => true,
_ => false,
}) {
unimplemented!("Rest element in arrat literal")
}
return e;
}
Expr::Call(CallExpr {
callee: ExprOrSuper::Expr(callee),
args,
Expand All @@ -40,7 +49,7 @@ impl Fold<Expr> for SpreadElement {
//
// f.apply(undefined, args)
//
callee.apply(span, expr!(span, undefined), vec![args_array.as_arg()])
callee.apply(span, expr!(DUMMY_SP, undefined), vec![args_array.as_arg()])
}
Expr::New(NewExpr {
callee,
Expand Down Expand Up @@ -124,15 +133,13 @@ fn concat_args(helpers: &Helpers, span: Span, args: Vec<ExprOrSpread>) -> Expr {
buf.push(
Expr::Call(CallExpr {
span,
callee: ExprOrSuper::Expr(
box Ident::new(js_word!("_toConsumableArray"), span).into(),
),
args: vec![ExprOrSpread { expr, spread: None }],
callee: quote_ident!("_toConsumableArray").as_callee(),
args: vec![expr.as_arg()],
})
.as_arg(),
);
}
None => tmp_arr.push(Some(ExprOrSpread { expr, spread: None })),
None => tmp_arr.push(Some(expr.as_arg())),
}
}
make_arr!();
Expand All @@ -141,7 +148,7 @@ fn concat_args(helpers: &Helpers, span: Span, args: Vec<ExprOrSpread>) -> Expr {
// TODO
span,

callee: ExprOrSuper::Expr(box Expr::Member(MemberExpr {
callee: MemberExpr {
// TODO: Mark
span,
prop: box Expr::Ident(Ident::new(js_word!("concat"), span)),
Expand All @@ -156,7 +163,8 @@ fn concat_args(helpers: &Helpers, span: Span, args: Vec<ExprOrSpread>) -> Expr {
})
})),
computed: false,
})),
}
.as_callee(),

args: buf,
})
Expand Down
22 changes: 7 additions & 15 deletions ecmascript/transforms/src/compat/es2015/sticky_regex.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::util::ExprFactory;
use std::iter;
use swc_common::{Fold, FoldWith};
use swc_ecma_ast::*;

Expand Down Expand Up @@ -33,22 +35,12 @@ impl Fold<Expr> for StickyRegex {
let span = mark!(span);

return Expr::New(NewExpr {
callee: box Ident {
span,
sym: js_word!("RegExp"),
}
.into(),
callee: box quote_ident!(span, "RegExp").into(),
args: Some(
vec![ExprOrSpread {
expr: str_lit(exp),
spread: None,
}]
.into_iter()
.chain(flags.map(|flags| ExprOrSpread {
expr: str_lit(flags),
spread: None,
}))
.collect(),
iter::once(str_lit(exp).as_arg())
.into_iter()
.chain(flags.map(|flags| str_lit(flags).as_arg()))
.collect(),
),
span,
});
Expand Down
Loading

0 comments on commit fae7bfc

Please sign in to comment.