enhance conditionals (#2758)

`x ? y || z : z` --> `x && y || z`
This commit is contained in:
Alex Lam S.L
2018-01-10 16:59:57 +08:00
committed by GitHub
parent bf832cde16
commit 09269be974
2 changed files with 57 additions and 0 deletions

View File

@@ -5278,6 +5278,20 @@ merge(Compressor.prototype, {
consequent
]).optimize(compressor);
}
// x ? y || z : z --> x && y || z
if (consequent instanceof AST_Binary
&& consequent.operator == "||"
&& consequent.right.equivalent_to(alternative)) {
return make_node(AST_Binary, self, {
operator: "||",
left: make_node(AST_Binary, self, {
operator: "&&",
left: self.condition,
right: consequent.left
}),
right: alternative
}).optimize(compressor);
}
var in_bool = compressor.in_boolean_context();
if (is_true(self.consequent)) {
if (is_false(self.alternative)) {