Skip to content

Commit

Permalink
fix(es/compat): Visit assign expr in generator (#7932)
Browse files Browse the repository at this point in the history
**Related issue:**
 - Closes #7809
  • Loading branch information
Austaras authored Sep 10, 2023
1 parent d15eaf0 commit 97068e1
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 61 deletions.
120 changes: 59 additions & 61 deletions crates/swc_ecma_transforms_compat/src/es2015/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -572,72 +572,70 @@ impl VisitMut for Generator {
e.visit_mut_children_with(self);
}

Expr::Assign(node) => {
if contains_yield(&node.right) {
match node.left.as_expr_mut() {
Some(Expr::Member(left)) => {
match &mut left.prop {
MemberProp::Ident(..) | MemberProp::PrivateName(..) => {
// a.b = yield;
//
// [intermediate]
// .local _a
// _a = a;
// .yield resumeLabel
// .mark resumeLabel
// _a.b = %sent%;

left.obj.visit_mut_with(self);
let obj = self.cache_expression(left.obj.take());

left.obj = Box::new(Expr::Ident(obj));
}
MemberProp::Computed(prop) => {
// [source]
// a[b] = yield;
//
// [intermediate]
// .local _a, _b
// _a = a;
// _b = b;
// .yield resumeLabel
// .mark resumeLabel
// _a[_b] = %sent%;
let prop_span = prop.span;

left.obj.visit_mut_with(self);
let obj = self.cache_expression(left.obj.take());

prop.visit_mut_with(self);
let prop = self.cache_expression(prop.expr.take());

left.obj = Box::new(Expr::Ident(obj));
left.prop = MemberProp::Computed(ComputedPropName {
span: prop_span,
expr: Box::new(Expr::Ident(prop)),
});
}
Expr::Assign(node) if contains_yield(&node.right) => {
match node.left.as_expr_mut() {
Some(Expr::Member(left)) => {
match &mut left.prop {
MemberProp::Ident(..) | MemberProp::PrivateName(..) => {
// a.b = yield;
//
// [intermediate]
// .local _a
// _a = a;
// .yield resumeLabel
// .mark resumeLabel
// _a.b = %sent%;

left.obj.visit_mut_with(self);
let obj = self.cache_expression(left.obj.take());

left.obj = Box::new(Expr::Ident(obj));
}
MemberProp::Computed(prop) => {
// [source]
// a[b] = yield;
//
// [intermediate]
// .local _a, _b
// _a = a;
// _b = b;
// .yield resumeLabel
// .mark resumeLabel
// _a[_b] = %sent%;
let prop_span = prop.span;

left.obj.visit_mut_with(self);
let obj = self.cache_expression(left.obj.take());

prop.visit_mut_with(self);
let prop = self.cache_expression(prop.expr.take());

left.obj = Box::new(Expr::Ident(obj));
left.prop = MemberProp::Computed(ComputedPropName {
span: prop_span,
expr: Box::new(Expr::Ident(prop)),
});
}
// [source]
}
_ => {
node.left.visit_mut_with(self);
}
// [source]
}
_ => {
node.left.visit_mut_with(self);
}
if node.op != op!("=") {
let left_of_right = self.cache_expression(node.left.take().expect_expr());
}
if node.op != op!("=") {
let left_of_right = self.cache_expression(node.left.take().expect_expr());

node.right.visit_mut_with(self);
node.right.visit_mut_with(self);

*e = Expr::Assign(AssignExpr {
span: node.right.span(),
op: node.op,
left: left_of_right.into(),
right: node.right.take(),
});
} else {
node.right.visit_mut_with(self);
}
*e = Expr::Assign(AssignExpr {
span: node.right.span(),
op: node.op,
left: left_of_right.into(),
right: node.right.take(),
});
} else {
node.right.visit_mut_with(self);
}
}

Expand Down
44 changes: 44 additions & 0 deletions crates/swc_ecma_transforms_compat/tests/es2015_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1937,3 +1937,47 @@ test_exec!(
await res;
"
);

test!(
Syntax::default(),
|_| {
let mark = Mark::fresh(Mark::root());
es2015::<SingleThreadedComments>(mark, None, Default::default())
},
issue_7809,
r#"
function a(fn) {
return _a.apply(this, arguments);
}
function _a() {
_a = _async_to_generator(function*(fn) {
(yield fn()).a = 1;
});
return _a.apply(this, arguments);
}
"#,
r#"
function a(fn) {
return _a.apply(this, arguments);
}
function _a() {
_a = _async_to_generator(function(fn) {
return _ts_generator(this, function(_state) {
switch(_state.label){
case 0:
return [
4,
fn()
];
case 1:
_state.sent().a = 1;
return [
2
];
}
});
});
return _a.apply(this, arguments);
}
"#
);

1 comment on commit 97068e1

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: 97068e1 Previous: a86e9f3 Ratio
es/full/bugs-1 284016 ns/iter (± 6804) 281446 ns/iter (± 4713) 1.01
es/full/minify/libraries/antd 1316220764 ns/iter (± 19121501) 1308539670 ns/iter (± 14215141) 1.01
es/full/minify/libraries/d3 278088805 ns/iter (± 4441309) 279231022 ns/iter (± 4714351) 1.00
es/full/minify/libraries/echarts 1067285415 ns/iter (± 9908038) 1065879422 ns/iter (± 12322133) 1.00
es/full/minify/libraries/jquery 84003552 ns/iter (± 264526) 83839776 ns/iter (± 363365) 1.00
es/full/minify/libraries/lodash 97654934 ns/iter (± 1189975) 96702540 ns/iter (± 1287241) 1.01
es/full/minify/libraries/moment 49616505 ns/iter (± 328700) 49547290 ns/iter (± 209341) 1.00
es/full/minify/libraries/react 17861425 ns/iter (± 149768) 17833131 ns/iter (± 27384) 1.00
es/full/minify/libraries/terser 219465151 ns/iter (± 4676018) 217106188 ns/iter (± 846177) 1.01
es/full/minify/libraries/three 387023054 ns/iter (± 3953134) 386504721 ns/iter (± 2405107) 1.00
es/full/minify/libraries/typescript 2665184416 ns/iter (± 17238354) 2642061415 ns/iter (± 17586945) 1.01
es/full/minify/libraries/victory 572237756 ns/iter (± 7341492) 572402185 ns/iter (± 13786019) 1.00
es/full/minify/libraries/vue 119172757 ns/iter (± 1071092) 118938525 ns/iter (± 426863) 1.00
es/full/codegen/es3 34291 ns/iter (± 70) 35024 ns/iter (± 253) 0.98
es/full/codegen/es5 34389 ns/iter (± 58) 34677 ns/iter (± 337) 0.99
es/full/codegen/es2015 34292 ns/iter (± 56) 34741 ns/iter (± 205) 0.99
es/full/codegen/es2016 34296 ns/iter (± 51) 34863 ns/iter (± 162) 0.98
es/full/codegen/es2017 34288 ns/iter (± 57) 34901 ns/iter (± 96) 0.98
es/full/codegen/es2018 34296 ns/iter (± 51) 34859 ns/iter (± 120) 0.98
es/full/codegen/es2019 34294 ns/iter (± 61) 34857 ns/iter (± 139) 0.98
es/full/codegen/es2020 34175 ns/iter (± 64) 34828 ns/iter (± 160) 0.98
es/full/all/es3 170480542 ns/iter (± 1223997) 168213081 ns/iter (± 745121) 1.01
es/full/all/es5 161787265 ns/iter (± 805475) 160411656 ns/iter (± 1008039) 1.01
es/full/all/es2015 121620361 ns/iter (± 997922) 122007333 ns/iter (± 1055692) 1.00
es/full/all/es2016 120606076 ns/iter (± 1146790) 121298526 ns/iter (± 997369) 0.99
es/full/all/es2017 119502037 ns/iter (± 538631) 119882264 ns/iter (± 517734) 1.00
es/full/all/es2018 118101856 ns/iter (± 738267) 116773650 ns/iter (± 499869) 1.01
es/full/all/es2019 117038566 ns/iter (± 926089) 116825901 ns/iter (± 532097) 1.00
es/full/all/es2020 113390362 ns/iter (± 519013) 112381928 ns/iter (± 545603) 1.01
es/full/parser 500860 ns/iter (± 6362) 499090 ns/iter (± 14134) 1.00
es/full/base/fixer 20584 ns/iter (± 128) 18028 ns/iter (± 102) 1.14
es/full/base/resolver_and_hygiene 80509 ns/iter (± 771) 80770 ns/iter (± 383) 1.00
serialization of serde 292 ns/iter (± 0) 297 ns/iter (± 3) 0.98
css/minify/libraries/bootstrap 28295853 ns/iter (± 187404) 28457803 ns/iter (± 142936) 0.99
css/visitor/compare/clone 1675531 ns/iter (± 31181) 1689140 ns/iter (± 7190) 0.99
css/visitor/compare/visit_mut_span 1792876 ns/iter (± 7335) 1806724 ns/iter (± 7866) 0.99
css/visitor/compare/visit_mut_span_panic 1872325 ns/iter (± 18056) 1845963 ns/iter (± 7792) 1.01
css/visitor/compare/fold_span 2589902 ns/iter (± 13051) 2606308 ns/iter (± 13749) 0.99
css/visitor/compare/fold_span_panic 2776744 ns/iter (± 33550) 2803923 ns/iter (± 15473) 0.99
css/lexer/bootstrap_5_1_3 4436807 ns/iter (± 18613) 4524068 ns/iter (± 3273) 0.98
css/lexer/foundation_6_7_4 3723153 ns/iter (± 1600) 3770817 ns/iter (± 1824) 0.99
css/lexer/tailwind_3_1_1 707074 ns/iter (± 398) 719045 ns/iter (± 320) 0.98
css/parser/bootstrap_5_1_3 18995753 ns/iter (± 127754) 19633023 ns/iter (± 72351) 0.97
css/parser/foundation_6_7_4 15215279 ns/iter (± 42140) 15561375 ns/iter (± 51304) 0.98
css/parser/tailwind_3_1_1 2941716 ns/iter (± 2836) 3068301 ns/iter (± 4015) 0.96
es/codegen/colors 737811 ns/iter (± 403456) 719448 ns/iter (± 397745) 1.03
es/codegen/large 3162189 ns/iter (± 1679780) 2890783 ns/iter (± 1526670) 1.09
es/codegen/with-parser/colors 45611 ns/iter (± 578) 45345 ns/iter (± 88) 1.01
es/codegen/with-parser/large 486716 ns/iter (± 903) 491684 ns/iter (± 491) 0.99
es/minify/libraries/antd 1164093851 ns/iter (± 17997715) 1221283888 ns/iter (± 20818333) 0.95
es/minify/libraries/d3 240159611 ns/iter (± 1794538) 242637769 ns/iter (± 1214776) 0.99
es/minify/libraries/echarts 924566326 ns/iter (± 17232739) 938389318 ns/iter (± 7996157) 0.99
es/minify/libraries/jquery 72977576 ns/iter (± 201554) 75462964 ns/iter (± 1055109) 0.97
es/minify/libraries/lodash 87033950 ns/iter (± 515385) 88713829 ns/iter (± 330523) 0.98
es/minify/libraries/moment 43438387 ns/iter (± 180178) 44036907 ns/iter (± 299934) 0.99
es/minify/libraries/react 15933941 ns/iter (± 65751) 16088190 ns/iter (± 39085) 0.99
es/minify/libraries/terser 188461928 ns/iter (± 3070853) 188721415 ns/iter (± 1525543) 1.00
es/minify/libraries/three 323454261 ns/iter (± 2529282) 324028473 ns/iter (± 1462053) 1.00
es/minify/libraries/typescript 2276968302 ns/iter (± 19646646) 2261348417 ns/iter (± 8764552) 1.01
es/minify/libraries/victory 480564388 ns/iter (± 6648337) 476719694 ns/iter (± 1191663) 1.01
es/minify/libraries/vue 106313387 ns/iter (± 735255) 107062977 ns/iter (± 245320) 0.99
es/visitor/compare/clone 1941254 ns/iter (± 2873) 1935829 ns/iter (± 2381) 1.00
es/visitor/compare/visit_mut_span 2287863 ns/iter (± 7683) 2276908 ns/iter (± 4413) 1.00
es/visitor/compare/visit_mut_span_panic 2324843 ns/iter (± 2816) 2322928 ns/iter (± 2090) 1.00
es/visitor/compare/fold_span 3377776 ns/iter (± 17197) 3387047 ns/iter (± 8234) 1.00
es/visitor/compare/fold_span_panic 3489558 ns/iter (± 6969) 3499996 ns/iter (± 4591) 1.00
es/lexer/colors 13029 ns/iter (± 43) 12762 ns/iter (± 34) 1.02
es/lexer/angular 6063492 ns/iter (± 16677) 6072341 ns/iter (± 13529) 1.00
es/lexer/backbone 788829 ns/iter (± 4393) 774517 ns/iter (± 2185) 1.02
es/lexer/jquery 4474719 ns/iter (± 11288) 4448339 ns/iter (± 13413) 1.01
es/lexer/jquery mobile 6922718 ns/iter (± 42960) 6791351 ns/iter (± 12228) 1.02
es/lexer/mootools 3519406 ns/iter (± 9592) 3509842 ns/iter (± 3835) 1.00
es/lexer/underscore 654900 ns/iter (± 1363) 651092 ns/iter (± 888) 1.01
es/lexer/three 21196250 ns/iter (± 41334) 21073747 ns/iter (± 30099) 1.01
es/lexer/yui 3783823 ns/iter (± 3184) 3785242 ns/iter (± 16130) 1.00
es/parser/colors 28291 ns/iter (± 91) 27372 ns/iter (± 79) 1.03
es/parser/angular 13621805 ns/iter (± 91167) 13629453 ns/iter (± 59427) 1.00
es/parser/backbone 1983677 ns/iter (± 9037) 2004740 ns/iter (± 7106) 0.99
es/parser/jquery 10880548 ns/iter (± 70520) 10966251 ns/iter (± 64688) 0.99
es/parser/jquery mobile 16661017 ns/iter (± 173598) 16743168 ns/iter (± 37863) 1.00
es/parser/mootools 8356641 ns/iter (± 24815) 8440413 ns/iter (± 48990) 0.99
es/parser/underscore 1711567 ns/iter (± 8641) 1731649 ns/iter (± 8169) 0.99
es/parser/three 47204900 ns/iter (± 505415) 48901875 ns/iter (± 576228) 0.97
es/parser/yui 8288570 ns/iter (± 58025) 8355963 ns/iter (± 51729) 0.99
es/preset-env/usage/builtin_type 135793 ns/iter (± 31720) 135868 ns/iter (± 32178) 1.00
es/preset-env/usage/property 17232 ns/iter (± 40) 16646 ns/iter (± 127) 1.04
es/resolver/typescript 90550822 ns/iter (± 710154) 91162423 ns/iter (± 1187244) 0.99
es/fixer/typescript 64759963 ns/iter (± 896710) 64308060 ns/iter (± 1149032) 1.01
es/hygiene/typescript 132069356 ns/iter (± 2128727) 129681341 ns/iter (± 3147050) 1.02
es/resolver_with_hygiene/typescript 244278934 ns/iter (± 1588661) 241488906 ns/iter (± 1096773) 1.01
es/visitor/base-perf/module_clone 58749 ns/iter (± 229) 58466 ns/iter (± 316) 1.00
es/visitor/base-perf/fold_empty 63625 ns/iter (± 337) 62559 ns/iter (± 230) 1.02
es/visitor/base-perf/fold_noop_impl_all 63135 ns/iter (± 270) 62930 ns/iter (± 275) 1.00
es/visitor/base-perf/fold_noop_impl_vec 63398 ns/iter (± 847) 62679 ns/iter (± 223) 1.01
es/visitor/base-perf/boxing_boxed_clone 57 ns/iter (± 0) 55 ns/iter (± 0) 1.04
es/visitor/base-perf/boxing_unboxed_clone 40 ns/iter (± 0) 40 ns/iter (± 0) 1
es/visitor/base-perf/boxing_boxed 109 ns/iter (± 0) 109 ns/iter (± 0) 1
es/visitor/base-perf/boxing_unboxed 79 ns/iter (± 0) 78 ns/iter (± 0) 1.01
es/visitor/base-perf/visit_empty 0 ns/iter (± 0) 0 ns/iter (± 0) NaN
es/visitor/base-perf/visit_contains_this 2573 ns/iter (± 45) 2521 ns/iter (± 9) 1.02
es/base/parallel/resolver/typescript 4467338973 ns/iter (± 229527541) 4514988160 ns/iter (± 193417253) 0.99
es/base/parallel/hygiene/typescript 1420449524 ns/iter (± 27225685) 1462564280 ns/iter (± 14119610) 0.97
misc/visitors/time-complexity/time 5 119 ns/iter (± 6) 136 ns/iter (± 6) 0.88
misc/visitors/time-complexity/time 10 306 ns/iter (± 2) 398 ns/iter (± 3) 0.77
misc/visitors/time-complexity/time 15 591 ns/iter (± 24) 676 ns/iter (± 5) 0.87
misc/visitors/time-complexity/time 20 1071 ns/iter (± 48) 1169 ns/iter (± 2) 0.92
misc/visitors/time-complexity/time 40 4200 ns/iter (± 2) 3564 ns/iter (± 57) 1.18
misc/visitors/time-complexity/time 60 8852 ns/iter (± 6) 7384 ns/iter (± 69) 1.20
es/full-target/es2016 230190 ns/iter (± 871) 229257 ns/iter (± 709) 1.00
es/full-target/es2017 220799 ns/iter (± 1012) 218712 ns/iter (± 823) 1.01
es/full-target/es2018 208796 ns/iter (± 1257) 208881 ns/iter (± 911) 1.00
es2020_nullish_coalescing 71417 ns/iter (± 538) 72415 ns/iter (± 419) 0.99
es2020_optional_chaining 81363 ns/iter (± 407) 80016 ns/iter (± 207) 1.02
es2022_class_properties 115210 ns/iter (± 425) 116106 ns/iter (± 385) 0.99
es2018_object_rest_spread 76057 ns/iter (± 266) 75402 ns/iter (± 328) 1.01
es2019_optional_catch_binding 64339 ns/iter (± 248) 67111 ns/iter (± 260) 0.96
es2017_async_to_generator 65660 ns/iter (± 216) 64658 ns/iter (± 171) 1.02
es2016_exponentiation 69570 ns/iter (± 213) 66595 ns/iter (± 176) 1.04
es2015_arrow 72826 ns/iter (± 143) 72603 ns/iter (± 262) 1.00
es2015_block_scoped_fn 71212 ns/iter (± 191) 68656 ns/iter (± 210) 1.04
es2015_block_scoping 124225 ns/iter (± 452) 123717 ns/iter (± 645) 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.