fix corner cases in inline & unused (#5534)

fixes #5533
This commit is contained in:
Alex Lam S.L 2022-06-30 08:34:45 +01:00 committed by GitHub
parent 2426657daa
commit 4c227cc6bd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 694 additions and 12 deletions

View File

@ -6948,7 +6948,7 @@ Compressor.prototype.compress = function(node) {
node.properties = properties; node.properties = properties;
return node; return node;
} }
if (node instanceof AST_SymbolDeclaration) return node.definition().id in in_use_ids ? node : null; if (node instanceof AST_SymbolDeclaration) return trim_decl(node);
}); });
var tt = new TreeTransformer(function(node, descend, in_list) { var tt = new TreeTransformer(function(node, descend, in_list) {
var parent = tt.parent(); var parent = tt.parent();
@ -7063,9 +7063,7 @@ Compressor.prototype.compress = function(node) {
} else { } else {
var trimmed = trim_destructured(rest, make_node(AST_Array, parent, { var trimmed = trim_destructured(rest, make_node(AST_Array, parent, {
elements: args.slice(argnames.length), elements: args.slice(argnames.length),
}), function(node) { }), trim_decl, !node.uses_arguments, rest);
return node.definition().id in in_use_ids ? node : null;
}, !node.uses_arguments, rest);
rest = trimmed.name; rest = trimmed.name;
args.length = argnames.length; args.length = argnames.length;
if (trimmed.value.elements.length) [].push.apply(args, trimmed.value.elements); if (trimmed.value.elements.length) [].push.apply(args, trimmed.value.elements);
@ -7095,6 +7093,8 @@ Compressor.prototype.compress = function(node) {
} else if (trim) { } else if (trim) {
log(sym, "Dropping unused function argument {name}"); log(sym, "Dropping unused function argument {name}");
argnames.pop(); argnames.pop();
def.eliminated++;
sym.unused = true;
} else { } else {
sym.unused = true; sym.unused = true;
} }
@ -7104,9 +7104,7 @@ Compressor.prototype.compress = function(node) {
if (!args || spread < i) { if (!args || spread < i) {
funarg = sym.transform(trimmer); funarg = sym.transform(trimmer);
} else { } else {
var trimmed = trim_destructured(sym, args[i], function(node) { var trimmed = trim_destructured(sym, args[i], trim_decl, trim_value, sym);
return node.definition().id in in_use_ids ? node : null;
}, trim_value, sym);
funarg = trimmed.name; funarg = trimmed.name;
if (trimmed.value) args[i] = trimmed.value; if (trimmed.value) args[i] = trimmed.value;
} }
@ -7706,6 +7704,12 @@ Compressor.prototype.compress = function(node) {
return (node instanceof AST_DefaultValue ? node.name : node) instanceof AST_SymbolDeclaration; return (node instanceof AST_DefaultValue ? node.name : node) instanceof AST_SymbolDeclaration;
} }
function trim_decl(node) {
if (node.definition().id in in_use_ids) return node;
if (node instanceof AST_SymbolFunarg) node.unused = true;
return null;
}
function trim_default(trimmer, node) { function trim_default(trimmer, node) {
node.value = node.value.transform(tt); node.value = node.value.transform(tt);
var name = node.name.transform(trimmer); var name = node.name.transform(trimmer);
@ -13660,7 +13664,7 @@ Compressor.prototype.compress = function(node) {
if (def.orig.length == 1 && fn.functions.has(name)) return; if (def.orig.length == 1 && fn.functions.has(name)) return;
if (!all(def.orig, function(sym) { if (!all(def.orig, function(sym) {
if (sym instanceof AST_SymbolConst) return false; if (sym instanceof AST_SymbolConst) return false;
if (sym instanceof AST_SymbolFunarg) return def.scope.resolve() !== fn; if (sym instanceof AST_SymbolFunarg) return !sym.unused && def.scope.resolve() !== fn;
if (sym instanceof AST_SymbolLet) return false; if (sym instanceof AST_SymbolLet) return false;
return true; return true;
})) return; })) return;

View File

@ -2463,3 +2463,347 @@ issue_5485: {
expect_stdout: "PASS" expect_stdout: "PASS"
node_version: ">=6" node_version: ">=6"
} }
issue_5533_1_keep_fargs: {
options = {
evaluate: true,
inline: true,
join_vars: true,
keep_fargs: true,
loops: true,
side_effects: true,
unused: true,
}
input: {
"use strict";
try {
(function() {
var a;
for (; 1;)
a = function() {
(function f(b = 42) {
b;
throw "PASS";
})();
}();
})();
} catch (e) {
console.log(e);
}
}
expect: {
"use strict";
try {
(function() {
for (;;)
throw "PASS";
})();
} catch (e) {
console.log(e);
}
}
expect_stdout: "PASS"
node_version: ">=6"
}
issue_5533_1_drop_fargs: {
options = {
evaluate: true,
inline: true,
join_vars: true,
keep_fargs: false,
loops: true,
side_effects: true,
unused: true,
}
input: {
"use strict";
try {
(function() {
var a;
for (; 1;)
a = function() {
(function f(b = 42) {
b;
throw "PASS";
})();
}();
})();
} catch (e) {
console.log(e);
}
}
expect: {
"use strict";
try {
(function() {
for (;;)
throw "PASS";
})();
} catch (e) {
console.log(e);
}
}
expect_stdout: "PASS"
node_version: ">=6"
}
issue_5533_2_keep_fargs: {
options = {
evaluate: true,
inline: true,
join_vars: true,
keep_fargs: true,
loops: true,
side_effects: true,
unused: true,
}
input: {
"use strict";
try {
(function() {
var a;
for (; 1;)
a = function() {
(function f([ b ] = []) {
b;
throw "PASS";
})();
}();
})();
} catch (e) {
console.log(e);
}
}
expect: {
"use strict";
try {
(function() {
for (;;) {
var [ [] = [] ] = [];
throw "PASS";
}
})();
} catch (e) {
console.log(e);
}
}
expect_stdout: "PASS"
node_version: ">=6"
}
issue_5533_2_drop_fargs: {
options = {
evaluate: true,
inline: true,
join_vars: true,
keep_fargs: false,
loops: true,
side_effects: true,
unused: true,
}
input: {
"use strict";
try {
(function() {
var a;
for (; 1;)
a = function() {
(function f([ b ] = []) {
b;
throw "PASS";
})();
}();
})();
} catch (e) {
console.log(e);
}
}
expect: {
"use strict";
try {
(function() {
for (;;) {
var [ [] = [] ] = [];
throw "PASS";
}
})();
} catch (e) {
console.log(e);
}
}
expect_stdout: "PASS"
node_version: ">=6"
}
issue_5533_3_keep_fargs: {
options = {
evaluate: true,
inline: true,
join_vars: true,
keep_fargs: true,
loops: true,
side_effects: true,
unused: true,
}
input: {
"use strict";
try {
(function() {
var a;
for (; 1;)
a = function() {
(function f(b = 42, c = null) {
c;
throw "PASS";
})();
}();
})();
} catch (e) {
console.log(e);
}
}
expect: {
"use strict";
try {
(function() {
for (;;)
throw "PASS";
})();
} catch (e) {
console.log(e);
}
}
expect_stdout: "PASS"
node_version: ">=6"
}
issue_5533_3_drop_fargs: {
options = {
evaluate: true,
inline: true,
join_vars: true,
keep_fargs: false,
loops: true,
side_effects: true,
unused: true,
}
input: {
"use strict";
try {
(function() {
var a;
for (; 1;)
a = function() {
(function f(b = 42, c = null) {
c;
throw "PASS";
})();
}();
})();
} catch (e) {
console.log(e);
}
}
expect: {
"use strict";
try {
(function() {
for (;;)
throw "PASS";
})();
} catch (e) {
console.log(e);
}
}
expect_stdout: "PASS"
node_version: ">=6"
}
issue_5533_4_keep_fargs: {
options = {
evaluate: true,
inline: true,
join_vars: true,
keep_fargs: true,
loops: true,
side_effects: true,
unused: true,
}
input: {
"use strict";
try {
(function() {
var a;
for (; 1;)
a = function() {
(function f(b = 42, [ c ] = []) {
c;
throw "PASS";
})();
}();
})();
} catch (e) {
console.log(e);
}
}
expect: {
"use strict";
try {
(function() {
for (;;) {
var [ [] = [] ] = [];
throw "PASS";
}
})();
} catch (e) {
console.log(e);
}
}
expect_stdout: "PASS"
node_version: ">=6"
}
issue_5533_4_drop_fargs: {
options = {
evaluate: true,
inline: true,
join_vars: true,
keep_fargs: false,
loops: true,
side_effects: true,
unused: true,
}
input: {
"use strict";
try {
(function() {
var a;
for (; 1;)
a = function() {
(function f(b = 42, [ c ] = []) {
c;
throw "PASS";
})();
}();
})();
} catch (e) {
console.log(e);
}
}
expect: {
"use strict";
try {
(function() {
for (;;) {
var [ [] = [] ] = [];
throw "PASS";
}
})();
} catch (e) {
console.log(e);
}
}
expect_stdout: "PASS"
node_version: ">=6"
}

View File

@ -3646,3 +3646,87 @@ issue_5485: {
expect_stdout: "PASS" expect_stdout: "PASS"
node_version: ">=6" node_version: ">=6"
} }
issue_5533_keep_fargs: {
options = {
evaluate: true,
inline: true,
join_vars: true,
keep_fargs: true,
loops: true,
side_effects: true,
unused: true,
}
input: {
"use strict";
try {
(function() {
var a;
for (; 1;)
a = function() {
(function f([ b ]) {
b;
throw "PASS";
})([]);
}();
})();
} catch (e) {
console.log(e);
}
}
expect: {
"use strict";
try {
(function() {
for (;;)
throw "PASS";
})();
} catch (e) {
console.log(e);
}
}
expect_stdout: "PASS"
node_version: ">=6"
}
issue_5533_drop_fargs: {
options = {
evaluate: true,
inline: true,
join_vars: true,
keep_fargs: false,
loops: true,
side_effects: true,
unused: true,
}
input: {
"use strict";
try {
(function() {
var a;
for (; 1;)
a = function() {
(function f([ b ]) {
b;
throw "PASS";
})([]);
}();
})();
} catch (e) {
console.log(e);
}
}
expect: {
"use strict";
try {
(function() {
for (;;)
throw "PASS";
})();
} catch (e) {
console.log(e);
}
}
expect_stdout: "PASS"
node_version: ">=6"
}

View File

@ -3685,3 +3685,85 @@ issue_5271: {
} }
expect_stdout: "42" expect_stdout: "42"
} }
issue_5533_keep_fargs: {
options = {
evaluate: true,
inline: true,
join_vars: true,
keep_fargs: true,
loops: true,
side_effects: true,
unused: true,
}
input: {
"use strict";
try {
(function() {
var a;
for (; 1;)
a = function() {
(function f(b) {
b;
throw "PASS";
})();
}();
})();
} catch (e) {
console.log(e);
}
}
expect: {
"use strict";
try {
(function() {
for (;;)
throw "PASS";
})();
} catch (e) {
console.log(e);
}
}
expect_stdout: "PASS"
}
issue_5533_drop_fargs: {
options = {
evaluate: true,
inline: true,
join_vars: true,
keep_fargs: false,
loops: true,
side_effects: true,
unused: true,
}
input: {
"use strict";
try {
(function() {
var a;
for (; 1;)
a = function() {
(function f(b) {
b;
throw "PASS";
})();
}();
})();
} catch (e) {
console.log(e);
}
}
expect: {
"use strict";
try {
(function() {
for (;;)
throw "PASS";
})();
} catch (e) {
console.log(e);
}
}
expect_stdout: "PASS"
}

View File

@ -1363,3 +1363,171 @@ issue_5391: {
expect_stdout: "NaN" expect_stdout: "NaN"
node_version: ">=8.3.0" node_version: ">=8.3.0"
} }
issue_5533_1_keep_fargs: {
options = {
evaluate: true,
inline: true,
join_vars: true,
keep_fargs: true,
loops: true,
side_effects: true,
unused: true,
}
input: {
"use strict";
try {
(function() {
var a;
for (; 1;)
a = function() {
(function f(...b) {
b;
throw "PASS";
})();
}();
})();
} catch (e) {
console.log(e);
}
}
expect: {
"use strict";
try {
(function() {
for (;;)
throw "PASS";
})();
} catch (e) {
console.log(e);
}
}
expect_stdout: "PASS"
node_version: ">=6"
}
issue_5533_1_drop_fargs: {
options = {
evaluate: true,
inline: true,
join_vars: true,
keep_fargs: false,
loops: true,
side_effects: true,
unused: true,
}
input: {
"use strict";
try {
(function() {
var a;
for (; 1;)
a = function() {
(function f(...b) {
b;
throw "PASS";
})();
}();
})();
} catch (e) {
console.log(e);
}
}
expect: {
"use strict";
try {
(function() {
for (;;)
throw "PASS";
})();
} catch (e) {
console.log(e);
}
}
expect_stdout: "PASS"
node_version: ">=6"
}
issue_5533_2_keep_fargs: {
options = {
evaluate: true,
inline: true,
join_vars: true,
keep_fargs: true,
loops: true,
side_effects: true,
unused: true,
}
input: {
"use strict";
try {
(function() {
var a;
for (; 1;)
a = function() {
(function f(...[ b ]) {
b;
throw "PASS";
})();
}();
})();
} catch (e) {
console.log(e);
}
}
expect: {
"use strict";
try {
(function() {
for (;;)
throw "PASS";
})();
} catch (e) {
console.log(e);
}
}
expect_stdout: "PASS"
node_version: ">=6"
}
issue_5533_2_drop_fargs: {
options = {
evaluate: true,
inline: true,
join_vars: true,
keep_fargs: false,
loops: true,
side_effects: true,
unused: true,
}
input: {
"use strict";
try {
(function() {
var a;
for (; 1;)
a = function() {
(function f(...[ b ]) {
b;
throw "PASS";
})();
}();
})();
} catch (e) {
console.log(e);
}
}
expect: {
"use strict";
try {
(function() {
for (;;)
throw "PASS";
})();
} catch (e) {
console.log(e);
}
}
expect_stdout: "PASS"
node_version: ">=6"
}

View File

@ -780,7 +780,7 @@ function compare_run_code(code, minify_options, result_cache, max_timeout) {
'"use strict";', '"use strict";',
"(async()=>{", "(async()=>{",
code, code,
"})().catch(e=>console.log(e));", '})().catch(e=>process.on("exit",()=>{throw e}));',
].join("\n"); ].join("\n");
return run_code(code, toplevel, result_cache, timeout); return run_code(code, toplevel, result_cache, timeout);
} }

View File

@ -2106,7 +2106,7 @@ function run_code(code, toplevel, timeout) {
'"use strict";', '"use strict";',
"(async()=>{", "(async()=>{",
code, code,
"})().catch(e=>console.log(e));", '})().catch(e=>process.on("exit",()=>{throw e}));',
].join("\n"); ].join("\n");
return sandbox.run_code(sandbox.patch_module_statements(code), toplevel, timeout); return sandbox.run_code(sandbox.patch_module_statements(code), toplevel, timeout);
} }