diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2021-05-14 18:49:46 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-05-15 01:49:46 +0800 |
commit | 2cff7c94e843e72dee1adf6c826a190f0e156441 (patch) | |
tree | 203b9086e8f327b4189e39acd51346b1f395c40d | |
parent | 75760481184c6d63595f8f7c3456b664d221abb5 (diff) | |
download | tracifyjs-2cff7c94e843e72dee1adf6c826a190f0e156441.tar.gz tracifyjs-2cff7c94e843e72dee1adf6c826a190f0e156441.zip |
fix corner case in `reduce_vars` (#4934)
fixes #4933
-rw-r--r-- | lib/compress.js | 12 | ||||
-rw-r--r-- | test/compress/varify.js | 52 |
2 files changed, 62 insertions, 2 deletions
diff --git a/lib/compress.js b/lib/compress.js index 18c1d265..6010752d 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -6096,7 +6096,7 @@ merge(Compressor.prototype, { return !compressor.exposed(def) && def.references.length == def.replaced; } : function(def) { if (!(def.id in in_use_ids)) return true; - if (def.orig.length < 2) return false; + if (def.orig.length - def.eliminated < 2) return false; // function argument will always overshadow its name if (def.orig[1] instanceof AST_SymbolFunarg) return true; // retain if referenced within destructured object of argument @@ -8551,7 +8551,7 @@ merge(Compressor.prototype, { def.scope = scope; scope.variables.set(def.name, def); }), - value: defn.value + value: defn.value, }); }) }); @@ -10672,6 +10672,14 @@ merge(Compressor.prototype, { lambda_def.recursive_refs = def.recursive_refs; } value.walk(new TreeWalker(function(node) { + if (node instanceof AST_SymbolDeclaration) { + if (node !== name) { + var def = node.definition(); + def.orig.push(node); + def.eliminated++; + } + return; + } if (!(node instanceof AST_SymbolRef)) return; var def = node.definition(); if (def === defun_def) { diff --git a/test/compress/varify.js b/test/compress/varify.js index f60c5474..75d50e6d 100644 --- a/test/compress/varify.js +++ b/test/compress/varify.js @@ -523,3 +523,55 @@ default_init: { expect_stdout: "PASS" node_version: ">=4" } + +issue_4933_1: { + options = { + reduce_vars: true, + toplevel: true, + unused: true, + varify: true, + } + input: { + console.log(f()); + function f() { + var a; + for (console in a = [ f ]) { + const b = a; + } + } + } + expect: { + console.log(function f() { + var a; + for (console in a = [ f ]) { + const b = a; + } + }()); + } + expect_stdout: "undefined" +} + +issue_4933_2: { + options = { + passes: 2, + reduce_vars: true, + toplevel: true, + unused: true, + varify: true, + } + input: { + console.log(f()); + function f() { + var a; + for (console in a = [ f ]) { + const b = a; + } + } + } + expect: { + console.log(function f() { + for (console in [ f ]); + }()); + } + expect_stdout: "undefined" +} |