Skip to content

Commit

Permalink
fix corner case in assignments (#5671)
Browse files Browse the repository at this point in the history
fixes #5670
  • Loading branch information
alexlamsl authored Sep 20, 2022
1 parent 3a6e581 commit 9efa02a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
23 changes: 15 additions & 8 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -11590,22 +11590,29 @@ Compressor.prototype.compress = function(node) {
if (seq !== self) return seq.optimize(compressor);
}
if (compressor.option("assignments") && lazy_op[self.operator]) {
var assign = self.right;
var right = self.right;
// a || (a = x) ---> a = a || x
// a && (a = x) ---> a = a && x
if (self.left instanceof AST_SymbolRef
&& assign instanceof AST_Assign
&& assign.operator == "="
&& self.left.equals(assign.left)) {
return make_node(AST_Assign, self, {
&& right instanceof AST_Assign
&& right.operator == "="
&& self.left.equals(right.left)) {
var left = right.left.clone();
var assign = make_node(AST_Assign, self, {
operator: "=",
left: assign.left,
left: left,
right: make_node(AST_Binary, self, {
operator: self.operator,
left: self.left,
right: assign.right,
right: right.right,
}),
}).optimize(compressor);
});
left.fixed = function() {
return assign.right;
};
left.fixed.assigns = [ assign ];
left.definition().references.push(left);
return assign.optimize(compressor);
}
}
if (compressor.option("comparisons")) switch (self.operator) {
Expand Down
20 changes: 20 additions & 0 deletions test/compress/assignments.js
Original file line number Diff line number Diff line change
Expand Up @@ -803,3 +803,23 @@ issue_4924_2: {
expect_stdout: "PASS"
node_version: ">=15"
}

issue_5670: {
options = {
assignments: true,
evaluate: true,
reduce_vars: true,
}
input: {
(function(a, b) {
a && a && (a = b += "") || console.log("PASS");
})();
}
expect: {
(function(a, b) {
a = a,
console.log("PASS");
})();
}
expect_stdout: "PASS"
}

0 comments on commit 9efa02a

Please sign in to comment.