Skip to content

Commit

Permalink
fix(es/minifier): Fix a bug related to inliner and the variable scopi…
Browse files Browse the repository at this point in the history
…ng (#8542)

**Related issue:**

 - Closes #8246
  • Loading branch information
kdy1 committed Jan 24, 2024
1 parent 2d15177 commit aa70131
Show file tree
Hide file tree
Showing 10 changed files with 223 additions and 23 deletions.
2 changes: 1 addition & 1 deletion crates/swc/tests/fixture/issues-5xxx/5865/output/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
var a;v=(a=r,b=>a.map(t=>{if(t)return t.foo}));
let a;v=(a=r,b=>a.map(t=>{if(t)return t.foo}));
73 changes: 73 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8246/input/.swcrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
{
"jsc": {
"parser": {
"syntax": "ecmascript",
"jsx": true
},
"transform": null,
"target": "es2015",
"loose": true,
"externalHelpers": false,
"minify": {
"mangle": {
"toplevel": false,
"keep_classnames": false,
"keep_fnames": false,
"keep_private_props": false,
"ie8": false,
"safari10": false
},
"compress": {
"arguments": false,
"arrows": true,
"booleans": true,
"booleans_as_integers": false,
"collapse_vars": true,
"comparisons": true,
"computed_props": true,
"conditionals": true,
"dead_code": true,
"directives": true,
"drop_console": false,
"drop_debugger": true,
"evaluate": true,
"expression": false,
"hoist_funs": false,
"hoist_props": true,
"hoist_vars": false,
"if_return": true,
"join_vars": true,
"keep_classnames": false,
"keep_fargs": true,
"keep_fnames": false,
"keep_infinity": false,
"loops": true,
"negate_iife": true,
"properties": true,
"reduce_funcs": false,
"reduce_vars": false,
"side_effects": true,
"switches": true,
"typeofs": true,
"unsafe": false,
"unsafe_arrows": false,
"unsafe_comps": false,
"unsafe_Function": false,
"unsafe_math": false,
"unsafe_symbols": false,
"unsafe_methods": false,
"unsafe_proto": false,
"unsafe_regexp": false,
"unsafe_undefined": false,
"unused": true,
"const_to_let": true,
"pristine_globals": true
}
}
},
"module": {
"type": "commonjs"
},
"isModule": false,
"minify": false
}
24 changes: 24 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8246/input/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function withLog(methods) {
const result = {}
for (const methodName in methods) {
result[methodName] = ((methodName) => function () {
console.log(methodName + ' invoked')
return methods[methodName].apply(this, arguments)
})(methodName)
}
return result
}

function main() {
const result = withLog({
test() {
console.log('method test executed')
},
another() {
console.log('method another executed')
}
})

result.test()
}
main()
21 changes: 21 additions & 0 deletions crates/swc/tests/fixture/issues-8xxx/8246/output/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function withLog(t) {
let e = {};
for(let o in t){
let n;
e[o] = (n = o, function() {
return console.log(n + ' invoked'), t[n].apply(this, arguments);
});
}
return e;
}
function main() {
withLog({
test () {
console.log('method test executed');
},
another () {
console.log('method another executed');
}
}).test();
}
main();
2 changes: 1 addition & 1 deletion crates/swc_ecma_minifier/src/compress/optimize/iife.rs
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ impl Optimizer<'_> {
self.prepend_stmts.push(
VarDecl {
span: DUMMY_SP,
kind: VarDeclKind::Var,
kind: VarDeclKind::Let,
declare: Default::default(),
decls: vars,
}
Expand Down
81 changes: 81 additions & 0 deletions crates/swc_ecma_minifier/tests/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11111,3 +11111,84 @@ fn issue_8119_2() {
false,
);
}

#[test]
fn issue_8246_1() {
run_exec_test(
r#"
function withLog(methods) {
const result = {};
for(const methodName in methods){
result[methodName] = ((methodName)=>function() {
console.log(methodName + ' invoked');
return methods[methodName].apply(this, arguments);
})(methodName);
}
return result;
}
function main() {
const result = withLog({
test () {
console.log('method test executed');
},
another () {
console.log('method another executed');
}
});
result.test();
}
main();
"#,
r#"
{
"ecma": 2015,
"arguments": false,
"arrows": true,
"booleans": true,
"booleans_as_integers": false,
"collapse_vars": true,
"comparisons": true,
"computed_props": true,
"conditionals": true,
"dead_code": true,
"directives": true,
"drop_console": false,
"drop_debugger": true,
"evaluate": true,
"expression": false,
"hoist_funs": false,
"hoist_props": true,
"hoist_vars": false,
"if_return": true,
"join_vars": true,
"keep_classnames": false,
"keep_fargs": true,
"keep_fnames": false,
"keep_infinity": false,
"loops": true,
"negate_iife": true,
"properties": true,
"reduce_funcs": false,
"reduce_vars": false,
"side_effects": true,
"switches": true,
"typeofs": true,
"unsafe": false,
"unsafe_arrows": false,
"unsafe_comps": false,
"unsafe_Function": false,
"unsafe_math": false,
"unsafe_symbols": false,
"unsafe_methods": false,
"unsafe_proto": false,
"unsafe_regexp": false,
"unsafe_undefined": false,
"unused": true,
"const_to_let": true,
"pristine_globals": true,
"passes": 5
}
"#,
false,
);
}
3 changes: 2 additions & 1 deletion crates/swc_ecma_minifier/tests/fixture/next/47005/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
{
9145: function(m, S, h) {
"use strict";
let E, k;
h.d(S, {
u: function() {
return eu;
}
});
var E, k, R, A = h(7294);
var R, A = h(7294);
var O = h(5893);
var L = Object.create;
var j = Object.defineProperty;
Expand Down
34 changes: 17 additions & 17 deletions crates/swc_ecma_minifier/tests/fixture/next/outstatic/1/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@
1973: function(module) {
eval(module.code);
},
1985: function(n, t, e) {
1985: function(t, e, n) {
"use strict";
var c = 0, r = (n)=>"checkbox" === n.type;
var u = (n, t)=>{
var e;
return n.has((e = t).substring(0, e.search(/\.\d+(\.|$)/)) || e);
var c = 0, r = (t)=>"checkbox" === t.type;
var u = (t, e)=>{
let n;
return t.has((n = e).substring(0, n.search(/\.\d+(\.|$)/)) || n);
}, o = ()=>{};
function s(n, t, e) {}
var a = (n)=>"radio" === n.type;
function f(n = {}) {
let t, e = {
...n
function s(t, e, n) {}
var a = (t)=>"radio" === t.type;
function f(t = {}) {
let e, n = {
...t
}, c = {}, r = 0, u = {}, o = {};
const s = 0, a = async (n)=>{
let t = n.target.name;
const e = 0;
const s = 0, a = async (t)=>{
let e = t.target.name;
const n = 0;
};
}
t.useForm = function(n = {}) {
const t = c.default.useRef();
t.current ? t.current.control._options = n : t.current = {
...f(n)
e.useForm = function(t = {}) {
const e = c.default.useRef();
e.current ? e.current.control._options = t : e.current = {
...f(t)
};
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
const func_bag = {
leak: leak
};
var x;
let x;
leak(x = func_bag.leak);
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const func_bag = {
func_bag.func2 = function() {
return void 0 === this ? "PASS" : "FAIL";
};
var x;
let x;
console.log((x = func_bag.func)());
var x1;
let x1;
console.log((x1 = func_bag.func2)());

0 comments on commit aa70131

Please sign in to comment.