From bc6e95052eaf60d71c544c8a88c8440d7a2b437c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 20 Jun 2023 13:12:31 +0900 Subject: [PATCH] fix(es/compat): Visit arrow body from optional chaining pass (#7549) **Related issue:** - Closes #7547. --- .../issues-2xxx/2020/case1/output/index.js | 2 +- .../issues-3xxx/3381/1/output/index.ts | 10 +++--- .../issues-4xxx/4108/1/output/index.ts | 10 +++--- .../tests/fixture/issues-7xxx/7547/input/1.js | 26 +++++++++++++++ .../fixture/issues-7xxx/7547/output/1.js | 32 +++++++++++++++++++ ...nStaticMembers2(target=es2015).1.normal.js | 6 ++-- ...taticMembers2(target=es2015).2.minified.js | 2 +- .../thisAndSuperInStaticMembers4.1.normal.js | 6 ++-- ...thisAndSuperInStaticMembers4.2.minified.js | 2 +- .../src/es2020/optional_chaining.rs | 2 ++ 10 files changed, 82 insertions(+), 16 deletions(-) create mode 100644 crates/swc/tests/fixture/issues-7xxx/7547/input/1.js create mode 100644 crates/swc/tests/fixture/issues-7xxx/7547/output/1.js diff --git a/crates/swc/tests/fixture/issues-2xxx/2020/case1/output/index.js b/crates/swc/tests/fixture/issues-2xxx/2020/case1/output/index.js index 5497337021c2..40ebfc68d349 100644 --- a/crates/swc/tests/fixture/issues-2xxx/2020/case1/output/index.js +++ b/crates/swc/tests/fixture/issues-2xxx/2020/case1/output/index.js @@ -1 +1 @@ -"use strict";const createRegisterItems=registerType=>async(a,b)=>{const a=root?.test}; +"use strict";const createRegisterItems=registerType=>async(a,b)=>{var _root;const a=(_root=root)===null||_root===void 0?void 0:_root.test}; diff --git a/crates/swc/tests/fixture/issues-3xxx/3381/1/output/index.ts b/crates/swc/tests/fixture/issues-3xxx/3381/1/output/index.ts index f0e37984340e..2dc6af8e048f 100644 --- a/crates/swc/tests/fixture/issues-3xxx/3381/1/output/index.ts +++ b/crates/swc/tests/fixture/issues-3xxx/3381/1/output/index.ts @@ -5,13 +5,15 @@ var dummyTsFunction = function() { 2 ]; var func1 = function(array) { - return(// Mapping array - array?.map(function(i) { + var // Mapping array + _array; + return (_array = array) === null || _array === void 0 ? void 0 : _array.map(function(i) { return i; - })); + }); }; var func2 = function(array) { - return array?.map(function(i) { + var _array; + return (_array = array) === null || _array === void 0 ? void 0 : _array.map(function(i) { return i; }); }; diff --git a/crates/swc/tests/fixture/issues-4xxx/4108/1/output/index.ts b/crates/swc/tests/fixture/issues-4xxx/4108/1/output/index.ts index 67a4b7f13a77..13384f275217 100644 --- a/crates/swc/tests/fixture/issues-4xxx/4108/1/output/index.ts +++ b/crates/swc/tests/fixture/issues-4xxx/4108/1/output/index.ts @@ -8,7 +8,7 @@ import { Transaction } from "@solana/web3.js"; import { WalletNotConnectedError } from "@solana/wallet-adapter-base"; export var getErrorForTransaction = function() { var _ref = _async_to_generator(function(connection, txid) { - var tx, errors; + var _tx, tx, errors; return _ts_generator(this, function(_state) { switch(_state.label){ case 0: @@ -26,7 +26,7 @@ export var getErrorForTransaction = function() { case 2: tx = _state.sent(); errors = []; - if (tx?.meta && tx.meta.logMessages) { + if (((_tx = tx) === null || _tx === void 0 ? void 0 : _tx.meta) && tx.meta.logMessages) { tx.meta.logMessages.forEach(function(log) { var regex = /Error: (.*)/gm; var m; @@ -366,7 +366,7 @@ export var sendTransactions = function() { }(); export var sendTransaction = function() { var _ref = _async_to_generator(function(connection, wallet, instructions, signers) { - var awaitConfirmation, commitment, includesFeePayer, block, transaction, _tmp, _transaction, _transaction1, _transaction2, rawTransaction, options, txid, slot, confirmation, errors; + var awaitConfirmation, commitment, includesFeePayer, block, transaction, _tmp, _transaction, _transaction1, _transaction2, rawTransaction, options, txid, slot, _confirmation, _confirmation1, confirmation, errors; var _arguments = arguments; return _ts_generator(this, function(_state) { switch(_state.label){ @@ -454,8 +454,8 @@ export var sendTransaction = function() { case 7: confirmation = _state.sent(); if (!confirmation) throw new Error("Timed out awaiting confirmation on transaction"); - slot = confirmation?.slot || 0; - if (!confirmation?.err) return [ + slot = ((_confirmation = confirmation) === null || _confirmation === void 0 ? void 0 : _confirmation.slot) || 0; + if (!((_confirmation1 = confirmation) === null || _confirmation1 === void 0 ? void 0 : _confirmation1.err)) return [ 3, 9 ]; diff --git a/crates/swc/tests/fixture/issues-7xxx/7547/input/1.js b/crates/swc/tests/fixture/issues-7xxx/7547/input/1.js new file mode 100644 index 000000000000..c11e1d143f06 --- /dev/null +++ b/crates/swc/tests/fixture/issues-7xxx/7547/input/1.js @@ -0,0 +1,26 @@ +// code below dont work +const a = {}; + +new Promise(r => { + r(a?.b); +}).then(a => { + return a?.b; +}); + +const anony = () => { + return a?.b; +} + +// code below works + +const b = a?.b; + +function fn() { + return a?.b; +} + +setTimeout(() => a?.b, 0); + +const anony2 = function () { return a?.b }; + +(function () { return a?.b })(); \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-7xxx/7547/output/1.js b/crates/swc/tests/fixture/issues-7xxx/7547/output/1.js new file mode 100644 index 000000000000..947f2dd60c2d --- /dev/null +++ b/crates/swc/tests/fixture/issues-7xxx/7547/output/1.js @@ -0,0 +1,32 @@ +// code below dont work +var _a; +var a = {}; +new Promise(function(r) { + var _a; + r((_a = a) === null || _a === void 0 ? void 0 : _a.b); +}).then(function(a) { + var _a; + return (_a = a) === null || _a === void 0 ? void 0 : _a.b; +}); +var anony = function() { + var _a; + return (_a = a) === null || _a === void 0 ? void 0 : _a.b; +}; +// code below works +var b = (_a = a) === null || _a === void 0 ? void 0 : _a.b; +function fn() { + var _a; + return (_a = a) === null || _a === void 0 ? void 0 : _a.b; +} +setTimeout(function() { + var _a; + return (_a = a) === null || _a === void 0 ? void 0 : _a.b; +}, 0); +var anony2 = function anony2() { + var _a; + return (_a = a) === null || _a === void 0 ? void 0 : _a.b; +}; +(function() { + var _a; + return (_a = a) === null || _a === void 0 ? void 0 : _a.b; +})(); diff --git a/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers2(target=es2015).1.normal.js b/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers2(target=es2015).1.normal.js index ccb181b72b76..556c8e36c0dc 100644 --- a/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers2(target=es2015).1.normal.js +++ b/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers2(target=es2015).1.normal.js @@ -24,13 +24,15 @@ class C extends B { C.y2 = C.x(); })(); (()=>{ - C.y3 = C?.x(); + var _C; + C.y3 = (_C = C) === null || _C === void 0 ? void 0 : _C.x(); })(); (()=>{ C.y4 = C["x"](); })(); (()=>{ - C.y5 = C?.["x"](); + var _C; + C.y5 = (_C = C) === null || _C === void 0 ? void 0 : _C["x"](); })(); (()=>{ C.z1 = _get(_get_prototype_of(C), "a", C); diff --git a/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers2(target=es2015).2.minified.js b/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers2(target=es2015).2.minified.js index a1893755e0ef..f9b5f4eab421 100644 --- a/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers2(target=es2015).2.minified.js +++ b/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers2(target=es2015).2.minified.js @@ -10,7 +10,7 @@ class C extends B { super(...args), this.x = 1, this.y = this.x, this.z = super.f(); } } -C.x = void 0, C.y1 = C.x, C.y2 = C.x(), C.y3 = C?.x(), C.y4 = C.x(), C.y5 = C?.x(), C.z1 = _get(_get_prototype_of(C), "a", C), C.z2 = _get(_get_prototype_of(C), "a", C), C.z3 = _get(_get_prototype_of(C), "f", C).call(C), C.z4 = _get(_get_prototype_of(C), "f", C).call(C), C.z5 = _set(_get_prototype_of(C), "a", 0, C, !0), C.z6 = _update(_get_prototype_of(C), "a", C, !0)._ += 1, C.z7 = void _set(_get_prototype_of(C), "a", 0, C, !0), C.z8 = [_update(_get_prototype_of(C), "a", C, !0)._] = [ +C.x = void 0, C.y1 = C.x, C.y2 = C.x(), C.y3 = null == C ? void 0 : C.x(), C.y4 = C.x(), C.y5 = null == C ? void 0 : C.x(), C.z1 = _get(_get_prototype_of(C), "a", C), C.z2 = _get(_get_prototype_of(C), "a", C), C.z3 = _get(_get_prototype_of(C), "f", C).call(C), C.z4 = _get(_get_prototype_of(C), "f", C).call(C), C.z5 = _set(_get_prototype_of(C), "a", 0, C, !0), C.z6 = _update(_get_prototype_of(C), "a", C, !0)._ += 1, C.z7 = void _set(_get_prototype_of(C), "a", 0, C, !0), C.z8 = [_update(_get_prototype_of(C), "a", C, !0)._] = [ 0 ], C.z9 = [_update(_get_prototype_of(C), "a", C, !0)._ = 0] = [ 0 diff --git a/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers4.1.normal.js b/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers4.1.normal.js index 04ad12d987a6..3b87d0378bc8 100644 --- a/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers4.1.normal.js +++ b/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers4.1.normal.js @@ -31,13 +31,15 @@ var C = /*#__PURE__*/ function(B1) { C.y2 = C.x(); })(); (function() { - C.y3 = C?.x(); + var _C; + C.y3 = (_C = C) === null || _C === void 0 ? void 0 : _C.x(); })(); (function() { C.y4 = C["x"](); })(); (function() { - C.y5 = C?.["x"](); + var _C; + C.y5 = (_C = C) === null || _C === void 0 ? void 0 : _C["x"](); })(); (function() { C.z3 = _get(_get_prototype_of(C), "f", C).call(C); diff --git a/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers4.2.minified.js b/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers4.2.minified.js index 89a95bb4148b..590b18fc210e 100644 --- a/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers4.2.minified.js +++ b/crates/swc/tests/tsc-references/thisAndSuperInStaticMembers4.2.minified.js @@ -15,4 +15,4 @@ var C = function(B1) { } return C; }(B); -C.x = void 0, C.y1 = C.x, C.y2 = C.x(), C.y3 = C?.x(), C.y4 = C.x(), C.y5 = C?.x(), C.z3 = _get(_get_prototype_of(C), "f", C).call(C), C.z4 = _get(_get_prototype_of(C), "f", C).call(C); +C.x = void 0, C.y1 = C.x, C.y2 = C.x(), C.y3 = null == C ? void 0 : C.x(), C.y4 = C.x(), C.y5 = null == C ? void 0 : C.x(), C.z3 = _get(_get_prototype_of(C), "f", C).call(C), C.z4 = _get(_get_prototype_of(C), "f", C).call(C); diff --git a/crates/swc_ecma_transforms_compat/src/es2020/optional_chaining.rs b/crates/swc_ecma_transforms_compat/src/es2020/optional_chaining.rs index 297facc11f96..960249c83c23 100644 --- a/crates/swc_ecma_transforms_compat/src/es2020/optional_chaining.rs +++ b/crates/swc_ecma_transforms_compat/src/es2020/optional_chaining.rs @@ -53,6 +53,8 @@ impl VisitMut for OptChaining { } _ => *expr = BlockStmtOrExpr::BlockStmt(stmt), } + } else { + expr.visit_mut_children_with(self); } }