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
// x || (y || z) ---> x || y || z // w || (x, y || z) ---> w || (x, y) || z
if (self.right instanceof AST_Binary && self.operator == self.right.operator) swap_chain(self, compressor); 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 == "+") { if (compressor.option("strings") && self.operator == "+") {
// "foo" + 42 + "" ---> "foo" + 42 // "foo" + 42 + "" ---> "foo" + 42
@@ -13101,7 +13111,7 @@ Compressor.prototype.compress = function(node) {
operator: "||", operator: "||",
left: booleanize(condition), left: booleanize(condition),
right: alternative, right: alternative,
}); }).optimize(compressor);
} }
if (is_false(consequent)) { if (is_false(consequent)) {
// c ? false : true ---> !c // c ? false : true ---> !c
@@ -13111,20 +13121,20 @@ Compressor.prototype.compress = function(node) {
operator: "&&", operator: "&&",
left: booleanize(condition.negate(compressor)), left: booleanize(condition.negate(compressor)),
right: alternative, right: alternative,
}); }).optimize(compressor);
} }
// c ? x : true ---> !c || x // c ? x : true ---> !c || x
if (is_true(alternative)) return make_node(AST_Binary, self, { if (is_true(alternative)) return make_node(AST_Binary, self, {
operator: "||", operator: "||",
left: booleanize(condition.negate(compressor)), left: booleanize(condition.negate(compressor)),
right: consequent, right: consequent,
}); }).optimize(compressor);
// c ? x : false ---> !!c && x // c ? x : false ---> !!c && x
if (is_false(alternative)) return make_node(AST_Binary, self, { if (is_false(alternative)) return make_node(AST_Binary, self, {
operator: "&&", operator: "&&",
left: booleanize(condition), left: booleanize(condition),
right: consequent, right: consequent,
}); }).optimize(compressor);
if (compressor.option("typeofs")) mark_locally_defined(condition, consequent, alternative); if (compressor.option("typeofs")) mark_locally_defined(condition, consequent, alternative);
return self; return self;

View File

@@ -127,7 +127,7 @@ if_return: {
if (w) { if (w) {
if (y) return; if (y) return;
} else if (z) return; } else if (z) return;
return x != y && (x && w(), y && z()), !0; return x != y && (x && w(), y) && z(), !0;
} }
} }
} }