fix corner cases with parameter scope (#5567)

fixes #5566
This commit is contained in:
Alex Lam S.L
2022-07-17 09:03:12 +01:00
committed by GitHub
parent 685ab357cc
commit ec4558be29
3 changed files with 193 additions and 35 deletions

View File

@@ -2647,16 +2647,17 @@ Compressor.prototype.compress = function(node) {
arg = null; arg = null;
return true; return true;
} }
if (node instanceof AST_ObjectIdentity && (fn_strict || !arg_scope)) { if (node instanceof AST_ObjectIdentity) {
arg = null; if (fn_strict || !arg_scope) arg = null;
return true; return true;
} }
if (node instanceof AST_SymbolRef && fn.variables.has(node.name)) { if (node instanceof AST_SymbolRef) {
var s = node.definition().scope; var def;
if (s !== scope) while (s = s.parent_scope) { if (node.in_arg && !is_safe_lexical(node.definition())
if (s === scope) return true; || (def = fn.variables.get(node.name)) && def !== node.definition()) {
arg = null;
} }
arg = null; return true;
} }
if (node instanceof AST_Scope && !is_arrow(node)) { if (node instanceof AST_Scope && !is_arrow(node)) {
var save_scope = arg_scope; var save_scope = arg_scope;
@@ -5985,7 +5986,7 @@ Compressor.prototype.compress = function(node) {
return true; return true;
} }
if (node instanceof AST_SymbolRef) { if (node instanceof AST_SymbolRef) {
if (self.inlined || node.redef) { if (self.inlined || node.redef || node.in_arg) {
result = false; result = false;
return true; return true;
} }
@@ -7679,6 +7680,7 @@ Compressor.prototype.compress = function(node) {
var def = node.left.definition(); var def = node.left.definition();
if (def.scope.resolve() === self) assignments.add(def.id, node); if (def.scope.resolve() === self) assignments.add(def.id, node);
} }
if (node instanceof AST_SymbolRef && node.in_arg) var_defs[node.definition().id] = 0;
if (node instanceof AST_Unary && node.expression instanceof AST_SymbolRef) { if (node instanceof AST_Unary && node.expression instanceof AST_SymbolRef) {
var def = node.expression.definition(); var def = node.expression.definition();
if (def.scope.resolve() === self) assignments.add(def.id, node); if (def.scope.resolve() === self) assignments.add(def.id, node);
@@ -8172,7 +8174,9 @@ Compressor.prototype.compress = function(node) {
// collect only vars which don't show up in self's arguments list // collect only vars which don't show up in self's arguments list
var defns = []; var defns = [];
if (self instanceof AST_Lambda) self.each_argname(function(argname) { if (self instanceof AST_Lambda) self.each_argname(function(argname) {
vars.del(argname.name); if (all(argname.definition().references, function(ref) {
return !ref.in_arg;
})) vars.del(argname.name);
}); });
vars.each(function(defn, name) { vars.each(function(defn, name) {
defn = defn.clone(); defn = defn.clone();

View File

@@ -180,6 +180,26 @@ collapse_arg_sequence: {
node_version: ">=6" node_version: ">=6"
} }
collapse_in_arg: {
options = {
collapse_vars: true,
keep_fargs: false,
unused: true,
}
input: {
(function(a, b = a) {
b("PASS");
})(console.log);
}
expect: {
(function(a) {
a("PASS");
})(console.log);
}
expect_stdout: "PASS"
node_version: ">=6"
}
collapse_value_1: { collapse_value_1: {
options = { options = {
collapse_vars: true, collapse_vars: true,
@@ -2872,3 +2892,127 @@ issue_5536: {
expect_stdout: "undefined" expect_stdout: "undefined"
node_version: ">=6" node_version: ">=6"
} }
issue_5566_1: {
options = {
unused: true,
}
input: {
(function(a, f = function() {
return a;
}) {
var a = "foo";
console.log(a, f());
})("bar");
}
expect: {
(function(a, f = function() {
return a;
}) {
var a = "foo";
console.log(a, f());
})("bar");
}
expect_stdout: "foo bar"
node_version: ">=6"
}
issue_5566_2: {
options = {
inline: true,
reduce_vars: true,
}
input: {
(function(a, f = function() {
return a;
}) {
function a() {}
console.log(typeof a, typeof f());
})(42);
}
expect: {
(function(a, f = function() {
return a;
}) {
function a() {}
console.log(typeof a, typeof f());
})(42);
}
expect_stdout: "function number"
node_version: ">=6"
}
issue_5566_3: {
options = {
reduce_vars: true,
unused: true,
}
input: {
(function(a, f = function() {
return a;
}) {
function a() {}
console.log(typeof a, typeof f());
})(42);
}
expect: {
(function(a, f = function() {
return a;
}) {
function a() {}
console.log(typeof a, typeof f());
})(42);
}
expect_stdout: "function number"
node_version: ">=6"
}
issue_5566_4: {
options = {
collapse_vars: true,
unused: true,
}
input: {
(function(a, b = function() {
return a;
}) {
var a = 0;
b()("PASS");
})(console.log);
}
expect: {
(function(a, b = function() {
return a;
}) {
var a = 0;
b()("PASS");
})(console.log);
}
expect_stdout: "PASS"
node_version: ">=6"
}
issue_5566_5: {
options = {
hoist_vars: true,
}
input: {
(function(a, f = function() {
return a;
}) {
var a = "foo";
var b;
console.log(a, f());
})("bar");
}
expect: {
(function(a, f = function() {
return a;
}) {
var b, a = "foo";
console.log(a, f());
})("bar");
}
expect_stdout: "foo bar"
node_version: ">=6"
}

View File

@@ -241,23 +241,6 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
CHANGED = true; CHANGED = true;
return node.name; return node.name;
} }
else if (node instanceof U.AST_DestructuredArray) {
var expr = node.elements[0];
if (expr && !(expr instanceof U.AST_Hole)) {
node.start._permute++;
CHANGED = true;
return expr;
}
}
else if (node instanceof U.AST_DestructuredObject) {
// first property's value
var expr = node.properties[0];
if (expr) {
node.start._permute++;
CHANGED = true;
return expr.value;
}
}
else if (node instanceof U.AST_Defun) { else if (node instanceof U.AST_Defun) {
switch (((node.start._permute += step) * steps | 0) % 2) { switch (((node.start._permute += step) * steps | 0) % 2) {
case 0: case 0:
@@ -275,6 +258,23 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
} }
} }
} }
else if (node instanceof U.AST_DestructuredArray) {
var expr = node.elements[0];
if (expr && !(expr instanceof U.AST_Hole)) {
node.start._permute++;
CHANGED = true;
return expr;
}
}
else if (node instanceof U.AST_DestructuredObject) {
// first property's value
var expr = node.properties[0];
if (expr) {
node.start._permute++;
CHANGED = true;
return expr.value;
}
}
else if (node instanceof U.AST_DWLoop) { else if (node instanceof U.AST_DWLoop) {
var expr = [ var expr = [
node.condition, node.condition,
@@ -296,6 +296,16 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
return to_statement(expr); return to_statement(expr);
} }
} }
else if (node instanceof U.AST_ExportDeclaration) {
node.start._permute++;
CHANGED = true;
return node.body;
}
else if (node instanceof U.AST_ExportDefault) {
node.start._permute++;
CHANGED = true;
return to_statement(node.body);
}
else if (node instanceof U.AST_Finally) { else if (node instanceof U.AST_Finally) {
// drop finally block // drop finally block
node.start._permute++; node.start._permute++;
@@ -351,6 +361,15 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
return to_statement(expr); return to_statement(expr);
} }
} }
else if (node instanceof U.AST_LabeledStatement) {
if (node.body instanceof U.AST_Statement
&& !has_loopcontrol(node.body, node.body, node)) {
// replace labelled statement with its non-labelled body
node.start._permute = REPLACEMENTS.length;
CHANGED = true;
return node.body;
}
}
else if (node instanceof U.AST_Object) { else if (node instanceof U.AST_Object) {
// first property's value // first property's value
var expr = node.properties[0]; var expr = node.properties[0];
@@ -441,15 +460,6 @@ module.exports = function reduce_test(testcase, minify_options, reduce_options)
return to_statement(node.definitions[0].value); return to_statement(node.definitions[0].value);
} }
} }
else if (node instanceof U.AST_LabeledStatement) {
if (node.body instanceof U.AST_Statement
&& !has_loopcontrol(node.body, node.body, node)) {
// replace labelled statement with its non-labelled body
node.start._permute = REPLACEMENTS.length;
CHANGED = true;
return node.body;
}
}
if (in_list) { if (in_list) {
// drop switch branches // drop switch branches