Skip to content

Commit 1e44e57

Browse files
authoredDec 28, 2023
fix(es/renamer): Correctly check top level (#8456)
**Related issue:** - Closes #8451. - Closes #8185.
1 parent c39e75c commit 1e44e57

File tree

14 files changed

+82
-38
lines changed

14 files changed

+82
-38
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"jsc": {
3+
"parser": {
4+
"syntax": "typescript",
5+
"tsx": false
6+
},
7+
"target": "es5",
8+
"loose": false,
9+
"minify": {
10+
"compress": false,
11+
"mangle": false
12+
}
13+
},
14+
"module": {
15+
"type": "es6"
16+
},
17+
"minify": false,
18+
"isModule": true
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const a = () => {
2+
const _define_property = 1;
3+
const fn = () => console.log({ [prop]: 'test' });
4+
};
5+
6+
const b = () => eval('console.log()');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(function hello() {
2+
console.log('outer stack', new Error().stack)
3+
})();
4+
5+
eval("")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { _ as _define_property } from "@swc/helpers/_/_define_property";
2+
var a = function() {
3+
var _define_property1 = 1;
4+
var fn = function() {
5+
return console.log(_define_property({}, prop, "test"));
6+
};
7+
};
8+
var b = function() {
9+
return eval("console.log()");
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(function hello() {
2+
console.log("outer stack", new Error().stack);
3+
})();
4+
eval("");

‎crates/swc_ecma_minifier/tests/fixture/issues/6628/output.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
!function() {
22
var CallbackType, Collector = function() {
3-
var u = function(u) {};
4-
return u.usePlugin = function(u, l, o) {}, u.plugins = [], u;
3+
var n = function(n) {};
4+
return n.usePlugin = function(n, u, l) {}, n.plugins = [], n;
55
}();
6-
!function(u) {
7-
u[u.Var = 0] = "Var", u[u.All = 1] = "All";
6+
!function(n) {
7+
n[n.Var = 0] = "Var", n[n.All = 1] = "All";
88
}(CallbackType || (CallbackType = {}));
99
var CepRule = function() {
1010
eval();

‎crates/swc_ecma_minifier/tests/fixture/next/43052/output.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use(function(__unused_webpack_module, exports, __webpack_require__) {
2-
!function(n, c) {
3-
c(exports, __webpack_require__(7294), __webpack_require__(1321));
2+
!function(e, t) {
3+
t(exports, __webpack_require__(7294), __webpack_require__(1321));
44
}(this, function(exports, React) {
55
"use strict";
66
var index_production = {
@@ -26,7 +26,7 @@ use(function(__unused_webpack_module, exports, __webpack_require__) {
2626
});
2727
});
2828
});
29-
(function n() {
30-
const n = 1;
31-
use(n);
29+
(function e() {
30+
const e = 1;
31+
use(e);
3232
});

‎crates/swc_ecma_minifier/tests/fixture/next/outstatic/1/output.js

+19-19
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@
66
1973: function(module) {
77
eval(module.code);
88
},
9-
1985: function(c, o, f) {
9+
1985: function(n, t, e) {
1010
"use strict";
11-
var i = 0, l = (c)=>"checkbox" === c.type;
12-
var k = (c, o)=>{
13-
var f;
14-
return c.has((f = o).substring(0, f.search(/\.\d+(\.|$)/)) || f);
15-
}, v = ()=>{};
16-
function b(c, o, f) {}
17-
var g = (c)=>"radio" === c.type;
18-
function m(c = {}) {
19-
let o, f = {
20-
...c
21-
}, i = {}, l = 0, k = {}, v = {};
22-
const b = 0, g = async (c)=>{
23-
let o = c.target.name;
24-
const f = 0;
11+
var c = 0, r = (n)=>"checkbox" === n.type;
12+
var u = (n, t)=>{
13+
var e;
14+
return n.has((e = t).substring(0, e.search(/\.\d+(\.|$)/)) || e);
15+
}, o = ()=>{};
16+
function s(n, t, e) {}
17+
var a = (n)=>"radio" === n.type;
18+
function f(n = {}) {
19+
let t, e = {
20+
...n
21+
}, c = {}, r = 0, u = {}, o = {};
22+
const s = 0, a = async (n)=>{
23+
let t = n.target.name;
24+
const e = 0;
2525
};
2626
}
27-
o.useForm = function(c = {}) {
28-
const o = i.default.useRef();
29-
o.current ? o.current.control._options = c : o.current = {
30-
...m(c)
27+
t.useForm = function(n = {}) {
28+
const t = c.default.useRef();
29+
t.current ? t.current.control._options = n : t.current = {
30+
...f(n)
3131
};
3232
};
3333
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export function useSyncExternalStore$2(r,o,u){return o()}eval();
1+
export function useSyncExternalStore$2(e,n,r){return n()}eval();
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export function foo(){return eval(),function(o,l,c){return l()}}export function bar(){const o=Math.random()>.5?1:2;console.log(o),console.log(o),console.log(o),console.log(o),console.log(o),console.log(o)}
1+
export function foo(){return eval(),function(o,n,l){return n()}}export function bar(){const o=Math.random()>.5?1:2;console.log(o),console.log(o),console.log(o),console.log(o),console.log(o),console.log(o)}

‎crates/swc_ecma_minifier/tests/terser/compress/issue_976/eval_collapse_vars/output.mangleOnly.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ function f1() {
99
eval(x, y, z, e);
1010
}
1111
function p1() {
12-
var o = foo(), r = bar(), eval = baz();
13-
return o + r + eval;
12+
var a = foo(), o = bar(), eval = baz();
13+
return a + o + eval;
1414
}
1515
function p2() {
1616
var a = foo(), b = bar(), eval = baz;

‎crates/swc_ecma_minifier/tests/terser/compress/issue_976/eval_mangle/output.mangleOnly.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function f1(n, eval, r, t, u) {
1+
function f1(n, eval, c, r, t) {
22
return n("c") + eval;
33
}
44
function f2(a, b, c, d, e) {

‎crates/swc_ecma_minifier/tests/terser/compress/issue_976/eval_unused/output.mangleOnly.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function f1(n, eval, r, t, u) {
1+
function f1(n, eval, c, r, t) {
22
return n("c") + eval;
33
}
44
function f2(a, b, c, d, e) {

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ where
5151
is_pat_decl: bool,
5252

5353
/// [None] if there's no `eval`.
54-
pub top_level_mark_for_eval: Option<Mark>,
54+
pub top_level_for_eval: Option<SyntaxContext>,
5555
}
5656

5757
impl<I> CustomBindingCollector<I>
5858
where
5959
I: IdentLike + Eq + Hash + Send + Sync,
6060
{
6161
fn add(&mut self, i: &Ident) {
62-
if let Some(top_level_mark) = self.top_level_mark_for_eval {
63-
if i.span.ctxt.outer().is_descendant_of(top_level_mark) {
62+
if let Some(top_level_ctxt) = self.top_level_for_eval {
63+
if i.span.ctxt == top_level_ctxt {
6464
self.preserved.insert(I::from_ident(i));
6565
return;
6666
}
@@ -201,7 +201,7 @@ where
201201
bindings: Default::default(),
202202
preserved: Default::default(),
203203
is_pat_decl: false,
204-
top_level_mark_for_eval,
204+
top_level_for_eval: top_level_mark_for_eval.map(|m| SyntaxContext::empty().apply_mark(m)),
205205
};
206206
n.visit_with(&mut v);
207207
(v.bindings, v.preserved)

0 commit comments

Comments
 (0)
Please sign in to comment.