Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement module transforms #133

Merged
merged 80 commits into from
Feb 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
80 commits
Select commit Hold shift + click to select a range
0d56eef
Groundwork for module transforamtinos
kdy1 Jan 30, 2019
4001f0b
fnv -> fx && more works
kdy1 Jan 30, 2019
83df651
improve hygiene and resolver (import/export)
kdy1 Jan 31, 2019
ef047bf
[WIP] commonjs
kdy1 Jan 31, 2019
2ffffb5
implement common js imports
kdy1 Jan 31, 2019
a96d21b
implement export * from 'foo'
kdy1 Feb 1, 2019
268b1b1
implement common js export
kdy1 Feb 1, 2019
5b83ca7
implement export default for common js pass
kdy1 Feb 1, 2019
c42b0c7
common js pass: allow exporting default from other sources
kdy1 Feb 1, 2019
3bdcf8c
preserve order
kdy1 Feb 1, 2019
b30e715
enable wildcard import for export * from 'foo';
kdy1 Feb 1, 2019
c9d31bf
improve `export { foo as default };`
kdy1 Feb 1, 2019
d4d04ff
rename a test
kdy1 Feb 1, 2019
c299c87
improve fixer (exports.default = exports.bar = void 0)
kdy1 Feb 1, 2019
da3dc19
initialize exported fields to void 0
kdy1 Feb 1, 2019
d035114
allow exporting all and default from same source
kdy1 Feb 1, 2019
3259c7b
Define _exportNames only if required
kdy1 Feb 1, 2019
0cfb283
handle simple variables
kdy1 Feb 1, 2019
9ad060f
minor improvement for a test
kdy1 Feb 1, 2019
0a3354d
Collect declared variables, and initilaize exports to undefined befor…
kdy1 Feb 1, 2019
2ffad70
fix a test
kdy1 Feb 1, 2019
992475b
Track exported variables
kdy1 Feb 2, 2019
800ec39
Mute resolver
kdy1 Feb 2, 2019
2a7af96
Use js_word! instead of &*sym
kdy1 Feb 2, 2019
d73d862
fix a test
kdy1 Feb 2, 2019
7defaf4
fix a test (interop_exports_variable)
kdy1 Feb 2, 2019
6ddc3c5
Use hoisting for export functions
kdy1 Feb 2, 2019
cbc9528
add a custom test
kdy1 Feb 2, 2019
9cfaf76
improve fixer (object assign in comma expression)
kdy1 Feb 2, 2019
afc6ec3
support updating exported varaibles with pattern
kdy1 Feb 2, 2019
7569c37
fix a test (exporting named function)
kdy1 Feb 2, 2019
baf0917
handle var exports correctly
kdy1 Feb 2, 2019
665775d
Don't define `esModule` in strict mode
kdy1 Feb 2, 2019
c3398c1
Allow exporting variables with pattern
kdy1 Feb 2, 2019
1bbbcae
fix `export {} from 'foo';`
kdy1 Feb 2, 2019
647e8af
Cast to number by prefixing with + when for ++ / -- operators
kdy1 Feb 2, 2019
611ecd7
Add no_interop option
kdy1 Feb 2, 2019
e15331a
remove dbg!
kdy1 Feb 2, 2019
5d78c8f
implement no interop
kdy1 Feb 2, 2019
26be6df
fix an interop test
kdy1 Feb 2, 2019
85e2244
Lazy option
kdy1 Feb 3, 2019
68bf240
implement lazy import
kdy1 Feb 3, 2019
ba0ef62
implement lazy blacklist
kdy1 Feb 3, 2019
14f1ca6
ignore some wrong tests
kdy1 Feb 3, 2019
9329938
fix common_js::tests::lazy_local*
kdy1 Feb 3, 2019
1bbb0a7
fix lazy_whitelist_import_named
kdy1 Feb 3, 2019
bdb6d44
fix lazy_whitelist_import_namespace
kdy1 Feb 3, 2019
ce4bc33
fix(?) lazy_whitelist_reexport_all
kdy1 Feb 3, 2019
6de68be
handle lazy blacklist correctly
kdy1 Feb 3, 2019
3e25a6a
ignore some wrong tests
kdy1 Feb 3, 2019
0abfec0
global this to undefined
kdy1 Feb 3, 2019
c402ddf
ignore a test related to comment
kdy1 Feb 3, 2019
5755197
Add noop pass
kdy1 Feb 3, 2019
58b9852
Rename module tests
kdy1 Feb 3, 2019
a012bc5
Handle state correctly
kdy1 Feb 3, 2019
17773fe
Groundwork for umd
kdy1 Feb 3, 2019
49f8ffa
handle umd imports
kdy1 Feb 4, 2019
e83bf55
Handle import interop
kdy1 Feb 4, 2019
1459f64
fix prepend_stmts
kdy1 Feb 4, 2019
00cb5ae
[WIP] umd imports
kdy1 Feb 4, 2019
e89ff8c
test! macro now takes a closure as argument
kdy1 Feb 4, 2019
3f51173
fix lints
kdy1 Feb 4, 2019
c6c4c01
actual.js -> input.js & expected.js -> output.js
kdy1 Feb 4, 2019
54bb39e
private imports & groundwork for umd config
kdy1 Feb 4, 2019
3b9825f
Move more codes to util
kdy1 Feb 4, 2019
2de22b5
Handle export * / fn / class
kdy1 Feb 4, 2019
56f31a4
init_export!("default") when exporting an expression as default
kdy1 Feb 4, 2019
bd9bc2f
Implement more From<T> for Expr
kdy1 Feb 4, 2019
d6574d0
ExprFactory::member(), computed_member()
kdy1 Feb 4, 2019
143cd37
fixup! ExprFactory::member(), computed_member()
kdy1 Feb 4, 2019
f06e2ef
fixup! ExprFactory::member(), computed_member()
kdy1 Feb 4, 2019
ac33bc5
run resolver before umd
kdy1 Feb 4, 2019
1936609
umd: allow updating exported variables
kdy1 Feb 4, 2019
e50a9a1
common_js: strict_mode
kdy1 Feb 4, 2019
2ffb624
update rustc
kdy1 Feb 4, 2019
b99caac
Remove amd & system_js pass
kdy1 Feb 4, 2019
3944c8e
common_js
kdy1 Feb 5, 2019
723b5d4
fix destructuring
kdy1 Feb 5, 2019
8edc4b7
Reduce compile time (1min 36sec to 1min 21sec)
kdy1 Feb 5, 2019
58a9010
fix computed_props
kdy1 Feb 5, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions atoms/words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,7 @@ debugger
declare
default
delete
displayName
do
else
enum
Expand Down Expand Up @@ -634,6 +635,7 @@ switch
symbol
target
this
this
throw
true
try
Expand Down
6 changes: 6 additions & 0 deletions common/src/syntax_pos/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -522,3 +522,9 @@ pub struct ExpnInfo {
/// resolution.
pub def_site: Option<Span>,
}

