Skip to content

Commit

Permalink
fix corner case in conditionals (#5667)
Browse files Browse the repository at this point in the history
fixes #5666
  • Loading branch information
alexlamsl authored Sep 17, 2022
1 parent 001f6f9 commit e4bff31
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 13 deletions.
24 changes: 11 additions & 13 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -12865,19 +12865,17 @@ Compressor.prototype.compress = function(node) {
var alt_tail = alternative.tail_node();
// x ? y : y ---> x, y
// x ? (a, c) : (b, c) ---> x ? a : b, c
if (seq_tail.equals(alt_tail)) {
return make_sequence(self, consequent.equals(alternative) ? [
condition,
consequent,
] : [
make_node(AST_Conditional, self, {
condition: condition,
consequent: pop_seq(consequent),
alternative: pop_seq(alternative),
}),
seq_tail,
]).optimize(compressor);
}
if (seq_tail.equals(alt_tail)) return make_sequence(self, consequent.equals(alternative) ? [
condition,
consequent,
] : [
make_node(AST_Conditional, self, {
condition: condition,
consequent: pop_seq(consequent),
alternative: pop_seq(alternative),
}),
alt_tail,
]).optimize(compressor);
// x ? y.p : z.p ---> (x ? y : z).p
// x ? y(a) : z(a) ---> (x ? y : z)(a)
// x ? y.f(a) : z.f(a) ---> (x ? y : z).f(a)
Expand Down
50 changes: 50 additions & 0 deletions test/compress/conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -2878,3 +2878,53 @@ issue_5546_3: {
}
expect_stdout: "PASS"
}

issue_5666_1: {
options = {
conditionals: true,
reduce_vars: true,
unused: true,
}
input: {
var a;
(function() {
var b = a;
a ? a = b : (b++, a = b);
})();
console.log(a);
}
expect: {
var a;
(function() {
var b = a;
a = (a ? 0 : b++, b);
})();
console.log(a);
}
expect_stdout: "NaN"
}

issue_5666_2: {
options = {
conditionals: true,
reduce_vars: true,
unused: true,
}
input: {
var a = "foo";
(function() {
var b = a;
a ? (b++, a = b) : a = b;
})();
console.log(a);
}
expect: {
var a = "foo";
(function() {
var b = a;
a = (a ? b++ : 0, b);
})();
console.log(a);
}
expect_stdout: "NaN"
}

0 comments on commit e4bff31

Please sign in to comment.