aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/compress.js60
1 files changed, 47 insertions, 13 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 4f37e834..26c11bd9 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -2658,24 +2658,58 @@ merge(Compressor.prototype, {
}
}
- // y?true:false --> !!y
- if (is_true(consequent) && is_false(alternative)) {
- if (self.condition.is_boolean()) {
- // boolean_expression ? true : false --> boolean_expression
- return self.condition;
- }
- self.condition = self.condition.negate(compressor);
- return make_node(AST_UnaryPrefix, self.condition, {
- operator: "!",
- expression: self.condition
+ if (is_true(self.consequent)) {
+ if (is_false(self.alternative)) {
+ // c ? true : false ---> !!c
+ return booleanize(self.condition);
+ }
+ // c ? true : x ---> !!c || x
+ return make_node(AST_Binary, self, {
+ operator: "||",
+ left: booleanize(self.condition),
+ right: self.alternative
+ });
+ }
+ if (is_false(self.consequent)) {
+ if (is_true(self.alternative)) {
+ // c ? false : true ---> !c
+ return booleanize(self.condition.negate(compressor));
+ }
+ // c ? false : x ---> !c && x
+ return make_node(AST_Binary, self, {
+ operator: "&&",
+ left: booleanize(self.condition.negate(compressor)),
+ right: self.alternative
+ });
+ }
+ if (is_true(self.alternative)) {
+ // c ? x : true ---> !c || x
+ return make_node(AST_Binary, self, {
+ operator: "||",
+ left: booleanize(self.condition.negate(compressor)),
+ right: self.consequent
});
}
- // y?false:true --> !y
- if (is_false(consequent) && is_true(alternative)) {
- return self.condition.negate(compressor)
+ if (is_false(self.alternative)) {
+ // c ? x : false ---> !!c && x
+ return make_node(AST_Binary, self, {
+ operator: "&&",
+ left: booleanize(self.condition),
+ right: self.consequent
+ });
}
+
return self;
+ function booleanize(node) {
+ if (node.is_boolean()) return node;
+ // !!expression
+ return make_node(AST_UnaryPrefix, node, {
+ operator: "!",
+ expression: node.negate(compressor)
+ });
+ }
+
// AST_True or !0
function is_true(node) {
return node instanceof AST_True