fix(es/transforms/compat): Fix regenerator pass for yield* expressions (#1580)

swc_ecma_transforms_compat:
 - `regenerator`: Fix delegating yield expressions.
This commit is contained in:
Devon Govett 2021-04-16 08:44:36 -07:00 committed by GitHub
parent 60ad0df3f5
commit efa7a9af78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 3 deletions

View File

@ -710,6 +710,13 @@ impl CaseHandler<'_> {
if arg.is_some() && e.delegate { if arg.is_some() && e.delegate {
let result = self.make_var(); let result = self.make_var();
let name = match &result {
Expr::Member(m) => match &*m.prop {
Expr::Ident(id) if !m.computed => id.sym.clone(),
_ => unreachable!(),
},
_ => unreachable!(),
};
let ret = ReturnStmt { let ret = ReturnStmt {
// Preserve span // Preserve span
@ -723,7 +730,13 @@ impl CaseHandler<'_> {
.as_callee(), .as_callee(),
args: vec![ args: vec![
arg.unwrap().as_arg(), arg.unwrap().as_arg(),
result.clone().as_arg(), Lit::Str(Str {
span: DUMMY_SP,
value: name,
has_escape: false,
kind: Default::default(),
})
.as_arg(),
after.to_stmt_index().as_arg(), after.to_stmt_index().as_arg(),
], ],
type_args: Default::default(), type_args: Default::default(),

View File

@ -1015,7 +1015,7 @@ export function myGenerator() {
1, 1,
2, 2,
3 3
], _ctx.t0, 1); ], \"t0\", 1);
case 1: case 1:
case 'end': case 'end':
return _ctx.stop(); return _ctx.stop();
@ -1025,6 +1025,21 @@ export function myGenerator() {
" "
); );
test_exec!(
syntax(),
|_| es2015::regenerator(Mark::fresh(Mark::root())),
delegate_context,
"function* a() {
yield 5;
return 7;
}
function* b() {
let x = yield* a();
yield (x + 1);
}
expect([...b()]).toEqual([5, 8]);"
);
test_exec!( test_exec!(
syntax(), syntax(),
|_| es2015::regenerator(Mark::fresh(Mark::root())), |_| es2015::regenerator(Mark::fresh(Mark::root())),

View File

@ -4499,7 +4499,7 @@ function myGenerator() {
1, 1,
2, 2,
3 3
], _ctx.t0, 1); ], \"t0\", 1);
case 1: case 1:
case 'end': case 'end':
return _ctx.stop(); return _ctx.stop();