Skip to content

Commit

Permalink
fix(es/transforms/compat): Fix destructuring (#2904)
Browse files Browse the repository at this point in the history
swc_ecma_transforms_compat:
 - `desturcturing`: Fix non-loose mode. (Closes #2841, Closes #2863)
  • Loading branch information
RiESAEX authored Nov 30, 2021
1 parent 073c378 commit 0c1f2eb
Show file tree
Hide file tree
Showing 63 changed files with 1,509 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,35 @@
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArrayLimit(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for(var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true){
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally{
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally{
if (_d) throw _e;
}
}
return _arr;
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
}
var a;
var x;
var ref;
ref = a, x = ref[0], ref;
ref = _slicedToArray(a, 1), x = ref[0], ref;
Original file line number Diff line number Diff line change
@@ -1 +1,20 @@
(void 0)[0];
var arr, i;
(i = 1, (function(arr) {
if (Array.isArray(arr)) return arr;
})(arr = void 0) || (function(arr, i) {
var _arr = [], _n = !0, _d = !1, _e = void 0;
try {
for(var _s, _i = arr[Symbol.iterator](); !(_n = (_s = _i.next()).done) && (_arr.push(_s.value), !i || _arr.length !== i); _n = !0);
} catch (err) {
_d = !0, _e = err;
} finally{
try {
_n || null == _i.return || _i.return();
} finally{
if (_d) throw _e;
}
}
return _arr;
})(arr, i) || (function() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
})())[0];
Original file line number Diff line number Diff line change
Expand Up @@ -332,13 +332,13 @@ function f20(v) {
var _v2 = _toArray(v), x = _v2[0], y = _v2[1], a1 = _v2.slice(2);
var _v3 = _toArray(v), x = _v3[0], y = _v3[1], z = _v3[2], a0 = _v3.slice(3);
var ref;
ref = v, a3 = ref.slice(0), ref;
ref = _toArray(v), a3 = ref.slice(0), ref;
var ref23;
ref23 = v, x = ref23[0], a2 = ref23.slice(1), ref23;
ref23 = _toArray(v), x = ref23[0], a2 = ref23.slice(1), ref23;
var ref24;
ref24 = v, x = ref24[0], y = ref24[1], a1 = ref24.slice(2), ref24;
ref24 = _toArray(v), x = ref24[0], y = ref24[1], a1 = ref24.slice(2), ref24;
var ref25;
ref25 = v, x = ref25[0], y = ref25[1], z = ref25[2], a0 = ref25.slice(3), ref25;
ref25 = _toArray(v), x = ref25[0], y = ref25[1], z = ref25[2], a0 = ref25.slice(3), ref25;
}
function f21(v) {
var x;
Expand All @@ -353,11 +353,11 @@ function f21(v) {
var _v5 = _toArray(v), x = _v5[0], y = _v5[1], a2 = _v5.slice(2);
var _v6 = _toArray(v), x = _v6[0], y = _v6[1], z = _v6[2], a3 = _v6.slice(3);
var ref;
ref = v, a0 = ref.slice(0), ref;
ref = _toArray(v), a0 = ref.slice(0), ref;
var ref26;
ref26 = v, x = ref26[0], a1 = ref26.slice(1), ref26;
ref26 = _toArray(v), x = ref26[0], a1 = ref26.slice(1), ref26;
var ref27;
ref27 = v, x = ref27[0], y = ref27[1], a2 = ref27.slice(2), ref27;
ref27 = _toArray(v), x = ref27[0], y = ref27[1], a2 = ref27.slice(2), ref27;
var ref28;
ref28 = v, x = ref28[0], y = ref28[1], z = ref28[2], a3 = ref28.slice(3), ref28;
ref28 = _toArray(v), x = ref28[0], y = ref28[1], z = ref28[2], a3 = ref28.slice(3), ref28;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ function f2(obj) {
var a1 = obj[1]; // string | null
var _obj = _slicedToArray(obj, 2), b0 = _obj[0], b1 = _obj[1];
var ref;
ref = obj, a0 = ref[0], a1 = ref[1], ref;
ref = _slicedToArray(obj, 2), a0 = ref[0], a1 = ref[1], ref;
if (obj[0] && obj[1]) {
var c0 = obj[0]; // number
var c1 = obj[1]; // string
var _obj1 = _slicedToArray(obj, 2), d0 = _obj1[0], d1 = _obj1[1];
var ref1;
ref1 = obj, c0 = ref1[0], c1 = ref1[1], ref1;
ref1 = _slicedToArray(obj, 2), c0 = ref1[0], c1 = ref1[1], ref1;
}
}
function f3(obj) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArrayLimit(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for(var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true){
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally{
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally{
if (_d) throw _e;
}
}
return _arr;
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
}
// @target: es5
// @declaration: true
var a;
var ref;
ref = a, ref;
var ref1;
ref1 = a, ref1;
ref1 = _slicedToArray(a, 0), ref1;
var ref2 = [
1,
2
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var arr, i;
i = 0, (function(arr) {
if (Array.isArray(arr)) return arr;
})(arr = void 0) || (function(arr, i) {
var _arr = [], _n = !0, _d = !1, _e = void 0;
try {
for(var _s, _i = arr[Symbol.iterator](); !(_n = (_s = _i.next()).done) && (_arr.push(_s.value), !i || _arr.length !== i); _n = !0);
} catch (err) {
_d = !0, _e = err;
} finally{
try {
_n || null == _i.return || _i.return();
} finally{
if (_d) throw _e;
}
}
return _arr;
})(arr, i) || (function() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
})();
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArrayLimit(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for(var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true){
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally{
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally{
if (_d) throw _e;
}
}
return _arr;
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
}
// @target: es5
// @declaration: true
// @downlevelIteration: true
var a;
var ref;
ref = a, ref;
var ref1;
ref1 = a, ref1;
ref1 = _slicedToArray(a, 0), ref1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var arr, i;
i = 0, (function(arr) {
if (Array.isArray(arr)) return arr;
})(arr = void 0) || (function(arr, i) {
var _arr = [], _n = !0, _d = !1, _e = void 0;
try {
for(var _s, _i = arr[Symbol.iterator](); !(_n = (_s = _i.next()).done) && (_arr.push(_s.value), !i || _arr.length !== i); _n = !0);
} catch (err) {
_d = !0, _e = err;
} finally{
try {
_n || null == _i.return || _i.return();
} finally{
if (_d) throw _e;
}
}
return _arr;
})(arr, i) || (function() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
})();
Original file line number Diff line number Diff line change
@@ -1,7 +1,38 @@
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArrayLimit(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for(var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true){
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally{
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally{
if (_d) throw _e;
}
}
return _arr;
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
}
// @target: es6
// @declaration: true
var a;
var ref;
ref = a, ref;
var ref1;
ref1 = a, ref1;
ref1 = _slicedToArray(a, 0), ref1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var arr, i;
i = 0, (function(arr) {
if (Array.isArray(arr)) return arr;
})(arr = void 0) || (function(arr, i) {
var _arr = [], _n = !0, _d = !1, _e = void 0;
try {
for(var _s, _i = arr[Symbol.iterator](); !(_n = (_s = _i.next()).done) && (_arr.push(_s.value), !i || _arr.length !== i); _n = !0);
} catch (err) {
_d = !0, _e = err;
} finally{
try {
_n || null == _i.return || _i.return();
} finally{
if (_d) throw _e;
}
}
return _arr;
})(arr, i) || (function() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
})();
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
function _arrayWithHoles(arr) {
if (Array.isArray(arr)) return arr;
}
function _iterableToArrayLimit(arr, i) {
var _arr = [];
var _n = true;
var _d = false;
var _e = undefined;
try {
for(var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true){
_arr.push(_s.value);
if (i && _arr.length === i) break;
}
} catch (err) {
_d = true;
_e = err;
} finally{
try {
if (!_n && _i["return"] != null) _i["return"]();
} finally{
if (_d) throw _e;
}
}
return _arr;
}
function _nonIterableRest() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
}
function _slicedToArray(arr, i) {
return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest();
}
// @target: es5
// @declaration: true
var a;
var x, y, z, a1, a2, a3;
var ref, ref1;
ref1 = (ref = a, x = ref.x, y = ref.y, z = ref.z, ref), ref1;
var ref2, ref3;
ref3 = (ref2 = a, a1 = ref2[0], a2 = ref2[1], a3 = ref2[2], ref2), ref3;
ref3 = _slicedToArray((ref2 = _slicedToArray(a, 3), a1 = ref2[0], a2 = ref2[1], a3 = ref2[2], ref2), 0), ref3;
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
var a, ref, ref1;
(ref = a).x, ref.y, ref.z, (ref1 = a)[0], ref1[1], ref1[2];
function _slicedToArray(arr, i) {
return (function(arr) {
if (Array.isArray(arr)) return arr;
})(arr) || (function(arr, i) {
var _arr = [], _n = !0, _d = !1, _e = void 0;
try {
for(var _s, _i = arr[Symbol.iterator](); !(_n = (_s = _i.next()).done) && (_arr.push(_s.value), !i || _arr.length !== i); _n = !0);
} catch (err) {
_d = !0, _e = err;
} finally{
try {
_n || null == _i.return || _i.return();
} finally{
if (_d) throw _e;
}
}
return _arr;
})(arr, i) || (function() {
throw new TypeError("Invalid attempt to destructure non-iterable instance");
})();
}
(ref = a).x, ref.y, ref.z, _slicedToArray(((ref1 = _slicedToArray(a, 3))[0], ref1[1], ref1[2], ref1), 0);
Loading

2 comments on commit 0c1f2eb

@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: 0c1f2eb Previous: 028d0ce Ratio
base_tr_fixer 26214 ns/iter (± 2238) 25549 ns/iter (± 2288) 1.03
base_tr_resolver_and_hygiene 157734 ns/iter (± 7096) 156391 ns/iter (± 11914) 1.01
codegen_es2015 60604 ns/iter (± 1839) 58224 ns/iter (± 5363) 1.04
codegen_es2016 60178 ns/iter (± 1802) 56387 ns/iter (± 13775) 1.07
codegen_es2017 57352 ns/iter (± 3697) 59270 ns/iter (± 5998) 0.97
codegen_es2018 59072 ns/iter (± 2444) 57604 ns/iter (± 4603) 1.03
codegen_es2019 59773 ns/iter (± 4065) 59627 ns/iter (± 5093) 1.00
codegen_es2020 58947 ns/iter (± 1996) 58763 ns/iter (± 4012) 1.00
codegen_es3 59749 ns/iter (± 2235) 57589 ns/iter (± 5438) 1.04
codegen_es5 59727 ns/iter (± 2533) 59186 ns/iter (± 5620) 1.01
full_es2015 205532445 ns/iter (± 10127881) 203272461 ns/iter (± 13445180) 1.01
full_es2016 166106500 ns/iter (± 8838306) 166720190 ns/iter (± 12917291) 1.00
full_es2017 175147187 ns/iter (± 15543417) 172793804 ns/iter (± 8730481) 1.01
full_es2018 176808307 ns/iter (± 7732845) 170826603 ns/iter (± 10976370) 1.04
full_es2019 172383489 ns/iter (± 7260799) 170808879 ns/iter (± 12256751) 1.01
full_es2020 171894193 ns/iter (± 13445366) 167974141 ns/iter (± 10435754) 1.02
full_es3 245379097 ns/iter (± 18682772) 238566509 ns/iter (± 18931466) 1.03
full_es5 228844045 ns/iter (± 12467285) 219160850 ns/iter (± 18653659) 1.04
parser 777327 ns/iter (± 37967) 761678 ns/iter (± 71072) 1.02
ser_ast_node 175 ns/iter (± 8) 167 ns/iter (± 19) 1.05
ser_serde 177 ns/iter (± 6) 171 ns/iter (± 18) 1.04
emit_colors 19834451 ns/iter (± 25679064) 5092563 ns/iter (± 3869915) 3.89
emit_large 105398381 ns/iter (± 160037359) 124652448 ns/iter (± 183307347) 0.85
base_clone 2955990 ns/iter (± 199392) 2683281 ns/iter (± 163463) 1.10
fold_span 4825818 ns/iter (± 632443) 4333699 ns/iter (± 457838) 1.11
fold_span_panic 4820092 ns/iter (± 495848) 4468267 ns/iter (± 453939) 1.08
visit_mut_span 3402290 ns/iter (± 226174) 3264143 ns/iter (± 315673) 1.04
visit_mut_span_panic 3487103 ns/iter (± 288807) 3282904 ns/iter (± 256781) 1.06
ast_clone 19826 ns/iter (± 4208) 19310 ns/iter (± 2170) 1.03
ast_clone_to_stable 56907 ns/iter (± 3479) 58268 ns/iter (± 5914) 0.98
ast_clone_to_stable_then_to_unstable 108462 ns/iter (± 8460) 107257 ns/iter (± 10923) 1.01
json_deserialize 2236473 ns/iter (± 158656) 2175393 ns/iter (± 226826) 1.03
json_serialize 104357 ns/iter (± 8085) 106174 ns/iter (± 13749) 0.98
boxing_boxed 148 ns/iter (± 6) 134 ns/iter (± 12) 1.10
boxing_boxed_clone 81 ns/iter (± 2) 74 ns/iter (± 7) 1.09
boxing_unboxed 132 ns/iter (± 1) 125 ns/iter (± 14) 1.06
boxing_unboxed_clone 74 ns/iter (± 1) 69 ns/iter (± 7) 1.07
time_10 379 ns/iter (± 4) 352 ns/iter (± 39) 1.08
time_15 791 ns/iter (± 4) 764 ns/iter (± 107) 1.04
time_20 1369 ns/iter (± 56) 1367 ns/iter (± 77) 1.00
time_40 4947 ns/iter (± 47) 4828 ns/iter (± 339) 1.02
time_5 120 ns/iter (± 1) 120 ns/iter (± 5) 1
time_60 10355 ns/iter (± 154) 10279 ns/iter (± 333) 1.01

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

@github-actions
Copy link

Choose a reason for hiding this comment

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

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 0c1f2eb Previous: 028d0ce Ratio
emit_colors 19834451 ns/iter (± 25679064) 5092563 ns/iter (± 3869915) 3.89

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

CC: @kdy1

Please sign in to comment.