aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/compress.js11
-rw-r--r--test/compress/booleans.js76
2 files changed, 84 insertions, 3 deletions
diff --git a/lib/compress.js b/lib/compress.js
index 5a1df7c3..010cb39b 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -8283,7 +8283,9 @@ merge(Compressor.prototype, {
if (is_empty(self.alternative)) self.alternative = null;
if (!compressor.option("conditionals")) return self;
- if (compressor.option("booleans")) mark_duplicate_condition(compressor, self.condition);
+ if (compressor.option("booleans") && !self.condition.has_side_effects(compressor)) {
+ mark_duplicate_condition(compressor, self.condition);
+ }
// if condition can be statically determined, warn and drop
// one of the blocks. note, statically determined implies
// “has no side effects”; also it doesn't work for cases like
@@ -11335,8 +11337,11 @@ merge(Compressor.prototype, {
return make_sequence(self, expressions);
}
if (!compressor.option("conditionals")) return self;
- if (compressor.option("booleans")) mark_duplicate_condition(compressor, self.condition);
- var condition = fuzzy_eval(compressor, self.condition);
+ var condition = self.condition;
+ if (compressor.option("booleans") && !condition.has_side_effects(compressor)) {
+ mark_duplicate_condition(compressor, condition);
+ }
+ condition = fuzzy_eval(compressor, condition);
if (!condition) {
AST_Node.warn("Condition always false [{file}:{line},{col}]", self.start);
return make_sequence(self, [ self.condition, self.alternative ]).optimize(compressor);
diff --git a/test/compress/booleans.js b/test/compress/booleans.js
index cd860f96..0926c0e4 100644
--- a/test/compress/booleans.js
+++ b/test/compress/booleans.js
@@ -558,3 +558,79 @@ issue_4374: {
}
expect_stdout: "0"
}
+
+issue_5028_1: {
+ options = {
+ booleans: true,
+ conditionals: true,
+ }
+ input: {
+ var a = 1;
+ console.log(function() {
+ return a-- ? a-- ? "FAIL 1" : "PASS" : "FAIL 2";
+ }());
+ }
+ expect: {
+ var a = 1;
+ console.log(function() {
+ return a-- ? a-- ? "FAIL 1" : "PASS" : "FAIL 2";
+ }());
+ }
+ expect_stdout: "PASS"
+}
+
+issue_5028_2: {
+ options = {
+ booleans: true,
+ conditionals: true,
+ dead_code: true,
+ if_return: true,
+ }
+ input: {
+ var a = 1;
+ (function() {
+ if (a--)
+ if (a--)
+ a = "FAIL";
+ else
+ return;
+ })();
+ console.log(a);
+ }
+ expect: {
+ var a = 1;
+ (function() {
+ a-- && a-- && (a = "FAIL");
+ })();
+ console.log(a);
+ }
+ expect_stdout: "-1"
+}
+
+issue_5028_3: {
+ options = {
+ booleans: true,
+ conditionals: true,
+ evaluate: true,
+ if_return: true,
+ }
+ input: {
+ var a = 1;
+ (function() {
+ if (a--)
+ if (a--)
+ a = "FAIL";
+ else
+ return;
+ })();
+ console.log(a);
+ }
+ expect: {
+ var a = 1;
+ (function() {
+ a-- && a-- && (a = "FAIL");
+ })();
+ console.log(a);
+ }
+ expect_stdout: "-1"
+}