From 62c628709187ae0e2420e21175f85b5e106f7ee8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Sep 2021 14:36:47 +0900 Subject: [PATCH 01/26] Add tests for #2248 --- tests/fixture/issue-2248/es2017/input/.swcrc | 5 +++++ tests/fixture/issue-2248/es2017/input/index.js | 7 +++++++ tests/fixture/issue-2248/es5/input/.swcrc | 5 +++++ tests/fixture/issue-2248/es5/input/index.js | 7 +++++++ 4 files changed, 24 insertions(+) create mode 100644 tests/fixture/issue-2248/es2017/input/.swcrc create mode 100644 tests/fixture/issue-2248/es2017/input/index.js create mode 100644 tests/fixture/issue-2248/es5/input/.swcrc create mode 100644 tests/fixture/issue-2248/es5/input/index.js diff --git a/tests/fixture/issue-2248/es2017/input/.swcrc b/tests/fixture/issue-2248/es2017/input/.swcrc new file mode 100644 index 000000000000..4c78ecb09571 --- /dev/null +++ b/tests/fixture/issue-2248/es2017/input/.swcrc @@ -0,0 +1,5 @@ +{ + "jsc": { + "target": "es2017" + } +} \ No newline at end of file diff --git a/tests/fixture/issue-2248/es2017/input/index.js b/tests/fixture/issue-2248/es2017/input/index.js new file mode 100644 index 000000000000..480663cb20c2 --- /dev/null +++ b/tests/fixture/issue-2248/es2017/input/index.js @@ -0,0 +1,7 @@ +export const foo = async () => { + try { + console.log(1) + } catch (err) { + console.log(err.message) + } +} \ No newline at end of file diff --git a/tests/fixture/issue-2248/es5/input/.swcrc b/tests/fixture/issue-2248/es5/input/.swcrc new file mode 100644 index 000000000000..088bc6e52178 --- /dev/null +++ b/tests/fixture/issue-2248/es5/input/.swcrc @@ -0,0 +1,5 @@ +{ + "jsc": { + "target": "es5" + } +} \ No newline at end of file diff --git a/tests/fixture/issue-2248/es5/input/index.js b/tests/fixture/issue-2248/es5/input/index.js new file mode 100644 index 000000000000..480663cb20c2 --- /dev/null +++ b/tests/fixture/issue-2248/es5/input/index.js @@ -0,0 +1,7 @@ +export const foo = async () => { + try { + console.log(1) + } catch (err) { + console.log(err.message) + } +} \ No newline at end of file From 6571bc8c59b4a23e63cd3e889a3185ea52cff3d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Sep 2021 14:44:24 +0900 Subject: [PATCH 02/26] Rename --- ecmascript/transforms/compat/src/es2015/regenerator/mod.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ecmascript/transforms/compat/src/es2015/regenerator/mod.rs b/ecmascript/transforms/compat/src/es2015/regenerator/mod.rs index c6be29a04856..ade446e9eafd 100644 --- a/ecmascript/transforms/compat/src/es2015/regenerator/mod.rs +++ b/ecmascript/transforms/compat/src/es2015/regenerator/mod.rs @@ -12,9 +12,9 @@ mod case; mod hoist; mod leap; -pub fn regenerator(global_mark: Mark) -> impl Fold { +pub fn regenerator(top_level_mark: Mark) -> impl Fold { Regenerator { - global_mark, + global_mark: top_level_mark, regenerator_runtime: Default::default(), top_level_vars: Default::default(), } From 599763cf33275c748152a78fa0da42102173c337 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Sep 2021 14:45:04 +0900 Subject: [PATCH 03/26] Add a test for #2248 --- .../compat/tests/es2017_async_to_generator.rs | 23 ++++++++++++++++++- .../async-to-generator/issue-2248/exec.js | 9 ++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 ecmascript/transforms/compat/tests/fixture/async-to-generator/issue-2248/exec.js diff --git a/ecmascript/transforms/compat/tests/es2017_async_to_generator.rs b/ecmascript/transforms/compat/tests/es2017_async_to_generator.rs index 3e8f75c98152..7d0db5e8ac57 100644 --- a/ecmascript/transforms/compat/tests/es2017_async_to_generator.rs +++ b/ecmascript/transforms/compat/tests/es2017_async_to_generator.rs @@ -1,4 +1,5 @@ use crate::es2015::regenerator; +use std::{fs::read_to_string, path::PathBuf}; use swc_common::{chain, Mark, Spanned}; use swc_ecma_ast::*; use swc_ecma_parser::Syntax; @@ -9,7 +10,7 @@ use swc_ecma_transforms_compat::{ es2017::async_to_generator, es2020::class_properties, }; -use swc_ecma_transforms_testing::{test, test_exec}; +use swc_ecma_transforms_testing::{compare_stdout, test, test_exec}; use swc_ecma_visit::{Fold, FoldWith}; struct ParenRemover; @@ -2684,3 +2685,23 @@ test_exec!( await res; " ); + +#[testing::fixture("tests/fixture/async-to-generator/**/exec.js")] +fn exec(input: PathBuf) { + let input = read_to_string(&input).unwrap(); + compare_stdout(Default::default(), |_| async_to_generator(), &input); +} + +#[testing::fixture("tests/fixture/async-to-generator/**/exec.js")] +fn exec_regenerator(input: PathBuf) { + let input = read_to_string(&input).unwrap(); + compare_stdout( + Default::default(), + |_| { + let top_level_mark = Mark::fresh(Mark::root()); + + chain!(async_to_generator(), regenerator(top_level_mark)) + }, + &input, + ); +} diff --git a/ecmascript/transforms/compat/tests/fixture/async-to-generator/issue-2248/exec.js b/ecmascript/transforms/compat/tests/fixture/async-to-generator/issue-2248/exec.js new file mode 100644 index 000000000000..f3321fab427e --- /dev/null +++ b/ecmascript/transforms/compat/tests/fixture/async-to-generator/issue-2248/exec.js @@ -0,0 +1,9 @@ +export const foo = async () => { + try { + console.log(1) + } catch (err) { + console.log(err.message) + } +} + +foo() \ No newline at end of file From 0e5e0eae06221e1764715d41c72290ab03acd456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Sep 2021 14:46:51 +0900 Subject: [PATCH 04/26] fixup for tests --- .../compat/tests/fixture/async-to-generator/issue-2248/exec.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ecmascript/transforms/compat/tests/fixture/async-to-generator/issue-2248/exec.js b/ecmascript/transforms/compat/tests/fixture/async-to-generator/issue-2248/exec.js index f3321fab427e..239feec4b5f1 100644 --- a/ecmascript/transforms/compat/tests/fixture/async-to-generator/issue-2248/exec.js +++ b/ecmascript/transforms/compat/tests/fixture/async-to-generator/issue-2248/exec.js @@ -1,4 +1,4 @@ -export const foo = async () => { +const foo = async () => { try { console.log(1) } catch (err) { From 2194f7083b2f1526c3c2f830144068bb143c7df5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Sep 2021 14:49:12 +0900 Subject: [PATCH 05/26] Update test refs --- .../fixture/issue-2248/es2017/output/index.js | 7 +++ tests/fixture/issue-2248/es5/output/index.js | 45 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 tests/fixture/issue-2248/es2017/output/index.js create mode 100644 tests/fixture/issue-2248/es5/output/index.js diff --git a/tests/fixture/issue-2248/es2017/output/index.js b/tests/fixture/issue-2248/es2017/output/index.js new file mode 100644 index 000000000000..6f6c434fb578 --- /dev/null +++ b/tests/fixture/issue-2248/es2017/output/index.js @@ -0,0 +1,7 @@ +export const foo = async ()=>{ + try { + console.log(1); + } catch (err) { + console.log(err.message); + } +}; diff --git a/tests/fixture/issue-2248/es5/output/index.js b/tests/fixture/issue-2248/es5/output/index.js new file mode 100644 index 000000000000..cc1987d619d0 --- /dev/null +++ b/tests/fixture/issue-2248/es5/output/index.js @@ -0,0 +1,45 @@ +import regeneratorRuntime from "regenerator-runtime"; +function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { + try { + var info = gen[key](arg); + var value = info.value; + } catch (error) { + reject(error); + return; + } + if (info.done) { + resolve(value); + } else { + Promise.resolve(value).then(_next, _throw); + } +} +function _asyncToGenerator(fn) { + return function() { + var self = this, args = arguments; + return new Promise(function(resolve, reject) { + var gen = fn.apply(self, args); + function _next(value) { + asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); + } + function _throw(err) { + asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); + } + _next(undefined); + }); + }; +} +export var foo = _asyncToGenerator(regeneratorRuntime.mark(function _callee() { + return regeneratorRuntime.wrap(function _callee$(_ctx) { + while(1)switch(_ctx.prev = _ctx.next){ + case 0: + try { + console.log(1); + } catch (err) { + console.log(err.message); + } + case 1: + case "end": + return _ctx.stop(); + } + }, _callee); +})); From b96674a398a72ffda41a92d61148e2230d6b011f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Sep 2021 14:58:15 +0900 Subject: [PATCH 06/26] Add a test for #2234 --- .../typescript/tests/fixture/ssue-2234/input.ts | 8 ++++++++ ecmascript/transforms/typescript/tests/strip.rs | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 ecmascript/transforms/typescript/tests/fixture/ssue-2234/input.ts diff --git a/ecmascript/transforms/typescript/tests/fixture/ssue-2234/input.ts b/ecmascript/transforms/typescript/tests/fixture/ssue-2234/input.ts new file mode 100644 index 000000000000..769836b2ec5f --- /dev/null +++ b/ecmascript/transforms/typescript/tests/fixture/ssue-2234/input.ts @@ -0,0 +1,8 @@ +namespace Shapes { + export namespace Polygons { + export class Triangle { } + export class Square { } + } +} +import polygons = Shapes.Polygons; +let sq = new polygons.Square(); \ No newline at end of file diff --git a/ecmascript/transforms/typescript/tests/strip.rs b/ecmascript/transforms/typescript/tests/strip.rs index 24b71c909b82..12f493cca3ab 100644 --- a/ecmascript/transforms/typescript/tests/strip.rs +++ b/ecmascript/transforms/typescript/tests/strip.rs @@ -1,3 +1,5 @@ +use std::path::PathBuf; + use swc_common::chain; use swc_ecma_parser::{Syntax, TsConfig}; use swc_ecma_transforms_base::resolver::resolver; @@ -6,7 +8,7 @@ use swc_ecma_transforms_compat::{ es2020::{class_properties, nullish_coalescing, optional_chaining}, }; use swc_ecma_transforms_proposal::decorators; -use swc_ecma_transforms_testing::{test, test_exec}; +use swc_ecma_transforms_testing::{test, test_exec, test_fixture}; use swc_ecma_transforms_typescript::{strip, strip::strip_with_config}; use swc_ecma_visit::Fold; @@ -4077,3 +4079,14 @@ to!( " ); + +#[testing::fixture("tests/fixture/**/input.ts")] +fn exec(input: PathBuf) { + let output = input.with_file_name("output.js"); + test_fixture( + Syntax::Typescript(Default::default()), + &|_| tr(), + &input, + &output, + ); +} From 19bee2c7f098969e3c76f81a4041e43e1a8bf3ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Sep 2021 15:11:44 +0900 Subject: [PATCH 07/26] Fix #2234 --- ecmascript/transforms/typescript/src/strip.rs | 33 ++++++++++--------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/ecmascript/transforms/typescript/src/strip.rs b/ecmascript/transforms/typescript/src/strip.rs index 324df61f0107..87f9bbb19f6b 100644 --- a/ecmascript/transforms/typescript/src/strip.rs +++ b/ecmascript/transforms/typescript/src/strip.rs @@ -1887,24 +1887,25 @@ impl VisitMut for Strip { } ModuleItem::ModuleDecl(ModuleDecl::TsImportEquals(import)) => { - if !import.is_export { - continue; - } - - stmts.push(ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl { + let var = Decl::Var(VarDecl { span: DUMMY_SP, - decl: Decl::Var(VarDecl { + kind: VarDeclKind::Var, + decls: vec![VarDeclarator { span: DUMMY_SP, - kind: VarDeclKind::Var, - decls: vec![VarDeclarator { - span: DUMMY_SP, - name: Pat::Ident(import.id.into()), - init: Some(Box::new(module_ref_to_expr(import.module_ref))), - definite: false, - }], - declare: false, - }), - }))); + name: Pat::Ident(import.id.into()), + init: Some(Box::new(module_ref_to_expr(import.module_ref))), + definite: false, + }], + declare: false, + }); + if import.is_export { + stmts.push(ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl { + span: DUMMY_SP, + decl: var, + }))); + } else { + stmts.push(ModuleItem::Stmt(Stmt::Decl(var))); + } } ModuleItem::ModuleDecl(ModuleDecl::TsExportAssignment(export)) => { From 84f9621cb78ff3dbfc9eeb8af45d5e97d6348561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Sep 2021 15:12:29 +0900 Subject: [PATCH 08/26] Update test refs --- .../tests/fixture/ssue-2234/output.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 ecmascript/transforms/typescript/tests/fixture/ssue-2234/output.js diff --git a/ecmascript/transforms/typescript/tests/fixture/ssue-2234/output.js b/ecmascript/transforms/typescript/tests/fixture/ssue-2234/output.js new file mode 100644 index 000000000000..33c9b3f5057d --- /dev/null +++ b/ecmascript/transforms/typescript/tests/fixture/ssue-2234/output.js @@ -0,0 +1,17 @@ +var Shapes; +(function(Shapes) { + var Polygons; + (function(Polygons) { + class Triangle { + } + Polygons.Triangle = Triangle; + class Square { + } + Polygons.Square = Square; + })(Polygons || (Polygons = { + })); + Shapes.Polygons = Polygons; +})(Shapes || (Shapes = { +})); +var polygons = Shapes.Polygons; +let sq = new polygons.Square(); From 086ea7a4c8db86a06a90ecabc4d0ebd3b46e3578 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Sep 2021 15:37:43 +0900 Subject: [PATCH 09/26] Rename --- .../typescript/tests/fixture/{ssue-2234 => issue-2234}/input.ts | 0 .../typescript/tests/fixture/{ssue-2234 => issue-2234}/output.js | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename ecmascript/transforms/typescript/tests/fixture/{ssue-2234 => issue-2234}/input.ts (100%) rename ecmascript/transforms/typescript/tests/fixture/{ssue-2234 => issue-2234}/output.js (100%) diff --git a/ecmascript/transforms/typescript/tests/fixture/ssue-2234/input.ts b/ecmascript/transforms/typescript/tests/fixture/issue-2234/input.ts similarity index 100% rename from ecmascript/transforms/typescript/tests/fixture/ssue-2234/input.ts rename to ecmascript/transforms/typescript/tests/fixture/issue-2234/input.ts diff --git a/ecmascript/transforms/typescript/tests/fixture/ssue-2234/output.js b/ecmascript/transforms/typescript/tests/fixture/issue-2234/output.js similarity index 100% rename from ecmascript/transforms/typescript/tests/fixture/ssue-2234/output.js rename to ecmascript/transforms/typescript/tests/fixture/issue-2234/output.js From 2e2bb3154623e8d0836ff9b474a7577e4892a336 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Sep 2021 15:38:02 +0900 Subject: [PATCH 10/26] Add a test for #2243 --- .../tests/fixture/issue-2243/input.ts | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 ecmascript/transforms/typescript/tests/fixture/issue-2243/input.ts diff --git a/ecmascript/transforms/typescript/tests/fixture/issue-2243/input.ts b/ecmascript/transforms/typescript/tests/fixture/issue-2243/input.ts new file mode 100644 index 000000000000..f104c86bcb32 --- /dev/null +++ b/ecmascript/transforms/typescript/tests/fixture/issue-2243/input.ts @@ -0,0 +1,25 @@ +export function Colors(member: Colors.KeyType): Colors { + return Colors.ValueFor(member); +} + +export module Colors { + export type KeyType = keyof typeof ValueMap; + + export const ValueMap = { + Red: { value: 0.0, label: 'Red' }, + Blue: { value: 1.0, label: 'Blue' }, + Green: { value: 2.0, label: 'Green' }, + } as const; + + export const Values: Colors[] = [0.0, 1.0, 2.0]; + + export function ValueFor(member: KeyType): Colors { + return ValueMap[member]?.value; + } + + export function LabelFor( + member: KeyType + ): Promise { + return ValueMap[member]?.label; + } +} \ No newline at end of file From 1c0f6040e162bd6d3cdf28c723330e6ae7f833ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Sep 2021 15:41:58 +0900 Subject: [PATCH 11/26] Ignore the test for #2243 --- .../typescript/tests/fixture/{issue-2243 => .issue-2243}/input.ts | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename ecmascript/transforms/typescript/tests/fixture/{issue-2243 => .issue-2243}/input.ts (100%) diff --git a/ecmascript/transforms/typescript/tests/fixture/issue-2243/input.ts b/ecmascript/transforms/typescript/tests/fixture/.issue-2243/input.ts similarity index 100% rename from ecmascript/transforms/typescript/tests/fixture/issue-2243/input.ts rename to ecmascript/transforms/typescript/tests/fixture/.issue-2243/input.ts From 430f0f170a33f0671e5f218d0d61690708a0af7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Sep 2021 15:42:33 +0900 Subject: [PATCH 12/26] Add a test for #1653 --- .../typescript/tests/fixture/issue-1653/input.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 ecmascript/transforms/typescript/tests/fixture/issue-1653/input.ts diff --git a/ecmascript/transforms/typescript/tests/fixture/issue-1653/input.ts b/ecmascript/transforms/typescript/tests/fixture/issue-1653/input.ts new file mode 100644 index 000000000000..b4bf3a3bd25c --- /dev/null +++ b/ecmascript/transforms/typescript/tests/fixture/issue-1653/input.ts @@ -0,0 +1,11 @@ +namespace X { + export namespace Z { + export const foo = 0 + } +} + +namespace Y { + export namespace Z { + export const bar = 1 + } +} From 86796ccbb7f300abb886d67aa84e5c18f8f3887a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Sep 2021 15:44:01 +0900 Subject: [PATCH 13/26] Update test refs --- ecmascript/transforms/typescript/tests/strip.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ecmascript/transforms/typescript/tests/strip.rs b/ecmascript/transforms/typescript/tests/strip.rs index 12f493cca3ab..908cdb319c34 100644 --- a/ecmascript/transforms/typescript/tests/strip.rs +++ b/ecmascript/transforms/typescript/tests/strip.rs @@ -462,7 +462,7 @@ to!(module_01, "module 'foo'{ }", ""); to!(declare_01, "declare var env: FOO", ""); -to!(import_equals, "import A = B.C", ""); +to!(import_equals, "import A = B.C", "var A = B.C;"); to!( issue_757, From 42b2914a618ff73eb70182474abeb0778d61a067 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Sep 2021 15:45:20 +0900 Subject: [PATCH 14/26] Fix --- ecmascript/transforms/typescript/src/strip.rs | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/ecmascript/transforms/typescript/src/strip.rs b/ecmascript/transforms/typescript/src/strip.rs index 87f9bbb19f6b..ea8d27f4b12d 100644 --- a/ecmascript/transforms/typescript/src/strip.rs +++ b/ecmascript/transforms/typescript/src/strip.rs @@ -1887,24 +1887,28 @@ impl VisitMut for Strip { } ModuleItem::ModuleDecl(ModuleDecl::TsImportEquals(import)) => { - let var = Decl::Var(VarDecl { - span: DUMMY_SP, - kind: VarDeclKind::Var, - decls: vec![VarDeclarator { + if !import.is_type_only { + let var = Decl::Var(VarDecl { span: DUMMY_SP, - name: Pat::Ident(import.id.into()), - init: Some(Box::new(module_ref_to_expr(import.module_ref))), - definite: false, - }], - declare: false, - }); - if import.is_export { - stmts.push(ModuleItem::ModuleDecl(ModuleDecl::ExportDecl(ExportDecl { - span: DUMMY_SP, - decl: var, - }))); - } else { - stmts.push(ModuleItem::Stmt(Stmt::Decl(var))); + kind: VarDeclKind::Var, + decls: vec![VarDeclarator { + span: DUMMY_SP, + name: Pat::Ident(import.id.into()), + init: Some(Box::new(module_ref_to_expr(import.module_ref))), + definite: false, + }], + declare: false, + }); + if import.is_export { + stmts.push(ModuleItem::ModuleDecl(ModuleDecl::ExportDecl( + ExportDecl { + span: DUMMY_SP, + decl: var, + }, + ))); + } else { + stmts.push(ModuleItem::Stmt(Stmt::Decl(var))); + } } } From 52fcf9bd39f5e76dc39b9e7b27fc962034977cf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Sep 2021 15:48:10 +0900 Subject: [PATCH 15/26] #1653 is not the case anymore --- .../tests/fixture/issue-1653/output.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 ecmascript/transforms/typescript/tests/fixture/issue-1653/output.js diff --git a/ecmascript/transforms/typescript/tests/fixture/issue-1653/output.js b/ecmascript/transforms/typescript/tests/fixture/issue-1653/output.js new file mode 100644 index 000000000000..8fc170d9d017 --- /dev/null +++ b/ecmascript/transforms/typescript/tests/fixture/issue-1653/output.js @@ -0,0 +1,18 @@ +var X; +(function(X) { + var Z; + (function(Z) { + Z.foo = 0; + })(Z || (Z = { + })); + X.Z = Z; +})(X || (X = { +})); +var Y; +(function(Y) { + (function(Z) { + Z.bar = 1; + })(Z || (Z = { + })); +})(Y || (Y = { +})); From cb37b7b73401d2d7a3a549d5e3eb92d220fa43cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Sep 2021 15:52:20 +0900 Subject: [PATCH 16/26] Rename --- .../compat/tests/fixture/classes/{ => issue-2071}/case1/exec.js | 0 .../compat/tests/fixture/classes/{ => issue-2071}/case2/exec.js | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename ecmascript/transforms/compat/tests/fixture/classes/{ => issue-2071}/case1/exec.js (100%) rename ecmascript/transforms/compat/tests/fixture/classes/{ => issue-2071}/case2/exec.js (100%) diff --git a/ecmascript/transforms/compat/tests/fixture/classes/case1/exec.js b/ecmascript/transforms/compat/tests/fixture/classes/issue-2071/case1/exec.js similarity index 100% rename from ecmascript/transforms/compat/tests/fixture/classes/case1/exec.js rename to ecmascript/transforms/compat/tests/fixture/classes/issue-2071/case1/exec.js diff --git a/ecmascript/transforms/compat/tests/fixture/classes/case2/exec.js b/ecmascript/transforms/compat/tests/fixture/classes/issue-2071/case2/exec.js similarity index 100% rename from ecmascript/transforms/compat/tests/fixture/classes/case2/exec.js rename to ecmascript/transforms/compat/tests/fixture/classes/issue-2071/case2/exec.js From 0b6fdc567dfe1af5d50158297075563822906cfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Sep 2021 15:53:48 +0900 Subject: [PATCH 17/26] Addd a test for #2228 --- .../fixture/classes/issue-2228/case1/exec.js | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 ecmascript/transforms/compat/tests/fixture/classes/issue-2228/case1/exec.js diff --git a/ecmascript/transforms/compat/tests/fixture/classes/issue-2228/case1/exec.js b/ecmascript/transforms/compat/tests/fixture/classes/issue-2228/case1/exec.js new file mode 100644 index 000000000000..9ab9189dd95e --- /dev/null +++ b/ecmascript/transforms/compat/tests/fixture/classes/issue-2228/case1/exec.js @@ -0,0 +1,17 @@ +class Foo { } + +class Bar extends Foo { + events = { + "abc: click": function abcClick() { + this.data = 123; + console.log(this); + } + } + + setData() { + this.data = 456 + } +} + +const bar = new Bar(); +console.log(bar.events) \ No newline at end of file From e5e2d201ee0bb38086a19ddd5aa0fa2cd5c63366 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Sep 2021 15:55:51 +0900 Subject: [PATCH 18/26] Fix tester --- ecmascript/transforms/compat/tests/es2015_classes.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ecmascript/transforms/compat/tests/es2015_classes.rs b/ecmascript/transforms/compat/tests/es2015_classes.rs index f4b7ecdd4898..9234e4263467 100644 --- a/ecmascript/transforms/compat/tests/es2015_classes.rs +++ b/ecmascript/transforms/compat/tests/es2015_classes.rs @@ -6539,7 +6539,7 @@ fn exec(input: PathBuf) { let src = read_to_string(&input).unwrap(); compare_stdout( Default::default(), - |t| classes(Some(t.comments.clone())), + |t| chain!(class_properties(), classes(Some(t.comments.clone()))), &src, ); } From d5bf2dfaa734ccd0ee5e9fa0381d09e105a86962 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Sep 2021 16:01:18 +0900 Subject: [PATCH 19/26] Harden the test for #2228 --- .../compat/tests/fixture/classes/issue-2228/case1/exec.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ecmascript/transforms/compat/tests/fixture/classes/issue-2228/case1/exec.js b/ecmascript/transforms/compat/tests/fixture/classes/issue-2228/case1/exec.js index 9ab9189dd95e..a70008c56f46 100644 --- a/ecmascript/transforms/compat/tests/fixture/classes/issue-2228/case1/exec.js +++ b/ecmascript/transforms/compat/tests/fixture/classes/issue-2228/case1/exec.js @@ -14,4 +14,9 @@ class Bar extends Foo { } const bar = new Bar(); -console.log(bar.events) \ No newline at end of file +console.log(bar.data); +console.log(bar.events) +console.log(bar.events["abc: click"]()); +console.log(bar.data); +bar.setData(); +console.log(bar.data); \ No newline at end of file From 58857abf3f524b74b6ef5521760051498158d460 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 14 Sep 2021 16:02:48 +0900 Subject: [PATCH 20/26] More test --- .../transforms/compat/tests/es2020_class_properties.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/ecmascript/transforms/compat/tests/es2020_class_properties.rs b/ecmascript/transforms/compat/tests/es2020_class_properties.rs index bdcc0ae5bd7b..c4cacd86f970 100644 --- a/ecmascript/transforms/compat/tests/es2020_class_properties.rs +++ b/ecmascript/transforms/compat/tests/es2020_class_properties.rs @@ -1,5 +1,6 @@ #![allow(deprecated)] +use std::{fs::read_to_string, path::PathBuf}; use swc_common::chain; use swc_ecma_parser::{EsConfig, Syntax, TsConfig}; use swc_ecma_transforms_base::resolver::resolver; @@ -10,7 +11,7 @@ use swc_ecma_transforms_compat::{ es2020::{class_properties, typescript_class_properties}, es3::reserved_words, }; -use swc_ecma_transforms_testing::{test, test_exec, Tester}; +use swc_ecma_transforms_testing::{compare_stdout, test, test_exec, Tester}; use swc_ecma_visit::Fold; fn ts() -> Syntax { @@ -5568,3 +5569,9 @@ test!( } " ); + +#[testing::fixture("tests/fixture/classes/**/exec.js")] +fn exec(input: PathBuf) { + let src = read_to_string(&input).unwrap(); + compare_stdout(Default::default(), |_| class_properties(), &src); +} From 8c6313e7f89beffca9d919f779066966d45aa799 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 15 Sep 2021 14:31:32 +0900 Subject: [PATCH 21/26] Fix #2228 --- .../compat/src/es2015/classes/constructor.rs | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/ecmascript/transforms/compat/src/es2015/classes/constructor.rs b/ecmascript/transforms/compat/src/es2015/classes/constructor.rs index e24691c0d4be..f573b4e5b838 100644 --- a/ecmascript/transforms/compat/src/es2015/classes/constructor.rs +++ b/ecmascript/transforms/compat/src/es2015/classes/constructor.rs @@ -1,5 +1,5 @@ use std::iter; -use swc_atoms::JsWord; +use swc_atoms::{js_word, JsWord}; use swc_common::{Mark, DUMMY_SP}; use swc_ecma_ast::*; use swc_ecma_transforms_base::helper; @@ -404,8 +404,26 @@ pub(super) fn replace_this_in_constructor(mark: Mark, c: Constructor) -> (Constr n } - fn fold_expr(&mut self, expr: Expr) -> Expr { - match expr { + fn fold_expr(&mut self, n: Expr) -> Expr { + // We pretend method folding mode for while folding injected `_defineProperty` + // calls. + match n { + Expr::Call(CallExpr { + callee: ExprOrSuper::Expr(ref callee), + .. + }) => match &**callee { + Expr::Ident(Ident { + sym: js_word!("_defineProperty"), + .. + }) => { + return n; + } + _ => {} + }, + _ => {} + } + + match n { Expr::This(..) => { self.found = true; let this = quote_ident!(DUMMY_SP.apply_mark(self.mark), "_this"); @@ -421,7 +439,8 @@ pub(super) fn replace_this_in_constructor(mark: Mark, c: Constructor) -> (Constr Expr::Ident(this) } } - _ => expr.fold_children_with(self), + + _ => n.fold_children_with(self), } } From 6c2b26267f8e42629e631a3fa30269286fd05283 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 15 Sep 2021 15:17:55 +0900 Subject: [PATCH 22/26] Add a test --- tests/fixture/issue-2232/case1/input/index.js | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 tests/fixture/issue-2232/case1/input/index.js diff --git a/tests/fixture/issue-2232/case1/input/index.js b/tests/fixture/issue-2232/case1/input/index.js new file mode 100644 index 000000000000..6fee35cf2ef2 --- /dev/null +++ b/tests/fixture/issue-2232/case1/input/index.js @@ -0,0 +1,7 @@ +class Foo { + async sendSomeMessage( + _parent, + { input: { toNumber, messageBody, ...all } }, + { dataSources }, + ) { } +} \ No newline at end of file From ac525aa8038878a0718435c92ed82808b1286ef8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 15 Sep 2021 15:20:02 +0900 Subject: [PATCH 23/26] Ensure that #2232 is not the case --- .../fixture/issue-2232/case1/output/index.js | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 tests/fixture/issue-2232/case1/output/index.js diff --git a/tests/fixture/issue-2232/case1/output/index.js b/tests/fixture/issue-2232/case1/output/index.js new file mode 100644 index 000000000000..a363f85051a8 --- /dev/null +++ b/tests/fixture/issue-2232/case1/output/index.js @@ -0,0 +1,107 @@ +import regeneratorRuntime from "regenerator-runtime"; +function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { + try { + var info = gen[key](arg); + var value = info.value; + } catch (error) { + reject(error); + return; + } + if (info.done) { + resolve(value); + } else { + Promise.resolve(value).then(_next, _throw); + } +} +function _asyncToGenerator(fn) { + return function() { + var self = this, args = arguments; + return new Promise(function(resolve, reject) { + var gen = fn.apply(self, args); + function _next(value) { + asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); + } + function _throw(err) { + asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); + } + _next(undefined); + }); + }; +} +function _classCallCheck(instance, Constructor) { + if (!(instance instanceof Constructor)) { + throw new TypeError("Cannot call a class as a function"); + } +} +function _defineProperties(target, props) { + for(var i = 0; i < props.length; i++){ + var descriptor = props[i]; + descriptor.enumerable = descriptor.enumerable || false; + descriptor.configurable = true; + if ("value" in descriptor) descriptor.writable = true; + Object.defineProperty(target, descriptor.key, descriptor); + } +} +function _createClass(Constructor, protoProps, staticProps) { + if (protoProps) _defineProperties(Constructor.prototype, protoProps); + if (staticProps) _defineProperties(Constructor, staticProps); + return Constructor; +} +function _objectWithoutProperties(source, excluded) { + if (source == null) return { + }; + var target = _objectWithoutPropertiesLoose(source, excluded); + var key, i; + if (Object.getOwnPropertySymbols) { + var sourceSymbolKeys = Object.getOwnPropertySymbols(source); + for(i = 0; i < sourceSymbolKeys.length; i++){ + key = sourceSymbolKeys[i]; + if (excluded.indexOf(key) >= 0) continue; + if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; + target[key] = source[key]; + } + } + return target; +} +function _objectWithoutPropertiesLoose(source, excluded) { + if (source == null) return { + }; + var target = { + }; + var sourceKeys = Object.keys(source); + var key, i; + for(i = 0; i < sourceKeys.length; i++){ + key = sourceKeys[i]; + if (excluded.indexOf(key) >= 0) continue; + target[key] = source[key]; + } + return target; +} +var Foo = /*#__PURE__*/ function() { + "use strict"; + function Foo() { + _classCallCheck(this, Foo); + } + _createClass(Foo, [ + { + key: "sendSomeMessage", + value: function sendSomeMessage(_parent, _param, _param1) { + return _asyncToGenerator(regeneratorRuntime.mark(function _callee() { + var toNumber, messageBody, all, dataSources; + return regeneratorRuntime.wrap(function _callee$(_ctx) { + var ref; + while(1)switch(_ctx.prev = _ctx.next){ + case 0: + var ref1, ref2; + ref1 = _param, ref = ref1.input, toNumber = ref.toNumber, messageBody = ref.messageBody, ref, ref1, all = _objectWithoutProperties(_param.input, ["toNumber", "messageBody"]), ref2 = _param1, dataSources = ref2.dataSources, ref2; + case 1: + case "end": + return _ctx.stop(); + } + }, _callee); + }))(); + } + } + ]); + return Foo; +}(); From 3637eb65bc0b0e23b86c5b29be3e93c3987c12da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 15 Sep 2021 16:16:24 +0900 Subject: [PATCH 24/26] [WIP] Fix `this`. --- .../compat/src/es2015/classes/constructor.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ecmascript/transforms/compat/src/es2015/classes/constructor.rs b/ecmascript/transforms/compat/src/es2015/classes/constructor.rs index f573b4e5b838..4f3115c84f93 100644 --- a/ecmascript/transforms/compat/src/es2015/classes/constructor.rs +++ b/ecmascript/transforms/compat/src/es2015/classes/constructor.rs @@ -395,6 +395,7 @@ pub(super) fn replace_this_in_constructor(mark: Mark, c: Constructor) -> (Constr mark: Mark, found: bool, wrap_with_assertion: bool, + in_injected_define_property_call: bool, } impl Fold for Replacer { @@ -416,6 +417,10 @@ pub(super) fn replace_this_in_constructor(mark: Mark, c: Constructor) -> (Constr sym: js_word!("_defineProperty"), .. }) => { + let old = self.in_injected_define_property_call; + self.in_injected_define_property_call = true; + let n = n.fold_children_with(self); + self.in_injected_define_property_call = old; return n; } _ => {} @@ -444,6 +449,13 @@ pub(super) fn replace_this_in_constructor(mark: Mark, c: Constructor) -> (Constr } } + fn fold_function(&mut self, n: Function) -> Function { + if self.in_injected_define_property_call { + return n; + } + n.fold_children_with(self) + } + fn fold_member_expr( &mut self, MemberExpr { @@ -473,6 +485,7 @@ pub(super) fn replace_this_in_constructor(mark: Mark, c: Constructor) -> (Constr found: false, mark, wrap_with_assertion: true, + in_injected_define_property_call: false, }; let c = c.fold_with(&mut v); From 5894ffac7519d9739b9878b720f786af5716f812 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 15 Sep 2021 16:19:50 +0900 Subject: [PATCH 25/26] fixup --- .../compat/src/es2015/classes/constructor.rs | 27 +++++++++++++++++++ .../compat/src/es2015/classes/mod.rs | 1 + 2 files changed, 28 insertions(+) diff --git a/ecmascript/transforms/compat/src/es2015/classes/constructor.rs b/ecmascript/transforms/compat/src/es2015/classes/constructor.rs index 4f3115c84f93..1645df1f4f3a 100644 --- a/ecmascript/transforms/compat/src/es2015/classes/constructor.rs +++ b/ecmascript/transforms/compat/src/es2015/classes/constructor.rs @@ -44,6 +44,10 @@ impl SuperCallFinder { macro_rules! ignore_return { ($name:ident, $T:ty) => { fn $name(&mut self, n: $T) -> $T { + if self.in_injected_define_property_call { + return n; + } + let old = self.ignore_return; self.ignore_return = true; let n = n.fold_children_with(self); @@ -164,6 +168,7 @@ pub(super) struct ConstructorFolder<'a> { pub is_constructor_default: bool, /// True when recursing into other function or class. pub ignore_return: bool, + pub in_injected_define_property_call: bool, } /// `None`: `return _possibleConstructorReturn` @@ -190,6 +195,28 @@ impl Fold for ConstructorFolder<'_> { _ => return expr, } + // We pretend method folding mode for while folding injected `_defineProperty` + // calls. + match expr { + Expr::Call(CallExpr { + callee: ExprOrSuper::Expr(ref callee), + .. + }) => match &**callee { + Expr::Ident(Ident { + sym: js_word!("_defineProperty"), + .. + }) => { + let old = self.in_injected_define_property_call; + self.in_injected_define_property_call = true; + let n = expr.fold_children_with(self); + self.in_injected_define_property_call = old; + return n; + } + _ => {} + }, + _ => {} + } + let expr = expr.fold_children_with(self); match expr { diff --git a/ecmascript/transforms/compat/src/es2015/classes/mod.rs b/ecmascript/transforms/compat/src/es2015/classes/mod.rs index fdec3a7a7f3a..cf7802af6106 100644 --- a/ecmascript/transforms/compat/src/es2015/classes/mod.rs +++ b/ecmascript/transforms/compat/src/es2015/classes/mod.rs @@ -493,6 +493,7 @@ where }, mark: this_mark, ignore_return: false, + in_injected_define_property_call: false, }); insert_this |= (mode == None && !is_always_initialized) From 949d51736b9149742e5b6c0b969bffe6adbe6bc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Wed, 15 Sep 2021 16:23:03 +0900 Subject: [PATCH 26/26] Bump version --- Cargo.lock | 4 ++-- ecmascript/transforms/compat/Cargo.toml | 2 +- ecmascript/transforms/typescript/Cargo.toml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8407569df9d9..0675ecebe172 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2769,7 +2769,7 @@ dependencies = [ [[package]] name = "swc_ecma_transforms_compat" -version = "0.34.2" +version = "0.34.3" dependencies = [ "arrayvec", "fxhash", @@ -2922,7 +2922,7 @@ dependencies = [ [[package]] name = "swc_ecma_transforms_typescript" -version = "0.40.2" +version = "0.40.3" dependencies = [ "fxhash", "serde", diff --git a/ecmascript/transforms/compat/Cargo.toml b/ecmascript/transforms/compat/Cargo.toml index 98d1bf63b024..c921f85f4bda 100644 --- a/ecmascript/transforms/compat/Cargo.toml +++ b/ecmascript/transforms/compat/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" license = "Apache-2.0/MIT" name = "swc_ecma_transforms_compat" repository = "https://github.com/swc-project/swc.git" -version = "0.34.2" +version = "0.34.3" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] diff --git a/ecmascript/transforms/typescript/Cargo.toml b/ecmascript/transforms/typescript/Cargo.toml index cc373a52a482..f0a2dc3084ef 100644 --- a/ecmascript/transforms/typescript/Cargo.toml +++ b/ecmascript/transforms/typescript/Cargo.toml @@ -6,7 +6,7 @@ edition = "2018" license = "Apache-2.0/MIT" name = "swc_ecma_transforms_typescript" repository = "https://github.com/swc-project/swc.git" -version = "0.40.2" +version = "0.40.3" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies]