simplify comparisons with undefined & null (#2862)

fixes #2857
This commit is contained in:
Alex Lam S.L
2018-02-01 16:50:54 +08:00
committed by GitHub
parent aa664dea0a
commit fad6766a90
2 changed files with 158 additions and 0 deletions

View File

@@ -4788,6 +4788,34 @@ merge(Compressor.prototype, {
return make_node(self.operator[0] == "=" ? AST_True : AST_False, self);
}
break;
case "&&":
case "||":
var lhs = self.left;
if (lhs.operator == self.operator) {
lhs = lhs.right;
}
if (lhs instanceof AST_Binary
&& lhs.operator == (self.operator == "&&" ? "!==" : "===")
&& self.right instanceof AST_Binary
&& lhs.operator == self.right.operator
&& (is_undefined(lhs.left, compressor) && self.right.left instanceof AST_Null
|| lhs.left instanceof AST_Null && is_undefined(self.right.left, compressor))
&& lhs.right.equivalent_to(self.right.right)) {
var combined = make_node(AST_Binary, self, {
operator: lhs.operator.slice(0, -1),
left: make_node(AST_Null, self),
right: lhs.right
});
if (lhs !== self.left) {
combined = make_node(AST_Binary, self, {
operator: self.operator,
left: self.left.left,
right: combined
});
}
return combined;
}
break;
}
if (self.operator == "+" && compressor.in_boolean_context()) {
var ll = self.left.evaluate(compressor);