fix delete related issues in collapse_vars and reduce_vars (#1689)

This commit is contained in:
Alex Lam S.L
2017-03-26 19:14:30 +08:00
committed by GitHub
parent 00996afd2c
commit 861a79ac9f
3 changed files with 46 additions and 28 deletions

View File

@@ -1207,8 +1207,10 @@ merge(Compressor.prototype, {
node.DEFMETHOD("is_string", func); node.DEFMETHOD("is_string", func);
}); });
var unary_side_effects = makePredicate("delete ++ --");
function isLHS(node, parent) { function isLHS(node, parent) {
return parent instanceof AST_Unary && (parent.operator == "++" || parent.operator == "--") return parent instanceof AST_Unary && unary_side_effects(parent.operator)
|| parent instanceof AST_Assign && parent.left === node; || parent instanceof AST_Assign && parent.left === node;
} }
@@ -1643,9 +1645,7 @@ merge(Compressor.prototype, {
|| this.alternative.has_side_effects(compressor); || this.alternative.has_side_effects(compressor);
}); });
def(AST_Unary, function(compressor){ def(AST_Unary, function(compressor){
return this.operator == "delete" return unary_side_effects(this.operator)
|| this.operator == "++"
|| this.operator == "--"
|| this.expression.has_side_effects(compressor); || this.expression.has_side_effects(compressor);
}); });
def(AST_SymbolRef, function(compressor){ def(AST_SymbolRef, function(compressor){
@@ -2196,26 +2196,19 @@ merge(Compressor.prototype, {
return node; return node;
}); });
def(AST_Unary, function(compressor, first_in_statement){ def(AST_Unary, function(compressor, first_in_statement){
switch (this.operator) { if (unary_side_effects(this.operator)) return this;
case "delete": if (this.operator == "typeof" && this.expression instanceof AST_SymbolRef) return null;
case "++": var expression = this.expression.drop_side_effect_free(compressor, first_in_statement);
case "--": if (first_in_statement
return this; && this instanceof AST_UnaryPrefix
case "typeof": && is_iife_call(expression)) {
if (this.expression instanceof AST_SymbolRef) return null; if (expression === this.expression && this.operator.length === 1) return this;
default: return make_node(AST_UnaryPrefix, this, {
var expression = this.expression.drop_side_effect_free(compressor, first_in_statement); operator: this.operator.length === 1 ? this.operator : "!",
if (first_in_statement expression: expression
&& this instanceof AST_UnaryPrefix });
&& is_iife_call(expression)) {
if (expression === this.expression && this.operator.length === 1) return this;
return make_node(AST_UnaryPrefix, this, {
operator: this.operator.length === 1 ? this.operator : "!",
expression: expression
});
}
return expression;
} }
return expression;
}); });
def(AST_SymbolRef, function() { def(AST_SymbolRef, function() {
return this.undeclared() ? this : null; return this.undeclared() ? this : null;
@@ -2945,10 +2938,7 @@ merge(Compressor.prototype, {
field = "left"; field = "left";
} }
} else if (cdr instanceof AST_Call } else if (cdr instanceof AST_Call
|| cdr instanceof AST_Unary || cdr instanceof AST_Unary && !unary_side_effects(cdr.operator)) {
&& cdr.operator != "delete"
&& cdr.operator != "++"
&& cdr.operator != "--") {
field = "expression"; field = "expression";
} else break; } else break;
parent = cdr; parent = cdr;

View File

@@ -894,7 +894,8 @@ collapse_vars_unary: {
} }
expect: { expect: {
function f0(o, p) { function f0(o, p) {
delete o[p]; var x = o[p];
delete x;
} }
function f1(n) { function f1(n) {
return n > +!!n return n > +!!n

View File

@@ -1544,3 +1544,30 @@ issue_1670_6: {
} }
expect_stdout: "1" expect_stdout: "1"
} }
unary_delete: {
options = {
evaluate: true,
reduce_vars: true,
unused: true,
}
input: {
var b = 10;
function f() {
var a;
if (delete a) b--;
}
f();
console.log(b);
}
expect: {
var b = 10;
function f() {
var a;
if (delete a) b--;
}
f();
console.log(b);
}
expect_stdout: true
}