diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-06-21 04:12:45 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-21 11:12:45 +0800 |
commit | 111366fca0352df45459ee8dc3fa6d8dcc337d92 (patch) | |
tree | 34603730576e5d1f7ffbb84404c44a7d397228e0 | |
parent | e368d3971588c78413a91d89c23356d2f511c723 (diff) | |
download | tracifyjs-111366fca0352df45459ee8dc3fa6d8dcc337d92.tar.gz tracifyjs-111366fca0352df45459ee8dc3fa6d8dcc337d92.zip |
fix corner case in `collapse_vars` (#5018)
fixes #5017
-rw-r--r-- | lib/compress.js | 7 | ||||
-rw-r--r-- | test/compress/collapse_vars.js | 5 | ||||
-rw-r--r-- | test/compress/destructured.js | 22 | ||||
-rw-r--r-- | test/compress/keep_fargs.js | 9 |
4 files changed, 33 insertions, 10 deletions
diff --git a/lib/compress.js b/lib/compress.js index f8289f4a..e4988947 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1820,7 +1820,7 @@ merge(Compressor.prototype, { if (is_lhs(node, parent)) { if (value_def && !hit_rhs) { assign_used = true; - replaced++; + if (node.definition().last_ref === node) replaced++; } return node; } else if (value_def) { @@ -1946,10 +1946,9 @@ merge(Compressor.prototype, { } } // Replace variable when found - if (node instanceof AST_SymbolRef - && node.name == def.name) { - if (!--replaced) abort = true; + if (node instanceof AST_SymbolRef && node.definition() === def) { if (is_lhs(node, multi_replacer.parent())) return node; + if (!--replaced) abort = true; var ref = rvalue.clone(); ref.scope = node.scope; ref.reference(); diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 66190e3e..30dc0248 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -4658,6 +4658,7 @@ replace_all_var_scope: { rename = true options = { collapse_vars: true, + reduce_vars: true, unused: true, } mangle = {} @@ -4676,7 +4677,7 @@ replace_all_var_scope: { (function(c, o) { switch (~a) { case (b += a): - case o++: + case +o: } })(--b, a); console.log(a, b); @@ -4728,7 +4729,7 @@ cascade_statement: { } function f3(a, b) { for (; a < b; a++) - if (c = a, a && b) + if ((c = a) && b) var c = c = b(a); } } diff --git a/test/compress/destructured.js b/test/compress/destructured.js index 41e337be..140d48e1 100644 --- a/test/compress/destructured.js +++ b/test/compress/destructured.js @@ -2625,3 +2625,25 @@ issue_4994: { expect_stdout: "PASS" node_version: ">=6" } + +issue_5017: { + options = { + collapse_vars: true, + reduce_vars: true, + toplevel: true, + } + input: { + var a = function() {}; + var b = c = a; + var c = [ c ] = [ c ]; + console.log(c[0] === a ? "PASS" : "FAIL"); + } + expect: { + var a = function() {}; + var b = a; + var c = [ c ] = [ c = a ]; + console.log(c[0] === a ? "PASS" : "FAIL"); + } + expect_stdout: "PASS" + node_version: ">=6" +} diff --git a/test/compress/keep_fargs.js b/test/compress/keep_fargs.js index 8761c330..3c2e4a74 100644 --- a/test/compress/keep_fargs.js +++ b/test/compress/keep_fargs.js @@ -1151,6 +1151,7 @@ replace_all_var_scope: { options = { collapse_vars: true, keep_fargs: false, + reduce_vars: true, unused: true, } mangle = {} @@ -1158,8 +1159,8 @@ replace_all_var_scope: { var a = 100, b = 10; (function(r, a) { switch (~a) { - case (b += a): - case a++: + case (b += a): + case a++: } })(--b, a); console.log(a, b); @@ -1168,8 +1169,8 @@ replace_all_var_scope: { var a = 100, b = 10; (function(c) { switch (~a) { - case (b += a): - case c++: + case (b += a): + case +c: } })((--b, a)); console.log(a, b); |