Skip to content

Commit

Permalink
fix corner case in collapse_vars (#3501)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexlamsl committed Oct 19, 2019
1 parent 543dd7d commit ca6dce4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 39 deletions.
48 changes: 9 additions & 39 deletions lib/compress.js
Expand Up @@ -1465,50 +1465,20 @@ merge(Compressor.prototype, {
hit_stack.pop();
}

function find_stop(node, level, write_only) {
function find_stop(node, level) {
var parent = scanner.parent(level);
if (parent instanceof AST_Assign) {
if (write_only
&& !(parent.left instanceof AST_PropAccess
|| parent.left.name in lvalues)) {
return find_stop(parent, level + 1, write_only);
}
return node;
}
if (parent instanceof AST_Binary) {
if (write_only && (!lazy_op[parent.operator] || parent.left === node)) {
return find_stop(parent, level + 1, write_only);
}
return node;
}
if (parent instanceof AST_Assign) return node;
if (parent instanceof AST_Binary) return node;
if (parent instanceof AST_Call) return node;
if (parent instanceof AST_Case) return node;
if (parent instanceof AST_Conditional) {
if (write_only && parent.condition === node) {
return find_stop(parent, level + 1, write_only);
}
return node;
}
if (parent instanceof AST_Definitions) {
return find_stop(parent, level + 1, true);
}
if (parent instanceof AST_Exit) {
return write_only ? find_stop(parent, level + 1, write_only) : node;
}
if (parent instanceof AST_If) {
if (write_only && parent.condition === node) {
return find_stop(parent, level + 1, write_only);
}
return node;
}
if (parent instanceof AST_Conditional) return node;
if (parent instanceof AST_Definitions) return find_stop(parent, level + 1);
if (parent instanceof AST_Exit) return node;
if (parent instanceof AST_If) return node;
if (parent instanceof AST_IterationStatement) return node;
if (parent instanceof AST_PropAccess) return node;
if (parent instanceof AST_Sequence) {
return find_stop(parent, level + 1, parent.tail_node() !== node);
}
if (parent instanceof AST_SimpleStatement) {
return find_stop(parent, level + 1, true);
}
if (parent instanceof AST_Sequence) return find_stop(parent, level + 1);
if (parent instanceof AST_SimpleStatement) return find_stop(parent, level + 1);
if (parent instanceof AST_Switch) return node;
if (parent instanceof AST_Unary) return node;
if (parent instanceof AST_VarDef) return node;
Expand Down
21 changes: 21 additions & 0 deletions test/compress/collapse_vars.js
Expand Up @@ -6237,3 +6237,24 @@ issue_3439_2: {
}
expect_stdout: "number"
}

cond_sequence_return: {
options = {
collapse_vars: true,
}
input: {
console.log(function(n) {
var c = 0;
for (var k in [0, 1])
if (c++, k == n) return c;
}(1));
}
expect: {
console.log(function(n) {
var c = 0;
for (var k in [0, 1])
if (c++, k == n) return c;
}(1));
}
expect_stdout: "2"
}

0 comments on commit ca6dce4

Please sign in to comment.