fix corner case in evaluate (#4138)

fixes #4137
This commit is contained in:
Alex Lam S.L
2020-09-21 23:49:32 +01:00
committed by GitHub
parent 8fa470c17c
commit 13cdc167a2
3 changed files with 264 additions and 203 deletions

View File

@@ -3106,7 +3106,12 @@ merge(Compressor.prototype, {
(function(def) {
def(AST_Node, return_false);
def(AST_Assign, function(compressor) {
return (this.operator == "=" || this.operator == "+=") && this.right.is_string(compressor);
switch (this.operator) {
case "+=":
if (this.left.is_string(compressor)) return true;
case "=":
return this.right.is_string(compressor);
}
});
def(AST_Binary, function(compressor) {
return this.operator == "+" &&
@@ -7365,10 +7370,9 @@ merge(Compressor.prototype, {
var indexFns = makePredicate("indexOf lastIndexOf");
var commutativeOperators = makePredicate("== === != !== * & | ^");
function is_object(node) {
while ((node = node.tail_node()) instanceof AST_SymbolRef) {
node = node.fixed_value();
if (!node) return false;
}
if (node instanceof AST_Assign) return node.operator == "=" && is_object(node.right);
if (node instanceof AST_Sequence) return is_object(node.tail_node());
if (node instanceof AST_SymbolRef) return is_object(node.fixed_value());
return node instanceof AST_Array
|| node instanceof AST_Lambda
|| node instanceof AST_New
@@ -7760,14 +7764,16 @@ merge(Compressor.prototype, {
associative = compressor.option("unsafe_math");
// +a - b => a - b
// a - +b => a - b
if (self.operator != "+") {
if (self.left instanceof AST_UnaryPrefix && self.left.operator == "+") {
self.left = self.left.expression;
[ "left", "right" ].forEach(function(operand) {
var node = self[operand];
if (node instanceof AST_UnaryPrefix && node.operator == "+") {
var exp = node.expression;
if (exp.is_boolean(compressor) || exp.is_number(compressor)
|| self.operator != "+" && exp.is_string(compressor)) {
self[operand] = exp;
}
}
if (self.right instanceof AST_UnaryPrefix && self.right.operator == "+") {
self.right = self.right.expression;
}
}
});
case "&":
case "|":
case "^":