Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(es/minifier): Avoid decl name when mangle with eval #9546

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading