From 1e44e57a095c151fc52685b9580c083620342fe7 Mon Sep 17 00:00:00 2001 From: Austaras Date: Fri, 29 Dec 2023 07:56:38 +0800 Subject: [PATCH] fix(es/renamer): Correctly check top level (#8456) **Related issue:** - Closes #8451. - Closes #8185. --- .../fixture/issues-8xxx/8185/input/.swcrc | 19 ++++++++++ .../fixture/issues-8xxx/8185/input/helper.js | 6 +++ .../fixture/issues-8xxx/8185/input/input.js | 5 +++ .../fixture/issues-8xxx/8185/output/helper.js | 10 +++++ .../fixture/issues-8xxx/8185/output/input.js | 4 ++ .../tests/fixture/issues/6628/output.js | 8 ++-- .../tests/fixture/next/43052/output.js | 10 ++--- .../tests/fixture/next/outstatic/1/output.js | 38 +++++++++---------- .../tests/full/issues/7094/1/output.js | 2 +- .../tests/full/issues/7094/2/output.js | 2 +- .../eval_collapse_vars/output.mangleOnly.js | 4 +- .../eval_mangle/output.mangleOnly.js | 2 +- .../eval_unused/output.mangleOnly.js | 2 +- .../src/rename/collector.rs | 8 ++-- 14 files changed, 82 insertions(+), 38 deletions(-) create mode 100644 crates/swc/tests/fixture/issues-8xxx/8185/input/.swcrc create mode 100644 crates/swc/tests/fixture/issues-8xxx/8185/input/helper.js create mode 100644 crates/swc/tests/fixture/issues-8xxx/8185/input/input.js create mode 100644 crates/swc/tests/fixture/issues-8xxx/8185/output/helper.js create mode 100644 crates/swc/tests/fixture/issues-8xxx/8185/output/input.js diff --git a/crates/swc/tests/fixture/issues-8xxx/8185/input/.swcrc b/crates/swc/tests/fixture/issues-8xxx/8185/input/.swcrc new file mode 100644 index 00000000000..887e89d7aff --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8185/input/.swcrc @@ -0,0 +1,19 @@ +{ + "jsc": { + "parser": { + "syntax": "typescript", + "tsx": false + }, + "target": "es5", + "loose": false, + "minify": { + "compress": false, + "mangle": false + } + }, + "module": { + "type": "es6" + }, + "minify": false, + "isModule": true +} \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-8xxx/8185/input/helper.js b/crates/swc/tests/fixture/issues-8xxx/8185/input/helper.js new file mode 100644 index 00000000000..f86c353e65f --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8185/input/helper.js @@ -0,0 +1,6 @@ +const a = () => { + const _define_property = 1; + const fn = () => console.log({ [prop]: 'test' }); +}; + +const b = () => eval('console.log()'); \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-8xxx/8185/input/input.js b/crates/swc/tests/fixture/issues-8xxx/8185/input/input.js new file mode 100644 index 00000000000..23e6ecb4989 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8185/input/input.js @@ -0,0 +1,5 @@ +(function hello() { + console.log('outer stack', new Error().stack) +})(); + +eval("") \ No newline at end of file diff --git a/crates/swc/tests/fixture/issues-8xxx/8185/output/helper.js b/crates/swc/tests/fixture/issues-8xxx/8185/output/helper.js new file mode 100644 index 00000000000..a9b602cff48 --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8185/output/helper.js @@ -0,0 +1,10 @@ +import { _ as _define_property } from "@swc/helpers/_/_define_property"; +var a = function() { + var _define_property1 = 1; + var fn = function() { + return console.log(_define_property({}, prop, "test")); + }; +}; +var b = function() { + return eval("console.log()"); +}; diff --git a/crates/swc/tests/fixture/issues-8xxx/8185/output/input.js b/crates/swc/tests/fixture/issues-8xxx/8185/output/input.js new file mode 100644 index 00000000000..c3830d975dd --- /dev/null +++ b/crates/swc/tests/fixture/issues-8xxx/8185/output/input.js @@ -0,0 +1,4 @@ +(function hello() { + console.log("outer stack", new Error().stack); +})(); +eval(""); diff --git a/crates/swc_ecma_minifier/tests/fixture/issues/6628/output.js b/crates/swc_ecma_minifier/tests/fixture/issues/6628/output.js index e158947e301..5d12f36e0bb 100644 --- a/crates/swc_ecma_minifier/tests/fixture/issues/6628/output.js +++ b/crates/swc_ecma_minifier/tests/fixture/issues/6628/output.js @@ -1,10 +1,10 @@ !function() { var CallbackType, Collector = function() { - var u = function(u) {}; - return u.usePlugin = function(u, l, o) {}, u.plugins = [], u; + var n = function(n) {}; + return n.usePlugin = function(n, u, l) {}, n.plugins = [], n; }(); - !function(u) { - u[u.Var = 0] = "Var", u[u.All = 1] = "All"; + !function(n) { + n[n.Var = 0] = "Var", n[n.All = 1] = "All"; }(CallbackType || (CallbackType = {})); var CepRule = function() { eval(); diff --git a/crates/swc_ecma_minifier/tests/fixture/next/43052/output.js b/crates/swc_ecma_minifier/tests/fixture/next/43052/output.js index 129e337bcfb..48f1422456f 100644 --- a/crates/swc_ecma_minifier/tests/fixture/next/43052/output.js +++ b/crates/swc_ecma_minifier/tests/fixture/next/43052/output.js @@ -1,6 +1,6 @@ use(function(__unused_webpack_module, exports, __webpack_require__) { - !function(n, c) { - c(exports, __webpack_require__(7294), __webpack_require__(1321)); + !function(e, t) { + t(exports, __webpack_require__(7294), __webpack_require__(1321)); }(this, function(exports, React) { "use strict"; var index_production = { @@ -26,7 +26,7 @@ use(function(__unused_webpack_module, exports, __webpack_require__) { }); }); }); -(function n() { - const n = 1; - use(n); +(function e() { + const e = 1; + use(e); }); diff --git a/crates/swc_ecma_minifier/tests/fixture/next/outstatic/1/output.js b/crates/swc_ecma_minifier/tests/fixture/next/outstatic/1/output.js index 0035ad7a591..ac9529b566a 100644 --- a/crates/swc_ecma_minifier/tests/fixture/next/outstatic/1/output.js +++ b/crates/swc_ecma_minifier/tests/fixture/next/outstatic/1/output.js @@ -6,28 +6,28 @@ 1973: function(module) { eval(module.code); }, - 1985: function(c, o, f) { + 1985: function(n, t, e) { "use strict"; - var i = 0, l = (c)=>"checkbox" === c.type; - var k = (c, o)=>{ - var f; - return c.has((f = o).substring(0, f.search(/\.\d+(\.|$)/)) || f); - }, v = ()=>{}; - function b(c, o, f) {} - var g = (c)=>"radio" === c.type; - function m(c = {}) { - let o, f = { - ...c - }, i = {}, l = 0, k = {}, v = {}; - const b = 0, g = async (c)=>{ - let o = c.target.name; - const f = 0; + var c = 0, r = (n)=>"checkbox" === n.type; + var u = (n, t)=>{ + var e; + return n.has((e = t).substring(0, e.search(/\.\d+(\.|$)/)) || e); + }, o = ()=>{}; + function s(n, t, e) {} + var a = (n)=>"radio" === n.type; + function f(n = {}) { + let t, e = { + ...n + }, c = {}, r = 0, u = {}, o = {}; + const s = 0, a = async (n)=>{ + let t = n.target.name; + const e = 0; }; } - o.useForm = function(c = {}) { - const o = i.default.useRef(); - o.current ? o.current.control._options = c : o.current = { - ...m(c) + t.useForm = function(n = {}) { + const t = c.default.useRef(); + t.current ? t.current.control._options = n : t.current = { + ...f(n) }; }; } diff --git a/crates/swc_ecma_minifier/tests/full/issues/7094/1/output.js b/crates/swc_ecma_minifier/tests/full/issues/7094/1/output.js index d1519ef8846..5ce963deee4 100644 --- a/crates/swc_ecma_minifier/tests/full/issues/7094/1/output.js +++ b/crates/swc_ecma_minifier/tests/full/issues/7094/1/output.js @@ -1 +1 @@ -export function useSyncExternalStore$2(r,o,u){return o()}eval(); +export function useSyncExternalStore$2(e,n,r){return n()}eval(); diff --git a/crates/swc_ecma_minifier/tests/full/issues/7094/2/output.js b/crates/swc_ecma_minifier/tests/full/issues/7094/2/output.js index 5bc1320e7c7..0d69f48b77a 100644 --- a/crates/swc_ecma_minifier/tests/full/issues/7094/2/output.js +++ b/crates/swc_ecma_minifier/tests/full/issues/7094/2/output.js @@ -1 +1 @@ -export function foo(){return eval(),function(o,l,c){return l()}}export function bar(){const o=Math.random()>.5?1:2;console.log(o),console.log(o),console.log(o),console.log(o),console.log(o),console.log(o)} +export function foo(){return eval(),function(o,n,l){return n()}}export function bar(){const o=Math.random()>.5?1:2;console.log(o),console.log(o),console.log(o),console.log(o),console.log(o),console.log(o)} diff --git a/crates/swc_ecma_minifier/tests/terser/compress/issue_976/eval_collapse_vars/output.mangleOnly.js b/crates/swc_ecma_minifier/tests/terser/compress/issue_976/eval_collapse_vars/output.mangleOnly.js index ab48543f799..8b29c81f957 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/issue_976/eval_collapse_vars/output.mangleOnly.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/issue_976/eval_collapse_vars/output.mangleOnly.js @@ -9,8 +9,8 @@ function f1() { eval(x, y, z, e); } function p1() { - var o = foo(), r = bar(), eval = baz(); - return o + r + eval; + var a = foo(), o = bar(), eval = baz(); + return a + o + eval; } function p2() { var a = foo(), b = bar(), eval = baz; diff --git a/crates/swc_ecma_minifier/tests/terser/compress/issue_976/eval_mangle/output.mangleOnly.js b/crates/swc_ecma_minifier/tests/terser/compress/issue_976/eval_mangle/output.mangleOnly.js index 0de3c3e0942..149ea82f02a 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/issue_976/eval_mangle/output.mangleOnly.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/issue_976/eval_mangle/output.mangleOnly.js @@ -1,4 +1,4 @@ -function f1(n, eval, r, t, u) { +function f1(n, eval, c, r, t) { return n("c") + eval; } function f2(a, b, c, d, e) { diff --git a/crates/swc_ecma_minifier/tests/terser/compress/issue_976/eval_unused/output.mangleOnly.js b/crates/swc_ecma_minifier/tests/terser/compress/issue_976/eval_unused/output.mangleOnly.js index 0de3c3e0942..149ea82f02a 100644 --- a/crates/swc_ecma_minifier/tests/terser/compress/issue_976/eval_unused/output.mangleOnly.js +++ b/crates/swc_ecma_minifier/tests/terser/compress/issue_976/eval_unused/output.mangleOnly.js @@ -1,4 +1,4 @@ -function f1(n, eval, r, t, u) { +function f1(n, eval, c, r, t) { return n("c") + eval; } function f2(a, b, c, d, e) { diff --git a/crates/swc_ecma_transforms_base/src/rename/collector.rs b/crates/swc_ecma_transforms_base/src/rename/collector.rs index 5cf176bd305..87cb4453a75 100644 --- a/crates/swc_ecma_transforms_base/src/rename/collector.rs +++ b/crates/swc_ecma_transforms_base/src/rename/collector.rs @@ -51,7 +51,7 @@ where is_pat_decl: bool, /// [None] if there's no `eval`. - pub top_level_mark_for_eval: Option, + pub top_level_for_eval: Option, } impl CustomBindingCollector @@ -59,8 +59,8 @@ where I: IdentLike + Eq + Hash + Send + Sync, { fn add(&mut self, i: &Ident) { - if let Some(top_level_mark) = self.top_level_mark_for_eval { - if i.span.ctxt.outer().is_descendant_of(top_level_mark) { + if let Some(top_level_ctxt) = self.top_level_for_eval { + if i.span.ctxt == top_level_ctxt { self.preserved.insert(I::from_ident(i)); return; } @@ -201,7 +201,7 @@ where bindings: Default::default(), preserved: Default::default(), is_pat_decl: false, - top_level_mark_for_eval, + top_level_for_eval: top_level_mark_for_eval.map(|m| SyntaxContext::empty().apply_mark(m)), }; n.visit_with(&mut v); (v.bindings, v.preserved)