fix corner cases related to in (#3964)

This commit is contained in:
Alex Lam S.L
2020-06-07 22:23:23 +01:00
committed by GitHub
parent 28b7b15da1
commit df3bb8028a
7 changed files with 106 additions and 11 deletions

View File

@@ -1464,6 +1464,7 @@ merge(Compressor.prototype, {
}
function is_last_node(node, parent) {
if (node.TYPE == "Binary") return node.operator == "in" && !is_object(node.right.tail_node());
if (node instanceof AST_Call) {
var fn = node.expression;
if (fn instanceof AST_SymbolRef) {
@@ -3824,7 +3825,8 @@ merge(Compressor.prototype, {
def(AST_Assign, return_true);
def(AST_Binary, function(compressor) {
return this.left.has_side_effects(compressor)
|| this.right.has_side_effects(compressor);
|| this.right.has_side_effects(compressor)
|| this.operator == "in" && !is_object(this.right.tail_node());
});
def(AST_Block, function(compressor) {
return any(this.body, compressor);
@@ -5131,6 +5133,15 @@ merge(Compressor.prototype, {
return this;
});
def(AST_Binary, function(compressor, first_in_statement) {
if (this.operator == "in" && !is_object(this.right.tail_node())) {
var left = this.left.drop_side_effect_free(compressor, first_in_statement);
if (left === this.left) return this;
var node = this.clone();
node.left = left || make_node(AST_Number, this.left, {
value: 0
});
return node;
}
var right = this.right.drop_side_effect_free(compressor, first_in_statement);
if (!right) return this.left.drop_side_effect_free(compressor, first_in_statement);
if (lazy_op[this.operator] && !(right instanceof AST_Function)) {
@@ -6879,8 +6890,14 @@ merge(Compressor.prototype, {
var indexFns = makePredicate("indexOf lastIndexOf");
var commutativeOperators = makePredicate("== === != !== * & | ^");
function is_object(node) {
while (node instanceof AST_SymbolRef) {
node = node.fixed_value();
if (!node) return false;
node = node.tail_node();
}
return node instanceof AST_Array
|| node instanceof AST_Lambda
|| node instanceof AST_New
|| node instanceof AST_Object;
}
@@ -6980,7 +6997,7 @@ merge(Compressor.prototype, {
else if (self.left instanceof AST_SymbolRef
&& self.right instanceof AST_SymbolRef
&& self.left.definition() === self.right.definition()
&& is_object(self.left.fixed_value())) {
&& is_object(self.left)) {
return make_node(self.operator[0] == "=" ? AST_True : AST_False, self);
}
break;

View File

@@ -305,6 +305,7 @@ function OutputStream(options) {
|| (ch == "/" && ch == prev)
|| ((ch == "+" || ch == "-") && ch == last)
|| str == "--" && last == "!"
|| str == "in" && prev == "/"
|| last == "--" && ch == ">") {
OUTPUT += " ";
current_col++;