Skip to content

Commit

Permalink
enhance conditionals (#5703)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl authored Oct 8, 2022
1 parent 4a1da49 commit a391897
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
22 changes: 16 additions & 6 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion test/compress/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ if_return: {
if (w) {
if (y) return;
} else if (z) return;
return x != y && (x && w(), y && z()), !0;
return x != y && (x && w(), y) && z(), !0;
}
}
}
Expand Down

0 comments on commit a391897

Please sign in to comment.