Skip to content

Commit

Permalink
fix corner cases in booleans (#5029)
Browse files Browse the repository at this point in the history
fixes #5028
  • Loading branch information
alexlamsl committed Jun 22, 2021
1 parent 8b05677 commit 980dbde
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
76 changes: 76 additions & 0 deletions test/compress/booleans.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

0 comments on commit 980dbde

Please sign in to comment.