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
// 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;