diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2018-01-26 14:21:11 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-01-26 14:21:11 +0800 |
commit | 193612ac67ef269d6b05fdf61c9638a5eebd76f7 (patch) | |
tree | 775406c79a6871980c1aa3a489a5845bfc2f1d85 | |
parent | 95cfce68eae6173a76e8f01fae08a91f5d8996d2 (diff) | |
download | tracifyjs-193612ac67ef269d6b05fdf61c9638a5eebd76f7.tar.gz tracifyjs-193612ac67ef269d6b05fdf61c9638a5eebd76f7.zip |
fix accounting after conversion to assignment (#2847)
Missing reference to `AST_SymbolRef` created by `unused` causes `collapse_vars` to misbehave.
fixes #2846
-rw-r--r-- | lib/compress.js | 4 | ||||
-rw-r--r-- | test/compress/drop-unused.js | 27 |
2 files changed, 30 insertions, 1 deletions
diff --git a/lib/compress.js b/lib/compress.js index 33b43134..07128aff 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -3038,9 +3038,11 @@ merge(Compressor.prototype, { if (var_defs.length > 1 && (!def.value || sym.orig.indexOf(def.name) > sym.eliminated)) { compressor.warn("Dropping duplicated definition of variable {name} [{file}:{line},{col}]", template(def.name)); if (def.value) { + var ref = make_node(AST_SymbolRef, def.name, def.name); + sym.references.push(ref); var assign = make_node(AST_Assign, def, { operator: "=", - left: make_node(AST_SymbolRef, def.name, def.name), + left: ref, right: def.value }); if (fixed_ids[sym.id] === def) { diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js index 0ac7bb33..e99d7ab1 100644 --- a/test/compress/drop-unused.js +++ b/test/compress/drop-unused.js @@ -1692,3 +1692,30 @@ issue_2768: { } expect_stdout: "PASS undefined" } + +issue_2846: { + options = { + collapse_vars: true, + reduce_vars: true, + toplevel: true, + unused: true, + } + input: { + function f(a, b) { + var a = 0; + b && b(a); + return a++; + } + var c = f(); + console.log(c); + } + expect: { + var c = function(a, b) { + a = 0; + b && b(a); + return a++; + }(); + console.log(c); + } + expect_stdout: "0" +} |