Skip to content

Commit aa70131

Browse files
authored
fix(es/minifier): Fix a bug related to inliner and the variable scoping (#8542)
**Related issue:** - Closes #8246
1 parent 2d15177 commit aa70131

File tree

10 files changed

+223
-23
lines changed

10 files changed

+223
-23
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
var a;v=(a=r,b=>a.map(t=>{if(t)return t.foo}));
1+
let a;v=(a=r,b=>a.map(t=>{if(t)return t.foo}));
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
{
2+
"jsc": {
3+
"parser": {
4+
"syntax": "ecmascript",
5+
"jsx": true
6+
},
7+
"transform": null,
8+
"target": "es2015",
9+
"loose": true,
10+
"externalHelpers": false,
11+
"minify": {
12+
"mangle": {
13+
"toplevel": false,
14+
"keep_classnames": false,
15+
"keep_fnames": false,
16+
"keep_private_props": false,
17+
"ie8": false,
18+
"safari10": false
19+
},
20+
"compress": {
21+
"arguments": false,
22+
"arrows": true,
23+
"booleans": true,
24+
"booleans_as_integers": false,
25+
"collapse_vars": true,
26+
"comparisons": true,
27+
"computed_props": true,
28+
"conditionals": true,
29+
"dead_code": true,
30+
"directives": true,
31+
"drop_console": false,
32+
"drop_debugger": true,
33+
"evaluate": true,
34+
"expression": false,
35+
"hoist_funs": false,
36+
"hoist_props": true,
37+
"hoist_vars": false,
38+
"if_return": true,
39+
"join_vars": true,
40+
"keep_classnames": false,
41+
"keep_fargs": true,
42+
"keep_fnames": false,
43+
"keep_infinity": false,
44+
"loops": true,
45+
"negate_iife": true,
46+
"properties": true,
47+
"reduce_funcs": false,
48+
"reduce_vars": false,
49+
"side_effects": true,
50+
"switches": true,
51+
"typeofs": true,
52+
"unsafe": false,
53+
"unsafe_arrows": false,
54+
"unsafe_comps": false,
55+
"unsafe_Function": false,
56+
"unsafe_math": false,
57+
"unsafe_symbols": false,
58+
"unsafe_methods": false,
59+
"unsafe_proto": false,
60+
"unsafe_regexp": false,
61+
"unsafe_undefined": false,
62+
"unused": true,
63+
"const_to_let": true,
64+
"pristine_globals": true
65+
}
66+
}
67+
},
68+
"module": {
69+
"type": "commonjs"
70+
},
71+
"isModule": false,
72+
"minify": false
73+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function withLog(methods) {
2+
const result = {}
3+
for (const methodName in methods) {
4+
result[methodName] = ((methodName) => function () {
5+
console.log(methodName + ' invoked')
6+
return methods[methodName].apply(this, arguments)
7+
})(methodName)
8+
}
9+
return result
10+
}
11+
12+
function main() {
13+
const result = withLog({
14+
test() {
15+
console.log('method test executed')
16+
},
17+
another() {
18+
console.log('method another executed')
19+
}
20+
})
21+
22+
result.test()
23+
}
24+
main()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function withLog(t) {
2+
let e = {};
3+
for(let o in t){
4+
let n;
5+
e[o] = (n = o, function() {
6+
return console.log(n + ' invoked'), t[n].apply(this, arguments);
7+
});
8+
}
9+
return e;
10+
}
11+
function main() {
12+
withLog({
13+
test () {
14+
console.log('method test executed');
15+
},
16+
another () {
17+
console.log('method another executed');
18+
}
19+
}).test();
20+
}
21+
main();

crates/swc_ecma_minifier/src/compress/optimize/iife.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ impl Optimizer<'_> {
519519
self.prepend_stmts.push(
520520
VarDecl {
521521
span: DUMMY_SP,
522-
kind: VarDeclKind::Var,
522+
kind: VarDeclKind::Let,
523523
declare: Default::default(),
524524
decls: vars,
525525
}

crates/swc_ecma_minifier/tests/exec.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11111,3 +11111,84 @@ fn issue_8119_2() {
1111111111
false,
1111211112
);
1111311113
}
11114+
11115+
#[test]
11116+
fn issue_8246_1() {
11117+
run_exec_test(
11118+
r#"
11119+
function withLog(methods) {
11120+
const result = {};
11121+
for(const methodName in methods){
11122+
result[methodName] = ((methodName)=>function() {
11123+
console.log(methodName + ' invoked');
11124+
return methods[methodName].apply(this, arguments);
11125+
})(methodName);
11126+
}
11127+
return result;
11128+
}
11129+
function main() {
11130+
const result = withLog({
11131+
test () {
11132+
console.log('method test executed');
11133+
},
11134+
another () {
11135+
console.log('method another executed');
11136+
}
11137+
});
11138+
result.test();
11139+
}
11140+
main();
11141+
"#,
11142+
r#"
11143+
{
11144+
"ecma": 2015,
11145+
"arguments": false,
11146+
"arrows": true,
11147+
"booleans": true,
11148+
"booleans_as_integers": false,
11149+
"collapse_vars": true,
11150+
"comparisons": true,
11151+
"computed_props": true,
11152+
"conditionals": true,
11153+
"dead_code": true,
11154+
"directives": true,
11155+
"drop_console": false,
11156+
"drop_debugger": true,
11157+
"evaluate": true,
11158+
"expression": false,
11159+
"hoist_funs": false,
11160+
"hoist_props": true,
11161+
"hoist_vars": false,
11162+
"if_return": true,
11163+
"join_vars": true,
11164+
"keep_classnames": false,
11165+
"keep_fargs": true,
11166+
"keep_fnames": false,
11167+
"keep_infinity": false,
11168+
"loops": true,
11169+
"negate_iife": true,
11170+
"properties": true,
11171+
"reduce_funcs": false,
11172+
"reduce_vars": false,
11173+
"side_effects": true,
11174+
"switches": true,
11175+
"typeofs": true,
11176+
"unsafe": false,
11177+
"unsafe_arrows": false,
11178+
"unsafe_comps": false,
11179+
"unsafe_Function": false,
11180+
"unsafe_math": false,
11181+
"unsafe_symbols": false,
11182+
"unsafe_methods": false,
11183+
"unsafe_proto": false,
11184+
"unsafe_regexp": false,
11185+
"unsafe_undefined": false,
11186+
"unused": true,
11187+
"const_to_let": true,
11188+
"pristine_globals": true,
11189+
"passes": 5
11190+
}
11191+
"#,
11192+
false,
11193+
);
11194+
}

crates/swc_ecma_minifier/tests/fixture/next/47005/output.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55
{
66
9145: function(m, S, h) {
77
"use strict";
8+
let E, k;
89
h.d(S, {
910
u: function() {
1011
return eu;
1112
}
1213
});
13-
var E, k, R, A = h(7294);
14+
var R, A = h(7294);
1415
var O = h(5893);
1516
var L = Object.create;
1617
var j = Object.defineProperty;

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@
66
1973: function(module) {
77
eval(module.code);
88
},
9-
1985: function(n, t, e) {
9+
1985: function(t, e, n) {
1010
"use strict";
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);
11+
var c = 0, r = (t)=>"checkbox" === t.type;
12+
var u = (t, e)=>{
13+
let n;
14+
return t.has((n = e).substring(0, n.search(/\.\d+(\.|$)/)) || n);
1515
}, o = ()=>{};
16-
function s(n, t, e) {}
17-
var a = (n)=>"radio" === n.type;
18-
function f(n = {}) {
19-
let t, e = {
20-
...n
16+
function s(t, e, n) {}
17+
var a = (t)=>"radio" === t.type;
18+
function f(t = {}) {
19+
let e, n = {
20+
...t
2121
}, c = {}, r = 0, u = {}, o = {};
22-
const s = 0, a = async (n)=>{
23-
let t = n.target.name;
24-
const e = 0;
22+
const s = 0, a = async (t)=>{
23+
let e = t.target.name;
24+
const n = 0;
2525
};
2626
}
27-
t.useForm = function(n = {}) {
28-
const t = c.default.useRef();
29-
t.current ? t.current.control._options = n : t.current = {
30-
...f(n)
27+
e.useForm = function(t = {}) {
28+
const e = c.default.useRef();
29+
e.current ? e.current.control._options = t : e.current = {
30+
...f(t)
3131
};
3232
};
3333
}

crates/swc_ecma_minifier/tests/terser/compress/identity/inline_identity_dont_lose_this_when_arg/output.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
const func_bag = {
33
leak: leak
44
};
5-
var x;
5+
let x;
66
leak(x = func_bag.leak);

crates/swc_ecma_minifier/tests/terser/compress/identity/inline_identity_lose_this/output.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const func_bag = {
88
func_bag.func2 = function() {
99
return void 0 === this ? "PASS" : "FAIL";
1010
};
11-
var x;
11+
let x;
1212
console.log((x = func_bag.func)());
13-
var x1;
13+
let x1;
1414
console.log((x1 = func_bag.func2)());

0 commit comments

Comments
 (0)