Fix regenerator (#851)

swc_ecma_transforms:
 - Fix regenerator scoping issue.
This commit is contained in:
강동윤 2020-06-18 17:04:49 +09:00 committed by GitHub
parent 66d42adf7e
commit ff440157a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 77 additions and 13 deletions

View File

@ -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
}
}

View File

@ -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);
"#
);

View File

@ -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 })"
);

View File

@ -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 `);
});

View File

@ -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": "강동윤 <kdy1997.dev@gmail.com>",