fix corner cases in booleans (#5029)

fixes #5028
This commit is contained in:
Alex Lam S.L
2021-06-23 00:42:57 +01:00
committed by GitHub
parent 8b05677c15
commit 980dbde171
2 changed files with 84 additions and 3 deletions

View File

@@ -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);

View File

@@ -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"
}