From 85fffdad022cf1bccf8763e7bb73fb15f62d2430 Mon Sep 17 00:00:00 2001 From: Alex Vasilev Date: Tue, 7 Feb 2023 19:07:46 +0300 Subject: [PATCH] fix(es/module): Fix SystemJS imports (#6879) **Related issue:** - Closes https://github.com/swc-project/swc/issues/5922. --- ...rtCallExpressionAsyncES3System.1.normal.js | 8 ++--- ...rtCallExpressionAsyncES5System.1.normal.js | 8 ++--- ...rtCallExpressionAsyncES6System.1.normal.js | 8 ++--- .../importCallExpressionES5System.1.normal.js | 4 +-- ...mportCallExpressionES5System.2.minified.js | 2 +- .../importCallExpressionInSystem3.1.normal.js | 2 +- ...mportCallExpressionInSystem3.2.minified.js | 2 +- .../importCallExpressionInSystem4.1.normal.js | 4 +-- ...mportCallExpressionInSystem4.2.minified.js | 2 +- ...rtMetaNarrowing(module=system).1.normal.js | 2 +- ...MetaNarrowing(module=system).2.minified.js | 2 +- .../src/system_js.rs | 28 ++++++++++++++---- .../tests/system_js.rs | 29 +++++++++++++++++++ 13 files changed, 74 insertions(+), 27 deletions(-) diff --git a/crates/swc/tests/tsc-references/importCallExpressionAsyncES3System.1.normal.js b/crates/swc/tests/tsc-references/importCallExpressionAsyncES3System.1.normal.js index b0f5e79738a1..dbb949cd4008 100644 --- a/crates/swc/tests/tsc-references/importCallExpressionAsyncES3System.1.normal.js +++ b/crates/swc/tests/tsc-references/importCallExpressionAsyncES3System.1.normal.js @@ -57,7 +57,7 @@ System.register([ case 0: return [ 4, - import("./test") // TWO + _context.import("./test") // TWO ]; case 1: req = _state.sent(); @@ -78,7 +78,7 @@ System.register([ case 0: return [ 4, - import("./test") // THREE + _context.import("./test") // THREE ]; case 1: req = _state.sent(); @@ -100,7 +100,7 @@ System.register([ case 0: return [ 4, - import("./test") // FOUR + _context.import("./test") // FOUR ]; case 1: req = _state.sent(); @@ -120,7 +120,7 @@ System.register([ case 0: return [ 4, - import("./test") // FIVE + _context.import("./test") // FIVE ]; case 1: req = _state.sent(); diff --git a/crates/swc/tests/tsc-references/importCallExpressionAsyncES5System.1.normal.js b/crates/swc/tests/tsc-references/importCallExpressionAsyncES5System.1.normal.js index b0f5e79738a1..dbb949cd4008 100644 --- a/crates/swc/tests/tsc-references/importCallExpressionAsyncES5System.1.normal.js +++ b/crates/swc/tests/tsc-references/importCallExpressionAsyncES5System.1.normal.js @@ -57,7 +57,7 @@ System.register([ case 0: return [ 4, - import("./test") // TWO + _context.import("./test") // TWO ]; case 1: req = _state.sent(); @@ -78,7 +78,7 @@ System.register([ case 0: return [ 4, - import("./test") // THREE + _context.import("./test") // THREE ]; case 1: req = _state.sent(); @@ -100,7 +100,7 @@ System.register([ case 0: return [ 4, - import("./test") // FOUR + _context.import("./test") // FOUR ]; case 1: req = _state.sent(); @@ -120,7 +120,7 @@ System.register([ case 0: return [ 4, - import("./test") // FIVE + _context.import("./test") // FIVE ]; case 1: req = _state.sent(); diff --git a/crates/swc/tests/tsc-references/importCallExpressionAsyncES6System.1.normal.js b/crates/swc/tests/tsc-references/importCallExpressionAsyncES6System.1.normal.js index e4ca9d2140c0..fbd77fd5a610 100644 --- a/crates/swc/tests/tsc-references/importCallExpressionAsyncES6System.1.normal.js +++ b/crates/swc/tests/tsc-references/importCallExpressionAsyncES6System.1.normal.js @@ -29,14 +29,14 @@ System.register([ _export("cl1", cl1 = class cl1 { m() { return _async_to_generator(function*() { - const req = yield import('./test') // TWO + const req = yield _context.import('./test') // TWO ; })(); } }); _export("obj", obj = { m: /*#__PURE__*/ _async_to_generator(function*() { - const req = yield import('./test') // THREE + const req = yield _context.import('./test') // THREE ; }) }); @@ -44,7 +44,7 @@ System.register([ constructor(){ this.p = { m: /*#__PURE__*/ _async_to_generator(function*() { - const req = yield import('./test') // FOUR + const req = yield _context.import('./test') // FOUR ; }) }; @@ -52,7 +52,7 @@ System.register([ }); _export("l", l = function() { var _ref = _async_to_generator(function*() { - const req = yield import('./test') // FIVE + const req = yield _context.import('./test') // FIVE ; }); return function l() { diff --git a/crates/swc/tests/tsc-references/importCallExpressionES5System.1.normal.js b/crates/swc/tests/tsc-references/importCallExpressionES5System.1.normal.js index 6b4ec4bdff3e..02ad2fb35251 100644 --- a/crates/swc/tests/tsc-references/importCallExpressionES5System.1.normal.js +++ b/crates/swc/tests/tsc-references/importCallExpressionES5System.1.normal.js @@ -39,7 +39,7 @@ System.register([ } var _proto = C.prototype; _proto.method = function method() { - var loadAsync = import("./0"); + var loadAsync = _context.import("./0"); }; return C; }(); @@ -50,7 +50,7 @@ System.register([ } var _proto = D.prototype; _proto.method = function method() { - var loadAsync = import("./0"); + var loadAsync = _context.import("./0"); }; return D; }()); diff --git a/crates/swc/tests/tsc-references/importCallExpressionES5System.2.minified.js b/crates/swc/tests/tsc-references/importCallExpressionES5System.2.minified.js index 38610528d9ae..4a3a0ec6979a 100644 --- a/crates/swc/tests/tsc-references/importCallExpressionES5System.2.minified.js +++ b/crates/swc/tests/tsc-references/importCallExpressionES5System.2.minified.js @@ -28,7 +28,7 @@ System.register([ _class_call_check(this, D); } return D.prototype.method = function() { - import("./0"); + _context.import("./0"); }, D; }()); } diff --git a/crates/swc/tests/tsc-references/importCallExpressionInSystem3.1.normal.js b/crates/swc/tests/tsc-references/importCallExpressionInSystem3.1.normal.js index d61cccc1826e..a1a3647340ff 100644 --- a/crates/swc/tests/tsc-references/importCallExpressionInSystem3.1.normal.js +++ b/crates/swc/tests/tsc-references/importCallExpressionInSystem3.1.normal.js @@ -18,7 +18,7 @@ System.register([], function(_export, _context) { System.register([], function(_export, _context) { "use strict"; async function foo() { - class C extends (await import("./0")).B { + class C extends (await _context.import("./0")).B { } var c = new C(); c.print(); diff --git a/crates/swc/tests/tsc-references/importCallExpressionInSystem3.2.minified.js b/crates/swc/tests/tsc-references/importCallExpressionInSystem3.2.minified.js index 31de832dd2e0..b988facc9ce3 100644 --- a/crates/swc/tests/tsc-references/importCallExpressionInSystem3.2.minified.js +++ b/crates/swc/tests/tsc-references/importCallExpressionInSystem3.2.minified.js @@ -15,7 +15,7 @@ System.register([], function(_export, _context) { System.register([], function(_export, _context) { "use strict"; async function foo() { - class C extends (await import("./0")).B { + class C extends (await _context.import("./0")).B { } new C().print(); } diff --git a/crates/swc/tests/tsc-references/importCallExpressionInSystem4.1.normal.js b/crates/swc/tests/tsc-references/importCallExpressionInSystem4.1.normal.js index 1dc54d285b8f..25e5b4e653a7 100644 --- a/crates/swc/tests/tsc-references/importCallExpressionInSystem4.1.normal.js +++ b/crates/swc/tests/tsc-references/importCallExpressionInSystem4.1.normal.js @@ -56,7 +56,7 @@ System.register([], function(_export, _context) { console.log(Zero.foo()); }, async (err)=>{ console.log(err); - let one = await import("./1"); + let one = await _context.import("./1"); console.log(one.backup()); }); } @@ -69,7 +69,7 @@ System.register([], function(_export, _context) { console.log(Zero.foo()); }, async (err)=>{ console.log(err); - let one = await import("./1"); + let one = await _context.import("./1"); console.log(one.backup()); }); } diff --git a/crates/swc/tests/tsc-references/importCallExpressionInSystem4.2.minified.js b/crates/swc/tests/tsc-references/importCallExpressionInSystem4.2.minified.js index 573ab529eb00..34c915555009 100644 --- a/crates/swc/tests/tsc-references/importCallExpressionInSystem4.2.minified.js +++ b/crates/swc/tests/tsc-references/importCallExpressionInSystem4.2.minified.js @@ -43,7 +43,7 @@ System.register([], function(_export, _context) { _context.import("./0"), this.myModule.then((Zero)=>{ console.log(Zero.foo()); }, async (err)=>{ - console.log(err), console.log((await import("./1")).backup()); + console.log(err), console.log((await _context.import("./1")).backup()); }); } }); diff --git a/crates/swc/tests/tsc-references/importMetaNarrowing(module=system).1.normal.js b/crates/swc/tests/tsc-references/importMetaNarrowing(module=system).1.normal.js index b684bda5208c..d6f45996e4bf 100644 --- a/crates/swc/tests/tsc-references/importMetaNarrowing(module=system).1.normal.js +++ b/crates/swc/tests/tsc-references/importMetaNarrowing(module=system).1.normal.js @@ -5,7 +5,7 @@ System.register([], function(_export, _context) { setters: [], execute: function() { if (_context.meta.foo) { - import.meta.foo(); + _context.meta.foo(); } } }; diff --git a/crates/swc/tests/tsc-references/importMetaNarrowing(module=system).2.minified.js b/crates/swc/tests/tsc-references/importMetaNarrowing(module=system).2.minified.js index 2b6a7c6f98ed..f6bf3223247e 100644 --- a/crates/swc/tests/tsc-references/importMetaNarrowing(module=system).2.minified.js +++ b/crates/swc/tests/tsc-references/importMetaNarrowing(module=system).2.minified.js @@ -3,7 +3,7 @@ System.register([], function(_export, _context) { return { setters: [], execute: function() { - _context.meta.foo && import.meta.foo(); + _context.meta.foo && _context.meta.foo(); } }; }); diff --git a/crates/swc_ecma_transforms_module/src/system_js.rs b/crates/swc_ecma_transforms_module/src/system_js.rs index 5ce05bb9fc41..52401b6d8121 100644 --- a/crates/swc_ecma_transforms_module/src/system_js.rs +++ b/crates/swc_ecma_transforms_module/src/system_js.rs @@ -537,7 +537,25 @@ impl SystemJs { impl Fold for SystemJs { noop_fold_type!(); + fn fold_call_expr(&mut self, expr: CallExpr) -> CallExpr { + let expr = expr.fold_children_with(self); + + match expr.callee { + Callee::Import(_) => CallExpr { + callee: self + .context_ident + .clone() + .make_member(quote_ident!("import")) + .as_callee(), + ..expr + }, + _ => expr, + } + } + fn fold_expr(&mut self, expr: Expr) -> Expr { + let expr = expr.fold_children_with(self); + match expr { Expr::Ident(ident) => self.fold_module_name_ident(ident), Expr::Assign(assign) => { @@ -562,10 +580,7 @@ impl Fold for SystemJs { .as_callee(), ..call }), - _ => Expr::Call(CallExpr { - args: call.args.fold_with(self), - ..call - }), + _ => Expr::Call(call), }, Expr::MetaProp(meta_prop_expr) => match meta_prop_expr.kind { MetaPropKind::ImportMeta => { @@ -577,6 +592,7 @@ impl Fold for SystemJs { if self.enter_async_fn == 0 { self.tla = true; } + Expr::Await(await_expr) } Expr::This(this_expr) => { @@ -585,7 +601,7 @@ impl Fold for SystemJs { } Expr::This(this_expr) } - _ => expr.fold_children_with(self), + _ => expr, } } @@ -610,6 +626,8 @@ impl Fold for SystemJs { } fn fold_prop(&mut self, prop: Prop) -> Prop { + let prop = prop.fold_children_with(self); + match prop { Prop::Shorthand(shorthand) => Prop::KeyValue(KeyValueProp { key: PropName::Ident(shorthand.clone()), diff --git a/crates/swc_ecma_transforms_module/tests/system_js.rs b/crates/swc_ecma_transforms_module/tests/system_js.rs index 237ffbbfbece..8ba9048585e0 100644 --- a/crates/swc_ecma_transforms_module/tests/system_js.rs +++ b/crates/swc_ecma_transforms_module/tests/system_js.rs @@ -142,6 +142,35 @@ test!( });"# ); +test!( + syntax(), + |tester| tr( + tester, + Config { + ..Default::default() + } + ), + imports, + r#" + import.meta.url; + import.meta.fn(); + await import('./test2'); + "#, + r#" + System.register([], function(_export, _context) { + "use strict"; + return { + setters: [], + execute: async function() { + _context.meta.url; + _context.meta.fn(); + await _context.import('./test2'); + } + }; + }); + "# +); + // TODO: test get-module-name-option, tla #[testing::fixture("tests/fixture/systemjs/**/input.mjs")]