enhance comparisons (#5358)

This commit is contained in:
Alex Lam S.L
2022-02-18 20:27:17 +00:00
committed by GitHub
parent 82e8ebd77d
commit 9686379884
3 changed files with 40 additions and 20 deletions

View File

@@ -3739,8 +3739,9 @@ Compressor.prototype.compress = function(node) {
function join_assigns(defn, body, keep) {
var exprs = extract_exprs(body);
if (!exprs) return;
keep = keep || 0;
var trimmed = false;
for (var i = exprs.length - (keep || 0); --i >= 0;) {
for (var i = exprs.length - keep; --i >= 0;) {
var expr = exprs[i];
if (!can_trim(expr)) continue;
var tail;
@@ -3762,7 +3763,6 @@ Compressor.prototype.compress = function(node) {
exprs = exprs.slice(0, i).concat(expr, tail);
}
if (defn instanceof AST_Definitions) {
keep = keep || 0;
for (var i = defn.definitions.length; --i >= 0;) {
var def = defn.definitions[i];
if (!def.value) continue;
@@ -11079,30 +11079,20 @@ Compressor.prototype.compress = function(node) {
// void 0 !== x && null !== x ---> null != x
// void 0 === x || null === x ---> null == x
var lhs = self.left;
if (lhs.operator == self.operator) {
lhs = lhs.right;
}
if (lhs.operator == self.operator) lhs = lhs.right;
var expr = lhs.right;
if (expr instanceof AST_Assign && expr.operator == "=") expr = expr.left;
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.has_side_effects(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;
&& !expr.has_side_effects(compressor)
&& expr.equivalent_to(self.right.right)) {
lhs.operator = lhs.operator.slice(0, -1);
lhs.left = make_node(AST_Null, self);
return self.left;
}
break;
}