diff options
Diffstat (limited to 'lib/compress.js')
-rw-r--r-- | lib/compress.js | 70 |
1 files changed, 50 insertions, 20 deletions
diff --git a/lib/compress.js b/lib/compress.js index adfbb793..5301d48f 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -158,6 +158,7 @@ merge(Compressor.prototype, { return true; } if (p instanceof AST_Binary && (p.operator == "&&" || p.operator == "||") + || p instanceof AST_Conditional || p.tail_node() === self) { self = p; } else { @@ -4090,49 +4091,72 @@ merge(Compressor.prototype, { if (compressor.option("evaluate")) { switch (self.operator) { case "&&": - var ll = self.left.evaluate(compressor); + var ll = self.left.truthy ? true : self.left.falsy ? false : self.left.evaluate(compressor); if (!ll) { compressor.warn("Condition left of && always false [{file}:{line},{col}]", self.start); return maintain_this_binding(compressor.parent(), compressor.self(), self.left).optimize(compressor); - } else if (ll !== self.left) { + } else if (!(ll instanceof AST_Node)) { compressor.warn("Condition left of && always true [{file}:{line},{col}]", self.start); - return maintain_this_binding(compressor.parent(), compressor.self(), self.right).optimize(compressor); + return make_sequence(self, [ self.left, self.right ]).optimize(compressor); } - if (compressor.in_boolean_context()) { - var rr = self.right.evaluate(compressor); - if (!rr) { + var rr = self.right.evaluate(compressor); + if (!rr) { + if (compressor.in_boolean_context()) { compressor.warn("Boolean && always false [{file}:{line},{col}]", self.start); return make_sequence(self, [ self.left, make_node(AST_False, self) ]).optimize(compressor); - } else if (rr !== self.right) { - compressor.warn("Dropping side-effect-free && in boolean context [{file}:{line},{col}]", self.start); + } else self.falsy = true; + } else if (!(rr instanceof AST_Node)) { + var parent = compressor.parent(); + if (parent.operator == "&&" && parent.left === compressor.self() || compressor.in_boolean_context()) { + compressor.warn("Dropping side-effect-free && [{file}:{line},{col}]", self.start); return self.left.optimize(compressor); } } + // x || false && y ---> x ? y : false + if (self.left.operator == "||") { + var lr = self.left.right.evaluate(compressor); + if (!lr) return make_node(AST_Conditional, self, { + condition: self.left.left, + consequent: self.right, + alternative: self.left.right + }).optimize(compressor); + } break; case "||": - var ll = self.left.evaluate(compressor); + var ll = self.left.truthy ? true : self.left.falsy ? false : self.left.evaluate(compressor); if (!ll) { compressor.warn("Condition left of || always false [{file}:{line},{col}]", self.start); - return maintain_this_binding(compressor.parent(), compressor.self(), self.right).optimize(compressor); - } else if (ll !== self.left) { + return make_sequence(self, [ self.left, self.right ]).optimize(compressor); + } else if (!(ll instanceof AST_Node)) { compressor.warn("Condition left of || always true [{file}:{line},{col}]", self.start); return maintain_this_binding(compressor.parent(), compressor.self(), self.left).optimize(compressor); } - if (compressor.in_boolean_context()) { - var rr = self.right.evaluate(compressor); - if (!rr) { - compressor.warn("Dropping side-effect-free || in boolean context [{file}:{line},{col}]", self.start); + var rr = self.right.evaluate(compressor); + if (!rr) { + var parent = compressor.parent(); + if (parent.operator == "||" && parent.left === compressor.self() || compressor.in_boolean_context()) { + compressor.warn("Dropping side-effect-free || [{file}:{line},{col}]", self.start); return self.left.optimize(compressor); - } else if (rr !== self.right) { + } + } else if (!(rr instanceof AST_Node)) { + if (compressor.in_boolean_context()) { compressor.warn("Boolean || always true [{file}:{line},{col}]", self.start); return make_sequence(self, [ self.left, make_node(AST_True, self) ]).optimize(compressor); - } + } else self.truthy = true; + } + if (self.left.operator == "&&") { + var lr = self.left.right.evaluate(compressor); + if (lr && !(lr instanceof AST_Node)) return make_node(AST_Conditional, self, { + condition: self.left.left, + consequent: self.left.right, + alternative: self.right + }).optimize(compressor); } break; } @@ -4640,7 +4664,7 @@ merge(Compressor.prototype, { consequent ]).optimize(compressor); } - + var in_bool = compressor.in_boolean_context(); if (is_true(self.consequent)) { if (is_false(self.alternative)) { // c ? true : false ---> !!c @@ -4696,18 +4720,24 @@ merge(Compressor.prototype, { // AST_True or !0 function is_true(node) { return node instanceof AST_True + || in_bool + && node instanceof AST_Constant + && node.getValue() || (node instanceof AST_UnaryPrefix && node.operator == "!" && node.expression instanceof AST_Constant - && !node.expression.value); + && !node.expression.getValue()); } // AST_False or !1 function is_false(node) { return node instanceof AST_False + || in_bool + && node instanceof AST_Constant + && !node.getValue() || (node instanceof AST_UnaryPrefix && node.operator == "!" && node.expression instanceof AST_Constant - && !!node.expression.value); + && node.expression.getValue()); } }); |