diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-10-05 11:59:03 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-10-05 18:59:03 +0800 |
commit | b91a2459c0f270b770c5d105df75f78c16af4749 (patch) | |
tree | 9978cb7343a1abc4c135e49fd5b8f29cd9886196 | |
parent | b7a57fc69de453fb84bf8f192bdb1fdfa628ed5b (diff) | |
download | tracifyjs-b91a2459c0f270b770c5d105df75f78c16af4749.tar.gz tracifyjs-b91a2459c0f270b770c5d105df75f78c16af4749.zip |
fix corner case in `unused` (#4185)
fixes #4184
-rw-r--r-- | lib/compress.js | 9 | ||||
-rw-r--r-- | test/compress/drop-unused.js | 30 |
2 files changed, 38 insertions, 1 deletions
diff --git a/lib/compress.js b/lib/compress.js index fbd07741..8c28d450 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -5133,6 +5133,13 @@ merge(Compressor.prototype, { return rhs.right; } + function cross_scope(def, sym) { + do { + if (def === sym) return false; + if (sym instanceof AST_Scope) return true; + } while (sym = sym.parent_scope); + } + function scan_ref_scoped(node, descend, init) { if (node instanceof AST_Assign && node.left instanceof AST_SymbolRef) { var def = node.left.definition(); @@ -5186,7 +5193,7 @@ merge(Compressor.prototype, { in_use_ids[node_def.id] = true; in_use.push(node_def); } - if (node.scope !== node_def.scope) { + if (cross_scope(node_def.scope, node.scope)) { var redef = node_def.redefined(); if (redef && !(redef.id in in_use_ids)) { in_use_ids[redef.id] = true; diff --git a/test/compress/drop-unused.js b/test/compress/drop-unused.js index 616bcaf7..257d7a76 100644 --- a/test/compress/drop-unused.js +++ b/test/compress/drop-unused.js @@ -3055,3 +3055,33 @@ single_use_catch_redefined: { } expect_stdout: true } + +issue_4184: { + options = { + reduce_vars: true, + unused: true, + } + input: { + (function() { + var a = function() {}, b = [ a, 1 && b, a = {} ]; + try { + throw 42; + } catch (a) { + { + console.log(a); + } + } + })(); + } + expect: { + (function() { + var b = [ function() {}, 1 && b, {} ]; + try { + throw 42; + } catch (a) { + console.log(a); + } + })(); + } + expect_stdout: "42" +} |