From ace8aaa0f4ad6ecae77e760473353f07b384880e Mon Sep 17 00:00:00 2001 From: kzc Date: Tue, 21 Jun 2016 14:52:13 -0400 Subject: [PATCH] Fix conditional expressions of form (x ? -1 : -1) Fixes #1154, #1153 --- lib/compress.js | 2 +- test/compress/conditionals.js | 38 +++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/compress.js b/lib/compress.js index db6a26d6..4152bd06 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -2746,7 +2746,7 @@ merge(Compressor.prototype, { if (consequent.is_constant(compressor) && alternative.is_constant(compressor) && consequent.equivalent_to(alternative)) { - var consequent_value = consequent.constant_value(); + var consequent_value = consequent.constant_value(compressor); if (self.condition.has_side_effects(compressor)) { return AST_Seq.from_array([self.condition, make_node_from_constant(compressor, consequent_value, self)]); } else { diff --git a/test/compress/conditionals.js b/test/compress/conditionals.js index f5eeb6f2..35cb26f7 100644 --- a/test/compress/conditionals.js +++ b/test/compress/conditionals.js @@ -868,3 +868,41 @@ trivial_boolean_ternary_expressions : { f(!(x >= y)); } } + +issue_1154: { + options = { + conditionals: true, + evaluate : true, + booleans : true, + }; + input: { + function f1(x) { return x ? -1 : -1; } + function f2(x) { return x ? +2 : +2; } + function f3(x) { return x ? ~3 : ~3; } + function f4(x) { return x ? !4 : !4; } + function f5(x) { return x ? void 5 : void 5; } + function f6(x) { return x ? typeof 6 : typeof 6; } + + function g1() { return g() ? -1 : -1; } + function g2() { return g() ? +2 : +2; } + function g3() { return g() ? ~3 : ~3; } + function g4() { return g() ? !4 : !4; } + function g5() { return g() ? void 5 : void 5; } + function g6() { return g() ? typeof 6 : typeof 6; } + } + expect: { + function f1(x) { return -1; } + function f2(x) { return 2; } + function f3(x) { return -4; } + function f4(x) { return !1; } + function f5(x) { return; } + function f6(x) { return "number"; } + + function g1() { return g(), -1; } + function g2() { return g(), 2; } + function g3() { return g(), -4; } + function g4() { return g(), !1; } + function g5() { return g(), void 0; } + function g6() { return g(), "number"; } + } +} -- 2.34.1