Skip to content

Commit

Permalink
fix corner case in collapse_vars (#4911)
Browse files Browse the repository at this point in the history
fixes #4910
  • Loading branch information
alexlamsl committed May 4, 2021
1 parent 3094eaa commit d464be3
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 10 deletions.
20 changes: 14 additions & 6 deletions lib/compress.js
Expand Up @@ -1777,6 +1777,7 @@ merge(Compressor.prototype, {
}
return node;
} else if (value_def) {
if (stop_if_hit && assign_pos == 0) assign_pos = remaining - replaced;
if (!hit_rhs) replaced++;
return node;
} else {
Expand Down Expand Up @@ -1904,14 +1905,14 @@ merge(Compressor.prototype, {
if (is_lhs(node, multi_replacer.parent())) return node;
var ref = rvalue.clone();
value_def.references.push(ref);
if (abort && candidate instanceof AST_Assign && remaining_refs(def) > 1) {
if (replaced == assign_pos) {
abort = true;
return make_node(AST_Assign, candidate, {
operator: "=",
left: node,
right: ref,
});
}
ref.fixed = false;
def.replaced++;
return ref;
}
Expand All @@ -1931,6 +1932,8 @@ merge(Compressor.prototype, {
hit_stack = candidates.pop();
var hit_index = 0;
var candidate = hit_stack[hit_stack.length - 1];
var assign_pos = -1;
var assign_used = false;
var remaining;
var value_def = null;
var stop_after = null;
Expand Down Expand Up @@ -1962,7 +1965,6 @@ merge(Compressor.prototype, {
var hit = funarg;
var abort = false;
var replaced = 0;
var assign_used = false;
var can_replace = !args || !hit;
if (!can_replace) {
for (var j = candidate.arg_index + 1; !abort && j < args.length; j++) {
Expand Down Expand Up @@ -2621,9 +2623,15 @@ merge(Compressor.prototype, {
if (scope.uses_arguments && is_funarg(def)) return lhs;
if (compressor.exposed(def)) return lhs;
remaining = remaining_refs(def);
if (def.fixed && lhs.fixed) remaining = Math.min(remaining, def.references.filter(function(ref) {
return ref.fixed === lhs.fixed;
}).length - 1);
if (def.fixed && lhs.fixed) {
var matches = def.references.filter(function(ref) {
return ref.fixed === lhs.fixed;
}).length - 1;
if (matches < remaining) {
remaining = matches;
assign_pos = 0;
}
}
mangleable_var(expr.right);
return lhs;
}
Expand Down
25 changes: 23 additions & 2 deletions test/compress/collapse_vars.js
Expand Up @@ -9049,7 +9049,7 @@ issue_4874: {
}
expect: {
var a;
a = null;
null;
(function(b) {
for (var c in a && a[console.log("PASS")])
console;
Expand Down Expand Up @@ -9125,8 +9125,29 @@ issue_4908: {
expect: {
var a = 0, b;
console || a++;
var c = d = a, d = [ d && d, d += 42 ];
var c = a, d = [ (d = a) && d, d += 42 ];
console.log(d[1]);
}
expect_stdout: "42"
}

issue_4910: {
options = {
collapse_vars: true,
reduce_vars: true,
toplevel: true,
}
input: {
var a = "foo", b;
var c = b = a;
1 && c[a = "bar"];
console.log(a, b);
}
expect: {
var a = "foo", b;
var c = b = a;
1 && b[a = "bar"];
console.log(a, b);
}
expect_stdout: "bar foo"
}
4 changes: 2 additions & 2 deletions test/compress/functions.js
Expand Up @@ -6107,7 +6107,7 @@ reduce_cross_reference_2: {
reduce_cross_reference_2_toplevel: {
options = {
collapse_vars: true,
passes: 3,
passes: 2,
pure_getters: "strict",
reduce_vars: true,
sequences: true,
Expand Down Expand Up @@ -6150,7 +6150,7 @@ reduce_cross_reference_3: {
reduce_cross_reference_3_toplevel: {
options = {
collapse_vars: true,
passes: 3,
passes: 2,
pure_getters: "strict",
reduce_vars: true,
sequences: true,
Expand Down

0 comments on commit d464be3

Please sign in to comment.