fix corner case in conditionals (#3244)

This commit is contained in:
Alex Lam S.L
2018-08-30 15:59:05 +08:00
committed by GitHub
parent 2bdaca10ae
commit ce7e220de4
2 changed files with 39 additions and 17 deletions

View File

@@ -6048,23 +6048,25 @@ merge(Compressor.prototype, {
// v // v
// exp = foo ? something : something_else; // exp = foo ? something : something_else;
var seq_tail = consequent.tail_node(); var seq_tail = consequent.tail_node();
var alt_tail = alternative.tail_node(); if (seq_tail instanceof AST_Assign) {
if (seq_tail instanceof AST_Assign var is_eq = seq_tail.operator == "=";
&& alt_tail instanceof AST_Assign var alt_tail = is_eq ? alternative.tail_node() : alternative;
&& seq_tail.operator == alt_tail.operator if ((is_eq || consequent instanceof AST_Assign)
&& seq_tail.left.equivalent_to(alt_tail.left) && alt_tail instanceof AST_Assign
&& (!condition.has_side_effects(compressor) && seq_tail.operator == alt_tail.operator
|| seq_tail.operator == "=" && seq_tail.left.equivalent_to(alt_tail.left)
&& !seq_tail.left.has_side_effects(compressor))) { && (!condition.has_side_effects(compressor)
return make_node(AST_Assign, self, { || is_eq && !seq_tail.left.has_side_effects(compressor))) {
operator: seq_tail.operator, return make_node(AST_Assign, self, {
left: seq_tail.left, operator: seq_tail.operator,
right: make_node(AST_Conditional, self, { left: seq_tail.left,
condition: condition, right: make_node(AST_Conditional, self, {
consequent: pop_lhs(consequent), condition: condition,
alternative: pop_lhs(alternative) consequent: pop_lhs(consequent),
}) alternative: pop_lhs(alternative)
}); })
});
}
} }
// x ? y(a) : y(b) --> y(x ? a : b) // x ? y(a) : y(b) --> y(x ? a : b)
var arg_index; var arg_index;

View File

@@ -1364,3 +1364,23 @@ cond_seq_assign_2: {
"42", "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"
}