fix corner case in evaluate (#3998)

fixes #3997
This commit is contained in:
Alex Lam S.L
2020-06-15 01:28:44 +08:00
committed by GitHub
parent 9aed0e3a73
commit ae9f56be10
2 changed files with 29 additions and 8 deletions

View File

@@ -3295,11 +3295,8 @@ merge(Compressor.prototype, {
if (node instanceof AST_Unary && unary_arithmetic[node.operator]) modified(node.expression);
});
function modified(node) {
if (node instanceof AST_Dot) {
if (node instanceof AST_PropAccess) {
modified(node.expression);
} else if (node instanceof AST_Sub) {
modified(node.expression);
node.property.walk(scan_modified);
} else if (node instanceof AST_SymbolRef) {
node.definition().references.forEach(function(ref) {
delete ref._eval;
@@ -3329,14 +3326,17 @@ merge(Compressor.prototype, {
}
var op = this.operator;
var node;
if (HOP(lhs, "_eval") || !(lhs instanceof AST_SymbolRef) || !lhs.fixed || !lhs.definition().fixed) {
node = op == "=" ? this.right : make_node(AST_Binary, this, {
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, {
operator: op.slice(0, -1),
left: lhs,
right: this.right,
});
} else {
node = lhs;
}
var value = node._eval(compressor, ignore_side_effects, cached, depth);
if (value === node) return this;

View File

@@ -2812,3 +2812,24 @@ operator_in: {
"true",
]
}
issue_3997: {
options = {
evaluate: true,
reduce_vars: true,
toplevel: true,
}
input: {
var a = function f(b) {
return b[b += this] = b;
}(0);
console.log(typeof a);
}
expect: {
var a = function f(b) {
return b[b += this] = b;
}(0);
console.log(typeof a);
}
expect_stdout: "string"
}