Skip to content

Commit e2242c4

Browse files
authoredSep 12, 2024··
fix(es/minifier): Avoid decl name when mangle with eval (#9546)
**Related issue:** - Closes #9542
1 parent 48bb8f4 commit e2242c4

File tree

5 files changed

+73
-4
lines changed

5 files changed

+73
-4
lines changed
 

‎.changeset/warm-lies-work.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: patch
3+
swc_ecma_minifier: patch
4+
---
5+
6+
fix(es/minifier): Avoid decl name when mangle with eval

‎crates/swc/tests/fixture/issues-5xxx/5068/1/output/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ function _templateObject() {
33
var data = _tagged_template_literal([
44
"\n position: absolute;\n"
55
]);
6-
_templateObject = function _templateObject() {
6+
_templateObject = function _templateObject1() {
77
return data;
88
};
99
return data;
@@ -12,7 +12,7 @@ function _templateObject1() {
1212
var data = _tagged_template_literal([
1313
"\n position: absolute;\n"
1414
]);
15-
_templateObject1 = function _templateObject() {
15+
_templateObject1 = function _templateObject1() {
1616
return data;
1717
};
1818
return data;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function test() {
2+
(function (module) {
3+
function FormatSendData(data) {}
4+
eval();
5+
})();
6+
7+
function n(i) {
8+
var r1 = t[i];
9+
if (void 0 !== r1) return r1.exports;
10+
var o1 = (t[i] = {
11+
exports: {},
12+
});
13+
return e[i](o1, o1.exports, n), o1.exports;
14+
}
15+
}
16+
test();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function test() {
2+
(function(module) {
3+
function FormatSendData(n) {}
4+
eval();
5+
})();
6+
function n(r) {
7+
var o = t[r];
8+
if (void 0 !== o) return o.exports;
9+
var i = (t[r] = {
10+
exports: {}
11+
});
12+
return e[r](i, i.exports, n), i.exports;
13+
}
14+
}
15+
test();

‎crates/swc_ecma_transforms_base/src/rename/mod.rs

+34-2
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,41 @@ where
282282

283283
unit!(visit_mut_private_method, PrivateMethod);
284284

285-
unit!(visit_mut_fn_decl, FnDecl, true);
285+
fn visit_mut_fn_decl(&mut self, n: &mut FnDecl) {
286+
if !self.config.ignore_eval && contains_eval(n, true) {
287+
n.visit_mut_children_with(self);
288+
} else {
289+
let id = n.ident.to_id();
290+
let inserted = self.preserved.insert(id.clone());
291+
let map = self.get_map(n, true, false, false);
292+
293+
if inserted {
294+
self.preserved.remove(&id);
295+
}
296+
297+
if !map.is_empty() {
298+
n.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
299+
}
300+
}
301+
}
286302

287-
unit!(visit_mut_class_decl, ClassDecl, true);
303+
fn visit_mut_class_decl(&mut self, n: &mut ClassDecl) {
304+
if !self.config.ignore_eval && contains_eval(n, true) {
305+
n.visit_mut_children_with(self);
306+
} else {
307+
let id = n.ident.to_id();
308+
let inserted = self.preserved.insert(id.clone());
309+
let map = self.get_map(n, true, false, false);
310+
311+
if inserted {
312+
self.preserved.remove(&id);
313+
}
314+
315+
if !map.is_empty() {
316+
n.visit_mut_with(&mut rename_with_config(&map, self.config.clone()));
317+
}
318+
}
319+
}
288320

289321
fn visit_mut_default_decl(&mut self, n: &mut DefaultDecl) {
290322
match n {

0 commit comments

Comments
 (0)
Please sign in to comment.