fix corner case in evaluate (#3934)

fixes #3933
This commit is contained in:
Alex Lam S.L
2020-05-29 10:48:26 +01:00
committed by GitHub
parent 6a5c63e1e3
commit 60c0bc1e6b
4 changed files with 170 additions and 43 deletions

View File

@@ -3288,15 +3288,18 @@ merge(Compressor.prototype, {
var lhs = this.left;
if (!ignore_side_effects) {
if (!(lhs instanceof AST_SymbolRef)) return this;
if (!HOP(lhs, "_eval") && !lhs.fixed) return this;
var def = lhs.definition();
if (def.undeclared) return this;
if (def.last_ref !== lhs) return this;
if (def.single_use == "m") return this;
if (!HOP(lhs, "_eval")) {
if (!lhs.fixed) return this;
var def = lhs.definition();
if (!def.fixed) return this;
if (def.undeclared) return this;
if (def.last_ref !== lhs) return this;
if (def.single_use == "m") return this;
}
}
var op = this.operator;
var node;
if (HOP(lhs, "_eval") || !lhs.fixed) {
if (HOP(lhs, "_eval") || !(lhs instanceof AST_SymbolRef) || !lhs.fixed_value()) {
node = op == "=" ? this.right : make_node(AST_Binary, this, {
operator: op.slice(0, -1),
left: lhs,
@@ -3356,7 +3359,7 @@ merge(Compressor.prototype, {
key = key._eval(compressor, ignore_side_effects, cached, depth);
if (key === prop.key) return this;
}
if (typeof Object.prototype[key] === 'function') {
if (typeof Object.prototype[key] === "function") {
return this;
}
if (prop.value instanceof AST_Function) continue;
@@ -3372,7 +3375,7 @@ merge(Compressor.prototype, {
var e = this.expression;
var op = this.operator;
// Function would be evaluated to an array and so typeof would
// incorrectly return 'object'. Hence making is a special case.
// incorrectly return "object". Hence making is a special case.
if (compressor.option("typeofs")
&& op == "typeof"
&& (e instanceof AST_Lambda
@@ -3400,7 +3403,11 @@ merge(Compressor.prototype, {
case "++":
case "--":
if (!(e instanceof AST_SymbolRef)) return this;
if (!ignore_side_effects && e.definition().last_ref !== e) return this;
if (!ignore_side_effects) {
var def = e.definition();
if (def.undeclared) return this;
if (def.last_ref !== e) return this;
}
if (HOP(e, "_eval")) v = +(op[0] + 1) + +v;
modified(e);
return v;
@@ -3409,8 +3416,17 @@ merge(Compressor.prototype, {
});
def(AST_UnaryPostfix, function(compressor, ignore_side_effects, cached, depth) {
var e = this.expression;
if (!e.fixed) return this;
if (!ignore_side_effects && e.definition().last_ref !== e) return this;
if (!(e instanceof AST_SymbolRef)) {
if (!ignore_side_effects) return this;
} else if (!HOP(e, "_eval")) {
if (!e.fixed) return this;
if (!ignore_side_effects) {
var def = e.definition();
if (!def.fixed) return this;
if (def.undeclared) return this;
if (def.last_ref !== e) return this;
}
}
var v = e._eval(compressor, ignore_side_effects, cached, depth + 1);
if (v === e) return this;
modified(e);
@@ -7835,35 +7851,36 @@ merge(Compressor.prototype, {
var seq = self.lift_sequences(compressor);
if (seq !== self) return seq.optimize(compressor);
}
if (!compressor.option("assignments")) return self;
if (self.operator == "=" && self.left instanceof AST_SymbolRef && self.right instanceof AST_Binary) {
// x = expr1 OP expr2
if (self.right.left instanceof AST_SymbolRef
&& self.right.left.name == self.left.name
&& ASSIGN_OPS[self.right.operator]) {
// x = x - 2 => x -= 2
self.operator = self.right.operator + "=";
self.right = self.right.right;
if (compressor.option("assignments")) {
if (self.operator == "=" && self.left instanceof AST_SymbolRef && self.right instanceof AST_Binary) {
// x = expr1 OP expr2
if (self.right.left instanceof AST_SymbolRef
&& self.right.left.name == self.left.name
&& ASSIGN_OPS[self.right.operator]) {
// x = x - 2 => x -= 2
self.operator = self.right.operator + "=";
self.right = self.right.right;
}
else if (self.right.right instanceof AST_SymbolRef
&& self.right.right.name == self.left.name
&& ASSIGN_OPS_COMMUTATIVE[self.right.operator]
&& !self.right.left.has_side_effects(compressor)) {
// x = 2 & x => x &= 2
self.operator = self.right.operator + "=";
self.right = self.right.left;
}
}
else if (self.right.right instanceof AST_SymbolRef
&& self.right.right.name == self.left.name
&& ASSIGN_OPS_COMMUTATIVE[self.right.operator]
&& !self.right.left.has_side_effects(compressor)) {
// x = 2 & x => x &= 2
self.operator = self.right.operator + "=";
self.right = self.right.left;
if ((self.operator == "-=" || self.operator == "+="
&& (self.left.is_boolean(compressor) || self.left.is_number(compressor)))
&& self.right instanceof AST_Number
&& self.right.value == 1) {
var op = self.operator.slice(0, -1);
return make_node(AST_UnaryPrefix, self, {
operator: op + op,
expression: self.left
});
}
}
if ((self.operator == "-=" || self.operator == "+="
&& (self.left.is_boolean(compressor) || self.left.is_number(compressor)))
&& self.right instanceof AST_Number
&& self.right.value == 1) {
var op = self.operator.slice(0, -1);
return make_node(AST_UnaryPrefix, self, {
operator: op + op,
expression: self.left
});
}
return try_evaluate(compressor, self);
function in_try(level, node) {