fix corner case in evaluate (#4501)

fixes #4500
This commit is contained in:
Alex Lam S.L
2021-01-03 18:31:20 +00:00
committed by GitHub
parent 623a0d920f
commit df2cfcb5fc
2 changed files with 34 additions and 2 deletions

View File

@@ -3894,7 +3894,13 @@ merge(Compressor.prototype, {
if (node instanceof AST_Unary && UNARY_POSTFIX[node.operator]) modified(node.expression);
});
function modified(node) {
if (node instanceof AST_PropAccess) {
if (node instanceof AST_DestructuredArray) {
node.elements.forEach(modified);
} else if (node instanceof AST_DestructuredObject) {
node.properties.forEach(function(prop) {
modified(prop.value);
});
} else if (node instanceof AST_PropAccess) {
modified(node.expression);
} else if (node instanceof AST_SymbolRef) {
node.definition().references.forEach(function(ref) {
@@ -3928,7 +3934,6 @@ merge(Compressor.prototype, {
if (!HOP(lhs, "_eval") && lhs instanceof AST_SymbolRef && lhs.fixed && lhs.definition().fixed) {
node = lhs;
} else if (op == "=") {
lhs.walk(scan_modified);
node = this.right;
} else {
node = make_node(AST_Binary, this, {
@@ -3937,6 +3942,7 @@ merge(Compressor.prototype, {
right: this.right,
});
}
lhs.walk(scan_modified);
var value = node._eval(compressor, ignore_side_effects, cached, depth);
if (typeof value == "object") return this;
modified(lhs);
@@ -4011,6 +4017,7 @@ merge(Compressor.prototype, {
}
var def = e instanceof AST_SymbolRef && e.definition();
if (!non_converting_unary[op] && !(def && def.fixed)) depth++;
e.walk(scan_modified);
var v = e._eval(compressor, ignore_side_effects, cached, depth);
if (v === e) {
if (ignore_side_effects && op == "void") return;
@@ -4054,6 +4061,7 @@ merge(Compressor.prototype, {
}
}
if (!(e instanceof AST_SymbolRef && e.definition().fixed)) depth++;
e.walk(scan_modified);
var v = e._eval(compressor, ignore_side_effects, cached, depth);
if (v === e) return this;
modified(e);

View File

@@ -2302,3 +2302,27 @@ issue_4485_3: {
expect_stdout: true
node_version: ">=6"
}
issue_4500: {
options = {
evaluate: true,
keep_fnames: true,
reduce_vars: true,
toplevel: true,
unused: true,
}
input: {
var a = function f(b) {
return [ b ] = [], b;
}("FAIL");
console.log(a || "PASS");
}
expect: {
var a = function f(b) {
return [ b ] = [], b;
}("FAIL");
console.log(a || "PASS");
}
expect_stdout: "PASS"
node_version: ">=6"
}