fix(es/minifier): Avoid decl name when mangle with eval (#9546)
Some checks failed
CI / Cargo fmt (push) Has been cancelled
CI / Cargo clippy (push) Has been cancelled
CI / Check license of dependencies (push) Has been cancelled
CI / Check (macos-latest) (push) Has been cancelled
CI / Check (ubuntu-latest) (push) Has been cancelled
CI / Check (windows-latest) (push) Has been cancelled
CI / Test wasm (binding_core_wasm) (push) Has been cancelled
CI / Test wasm (binding_minifier_wasm) (push) Has been cancelled
CI / Test wasm (binding_typescript_wasm) (push) Has been cancelled
CI / List crates (push) Has been cancelled
CI / Test node bindings - ${{ matrix.os }} (macos-latest) (push) Has been cancelled
CI / Test node bindings - ${{ matrix.os }} (windows-latest) (push) Has been cancelled
CI / Test with @swc/cli (push) Has been cancelled
CI / Miri (better_scoped_tls) (push) Has been cancelled
CI / Miri (string_enum) (push) Has been cancelled
CI / Miri (swc) (push) Has been cancelled
CI / Miri (swc_bundler) (push) Has been cancelled
CI / Miri (swc_ecma_codegen) (push) Has been cancelled
CI / Miri (swc_ecma_minifier) (push) Has been cancelled
Benchmark / Bench everything (push) Has been cancelled
CI / Test - ${{ matrix.settings.crate }} - ${{ matrix.settings.os }} (push) Has been cancelled
CI / Done (push) Has been cancelled

**Related issue:**

 - Closes https://github.com/swc-project/swc/issues/9542
This commit is contained in:
Austaras 2024-09-12 10:25:28 +08:00 committed by GitHub
parent 48bb8f4277
commit e2242c41c4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 73 additions and 4 deletions

View File

@ -0,0 +1,6 @@
---
swc_core: patch
swc_ecma_minifier: patch
---
fix(es/minifier): Avoid decl name when mangle with eval

View File

@ -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;

View File

@ -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();

View File

@ -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();

View File

@ -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 {