diff --git a/.changeset/warm-lies-work.md b/.changeset/warm-lies-work.md new file mode 100644 index 00000000000..b6b4bbc6f3d --- /dev/null +++ b/.changeset/warm-lies-work.md @@ -0,0 +1,6 @@ +--- +swc_core: patch +swc_ecma_minifier: patch +--- + +fix(es/minifier): Avoid decl name when mangle with eval diff --git a/crates/swc/tests/fixture/issues-5xxx/5068/1/output/index.js b/crates/swc/tests/fixture/issues-5xxx/5068/1/output/index.js index c0b97cb057e..7e1982094b6 100644 --- a/crates/swc/tests/fixture/issues-5xxx/5068/1/output/index.js +++ b/crates/swc/tests/fixture/issues-5xxx/5068/1/output/index.js @@ -3,7 +3,7 @@ function _templateObject() { var data = _tagged_template_literal([ "\n position: absolute;\n" ]); - _templateObject = function _templateObject() { + _templateObject = function _templateObject1() { return data; }; return data; @@ -12,7 +12,7 @@ function _templateObject1() { var data = _tagged_template_literal([ "\n position: absolute;\n" ]); - _templateObject1 = function _templateObject() { + _templateObject1 = function _templateObject1() { return data; }; return data; diff --git a/crates/swc_ecma_minifier/tests/mangle/issue-9542/input.js b/crates/swc_ecma_minifier/tests/mangle/issue-9542/input.js new file mode 100644 index 00000000000..d44d630a48f --- /dev/null +++ b/crates/swc_ecma_minifier/tests/mangle/issue-9542/input.js @@ -0,0 +1,16 @@ +function test() { + (function (module) { + function FormatSendData(data) {} + eval(); + })(); + + function n(i) { + var r1 = t[i]; + if (void 0 !== r1) return r1.exports; + var o1 = (t[i] = { + exports: {}, + }); + return e[i](o1, o1.exports, n), o1.exports; + } +} +test(); diff --git a/crates/swc_ecma_minifier/tests/mangle/issue-9542/output.js b/crates/swc_ecma_minifier/tests/mangle/issue-9542/output.js new file mode 100644 index 00000000000..9b5612beb92 --- /dev/null +++ b/crates/swc_ecma_minifier/tests/mangle/issue-9542/output.js @@ -0,0 +1,15 @@ +function test() { + (function(module) { + function FormatSendData(n) {} + eval(); + })(); + function n(r) { + var o = t[r]; + if (void 0 !== o) return o.exports; + var i = (t[r] = { + exports: {} + }); + return e[r](i, i.exports, n), i.exports; + } +} +test(); diff --git a/crates/swc_ecma_transforms_base/src/rename/mod.rs b/crates/swc_ecma_transforms_base/src/rename/mod.rs index fdd55aa9415..03aab3ac96f 100644 --- a/crates/swc_ecma_transforms_base/src/rename/mod.rs +++ b/crates/swc_ecma_transforms_base/src/rename/mod.rs @@ -282,9 +282,41 @@ where unit!(visit_mut_private_method, PrivateMethod); - unit!(visit_mut_fn_decl, FnDecl, true); + fn visit_mut_fn_decl(&mut self, n: &mut FnDecl) { + if !self.config.ignore_eval && contains_eval(n, true) { + n.visit_mut_children_with(self); + } else { + let id = n.ident.to_id(); + let inserted = self.preserved.insert(id.clone()); + let map = self.get_map(n, true, false, false); - unit!(visit_mut_class_decl, ClassDecl, true); + if inserted { + self.preserved.remove(&id); + } + + if !map.is_empty() { + n.visit_mut_with(&mut rename_with_config(&map, self.config.clone())); + } + } + } + + fn visit_mut_class_decl(&mut self, n: &mut ClassDecl) { + if !self.config.ignore_eval && contains_eval(n, true) { + n.visit_mut_children_with(self); + } else { + let id = n.ident.to_id(); + let inserted = self.preserved.insert(id.clone()); + let map = self.get_map(n, true, false, false); + + if inserted { + self.preserved.remove(&id); + } + + if !map.is_empty() { + n.visit_mut_with(&mut rename_with_config(&map, self.config.clone())); + } + } + } fn visit_mut_default_decl(&mut self, n: &mut DefaultDecl) { match n {