Skip to content

Commit

Permalink
fix corner case in collapse_vars (#3627)
Browse files Browse the repository at this point in the history
fixes #3626
  • Loading branch information
alexlamsl authored Dec 4, 2019
1 parent d68ddc3 commit bf7e4ca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/compress.js
Original file line number Diff line number Diff line change
Expand Up @@ -1578,9 +1578,11 @@ merge(Compressor.prototype, {
}
if (parent instanceof AST_Binary) {
if (lazy_op[parent.operator] && parent.left !== node) {
var grandparent = scanner.parent(level + 1);
if (!(grandparent instanceof AST_Binary)) return node;
if (grandparent.operator != parent.operator) return node;
do {
node = parent;
parent = scanner.parent(++level);
} while (parent instanceof AST_Binary && parent.operator == node.operator);
return node;
}
return find_stop_value(parent, level + 1);
}
Expand Down
37 changes: 37 additions & 0 deletions test/compress/collapse_vars.js
Original file line number Diff line number Diff line change
Expand Up @@ -7308,3 +7308,40 @@ substitution_unary: {
"true 42 42",
]
}

issue_3626_1: {
options = {
collapse_vars: true,
}
input: {
var a = "foo", b = 42;
a.p && (b = a) && a;
console.log(a, b);
}
expect: {
var a = "foo", b = 42;
a.p && (b = a) && a;
console.log(a, b);
}
expect_stdout: "foo 42"
}

issue_3626_2: {
options = {
collapse_vars: true,
conditionals: true,
}
input: {
var a = "foo", b = 42, c = null;
if (a && a.p)
if (b = a)
c++ + a;
console.log(a, b, c);
}
expect: {
var a = "foo", b = 42, c = null;
a && a.p && (b = a) && c++ + a;
console.log(a, b, c);
}
expect_stdout: "foo 42 null"
}

0 comments on commit bf7e4ca

Please sign in to comment.