diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-06-22 22:03:11 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-23 05:03:11 +0800 |
commit | 8b05677c15308923a76a6c769dc628a93901556d (patch) | |
tree | a0faadfd9e2d5f05e54ac59754245a5329ba903b | |
parent | 95090dbf24dbda120924eb3cd003f80e0128f52a (diff) | |
download | tracifyjs-8b05677c15308923a76a6c769dc628a93901556d.tar.gz tracifyjs-8b05677c15308923a76a6c769dc628a93901556d.zip |
fix corner case in `collapse_vars` (#5026)
fixes #5025
-rw-r--r-- | lib/compress.js | 7 | ||||
-rw-r--r-- | test/compress/collapse_vars.js | 3 | ||||
-rw-r--r-- | test/compress/functions.js | 30 | ||||
-rw-r--r-- | test/compress/keep_fargs.js | 3 |
4 files changed, 34 insertions, 9 deletions
diff --git a/lib/compress.js b/lib/compress.js index 66441e80..5a1df7c3 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1818,10 +1818,7 @@ merge(Compressor.prototype, { return node; } if (is_lhs(node, parent)) { - if (value_def && !hit_rhs) { - assign_used = true; - if (node.definition().last_ref === node) replaced++; - } + if (value_def && !hit_rhs) assign_used = true; return node; } else if (value_def) { if (stop_if_hit && assign_pos == 0) assign_pos = remaining - replaced; @@ -2024,7 +2021,7 @@ merge(Compressor.prototype, { statements[i].transform(scanner); } if (value_def) { - if (!replaced || remaining > replaced) { + if (!replaced || remaining > replaced + assign_used) { candidates.push(hit_stack); force_single = true; continue; diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index 50db51b0..e2d567b4 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -4658,7 +4658,6 @@ replace_all_var_scope: { rename = true options = { collapse_vars: true, - reduce_vars: true, unused: true, } mangle = {} @@ -4677,7 +4676,7 @@ replace_all_var_scope: { (function(c, o) { switch (~a) { case (b += a): - case +o: + case o++: } })(--b, a); console.log(a, b); diff --git a/test/compress/functions.js b/test/compress/functions.js index 988f589e..b3a6011f 100644 --- a/test/compress/functions.js +++ b/test/compress/functions.js @@ -6228,3 +6228,33 @@ recursive_collapse: { } expect_stdout: "PASS" } + +issue_5025: { + options = { + collapse_vars: true, + inline: true, + reduce_vars: true, + unused: true, + } + input: { + function f(a) { + function g() { + b = 42; + } + g(b = a); + var b = this; + console.log(typeof b); + } + f(); + } + expect: { + function f(a) { + b = a, + void (b = 42); + var b = this; + console.log(typeof b); + } + f(); + } + expect_stdout: "object" +} diff --git a/test/compress/keep_fargs.js b/test/compress/keep_fargs.js index 3c2e4a74..2316646e 100644 --- a/test/compress/keep_fargs.js +++ b/test/compress/keep_fargs.js @@ -1151,7 +1151,6 @@ replace_all_var_scope: { options = { collapse_vars: true, keep_fargs: false, - reduce_vars: true, unused: true, } mangle = {} @@ -1170,7 +1169,7 @@ replace_all_var_scope: { (function(c) { switch (~a) { case (b += a): - case +c: + case c++: } })((--b, a)); console.log(a, b); |