From ff440157a0a7cba215d99caad3c92d11a6d51ad1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Thu, 18 Jun 2020 17:04:49 +0900 Subject: [PATCH] Fix regenerator (#851) swc_ecma_transforms: - Fix regenerator scoping issue. --- .../src/compat/es2015/regenerator/mod.rs | 30 +++++++++++-------- ecmascript/transforms/tests/es2015_classes.rs | 23 ++++++++++++++ .../transforms/tests/es2015_regenerator.rs | 11 +++++++ node-swc/__tests__/transform/issue846_test.js | 24 +++++++++++++++ package.json | 2 +- 5 files changed, 77 insertions(+), 13 deletions(-) create mode 100644 node-swc/__tests__/transform/issue846_test.js diff --git a/ecmascript/transforms/src/compat/es2015/regenerator/mod.rs b/ecmascript/transforms/src/compat/es2015/regenerator/mod.rs index 2d21bbeed70..b44df3c17d2 100644 --- a/ecmascript/transforms/src/compat/es2015/regenerator/mod.rs +++ b/ecmascript/transforms/src/compat/es2015/regenerator/mod.rs @@ -85,21 +85,27 @@ where return items; } - let mut items = items.fold_children(self); + let mut new = Vec::with_capacity(items.len() + 2); - if !self.top_level_vars.is_empty() { - prepend( - &mut items, - T::from_stmt(Stmt::Decl(Decl::Var(VarDecl { - span: DUMMY_SP, - kind: VarDeclKind::Var, - declare: false, - decls: replace(&mut self.top_level_vars, Default::default()), - }))), - ); + for item in items { + let item = item.fold_with(self); + + if !self.top_level_vars.is_empty() { + prepend( + &mut new, + T::from_stmt(Stmt::Decl(Decl::Var(VarDecl { + span: DUMMY_SP, + kind: VarDeclKind::Var, + declare: false, + decls: replace(&mut self.top_level_vars, Default::default()), + }))), + ); + } + + new.push(item); } - items + new } } diff --git a/ecmascript/transforms/tests/es2015_classes.rs b/ecmascript/transforms/tests/es2015_classes.rs index 4522e4dc703..bd1abfe7b33 100644 --- a/ecmascript/transforms/tests/es2015_classes.rs +++ b/ecmascript/transforms/tests/es2015_classes.rs @@ -6109,3 +6109,26 @@ with (env) { "# ); + +test_exec!( + syntax(), + |_| Classes::default(), + issue_846, + r#" +class SomeClass { + someMethod() { + return 1; + } +} + +class OtherClass extends SomeClass { + anotherMethod() { + expect(super.someMethod()).toBe(1); + return 2; + } +} + +const obj = new OtherClass(); +expect(obj.anotherMethod()).toBe(2); +"# +); diff --git a/ecmascript/transforms/tests/es2015_regenerator.rs b/ecmascript/transforms/tests/es2015_regenerator.rs index 8cb2d473c1e..75897989301 100644 --- a/ecmascript/transforms/tests/es2015_regenerator.rs +++ b/ecmascript/transforms/tests/es2015_regenerator.rs @@ -1116,3 +1116,14 @@ export function myGenerator() { } " ); + +test_exec!( + syntax(), + |_| es2015::regenerator(Mark::fresh(Mark::root())), + issue_849_1, + "function* gen() { yield 1 }; +function genFactory() { return function*() { yield 1 }; } +const v = genFactory()(); +expect(v.next()).toEqual({ value: 1, done: false }) +expect(v.next()).toEqual({ done: true })" +); diff --git a/node-swc/__tests__/transform/issue846_test.js b/node-swc/__tests__/transform/issue846_test.js new file mode 100644 index 00000000000..f508978b582 --- /dev/null +++ b/node-swc/__tests__/transform/issue846_test.js @@ -0,0 +1,24 @@ +const swc = require('../../../'); + + +it("should handle es2019", () => { + expect( + swc.transformSync(`class SomeClass { + someMethod() {} +} + +class OtherClass extends SomeClass { + anotherMethod() { + super.someMethod() + } +}`, { + jsc: { + parser: { + syntax: "ecmascript", + }, + target: "es2019", + } + }) + .code.trim() + ).toContain(`class `); +}); \ No newline at end of file diff --git a/package.json b/package.json index 44965587e3d..7e4f622f7c6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@swc/core", - "version": "1.2.4", + "version": "1.2.5", "description": "Super-fast alternative for babel", "main": "./index.js", "author": "강동윤 ",