enhance conditionals (#5703)

This commit is contained in:
Alex Lam S.L
2022-10-08 02:59:33 +01:00
committed by GitHub
parent 4a1da492dd
commit a391897388
2 changed files with 17 additions and 7 deletions

View File

@@ -11806,8 +11806,18 @@ Compressor.prototype.compress = function(node) {
}
}
// x && (y && z) ---> x && y && z
// x || (y || z) ---> x || y || z
if (self.right instanceof AST_Binary && self.operator == self.right.operator) swap_chain(self, compressor);
// w || (x, y || z) ---> w || (x, y) || z
var rhs = self.right.tail_node();
if (rhs instanceof AST_Binary && self.operator == rhs.operator) {
if (rhs !== self.right) {
var exprs = self.right.expressions.slice(0, -1);
exprs.push(rhs.left);
rhs = rhs.clone();
rhs.left = make_sequence(self.right, exprs);
self.right = rhs;
}
swap_chain(self, compressor);
}
}
if (compressor.option("strings") && self.operator == "+") {
// "foo" + 42 + "" ---> "foo" + 42
@@ -13101,7 +13111,7 @@ Compressor.prototype.compress = function(node) {
operator: "||",
left: booleanize(condition),
right: alternative,
});
}).optimize(compressor);
}
if (is_false(consequent)) {
// c ? false : true ---> !c
@@ -13111,20 +13121,20 @@ Compressor.prototype.compress = function(node) {
operator: "&&",
left: booleanize(condition.negate(compressor)),
right: alternative,
});
}).optimize(compressor);
}
// c ? x : true ---> !c || x
if (is_true(alternative)) return make_node(AST_Binary, self, {
operator: "||",
left: booleanize(condition.negate(compressor)),
right: consequent,
});
}).optimize(compressor);
// c ? x : false ---> !!c && x
if (is_false(alternative)) return make_node(AST_Binary, self, {
operator: "&&",
left: booleanize(condition),
right: consequent,
});
}).optimize(compressor);
if (compressor.option("typeofs")) mark_locally_defined(condition, consequent, alternative);
return self;