diff options
-rw-r--r-- | lib/compress.js | 20 | ||||
-rw-r--r-- | test/compress/collapse_vars.js | 25 | ||||
-rw-r--r-- | test/compress/functions.js | 4 |
3 files changed, 39 insertions, 10 deletions
diff --git a/lib/compress.js b/lib/compress.js index 4c9540d9..ba683e24 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -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 { @@ -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; } @@ -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; @@ -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++) { @@ -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; } diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 0c23ad8c..4834dd7d 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -9049,7 +9049,7 @@ issue_4874: { } expect: { var a; - a = null; + null; (function(b) { for (var c in a && a[console.log("PASS")]) console; @@ -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" +} diff --git a/test/compress/functions.js b/test/compress/functions.js index baab29d2..5713a37b 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -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, @@ -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, |