From c7410cc9cb228b18ee4305052e1059d59c1a8031 Mon Sep 17 00:00:00 2001 From: austaras Date: Thu, 17 Nov 2022 02:42:35 +0800 Subject: [PATCH] fix(es/compat): Consecutive optional call --- .../controlFlowOptionalChain.1.normal.js | 16 +++++------ .../controlFlowOptionalChain.2.minified.js | 4 +-- .../tsc-references/deleteChain.1.normal.js | 16 +++++------ .../tsc-references/deleteChain.2.minified.js | 4 +-- .../elementAccessChain.1.normal.js | 14 +++++----- .../elementAccessChain.2.minified.js | 4 +-- .../exhaustiveSwitchStatements1.1.normal.js | 4 +-- ...icalAssignment8(target=es2015).1.normal.js | 12 ++++---- .../nullishCoalescingOperator12.1.normal.js | 4 +-- .../nullishCoalescingOperator12.2.minified.js | 4 +-- .../propertyAccessChain.1.normal.js | 6 ++-- .../propertyAccessChain.2.minified.js | 4 +-- .../src/es2020/opt_chaining.rs | 10 ++++--- .../tests/es2020_optional_chaining.rs | 19 +++++++++---- crates/swc_ecma_utils/src/lib.rs | 28 +++++++++++++++++-- 15 files changed, 90 insertions(+), 59 deletions(-) diff --git a/crates/swc/tests/tsc-references/controlFlowOptionalChain.1.normal.js b/crates/swc/tests/tsc-references/controlFlowOptionalChain.1.normal.js index 32ddc6f4a8a8..3042390257f1 100644 --- a/crates/swc/tests/tsc-references/controlFlowOptionalChain.1.normal.js +++ b/crates/swc/tests/tsc-references/controlFlowOptionalChain.1.normal.js @@ -2,7 +2,7 @@ // assignments in shortcutting chain import _instanceof from "@swc/helpers/src/_instanceof.mjs"; import _type_of from "@swc/helpers/src/_type_of.mjs"; -var _o4_x, _o4_x1, _o5_x, _ref, _o5_x1, _o5_x2, _o5_x3, _ref1, _arr_i; +var _o4_x, _o4_x1, _o5_x_y_z, _o5_x, _o5_x1, _o5_x2, _o5_x_y_z1, _o5_x3, _arr_i; var a; o === null || o === void 0 ? void 0 : o[a = 1]; a.toString(); @@ -68,28 +68,28 @@ if ((_o4_x = o4.x) === null || _o4_x === void 0 ? void 0 : _o4_x.y) { o4.x; (_o4_x1 = o4.x) === null || _o4_x1 === void 0 ? void 0 : _o4_x1.y; o4.x.y; -if ((_ref = (_o5_x = o5.x) === null || _o5_x === void 0 ? void 0 : _o5_x.y.z) === null || _ref === void 0 ? void 0 : _ref.w) { - var _o5_x_y_z, _o5_x4, _o5_x5, _ref2; +if ((_o5_x_y_z = (_o5_x = o5.x) === null || _o5_x === void 0 ? void 0 : _o5_x.y.z) === null || _o5_x_y_z === void 0 ? void 0 : _o5_x_y_z.w) { + var _o5_x_y_z2, _o5_x4, _o5_x_y_z3, _o5_x5; o5.x; o5.x.y; o5.x.y.z; o5.x.y.z.w; // true - (_o5_x_y_z = o5.x.y.z) === null || _o5_x_y_z === void 0 ? void 0 : _o5_x_y_z.w; // true + (_o5_x_y_z2 = o5.x.y.z) === null || _o5_x_y_z2 === void 0 ? void 0 : _o5_x_y_z2.w; // true (_o5_x4 = o5.x) === null || _o5_x4 === void 0 ? void 0 : _o5_x4.y.z.w; // true - (_ref2 = (_o5_x5 = o5.x) === null || _o5_x5 === void 0 ? void 0 : _o5_x5.y.z) === null || _ref2 === void 0 ? void 0 : _ref2.w; // true + (_o5_x_y_z3 = (_o5_x5 = o5.x) === null || _o5_x5 === void 0 ? void 0 : _o5_x5.y.z) === null || _o5_x_y_z3 === void 0 ? void 0 : _o5_x_y_z3.w; // true } else { - var _o5_x6, _o5_x7, _o5_x8, _ref3; + var _o5_x6, _o5_x7, _o5_x_y_z4, _o5_x8; o5.x; (_o5_x6 = o5.x) === null || _o5_x6 === void 0 ? void 0 : _o5_x6.y; (_o5_x7 = o5.x) === null || _o5_x7 === void 0 ? void 0 : _o5_x7.y.z; - (_ref3 = (_o5_x8 = o5.x) === null || _o5_x8 === void 0 ? void 0 : _o5_x8.y.z) === null || _ref3 === void 0 ? void 0 : _ref3.w; + (_o5_x_y_z4 = (_o5_x8 = o5.x) === null || _o5_x8 === void 0 ? void 0 : _o5_x8.y.z) === null || _o5_x_y_z4 === void 0 ? void 0 : _o5_x_y_z4.w; o5.x.y; o5.x.y.z.w; } o5.x; (_o5_x1 = o5.x) === null || _o5_x1 === void 0 ? void 0 : _o5_x1.y; (_o5_x2 = o5.x) === null || _o5_x2 === void 0 ? void 0 : _o5_x2.y.z; -(_ref1 = (_o5_x3 = o5.x) === null || _o5_x3 === void 0 ? void 0 : _o5_x3.y.z) === null || _ref1 === void 0 ? void 0 : _ref1.w; +(_o5_x_y_z1 = (_o5_x3 = o5.x) === null || _o5_x3 === void 0 ? void 0 : _o5_x3.y.z) === null || _o5_x_y_z1 === void 0 ? void 0 : _o5_x_y_z1.w; o5.x.y; o5.x.y.z.w; if (o6 === null || o6 === void 0 ? void 0 : o6.f()) { diff --git a/crates/swc/tests/tsc-references/controlFlowOptionalChain.2.minified.js b/crates/swc/tests/tsc-references/controlFlowOptionalChain.2.minified.js index 79b8ecd11f3c..f9022ded8b6c 100644 --- a/crates/swc/tests/tsc-references/controlFlowOptionalChain.2.minified.js +++ b/crates/swc/tests/tsc-references/controlFlowOptionalChain.2.minified.js @@ -2,7 +2,7 @@ function someFunction(someOptionalObject) { (null == someOptionalObject ? void 0 : someOptionalObject.someProperty) !== lastSomeProperty && (console.log(someOptionalObject), console.log(someOptionalObject.someProperty), lastSomeProperty = null == someOptionalObject ? void 0 : someOptionalObject.someProperty); } -null == o || o[a = 1], a.toString(), null == o || o.x[b = 1], b.toString(), null == o || o(c = 1), c.toString(), null == o || o.x(d = 1), d.toString(), null == f || f(x), f(x), f(x), (null == o2 ? void 0 : o2.f(x)) ? (o2.f, null == o2 || o2.f, null == o2 || o2.f(x)) : (null == o2 || o2.f, o2.f), null == o2 || o2.f, o2.f, (null == o3 ? void 0 : o3.x) === 1 ? (o3.x, null == o3 || o3.x) : (null == o3 || o3.x, o3.x), null == o3 || o3.x, o3.x, (null === (_o4_x = o4.x) || void 0 === _o4_x ? void 0 : _o4_x.y) ? (o4.x, o4.x.y, null === (_o4_x2 = o4.x) || void 0 === _o4_x2 || _o4_x2.y) : (o4.x, null === (_o4_x3 = o4.x) || void 0 === _o4_x3 || _o4_x3.y, o4.x.y), o4.x, null === (_o4_x1 = o4.x) || void 0 === _o4_x1 || _o4_x1.y, o4.x.y, (null === (_ref = null === (_o5_x = o5.x) || void 0 === _o5_x ? void 0 : _o5_x.y.z) || void 0 === _ref ? void 0 : _ref.w) ? (o5.x, o5.x.y, o5.x.y.z, o5.x.y.z.w, null === (_o5_x_y_z = o5.x.y.z) || void 0 === _o5_x_y_z || _o5_x_y_z.w, null === (_o5_x4 = o5.x) || void 0 === _o5_x4 || _o5_x4.y.z.w, null === (_ref2 = null === (_o5_x5 = o5.x) || void 0 === _o5_x5 ? void 0 : _o5_x5.y.z) || void 0 === _ref2 || _ref2.w) : (o5.x, null === (_o5_x6 = o5.x) || void 0 === _o5_x6 || _o5_x6.y, null === (_o5_x7 = o5.x) || void 0 === _o5_x7 || _o5_x7.y.z, null === (_ref3 = null === (_o5_x8 = o5.x) || void 0 === _o5_x8 ? void 0 : _o5_x8.y.z) || void 0 === _ref3 || _ref3.w, o5.x.y, o5.x.y.z.w), o5.x, null === (_o5_x1 = o5.x) || void 0 === _o5_x1 || _o5_x1.y, null === (_o5_x2 = o5.x) || void 0 === _o5_x2 || _o5_x2.y.z, null === (_ref1 = null === (_o5_x3 = o5.x) || void 0 === _o5_x3 ? void 0 : _o5_x3.y.z) || void 0 === _ref1 || _ref1.w, o5.x.y, o5.x.y.z.w, (null == o6 ? void 0 : o6.f()) || null == o6 || o6.f, o6.f, null == o6 || o6.f, o6.f, someFunction({ +null == o || o[a = 1], a.toString(), null == o || o.x[b = 1], b.toString(), null == o || o(c = 1), c.toString(), null == o || o.x(d = 1), d.toString(), null == f || f(x), f(x), f(x), (null == o2 ? void 0 : o2.f(x)) ? (o2.f, null == o2 || o2.f, null == o2 || o2.f(x)) : (null == o2 || o2.f, o2.f), null == o2 || o2.f, o2.f, (null == o3 ? void 0 : o3.x) === 1 ? (o3.x, null == o3 || o3.x) : (null == o3 || o3.x, o3.x), null == o3 || o3.x, o3.x, (null === (_o4_x = o4.x) || void 0 === _o4_x ? void 0 : _o4_x.y) ? (o4.x, o4.x.y, null === (_o4_x2 = o4.x) || void 0 === _o4_x2 || _o4_x2.y) : (o4.x, null === (_o4_x3 = o4.x) || void 0 === _o4_x3 || _o4_x3.y, o4.x.y), o4.x, null === (_o4_x1 = o4.x) || void 0 === _o4_x1 || _o4_x1.y, o4.x.y, (null === (_o5_x_y_z = null === (_o5_x = o5.x) || void 0 === _o5_x ? void 0 : _o5_x.y.z) || void 0 === _o5_x_y_z ? void 0 : _o5_x_y_z.w) ? (o5.x, o5.x.y, o5.x.y.z, o5.x.y.z.w, null === (_o5_x_y_z2 = o5.x.y.z) || void 0 === _o5_x_y_z2 || _o5_x_y_z2.w, null === (_o5_x4 = o5.x) || void 0 === _o5_x4 || _o5_x4.y.z.w, null === (_o5_x_y_z3 = null === (_o5_x5 = o5.x) || void 0 === _o5_x5 ? void 0 : _o5_x5.y.z) || void 0 === _o5_x_y_z3 || _o5_x_y_z3.w) : (o5.x, null === (_o5_x6 = o5.x) || void 0 === _o5_x6 || _o5_x6.y, null === (_o5_x7 = o5.x) || void 0 === _o5_x7 || _o5_x7.y.z, null === (_o5_x_y_z4 = null === (_o5_x8 = o5.x) || void 0 === _o5_x8 ? void 0 : _o5_x8.y.z) || void 0 === _o5_x_y_z4 || _o5_x_y_z4.w, o5.x.y, o5.x.y.z.w), o5.x, null === (_o5_x1 = o5.x) || void 0 === _o5_x1 || _o5_x1.y, null === (_o5_x2 = o5.x) || void 0 === _o5_x2 || _o5_x2.y.z, null === (_o5_x_y_z1 = null === (_o5_x3 = o5.x) || void 0 === _o5_x3 ? void 0 : _o5_x3.y.z) || void 0 === _o5_x_y_z1 || _o5_x_y_z1.w, o5.x.y, o5.x.y.z.w, (null == o6 ? void 0 : o6.f()) || null == o6 || o6.f, o6.f, null == o6 || o6.f, o6.f, someFunction({ someProperty: 42 }), someFunction(void 0); -for(var _o4_x, _o4_x1, _o5_x, _ref, _o5_x1, _o5_x2, _o5_x3, _ref1, _arr_i, a, b, c, d, _o4_x2, _o4_x3, _o5_x_y_z, _o5_x4, _o5_x5, _ref2, _o5_x6, _o5_x7, _o5_x8, _ref3, lastSomeProperty, _arr_i1, i = 0; (null === (_arr_i = arr[i]) || void 0 === _arr_i ? void 0 : _arr_i.tag) === "left";)i += 1, (null === (_arr_i1 = arr[i]) || void 0 === _arr_i1 ? void 0 : _arr_i1.tag) === "right" && console.log("I should ALSO be reachable"); +for(var _o4_x, _o4_x1, _o5_x_y_z, _o5_x, _o5_x1, _o5_x2, _o5_x_y_z1, _o5_x3, _arr_i, a, b, c, d, _o4_x2, _o4_x3, _o5_x_y_z2, _o5_x4, _o5_x_y_z3, _o5_x5, _o5_x6, _o5_x7, _o5_x_y_z4, _o5_x8, lastSomeProperty, _arr_i1, i = 0; (null === (_arr_i = arr[i]) || void 0 === _arr_i ? void 0 : _arr_i.tag) === "left";)i += 1, (null === (_arr_i1 = arr[i]) || void 0 === _arr_i1 ? void 0 : _arr_i1.tag) === "right" && console.log("I should ALSO be reachable"); diff --git a/crates/swc/tests/tsc-references/deleteChain.1.normal.js b/crates/swc/tests/tsc-references/deleteChain.1.normal.js index 1cdb6a25eba8..03736e44e3a6 100644 --- a/crates/swc/tests/tsc-references/deleteChain.1.normal.js +++ b/crates/swc/tests/tsc-references/deleteChain.1.normal.js @@ -1,15 +1,15 @@ //// [deleteChain.ts] -var _o3_b, _o3_b1, _o4_b, _ref, _o4_b1, _ref1, _o4_b2, _ref2, _o5_b, _ref3, _o5_b1, _ref4, _o6_b, _ref5, _o6_b1, _ref6; +var _o3_b, _o3_b1, _o4_b_c_d, _o4_b, _o4_b_c_d1, _o4_b1, _o4_b_c_d2, _o4_b2, _o5_b_c_d, _o5_b, _o5_b_c_d1, _o5_b1, _o6_b_c_d, _o6_b, _o6_b_c_d1, _o6_b1; o1 === null || o1 === void 0 ? void 0 : delete o1.b; delete (o1 === null || o1 === void 0 ? void 0 : o1.b); o2 === null || o2 === void 0 ? void 0 : delete o2.b.c; o2 === null || o2 === void 0 ? void 0 : delete o2.b.c; (_o3_b = o3.b) === null || _o3_b === void 0 ? void 0 : delete _o3_b.c; delete ((_o3_b1 = o3.b) === null || _o3_b1 === void 0 ? void 0 : _o3_b1.c); -(_ref = (_o4_b = o4.b) === null || _o4_b === void 0 ? void 0 : _o4_b.c.d) === null || _ref === void 0 ? void 0 : delete _ref.e; -(_ref1 = (_o4_b1 = o4.b) === null || _o4_b1 === void 0 ? void 0 : _o4_b1.c.d) === null || _ref1 === void 0 ? void 0 : delete _ref1.e; -delete ((_ref2 = (_o4_b2 = o4.b) === null || _o4_b2 === void 0 ? void 0 : _o4_b2.c.d) === null || _ref2 === void 0 ? void 0 : _ref2.e); -(_ref3 = (_o5_b = o5.b) === null || _o5_b === void 0 ? void 0 : _o5_b.call(o5).c.d) === null || _ref3 === void 0 ? void 0 : delete _ref3.e; -delete ((_ref4 = (_o5_b1 = o5.b) === null || _o5_b1 === void 0 ? void 0 : _o5_b1.call(o5).c.d) === null || _ref4 === void 0 ? void 0 : _ref4.e); -(_ref5 = (_o6_b = o6.b) === null || _o6_b === void 0 ? void 0 : _o6_b["c"].d) === null || _ref5 === void 0 ? void 0 : delete _ref5["e"]; -delete ((_ref6 = (_o6_b1 = o6.b) === null || _o6_b1 === void 0 ? void 0 : _o6_b1["c"].d) === null || _ref6 === void 0 ? void 0 : _ref6["e"]); +(_o4_b_c_d = (_o4_b = o4.b) === null || _o4_b === void 0 ? void 0 : _o4_b.c.d) === null || _o4_b_c_d === void 0 ? void 0 : delete _o4_b_c_d.e; +(_o4_b_c_d1 = (_o4_b1 = o4.b) === null || _o4_b1 === void 0 ? void 0 : _o4_b1.c.d) === null || _o4_b_c_d1 === void 0 ? void 0 : delete _o4_b_c_d1.e; +delete ((_o4_b_c_d2 = (_o4_b2 = o4.b) === null || _o4_b2 === void 0 ? void 0 : _o4_b2.c.d) === null || _o4_b_c_d2 === void 0 ? void 0 : _o4_b_c_d2.e); +(_o5_b_c_d = (_o5_b = o5.b) === null || _o5_b === void 0 ? void 0 : _o5_b.call(o5).c.d) === null || _o5_b_c_d === void 0 ? void 0 : delete _o5_b_c_d.e; +delete ((_o5_b_c_d1 = (_o5_b1 = o5.b) === null || _o5_b1 === void 0 ? void 0 : _o5_b1.call(o5).c.d) === null || _o5_b_c_d1 === void 0 ? void 0 : _o5_b_c_d1.e); +(_o6_b_c_d = (_o6_b = o6.b) === null || _o6_b === void 0 ? void 0 : _o6_b["c"].d) === null || _o6_b_c_d === void 0 ? void 0 : delete _o6_b_c_d["e"]; +delete ((_o6_b_c_d1 = (_o6_b1 = o6.b) === null || _o6_b1 === void 0 ? void 0 : _o6_b1["c"].d) === null || _o6_b_c_d1 === void 0 ? void 0 : _o6_b_c_d1["e"]); diff --git a/crates/swc/tests/tsc-references/deleteChain.2.minified.js b/crates/swc/tests/tsc-references/deleteChain.2.minified.js index 57a234985d4f..a31b3d6f43cd 100644 --- a/crates/swc/tests/tsc-references/deleteChain.2.minified.js +++ b/crates/swc/tests/tsc-references/deleteChain.2.minified.js @@ -1,3 +1,3 @@ //// [deleteChain.ts] -var _o3_b, _o3_b1, _o4_b, _ref, _o4_b1, _ref1, _o4_b2, _ref2, _o5_b, _ref3, _o5_b1, _ref4, _o6_b, _ref5, _o6_b1, _ref6; -null == o1 || delete o1.b, delete (null == o1 ? void 0 : o1.b), null == o2 || delete o2.b.c, null == o2 || delete o2.b.c, null === (_o3_b = o3.b) || void 0 === _o3_b || delete _o3_b.c, delete (null === (_o3_b1 = o3.b) || void 0 === _o3_b1 ? void 0 : _o3_b1.c), null === (_ref = null === (_o4_b = o4.b) || void 0 === _o4_b ? void 0 : _o4_b.c.d) || void 0 === _ref || delete _ref.e, null === (_ref1 = null === (_o4_b1 = o4.b) || void 0 === _o4_b1 ? void 0 : _o4_b1.c.d) || void 0 === _ref1 || delete _ref1.e, delete (null === (_ref2 = null === (_o4_b2 = o4.b) || void 0 === _o4_b2 ? void 0 : _o4_b2.c.d) || void 0 === _ref2 ? void 0 : _ref2.e), null === (_ref3 = null === (_o5_b = o5.b) || void 0 === _o5_b ? void 0 : _o5_b.call(o5).c.d) || void 0 === _ref3 || delete _ref3.e, delete (null === (_ref4 = null === (_o5_b1 = o5.b) || void 0 === _o5_b1 ? void 0 : _o5_b1.call(o5).c.d) || void 0 === _ref4 ? void 0 : _ref4.e), null === (_ref5 = null === (_o6_b = o6.b) || void 0 === _o6_b ? void 0 : _o6_b.c.d) || void 0 === _ref5 || delete _ref5.e, delete (null === (_ref6 = null === (_o6_b1 = o6.b) || void 0 === _o6_b1 ? void 0 : _o6_b1.c.d) || void 0 === _ref6 ? void 0 : _ref6.e); +var _o3_b, _o3_b1, _o4_b_c_d, _o4_b, _o4_b_c_d1, _o4_b1, _o4_b_c_d2, _o4_b2, _o5_b_c_d, _o5_b, _o5_b_c_d1, _o5_b1, _o6_b_c_d, _o6_b, _o6_b_c_d1, _o6_b1; +null == o1 || delete o1.b, delete (null == o1 ? void 0 : o1.b), null == o2 || delete o2.b.c, null == o2 || delete o2.b.c, null === (_o3_b = o3.b) || void 0 === _o3_b || delete _o3_b.c, delete (null === (_o3_b1 = o3.b) || void 0 === _o3_b1 ? void 0 : _o3_b1.c), null === (_o4_b_c_d = null === (_o4_b = o4.b) || void 0 === _o4_b ? void 0 : _o4_b.c.d) || void 0 === _o4_b_c_d || delete _o4_b_c_d.e, null === (_o4_b_c_d1 = null === (_o4_b1 = o4.b) || void 0 === _o4_b1 ? void 0 : _o4_b1.c.d) || void 0 === _o4_b_c_d1 || delete _o4_b_c_d1.e, delete (null === (_o4_b_c_d2 = null === (_o4_b2 = o4.b) || void 0 === _o4_b2 ? void 0 : _o4_b2.c.d) || void 0 === _o4_b_c_d2 ? void 0 : _o4_b_c_d2.e), null === (_o5_b_c_d = null === (_o5_b = o5.b) || void 0 === _o5_b ? void 0 : _o5_b.call(o5).c.d) || void 0 === _o5_b_c_d || delete _o5_b_c_d.e, delete (null === (_o5_b_c_d1 = null === (_o5_b1 = o5.b) || void 0 === _o5_b1 ? void 0 : _o5_b1.call(o5).c.d) || void 0 === _o5_b_c_d1 ? void 0 : _o5_b_c_d1.e), null === (_o6_b_c_d = null === (_o6_b = o6.b) || void 0 === _o6_b ? void 0 : _o6_b.c.d) || void 0 === _o6_b_c_d || delete _o6_b_c_d.e, delete (null === (_o6_b_c_d1 = null === (_o6_b1 = o6.b) || void 0 === _o6_b1 ? void 0 : _o6_b1.c.d) || void 0 === _o6_b_c_d1 ? void 0 : _o6_b_c_d1.e); diff --git a/crates/swc/tests/tsc-references/elementAccessChain.1.normal.js b/crates/swc/tests/tsc-references/elementAccessChain.1.normal.js index d352d3befea3..e04b67747819 100644 --- a/crates/swc/tests/tsc-references/elementAccessChain.1.normal.js +++ b/crates/swc/tests/tsc-references/elementAccessChain.1.normal.js @@ -1,16 +1,16 @@ //// [elementAccessChain.ts] -var _o3_b, _o3_b1, _o4_b, _ref, _o4_b1, _ref1, _o5_b, _ref2, _o5_b1, _ref3, _o5_b2, _ref4, _o5_b3, _ref5, _o6; +var _o3_b, _o3_b1, _o4_b_c_d, _o4_b, _o4_b_c_d1, _o4_b1, _o5_b_c_d, _o5_b, _o5_b_c_d1, _o5_b1, _o5_b_c_d2, _o5_b2, _o5_b_c_d3, _o5_b3, _o6; o1 === null || o1 === void 0 ? void 0 : o1["b"]; o2 === null || o2 === void 0 ? void 0 : o2["b"].c; o2 === null || o2 === void 0 ? void 0 : o2.b["c"]; (_o3_b = o3["b"]) === null || _o3_b === void 0 ? void 0 : _o3_b.c; (_o3_b1 = o3.b) === null || _o3_b1 === void 0 ? void 0 : _o3_b1["c"]; -(_ref = (_o4_b = o4.b) === null || _o4_b === void 0 ? void 0 : _o4_b["c"].d) === null || _ref === void 0 ? void 0 : _ref.e; -(_ref1 = (_o4_b1 = o4.b) === null || _o4_b1 === void 0 ? void 0 : _o4_b1["c"].d) === null || _ref1 === void 0 ? void 0 : _ref1["e"]; -(_ref2 = (_o5_b = o5.b) === null || _o5_b === void 0 ? void 0 : _o5_b.call(o5)["c"].d) === null || _ref2 === void 0 ? void 0 : _ref2.e; -(_ref3 = (_o5_b1 = o5.b) === null || _o5_b1 === void 0 ? void 0 : _o5_b1.call(o5)["c"].d) === null || _ref3 === void 0 ? void 0 : _ref3["e"]; -(_ref4 = (_o5_b2 = o5["b"]) === null || _o5_b2 === void 0 ? void 0 : _o5_b2.call(o5)["c"].d) === null || _ref4 === void 0 ? void 0 : _ref4.e; -(_ref5 = (_o5_b3 = o5["b"]) === null || _o5_b3 === void 0 ? void 0 : _o5_b3.call(o5)["c"].d) === null || _ref5 === void 0 ? void 0 : _ref5["e"]; +(_o4_b_c_d = (_o4_b = o4.b) === null || _o4_b === void 0 ? void 0 : _o4_b["c"].d) === null || _o4_b_c_d === void 0 ? void 0 : _o4_b_c_d.e; +(_o4_b_c_d1 = (_o4_b1 = o4.b) === null || _o4_b1 === void 0 ? void 0 : _o4_b1["c"].d) === null || _o4_b_c_d1 === void 0 ? void 0 : _o4_b_c_d1["e"]; +(_o5_b_c_d = (_o5_b = o5.b) === null || _o5_b === void 0 ? void 0 : _o5_b.call(o5)["c"].d) === null || _o5_b_c_d === void 0 ? void 0 : _o5_b_c_d.e; +(_o5_b_c_d1 = (_o5_b1 = o5.b) === null || _o5_b1 === void 0 ? void 0 : _o5_b1.call(o5)["c"].d) === null || _o5_b_c_d1 === void 0 ? void 0 : _o5_b_c_d1["e"]; +(_o5_b_c_d2 = (_o5_b2 = o5["b"]) === null || _o5_b2 === void 0 ? void 0 : _o5_b2.call(o5)["c"].d) === null || _o5_b_c_d2 === void 0 ? void 0 : _o5_b_c_d2.e; +(_o5_b_c_d3 = (_o5_b3 = o5["b"]) === null || _o5_b3 === void 0 ? void 0 : _o5_b3.call(o5)["c"].d) === null || _o5_b_c_d3 === void 0 ? void 0 : _o5_b_c_d3["e"]; (_o6 = o6()) === null || _o6 === void 0 ? void 0 : _o6["x"]; // GH#36031 o2 === null || o2 === void 0 ? void 0 : o2["b"].c; diff --git a/crates/swc/tests/tsc-references/elementAccessChain.2.minified.js b/crates/swc/tests/tsc-references/elementAccessChain.2.minified.js index e2feac34ba7f..f25fe9bb5ebc 100644 --- a/crates/swc/tests/tsc-references/elementAccessChain.2.minified.js +++ b/crates/swc/tests/tsc-references/elementAccessChain.2.minified.js @@ -1,3 +1,3 @@ //// [elementAccessChain.ts] -var _o3_b, _o3_b1, _o4_b, _ref, _o4_b1, _ref1, _o5_b, _ref2, _o5_b1, _ref3, _o5_b2, _ref4, _o5_b3, _ref5, _o6; -null == o1 || o1.b, null == o2 || o2.b.c, null == o2 || o2.b.c, null === (_o3_b = o3.b) || void 0 === _o3_b || _o3_b.c, null === (_o3_b1 = o3.b) || void 0 === _o3_b1 || _o3_b1.c, null === (_ref = null === (_o4_b = o4.b) || void 0 === _o4_b ? void 0 : _o4_b.c.d) || void 0 === _ref || _ref.e, null === (_ref1 = null === (_o4_b1 = o4.b) || void 0 === _o4_b1 ? void 0 : _o4_b1.c.d) || void 0 === _ref1 || _ref1.e, null === (_ref2 = null === (_o5_b = o5.b) || void 0 === _o5_b ? void 0 : _o5_b.call(o5).c.d) || void 0 === _ref2 || _ref2.e, null === (_ref3 = null === (_o5_b1 = o5.b) || void 0 === _o5_b1 ? void 0 : _o5_b1.call(o5).c.d) || void 0 === _ref3 || _ref3.e, null === (_ref4 = null === (_o5_b2 = o5.b) || void 0 === _o5_b2 ? void 0 : _o5_b2.call(o5).c.d) || void 0 === _ref4 || _ref4.e, null === (_ref5 = null === (_o5_b3 = o5.b) || void 0 === _o5_b3 ? void 0 : _o5_b3.call(o5).c.d) || void 0 === _ref5 || _ref5.e, null === (_o6 = o6()) || void 0 === _o6 || _o6.x, null == o2 || o2.b.c, null == o2 || o2.b.c, null == o2 || o2.b.c, null == o2 || o2.b.c; +var _o3_b, _o3_b1, _o4_b_c_d, _o4_b, _o4_b_c_d1, _o4_b1, _o5_b_c_d, _o5_b, _o5_b_c_d1, _o5_b1, _o5_b_c_d2, _o5_b2, _o5_b_c_d3, _o5_b3, _o6; +null == o1 || o1.b, null == o2 || o2.b.c, null == o2 || o2.b.c, null === (_o3_b = o3.b) || void 0 === _o3_b || _o3_b.c, null === (_o3_b1 = o3.b) || void 0 === _o3_b1 || _o3_b1.c, null === (_o4_b_c_d = null === (_o4_b = o4.b) || void 0 === _o4_b ? void 0 : _o4_b.c.d) || void 0 === _o4_b_c_d || _o4_b_c_d.e, null === (_o4_b_c_d1 = null === (_o4_b1 = o4.b) || void 0 === _o4_b1 ? void 0 : _o4_b1.c.d) || void 0 === _o4_b_c_d1 || _o4_b_c_d1.e, null === (_o5_b_c_d = null === (_o5_b = o5.b) || void 0 === _o5_b ? void 0 : _o5_b.call(o5).c.d) || void 0 === _o5_b_c_d || _o5_b_c_d.e, null === (_o5_b_c_d1 = null === (_o5_b1 = o5.b) || void 0 === _o5_b1 ? void 0 : _o5_b1.call(o5).c.d) || void 0 === _o5_b_c_d1 || _o5_b_c_d1.e, null === (_o5_b_c_d2 = null === (_o5_b2 = o5.b) || void 0 === _o5_b2 ? void 0 : _o5_b2.call(o5).c.d) || void 0 === _o5_b_c_d2 || _o5_b_c_d2.e, null === (_o5_b_c_d3 = null === (_o5_b3 = o5.b) || void 0 === _o5_b3 ? void 0 : _o5_b3.call(o5).c.d) || void 0 === _o5_b_c_d3 || _o5_b_c_d3.e, null === (_o6 = o6()) || void 0 === _o6 || _o6.x, null == o2 || o2.b.c, null == o2 || o2.b.c, null == o2 || o2.b.c, null == o2 || o2.b.c; diff --git a/crates/swc/tests/tsc-references/exhaustiveSwitchStatements1.1.normal.js b/crates/swc/tests/tsc-references/exhaustiveSwitchStatements1.1.normal.js index 0411c3422b03..1eb599d9c8e5 100644 --- a/crates/swc/tests/tsc-references/exhaustiveSwitchStatements1.1.normal.js +++ b/crates/swc/tests/tsc-references/exhaustiveSwitchStatements1.1.normal.js @@ -199,8 +199,8 @@ Animal; Animal[Animal["CAT"] = 1] = "CAT"; })(Animal || (Animal = {})); function expression() { - var _ref; - switch((_ref = zoo === null || zoo === void 0 ? void 0 : zoo.animal) !== null && _ref !== void 0 ? _ref : Animal.DOG){ + var _zoo_animal; + switch((_zoo_animal = zoo === null || zoo === void 0 ? void 0 : zoo.animal) !== null && _zoo_animal !== void 0 ? _zoo_animal : Animal.DOG){ case Animal.DOG: return Animal.DOG; case Animal.CAT: diff --git a/crates/swc/tests/tsc-references/logicalAssignment8(target=es2015).1.normal.js b/crates/swc/tests/tsc-references/logicalAssignment8(target=es2015).1.normal.js index 0a96c8fcb4f2..1f503ac8c6d7 100644 --- a/crates/swc/tests/tsc-references/logicalAssignment8(target=es2015).1.normal.js +++ b/crates/swc/tests/tsc-references/logicalAssignment8(target=es2015).1.normal.js @@ -1,13 +1,13 @@ //// [logicalAssignment8.ts] function foo1(results) { - var _ref; - (results || (results = (_ref = bar === null || bar === void 0 ? void 0 : bar.value) !== null && _ref !== void 0 ? _ref : [])).push(100); + var _bar_value; + (results || (results = (_bar_value = bar === null || bar === void 0 ? void 0 : bar.value) !== null && _bar_value !== void 0 ? _bar_value : [])).push(100); } function foo2(results) { - var _ref; - (results !== null && results !== void 0 ? results : results = (_ref = bar === null || bar === void 0 ? void 0 : bar.value) !== null && _ref !== void 0 ? _ref : []).push(100); + var _bar_value; + (results !== null && results !== void 0 ? results : results = (_bar_value = bar === null || bar === void 0 ? void 0 : bar.value) !== null && _bar_value !== void 0 ? _bar_value : []).push(100); } function foo3(results) { - var _ref; - (results && (results = (_ref = bar === null || bar === void 0 ? void 0 : bar.value) !== null && _ref !== void 0 ? _ref : [])).push(100); + var _bar_value; + (results && (results = (_bar_value = bar === null || bar === void 0 ? void 0 : bar.value) !== null && _bar_value !== void 0 ? _bar_value : [])).push(100); } diff --git a/crates/swc/tests/tsc-references/nullishCoalescingOperator12.1.normal.js b/crates/swc/tests/tsc-references/nullishCoalescingOperator12.1.normal.js index 2480b707e6cb..0dfb46befe76 100644 --- a/crates/swc/tests/tsc-references/nullishCoalescingOperator12.1.normal.js +++ b/crates/swc/tests/tsc-references/nullishCoalescingOperator12.1.normal.js @@ -2,5 +2,5 @@ const obj = { arr: [] }; -var _ref; -for (const i of (_ref = obj === null || obj === void 0 ? void 0 : obj.arr) !== null && _ref !== void 0 ? _ref : []){} +var _obj_arr; +for (const i of (_obj_arr = obj === null || obj === void 0 ? void 0 : obj.arr) !== null && _obj_arr !== void 0 ? _obj_arr : []){} diff --git a/crates/swc/tests/tsc-references/nullishCoalescingOperator12.2.minified.js b/crates/swc/tests/tsc-references/nullishCoalescingOperator12.2.minified.js index 6bfece459e0e..60fdbe8d6b13 100644 --- a/crates/swc/tests/tsc-references/nullishCoalescingOperator12.2.minified.js +++ b/crates/swc/tests/tsc-references/nullishCoalescingOperator12.2.minified.js @@ -1,6 +1,6 @@ //// [nullishCoalescingOperator12.ts] -var _ref; +var _obj_arr; const obj = { arr: [] }; -for (const i of null !== (_ref = null == obj ? void 0 : obj.arr) && void 0 !== _ref ? _ref : []); +for (const i of null !== (_obj_arr = null == obj ? void 0 : obj.arr) && void 0 !== _obj_arr ? _obj_arr : []); diff --git a/crates/swc/tests/tsc-references/propertyAccessChain.1.normal.js b/crates/swc/tests/tsc-references/propertyAccessChain.1.normal.js index f9857c3db764..fdd1fb55312a 100644 --- a/crates/swc/tests/tsc-references/propertyAccessChain.1.normal.js +++ b/crates/swc/tests/tsc-references/propertyAccessChain.1.normal.js @@ -1,10 +1,10 @@ //// [propertyAccessChain.ts] -var _o3_b, _o4_b, _ref, _o5_b, _ref1, _o6; +var _o3_b, _o4_b_c_d, _o4_b, _o5_b_c_d, _o5_b, _o6; o1 === null || o1 === void 0 ? void 0 : o1.b; o2 === null || o2 === void 0 ? void 0 : o2.b.c; (_o3_b = o3.b) === null || _o3_b === void 0 ? void 0 : _o3_b.c; -(_ref = (_o4_b = o4.b) === null || _o4_b === void 0 ? void 0 : _o4_b.c.d) === null || _ref === void 0 ? void 0 : _ref.e; -(_ref1 = (_o5_b = o5.b) === null || _o5_b === void 0 ? void 0 : _o5_b.call(o5).c.d) === null || _ref1 === void 0 ? void 0 : _ref1.e; +(_o4_b_c_d = (_o4_b = o4.b) === null || _o4_b === void 0 ? void 0 : _o4_b.c.d) === null || _o4_b_c_d === void 0 ? void 0 : _o4_b_c_d.e; +(_o5_b_c_d = (_o5_b = o5.b) === null || _o5_b === void 0 ? void 0 : _o5_b.call(o5).c.d) === null || _o5_b_c_d === void 0 ? void 0 : _o5_b_c_d.e; (_o6 = o6()) === null || _o6 === void 0 ? void 0 : _o6.x; // GH#34109 (o1 === null || o1 === void 0 ? void 0 : o1.b) ? 1 : 0; diff --git a/crates/swc/tests/tsc-references/propertyAccessChain.2.minified.js b/crates/swc/tests/tsc-references/propertyAccessChain.2.minified.js index e750bda9691b..a8881a131cd4 100644 --- a/crates/swc/tests/tsc-references/propertyAccessChain.2.minified.js +++ b/crates/swc/tests/tsc-references/propertyAccessChain.2.minified.js @@ -1,3 +1,3 @@ //// [propertyAccessChain.ts] -var _o3_b, _o4_b, _ref, _o5_b, _ref1, _o6; -null == o1 || o1.b, null == o2 || o2.b.c, null === (_o3_b = o3.b) || void 0 === _o3_b || _o3_b.c, null === (_ref = null === (_o4_b = o4.b) || void 0 === _o4_b ? void 0 : _o4_b.c.d) || void 0 === _ref || _ref.e, null === (_ref1 = null === (_o5_b = o5.b) || void 0 === _o5_b ? void 0 : _o5_b.call(o5).c.d) || void 0 === _ref1 || _ref1.e, null === (_o6 = o6()) || void 0 === _o6 || _o6.x, null == o1 || o1.b, null == o2 || o2.b.c, null == o2 || o2.b.c; +var _o3_b, _o4_b_c_d, _o4_b, _o5_b_c_d, _o5_b, _o6; +null == o1 || o1.b, null == o2 || o2.b.c, null === (_o3_b = o3.b) || void 0 === _o3_b || _o3_b.c, null === (_o4_b_c_d = null === (_o4_b = o4.b) || void 0 === _o4_b ? void 0 : _o4_b.c.d) || void 0 === _o4_b_c_d || _o4_b_c_d.e, null === (_o5_b_c_d = null === (_o5_b = o5.b) || void 0 === _o5_b ? void 0 : _o5_b.call(o5).c.d) || void 0 === _o5_b_c_d || _o5_b_c_d.e, null === (_o6 = o6()) || void 0 === _o6 || _o6.x, null == o1 || o1.b, null == o2 || o2.b.c, null == o2 || o2.b.c; diff --git a/crates/swc_ecma_transforms_compat/src/es2020/opt_chaining.rs b/crates/swc_ecma_transforms_compat/src/es2020/opt_chaining.rs index 2a8b4af1050b..561b33356375 100644 --- a/crates/swc_ecma_transforms_compat/src/es2020/opt_chaining.rs +++ b/crates/swc_ecma_transforms_compat/src/es2020/opt_chaining.rs @@ -380,9 +380,7 @@ impl OptChaining { _ => {} } - e.base.visit_mut_children_with(self); - - match &mut e.base { + let mut base = match &mut e.base { OptChainBase::Member(MemberExpr { obj, prop, .. }) => { let obj_span = obj.span(); @@ -548,7 +546,11 @@ impl OptChaining { alt, } } - } + }; + + base.visit_mut_with(self); + + base } } #[derive(Default)] diff --git a/crates/swc_ecma_transforms_compat/tests/es2020_optional_chaining.rs b/crates/swc_ecma_transforms_compat/tests/es2020_optional_chaining.rs index cf4f48943b93..9bed1e695c5a 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2020_optional_chaining.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2020_optional_chaining.rs @@ -433,11 +433,11 @@ orders[client.key]?.price; "#, r#" -var _ref, _a_b, _ref1, _a_b_c, _a_b_c_d, _orders_, _orders_client_key, _c; +var _a_b_c, _a_b_c_d, _a_b, _a_b_c1, _a_b_c_d1, _orders_, _orders_client_key, _c; foo === null || foo === void 0 ? void 0 : foo.bar; -(_ref = a === null || a === void 0 ? void 0 : a.b.c) === null || _ref === void 0 ? void 0 : _ref.d.e; -(_ref1 = (_a_b = a.b) === null || _a_b === void 0 ? void 0 : _a_b.c.d) === null || _ref1 === void 0 ? void 0 : _ref1.e; -(_a_b_c = a.b.c) === null || _a_b_c === void 0 ? void 0 : (_a_b_c_d = _a_b_c.d) === null || _a_b_c_d === void 0 ? void 0 : _a_b_c_d.e; +(_a_b_c = a === null || a === void 0 ? void 0 : a.b.c) === null || _a_b_c === void 0 ? void 0 : _a_b_c.d.e; +(_a_b_c_d = (_a_b = a.b) === null || _a_b === void 0 ? void 0 : _a_b.c.d) === null || _a_b_c_d === void 0 ? void 0 : _a_b_c_d.e; +(_a_b_c1 = a.b.c) === null || _a_b_c1 === void 0 ? void 0 : (_a_b_c_d1 = _a_b_c1.d) === null || _a_b_c_d1 === void 0 ? void 0 : _a_b_c_d1.e; orders === null || orders === void 0 ? void 0 : orders[0].price; orders === null || orders === void 0 ? void 0 : (_orders_ = orders[0]) === null || _orders_ === void 0 ? void 0 : _orders_.price; orders[client === null || client === void 0 ? void 0 : client.key].price; @@ -518,7 +518,7 @@ foo?.bar()?.() "#, r#" -var _foo_bar, _foo_bar1, _foo, _foo_bar2, _foo_bar3, _foo_bar_call, _foo_bar4, _foo_bar5, _foo_bar_call1, _ref; +var _foo_bar, _foo_bar1, _foo, _foo_bar2, _foo_bar3, _foo_bar_call, _foo_bar4, _foo_bar5, _foo_bar_call1, _foo_bar6; foo === null || foo === void 0 ? void 0 : foo(foo); foo === null || foo === void 0 ? void 0 : foo.bar(); (_foo_bar = foo.bar) === null || _foo_bar === void 0 ? void 0 : _foo_bar.call(foo, foo.bar, false); @@ -529,7 +529,7 @@ foo === null || foo === void 0 ? void 0 : (_foo = foo()) === null || _foo === vo (_foo_bar3 = foo.bar) === null || _foo_bar3 === void 0 ? void 0 : (_foo_bar_call = _foo_bar3.call(foo)) === null || _foo_bar_call === void 0 ? void 0 : _foo_bar_call.baz; foo === null || foo === void 0 ? void 0 : (_foo_bar4 = foo.bar) === null || _foo_bar4 === void 0 ? void 0 : _foo_bar4.call(foo).baz; foo === null || foo === void 0 ? void 0 : (_foo_bar5 = foo.bar) === null || _foo_bar5 === void 0 ? void 0 : (_foo_bar_call1 = _foo_bar5.call(foo)) === null || _foo_bar_call1 === void 0 ? void 0 : _foo_bar_call1.baz; -(_ref = foo === null || foo === void 0 ? void 0 : foo.bar()) === null || _ref === void 0 ? void 0 : _ref(); +(_foo_bar6 = foo === null || foo === void 0 ? void 0 : foo.bar()) === null || _foo_bar6 === void 0 ? void 0 : _foo_bar6(); "# ); @@ -949,6 +949,13 @@ test!( " ); +test_exec!( + syntax(), + |_| tr(Default::default()), + issue_6346, + "expect([1]?.filter(() => true).map?.(() => 2)).toEqual([2]);" +); + test_exec!( ignore, syntax(), diff --git a/crates/swc_ecma_utils/src/lib.rs b/crates/swc_ecma_utils/src/lib.rs index 66886fbadbb3..f2aa790f538a 100644 --- a/crates/swc_ecma_utils/src/lib.rs +++ b/crates/swc_ecma_utils/src/lib.rs @@ -1925,7 +1925,11 @@ pub fn alias_ident_for(expr: &Expr, default: &str) -> Ident { ident: Some(ident), .. }) => Some(ident.sym.to_string()), - Expr::Call(CallExpr { + Expr::OptChain(OptChainExpr { + base: OptChainBase::Call(OptCall { callee: expr, .. }), + .. + }) + | Expr::Call(CallExpr { callee: Callee::Expr(expr), .. }) => sym(expr), @@ -1940,13 +1944,31 @@ pub fn alias_ident_for(expr: &Expr, default: &str) -> Ident { .. }) => Some(format!("super_{}", sym(expr).unwrap_or_default())), - Expr::Member(MemberExpr { + Expr::OptChain(OptChainExpr { + base: + OptChainBase::Member(MemberExpr { + prop: MemberProp::Ident(ident), + obj, + .. + }), + .. + }) + | Expr::Member(MemberExpr { prop: MemberProp::Ident(ident), obj, .. }) => Some(format!("{}_{}", sym(obj).unwrap_or_default(), ident.sym)), - Expr::Member(MemberExpr { + Expr::OptChain(OptChainExpr { + base: + OptChainBase::Member(MemberExpr { + prop: MemberProp::Computed(ComputedPropName { expr, .. }), + obj, + .. + }), + .. + }) + | Expr::Member(MemberExpr { prop: MemberProp::Computed(ComputedPropName { expr, .. }), obj, ..