Skip to content

Commit

Permalink
fix(es/minifier): Avoid decl name when mangle with eval (#9546)
Browse files Browse the repository at this point in the history
**Related issue:**

 - Closes #9542
  • Loading branch information
Austaras committed Sep 12, 2024
1 parent 48bb8f4 commit e2242c4
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changeset/warm-lies-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: patch
swc_ecma_minifier: patch
---

fix(es/minifier): Avoid decl name when mangle with eval
4 changes: 2 additions & 2 deletions crates/swc/tests/fixture/issues-5xxx/5068/1/output/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions crates/swc_ecma_minifier/tests/mangle/issue-9542/input.js
Original file line number Diff line number Diff line change
@@ -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();
15 changes: 15 additions & 0 deletions crates/swc_ecma_minifier/tests/mangle/issue-9542/output.js
Original file line number Diff line number Diff line change
@@ -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();
36 changes: 34 additions & 2 deletions crates/swc_ecma_transforms_base/src/rename/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

if inserted {
self.preserved.remove(&id);
}

if !map.is_empty() {
n.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
}
}
}

unit!(visit_mut_class_decl, ClassDecl, true);
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 {
Expand Down

0 comments on commit e2242c4

Please sign in to comment.