Skip to content

Commit

Permalink
fix corner case in conditionals (#3577)
Browse files Browse the repository at this point in the history
fixes #3576
  • Loading branch information
alexlamsl committed Nov 8, 2019
1 parent 10648c9 commit 5045e14
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
15 changes: 4 additions & 11 deletions lib/compress.js
Expand Up @@ -6673,7 +6673,7 @@ merge(Compressor.prototype, {
&& alt_tail instanceof AST_Assign
&& seq_tail.operator == alt_tail.operator
&& seq_tail.left.equivalent_to(alt_tail.left)
&& (is_eq && !seq_tail.left.has_side_effects(compressor)
&& (is_eq && seq_tail.left instanceof AST_SymbolRef
|| !condition.has_side_effects(compressor)
&& can_shift_lhs_of_tail(consequent)
&& can_shift_lhs_of_tail(alternative))) {
Expand Down Expand Up @@ -6842,16 +6842,9 @@ merge(Compressor.prototype, {
}

function can_shift_lhs_of_tail(node) {
if (node === node.tail_node()) return true;
var exprs = node.expressions;
for (var i = exprs.length - 1; --i >= 0;) {
var expr = exprs[i];
if (!(expr instanceof AST_Assign) && expr.has_side_effects(compressor)
|| expr.operator != "="
|| expr.left.has_side_effects(compressor)
|| expr.right.has_side_effects(compressor)) return false;
}
return true;
return node === node.tail_node() || all(node.expressions.slice(0, -1), function(expr) {
return !expr.has_side_effects(compressor);
});
}

function pop_lhs(node) {
Expand Down
26 changes: 26 additions & 0 deletions test/compress/conditionals.js
Expand Up @@ -1489,3 +1489,29 @@ angularjs_chain: {
}
}
}

issue_3576: {
options = {
conditionals: true,
evaluate: true,
pure_getters: "strict",
reduce_vars: true,
}
input: {
var c = "FAIL";
(function(a) {
(a = -1) ? (a && (a.a = 0)) : (a && (a.a = 0));
a && a[c = "PASS"]++;
})();
console.log(c);
}
expect: {
var c = "FAIL";
(function(a) {
a = -1, a, a.a = 0;
a, a[c = "PASS"]++;
})();
console.log(c);
}
expect_stdout: "PASS"
}

0 comments on commit 5045e14

Please sign in to comment.