Skip to content

Commit

Permalink
fix corner case in conditionals (#3244)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl authored Aug 30, 2018
1 parent 2bdaca1 commit ce7e220
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
36 changes: 19 additions & 17 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -6048,23 +6048,25 @@ merge(Compressor.prototype, {
// v
// exp = foo ? something : something_else;
var seq_tail = consequent.tail_node();
var alt_tail = alternative.tail_node();
if (seq_tail instanceof AST_Assign
&& alt_tail instanceof AST_Assign
&& seq_tail.operator == alt_tail.operator
&& seq_tail.left.equivalent_to(alt_tail.left)
&& (!condition.has_side_effects(compressor)
|| seq_tail.operator == "="
&& !seq_tail.left.has_side_effects(compressor))) {
return make_node(AST_Assign, self, {
operator: seq_tail.operator,
left: seq_tail.left,
right: make_node(AST_Conditional, self, {
condition: condition,
consequent: pop_lhs(consequent),
alternative: pop_lhs(alternative)
})
});
if (seq_tail instanceof AST_Assign) {
var is_eq = seq_tail.operator == "=";
var alt_tail = is_eq ? alternative.tail_node() : alternative;
if ((is_eq || consequent instanceof AST_Assign)
&& alt_tail instanceof AST_Assign
&& seq_tail.operator == alt_tail.operator
&& seq_tail.left.equivalent_to(alt_tail.left)
&& (!condition.has_side_effects(compressor)
|| is_eq && !seq_tail.left.has_side_effects(compressor))) {
return make_node(AST_Assign, self, {
operator: seq_tail.operator,
left: seq_tail.left,
right: make_node(AST_Conditional, self, {
condition: condition,
consequent: pop_lhs(consequent),
alternative: pop_lhs(alternative)
})
});
}
}
// x ? y(a) : y(b) --> y(x ? a : b)
var arg_index;
Expand Down
20 changes: 20 additions & 0 deletions test/compress/conditionals.js
Original file line number Diff line number Diff line change
Expand Up @@ -1364,3 +1364,23 @@ cond_seq_assign_2: {
"42",
]
}

cond_seq_assign_3: {
options = {
conditionals: true,
}
input: {
var c = 0;
if (this)
c = 1 + c, c = c + 1;
else
c = 1 + c, c = c + 1;
console.log(c);
}
expect: {
var c = 0;
this, c = 1 + c, c += 1;
console.log(c);
}
expect_stdout: "2"
}

0 comments on commit ce7e220

Please sign in to comment.