From b394f9f1d3efd24fd55b4ac5efdbaee91b9d8021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Donny/=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Tue, 29 Nov 2022 13:12:11 +0900 Subject: [PATCH] fix(es/minifier): Don't drop used variables from sequential inliner (#6520) **Related issue:** - Closes https://github.com/swc-project/swc/issues/6510. --- .../nonPrimitiveAssignError.2.minified.js | 2 +- .../src/compress/optimize/sequences.rs | 3 ++- .../tests/fixture/next/joda/input.js | 26 +++++++++++++++++++ .../tests/fixture/next/joda/output.js | 15 +++++++++++ .../tests/pass-1/joda/1/input.js | 26 +++++++++++++++++++ .../tests/pass-1/joda/1/output.js | 19 ++++++++++++++ .../tests/pass-1/joda/2/input.js | 19 ++++++++++++++ .../tests/pass-1/joda/2/output.js | 17 ++++++++++++ 8 files changed, 125 insertions(+), 2 deletions(-) create mode 100644 crates/swc_ecma_minifier/tests/fixture/next/joda/input.js create mode 100644 crates/swc_ecma_minifier/tests/fixture/next/joda/output.js create mode 100644 crates/swc_ecma_minifier/tests/pass-1/joda/1/input.js create mode 100644 crates/swc_ecma_minifier/tests/pass-1/joda/1/output.js create mode 100644 crates/swc_ecma_minifier/tests/pass-1/joda/2/input.js create mode 100644 crates/swc_ecma_minifier/tests/pass-1/joda/2/output.js diff --git a/crates/swc/tests/tsc-references/nonPrimitiveAssignError.2.minified.js b/crates/swc/tests/tsc-references/nonPrimitiveAssignError.2.minified.js index 7e8ffe4f413c..d1d30ad8d3d6 100644 --- a/crates/swc/tests/tsc-references/nonPrimitiveAssignError.2.minified.js +++ b/crates/swc/tests/tsc-references/nonPrimitiveAssignError.2.minified.js @@ -4,4 +4,4 @@ var a, x = {}, y = { }; x = a, y = a, a = x, a = y; var s = "fooo"; -a = 123, a = !0, n = a = s, b = a, s = a, a = 123, a = !0, a = "string"; +a = 123, a = !0, s = a = s, a = 123, a = !0, a = "string"; diff --git a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs index fb4d45086cdb..b45d9a9b4a6d 100644 --- a/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs +++ b/crates/swc_ecma_minifier/src/compress/optimize/sequences.rs @@ -2222,7 +2222,8 @@ where if let Some(usage) = self.data.vars.get(&left_id.to_id()) { // We are eliminating one usage, so we use 1 instead of // 0 - if !$force_drop && usage.usage_count == 1 { + if !$force_drop && usage.usage_count == 1 && usage.assign_count == 0 + { report_change!("sequences: Dropping inlined variable"); a.name.take(); } diff --git a/crates/swc_ecma_minifier/tests/fixture/next/joda/input.js b/crates/swc_ecma_minifier/tests/fixture/next/joda/input.js new file mode 100644 index 000000000000..f94d74fc67bb --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/next/joda/input.js @@ -0,0 +1,26 @@ +"use strict"; +(self["webpackChunk_N_E"] = self["webpackChunk_N_E"] || []).push([[715], { + +/***/ 3266: +/***/ (function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { + +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ "h": function () { return /* binding */ LocalDate; } + /* harmony export */ +}); + + var isInit = false; + + function init() { + if (isInit) { + return; + } + + isInit = true; + } + + init(); + + }) + +}]); \ No newline at end of file diff --git a/crates/swc_ecma_minifier/tests/fixture/next/joda/output.js b/crates/swc_ecma_minifier/tests/fixture/next/joda/output.js new file mode 100644 index 000000000000..b5a63dd451f1 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/fixture/next/joda/output.js @@ -0,0 +1,15 @@ +"use strict"; +(self.webpackChunk_N_E = self.webpackChunk_N_E || []).push([ + [ + 715 + ], + { + 3266: function(__unused_webpack_module, __webpack_exports__, __webpack_require__) { + __webpack_require__.d(__webpack_exports__, { + h: function() { + return LocalDate; + } + }); + } + } +]); diff --git a/crates/swc_ecma_minifier/tests/pass-1/joda/1/input.js b/crates/swc_ecma_minifier/tests/pass-1/joda/1/input.js new file mode 100644 index 000000000000..f94d74fc67bb --- /dev/null +++ b/crates/swc_ecma_minifier/tests/pass-1/joda/1/input.js @@ -0,0 +1,26 @@ +"use strict"; +(self["webpackChunk_N_E"] = self["webpackChunk_N_E"] || []).push([[715], { + +/***/ 3266: +/***/ (function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { + +/* harmony export */ __webpack_require__.d(__webpack_exports__, { +/* harmony export */ "h": function () { return /* binding */ LocalDate; } + /* harmony export */ +}); + + var isInit = false; + + function init() { + if (isInit) { + return; + } + + isInit = true; + } + + init(); + + }) + +}]); \ No newline at end of file diff --git a/crates/swc_ecma_minifier/tests/pass-1/joda/1/output.js b/crates/swc_ecma_minifier/tests/pass-1/joda/1/output.js new file mode 100644 index 000000000000..c13efe64f565 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/pass-1/joda/1/output.js @@ -0,0 +1,19 @@ +"use strict"; +(self.webpackChunk_N_E = self.webpackChunk_N_E || []).push([ + [ + 715 + ], + { + 3266: function(__unused_webpack_module, __webpack_exports__, __webpack_require__) { + __webpack_require__.d(__webpack_exports__, { + h: function() { + return LocalDate; + } + }); + var isInit = !1; + !function() { + if (!isInit) isInit = !0; + }(); + } + } +]); diff --git a/crates/swc_ecma_minifier/tests/pass-1/joda/2/input.js b/crates/swc_ecma_minifier/tests/pass-1/joda/2/input.js new file mode 100644 index 000000000000..c0d515401cb1 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/pass-1/joda/2/input.js @@ -0,0 +1,19 @@ +"use strict"; +(self.webpackChunk_N_E = self.webpackChunk_N_E || []).push([ + [ + 715 + ], + { + 3266: function (__unused_webpack_module, __webpack_exports__, __webpack_require__) { + __webpack_require__.d(__webpack_exports__, { + h: function () { + return LocalDate; + } + }); + var isInit = !1; + !function () { + if (!isInit) isInit = !0; + }(); + } + } +]); diff --git a/crates/swc_ecma_minifier/tests/pass-1/joda/2/output.js b/crates/swc_ecma_minifier/tests/pass-1/joda/2/output.js new file mode 100644 index 000000000000..778d2ac031cd --- /dev/null +++ b/crates/swc_ecma_minifier/tests/pass-1/joda/2/output.js @@ -0,0 +1,17 @@ +"use strict"; +(self.webpackChunk_N_E = self.webpackChunk_N_E || []).push([ + [ + 715 + ], + { + 3266: function(__unused_webpack_module, __webpack_exports__, __webpack_require__) { + __webpack_require__.d(__webpack_exports__, { + h: function() { + return LocalDate; + } + }); + var isInit = !1; + isInit = !0; + } + } +]);