Skip to content

Commit

Permalink
fix(es/transforms/compat): Fix destructuring (#2866)
Browse files Browse the repository at this point in the history
swc_ecma_transforms_compat:
 - Check length correctly. (Closes #2819, Closes #2821, Closes #2843)
  • Loading branch information
RiESAEX authored Nov 25, 2021
1 parent 2c9de7d commit a6398e9
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function f0() {
1,
"hello"
], x = ref4[0], y = ref4[1], z = ref4[2];
var x = 0;
var x = 2;
var x;
var y;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,9 @@ var ref6 = [
2,
3
], c7 = ref6[1];
var c8 = 1;
var c9 = 1;
var c8 = 4;
var c9 = 4;
var c10 = [
1,
2,
3,
4,
"hello"
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,9 @@ var ref6 = [
2,
3
], c7 = ref6[1];
var c8 = 1;
var c9 = 1;
var c8 = 4;
var c9 = 4;
var c10 = [
1,
2,
3,
4,
"hello"
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,9 @@ var ref6 = [
2,
3
], c7 = ref6[1];
var c8 = 1;
var c9 = 1;
var c8 = 4;
var c9 = 4;
var c10 = [
1,
2,
3,
4,
"hello"
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@ var tmp = 3, b0 = tmp === void 0 ? 3 : tmp, tmp1 = false, b1 = tmp1 === void 0 ?
// The type T associated with a binding element is determined as follows:
// If the binding element is a rest element, T is an array type with
// an element type E, where E is the type of the numeric index signature of S.
var c1 = 1, c2 = 2, ref1 = {
c3: 4,
c5: 0
}, c4 = ref1.c3, c5 = ref1.c5, c6 = []; // Error
var ref1 = [
1,
2,
{
c3: 4,
c5: 0
}
], c1 = ref1[0], c2 = ref1[1], ref2 = ref1[2], c4 = ref2.c3, c5 = ref2.c5, c6 = ref1.slice(4); // Error
// When a destructuring variable declaration, binding property, or binding element specifies
// an initializer expression, the type of the initializer expression is required to be assignable
// to the widened form of the type associated with the destructuring variable declaration, binding property, or binding element.
var ref2 = {
var ref3 = {
d: {
d1: [
1,
2
]
}
}, _d = ref2.d, _d1 = _d.d1, d1 = _d1 === void 0 ? [
}, _d = ref3.d, _d1 = _d.d1, d1 = _d1 === void 0 ? [
"string",
null
] : _d1; // Error
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,16 @@ var ref = {
a2: 1
};
ref.a1, ref.a2;
var ref1 = [
1,
2,
{
c3: 4,
c5: 0
}
], c1 = ref1[0], c2 = ref1[1], ref2 = ref1[2];
ref2.c3, ref2.c5, ref1.slice(4), void 0 === _d1;
var _d1 = [
1,
2
]; // Error
10 changes: 8 additions & 2 deletions crates/swc_ecma_transforms_compat/src/es2015/destructuring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ impl AssignFolder {
if is_literal(&init) {
match *init {
Expr::Array(arr)
if elems.len() == arr.elems.len() || has_rest_pat(&elems) =>
if elems.len() == arr.elems.len()
|| (elems.len() < arr.elems.len() && has_rest_pat(&elems)) =>
{
let mut arr_elems = Some(arr.elems.into_iter());
elems.into_iter().for_each(|p| match p {
Expand Down Expand Up @@ -193,7 +194,12 @@ impl AssignFolder {
)
}

None => {}
None => {
arr_elems
.as_mut()
.expect("pattern after rest element?")
.next();
}
});
return;
}
Expand Down
20 changes: 20 additions & 0 deletions crates/swc_ecma_transforms_compat/tests/es2015_destructuring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,26 @@ fn tr() -> impl Fold {
destructuring(Config { loose: true })
}

test!(
syntax(),
|_| tr(),
issue_2819,
r#"const [first, , third] = ["red", "yellow", "green"]"#,
r#"const first = "red", third = "green";"#,
ok_if_code_eq
);

test!(
syntax(),
|_| tr(),
issue_2821,
r#"const [x, y, ...z] = [1];"#,
r#"const ref = [
1
], x = ref[0], y = ref[1], z = ref.slice(2)"#,
ok_if_code_eq
);

test!(
syntax(),
|_| tr(),
Expand Down

1 comment on commit a6398e9

@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: a6398e9 Previous: 2c9de7d Ratio
base_tr_fixer 26916 ns/iter (± 1199) 27166 ns/iter (± 349) 0.99
base_tr_resolver_and_hygiene 153797 ns/iter (± 18036) 151195 ns/iter (± 28047) 1.02
codegen_es2015 57936 ns/iter (± 4840) 60378 ns/iter (± 323) 0.96
codegen_es2016 58990 ns/iter (± 3455) 60105 ns/iter (± 522) 0.98
codegen_es2017 58895 ns/iter (± 2790) 60595 ns/iter (± 492) 0.97
codegen_es2018 59866 ns/iter (± 4719) 60481 ns/iter (± 298) 0.99
codegen_es2019 57367 ns/iter (± 5698) 60022 ns/iter (± 528) 0.96
codegen_es2020 56912 ns/iter (± 5501) 60617 ns/iter (± 425) 0.94
codegen_es3 58322 ns/iter (± 5443) 60382 ns/iter (± 344) 0.97
codegen_es5 57533 ns/iter (± 5663) 60326 ns/iter (± 292) 0.95
full_es2015 205328607 ns/iter (± 13633452) 208529318 ns/iter (± 6754933) 0.98
full_es2016 164814955 ns/iter (± 11793793) 165177690 ns/iter (± 4385229) 1.00
full_es2017 172293412 ns/iter (± 10235110) 174010524 ns/iter (± 8293877) 0.99
full_es2018 172393959 ns/iter (± 11068356) 173015907 ns/iter (± 10495719) 1.00
full_es2019 168169699 ns/iter (± 7614216) 171474689 ns/iter (± 9863420) 0.98
full_es2020 168840329 ns/iter (± 11086763) 172057970 ns/iter (± 8277205) 0.98
full_es3 238859573 ns/iter (± 21609186) 243387923 ns/iter (± 14822506) 0.98
full_es5 222203175 ns/iter (± 16028380) 224066648 ns/iter (± 11760313) 0.99
parser 754807 ns/iter (± 42211) 758180 ns/iter (± 14219) 1.00
ser_ast_node 179 ns/iter (± 25) 179 ns/iter (± 3) 1
ser_serde 190 ns/iter (± 15) 190 ns/iter (± 2) 1
emit_colors 23340420 ns/iter (± 30495165) 21975166 ns/iter (± 28714137) 1.06
emit_large 132825505 ns/iter (± 195650796) 109245231 ns/iter (± 167962936) 1.22
base_clone 2573520 ns/iter (± 240829) 2666569 ns/iter (± 54234) 0.97
fold_span 4474574 ns/iter (± 579389) 4493000 ns/iter (± 174097) 1.00
fold_span_panic 4566285 ns/iter (± 584580) 4755525 ns/iter (± 104453) 0.96
visit_mut_span 3146655 ns/iter (± 287175) 3250149 ns/iter (± 153726) 0.97
visit_mut_span_panic 3216370 ns/iter (± 321573) 3306490 ns/iter (± 69323) 0.97
ast_clone 19956 ns/iter (± 859) 19593 ns/iter (± 228) 1.02
ast_clone_to_stable 59170 ns/iter (± 4303) 59530 ns/iter (± 786) 0.99
ast_clone_to_stable_then_to_unstable 109185 ns/iter (± 2729) 109161 ns/iter (± 1236) 1.00
json_deserialize 2298419 ns/iter (± 49757) 2303634 ns/iter (± 9997) 1.00
json_serialize 101059 ns/iter (± 5976) 100831 ns/iter (± 1007) 1.00
boxing_boxed 172 ns/iter (± 12) 172 ns/iter (± 0) 1
boxing_boxed_clone 80 ns/iter (± 5) 81 ns/iter (± 0) 0.99
boxing_unboxed 154 ns/iter (± 8) 154 ns/iter (± 0) 1
boxing_unboxed_clone 85 ns/iter (± 6) 77 ns/iter (± 0) 1.10
time_10 364 ns/iter (± 53) 369 ns/iter (± 4) 0.99
time_15 831 ns/iter (± 85) 763 ns/iter (± 3) 1.09
time_20 1332 ns/iter (± 67) 1391 ns/iter (± 14) 0.96
time_40 4818 ns/iter (± 242) 5021 ns/iter (± 78) 0.96
time_5 120 ns/iter (± 13) 121 ns/iter (± 1) 0.99
time_60 9985 ns/iter (± 590) 10419 ns/iter (± 112) 0.96

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

Please sign in to comment.