impl Default for Mark {
fn default() -> Self {
Mark::root()
}
}
40 changes: 38 additions & 2 deletions ecmascript/ast/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
function::Function,
ident::{Ident, PrivateName},
jsx::{JSXElement, JSXEmptyExpr, JSXFragment, JSXMemberExpr, JSXNamespacedName},
lit::{Lit, Str},
lit::{Bool, Lit, Number, Str},
operators::{AssignOp, BinaryOp, UnaryOp, UpdateOp},
pat::Pat,
prop::Prop,
Expand All @@ -15,7 +15,7 @@ use crate::{
};
#[cfg(feature = "fold")]
use swc_common::Fold;
use swc_common::{ast_node, Span, Spanned};
use swc_common::{ast_node, Span, Spanned, DUMMY_SP};

#[ast_node]
pub enum Expr {
Expand Down Expand Up @@ -315,3 +315,39 @@ pub enum PatOrExpr {
Pat(Box<Pat>),
Expr(Box<Expr>),
}

impl From<bool> for Expr {
fn from(value: bool) -> Self {
Expr::Lit(Lit::Bool(Bool {
span: DUMMY_SP,
value,
}))
}
}

impl From<f64> for Expr {
fn from(value: f64) -> Self {
Expr::Lit(Lit::Num(Number {
span: DUMMY_SP,
value,
}))
}
}

impl From<Bool> for Expr {
fn from(v: Bool) -> Self {
Expr::Lit(Lit::Bool(v))
}
}

impl From<Number> for Expr {
fn from(v: Number) -> Self {
Expr::Lit(Lit::Num(v))
}
}

impl From<Str> for Expr {
fn from(v: Str) -> Self {
Expr::Lit(Lit::Str(v))
}
}
4 changes: 1 addition & 3 deletions ecmascript/ast/src/module_decl.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
decl::{Decl, VarDecl},
decl::Decl,
expr::{ClassExpr, Expr, FnExpr},
ident::Ident,
lit::Str,
Expand Down Expand Up @@ -53,8 +53,6 @@ pub enum ExportDefaultDecl {

Fn(FnExpr),

Var(VarDecl),

TsInterfaceDecl(TsInterfaceDecl),
}

Expand Down
1 change: 0 additions & 1 deletion ecmascript/codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ impl<'a> Emitter<'a> {
match *node {
ExportDefaultDecl::Class(ref n) => emit!(n),
ExportDefaultDecl::Fn(ref n) => emit!(n),
ExportDefaultDecl::Var(ref n) => emit!(n),
ExportDefaultDecl::TsInterfaceDecl(ref n) => emit!(n),
}
semi!();
Expand Down
3 changes: 2 additions & 1 deletion ecmascript/transforms/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ swc_common = { path ="../../common" }
swc_ecma_ast = { path ="../ast" }
swc_ecma_parser = { path ="../parser", features = ["verify"] }
either = "1.5"
fnv = "1"
fxhash = "0.2"
lazy_static = "1"
objekt = "0.1"
serde = { version = "1", features = ["derive"] }
indexmap = "1"
ordered-float = "1.0.1"
Inflector = { version = "0.11.4", default-features = false }

[dev-dependencies]
testing = { path ="../../testing" }
Expand Down
8 changes: 1 addition & 7 deletions ecmascript/transforms/src/compat/es2015/arrow/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,7 @@ impl Fold<Expr> for Arrow {

Expr::Call(CallExpr {
span,
callee: Expr::Member(MemberExpr {
span,
obj: ExprOrSuper::Expr(box fn_expr),
prop: box quote_ident!("bind").into(),
computed: false,
})
.as_callee(),
callee: fn_expr.member(quote_ident!("bind")).as_callee(),
args: vec![ThisExpr { span: DUMMY_SP }.as_arg()],
type_args: Default::default(),
})
Expand Down
20 changes: 10 additions & 10 deletions ecmascript/transforms/src/compat/es2015/arrow/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;

test!(
::swc_ecma_parser::Syntax::default(),
Arrow,
|_, _| Arrow,
destructuring,
r#"let foo = ({bar}) => undefined;"#,
r#"let foo = function ({bar}) {
Expand All @@ -12,7 +12,7 @@ test!(

test!(
::swc_ecma_parser::Syntax::default(),
Arrow,
|_, _| Arrow,
basic,
r#"let echo = (bar) => bar"#,
r#"let echo = function(bar) {
Expand All @@ -22,7 +22,7 @@ test!(

test!(
::swc_ecma_parser::Syntax::default(),
Arrow,
|_, _| Arrow,
empty_arguments,
r#"var t = () => 5 + 5;"#,
r#"var t = function () {
Expand All @@ -32,7 +32,7 @@ test!(

test!(
::swc_ecma_parser::Syntax::default(),
Arrow,
|_, _| Arrow,
expression,
r#"arr.map(x => x * x);"#,
r#"arr.map(function (x) {
Expand All @@ -42,7 +42,7 @@ test!(

test!(
::swc_ecma_parser::Syntax::default(),
Arrow,
|_, _| Arrow,
inside_call,
r#"arr.map(i => i + 1);"#,
r#"arr.map(function (i) {
Expand All @@ -52,7 +52,7 @@ test!(

test!(
::swc_ecma_parser::Syntax::default(),
Arrow,
|_, _| Arrow,
multiple_arguments,
r#"var t = (i, x) => i * x;"#,
r#"var t = function (i, x) {
Expand All @@ -61,7 +61,7 @@ test!(
);

// test!(::swc_ecma_parser::Syntax::default(),
// Arrow,
// |_, _| Arrow,
// nested,
// r#"module.exports = {
// init: function () {
Expand Down Expand Up @@ -96,7 +96,7 @@ test!(

test!(
::swc_ecma_parser::Syntax::default(),
Arrow,
|_, _| Arrow,
paren_insertion,
r#"var t = i => i * 5;"#,
r#"var t = function (i) {
Expand All @@ -106,7 +106,7 @@ test!(

test!(
::swc_ecma_parser::Syntax::default(),
Arrow,
|_, _| Arrow,
single_argument,
r#"var t = (i) => i * 5;"#,
r#"var t = function (i) {
Expand All @@ -116,7 +116,7 @@ test!(

test!(
::swc_ecma_parser::Syntax::default(),
Arrow,
|_, _| Arrow,
statement,
r#"nums.forEach(v => {
if (v % 5 === 0) {
Expand Down
2 changes: 1 addition & 1 deletion ecmascript/transforms/src/compat/es2015/block_scoped_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ mod tests {

test!(
::swc_ecma_parser::Syntax::default(),
BlockScopedFns,
|_, _| BlockScopedFns,
basic,
r#"{
function name (n) {
Expand Down
13 changes: 3 additions & 10 deletions ecmascript/transforms/src/compat/es2015/classes/constructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,16 +365,9 @@ pub(super) fn make_possible_return_value(helpers: &Helpers, mode: ReturningMode)
vec![ThisExpr { span: DUMMY_SP }.as_arg(), {
let apply = box Expr::Call(CallExpr {
span: DUMMY_SP,
callee: MemberExpr {
span: DUMMY_SP,
obj: ExprOrSuper::Expr(box get_prototype_of(
helpers,
&Expr::Ident(class_name),
)),
computed: false,
prop: box Expr::Ident(fn_name),
}
.as_callee(),
callee: get_prototype_of(helpers, &Expr::Ident(class_name))
.member(fn_name)
.as_callee(),

// super(foo, bar) => possibleReturnCheck(this, foo, bar)
args,
Expand Down
14 changes: 2 additions & 12 deletions ecmascript/transforms/src/compat/es2015/classes/super_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,7 @@ impl<'a> SuperCalleeFolder<'a> {
Expr::Ident(self.class_name.clone())
} else {
// Foo.prototype
Expr::Member(MemberExpr {
span: super_token,
obj: ExprOrSuper::Expr(box Expr::Ident(self.class_name.clone())),
prop: box Expr::Ident(quote_ident!("prototype")),
computed: false,
})
self.class_name.clone().member(quote_ident!("prototype"))
},
)
.as_arg();
Expand Down Expand Up @@ -271,12 +266,7 @@ impl<'a> SuperCalleeFolder<'a> {

let proto_arg = get_prototype_of(
self.helpers,
&Expr::Member(MemberExpr {
span: super_token,
obj: ExprOrSuper::Expr(box Expr::Ident(self.class_name.clone())),
prop: box Expr::Ident(quote_ident!("prototype")),
computed: false,
}),
&self.class_name.clone().member(quote_ident!("prototype")),
)
.as_arg();

Expand Down
Loading