Skip to content

Commit

Permalink
fix corner case in comparisons (#3877)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed May 10, 2020
1 parent c76ee4b commit c1dd49e
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/compress.js
Expand Up @@ -6819,7 +6819,7 @@ merge(Compressor.prototype, {
if ((self.left.is_string(compressor) && self.right.is_string(compressor)) ||
(self.left.is_number(compressor) && self.right.is_number(compressor)) ||
(self.left.is_boolean(compressor) && self.right.is_boolean(compressor)) ||
self.left.equivalent_to(self.right)) {
can_self_compare(self.left) && self.left.equivalent_to(self.right)) {
self.operator = self.operator.slice(0, 2);
}
// XXX: intentionally falling down to the next case
Expand Down Expand Up @@ -7342,6 +7342,13 @@ merge(Compressor.prototype, {
}
return try_evaluate(compressor, self);

function can_self_compare(node) {
if (node instanceof AST_Dot) return can_self_compare(node.expression);
if (node instanceof AST_Sub) return can_self_compare(node.expression) && can_self_compare(node.property);
if (node instanceof AST_Symbol) return true;
return !node.has_side_effects(compressor);
}

function align(ref, op) {
switch (ref) {
case "-":
Expand Down
30 changes: 30 additions & 0 deletions test/compress/comparisons.js
Expand Up @@ -93,6 +93,36 @@ self_comparison_2: {
expect_stdout: "false true"
}

self_comparison_3: {
options = {
comparisons: true,
}
input: {
var a;
function f() {
var b = a;
a = null;
return b;
}
for (var i = 0; i < 2; i++)
console.log(f() === f());
}
expect: {
var a;
function f() {
var b = a;
a = null;
return b;
}
for (var i = 0; i < 2; i++)
console.log(f() === f());
}
expect_stdout: [
"false",
"true",
]
}

issue_2857_1: {
options = {
comparisons: true,
Expand Down

0 comments on commit c1dd49e

Please sign in to comment.