diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-05-14 00:52:42 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-14 07:52:42 +0800 |
commit | 938368ba219f29703b9f9e67c6fb0d1c0723220a (patch) | |
tree | 6f0c4c03c96d35c7eb2cccf66d2e6685e70a8438 | |
parent | fe2f1965d6efcd1807eea087708ffdb9a5798db0 (diff) | |
download | tracifyjs-938368ba219f29703b9f9e67c6fb0d1c0723220a.tar.gz tracifyjs-938368ba219f29703b9f9e67c6fb0d1c0723220a.zip |
enhance `collapse_vars` (#3896)
-rw-r--r-- | lib/compress.js | 10 | ||||
-rw-r--r-- | test/compress/collapse_vars.js | 35 |
2 files changed, 39 insertions, 6 deletions
diff --git a/lib/compress.js b/lib/compress.js index 8cf7262a..93dbe063 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -1350,7 +1350,7 @@ merge(Compressor.prototype, { var lhs = get_lhs(candidate); var side_effects = lhs && lhs.has_side_effects(compressor); var scan_lhs = lhs && !side_effects && !is_lhs_read_only(lhs, compressor); - var scan_rhs = foldable(get_rhs(candidate)); + var scan_rhs = foldable(candidate); if (!scan_lhs && !scan_rhs) continue; var modify_toplevel = false; // Locate symbols which may execute code outside of scanning range @@ -1841,10 +1841,6 @@ merge(Compressor.prototype, { } } - function get_rhs(expr) { - return candidate instanceof AST_Assign && candidate.operator == "=" && candidate.right; - } - function invariant(expr) { if (expr instanceof AST_Array) return false; if (expr instanceof AST_Binary && lazy_op[expr.operator]) { @@ -1859,7 +1855,9 @@ merge(Compressor.prototype, { } function foldable(expr) { - if (!expr) return false; + while (expr instanceof AST_Assign && expr.operator == "=") { + expr = expr.right; + } if (expr instanceof AST_SymbolRef) { var value = expr.evaluate(compressor); if (value === expr) return rhs_exact_match; diff --git a/test/compress/collapse_vars.js b/test/compress/collapse_vars.js index dee370cf..0dd8754d 100644 --- a/test/compress/collapse_vars.js +++ b/test/compress/collapse_vars.js @@ -8015,3 +8015,38 @@ issue_3891: { } expect_stdout: "function" } + +issue_3894: { + options = { + collapse_vars: true, + inline: true, + reduce_vars: true, + unused: true, + } + input: { + function log(msg) { + console.log(msg ? "FAIL" : "PASS"); + } + var a, c; + (function(b) { + a = c = 0, + log(b); + })(-0); + log(a); + log(c); + } + expect: { + function log(msg) { + console.log(msg ? "FAIL" : "PASS"); + } + var a, c; + void log(-(a = c = 0)); + log(a); + log(c); + } + expect_stdout: [ + "PASS", + "PASS", + "PASS", + ] +} |