diff options
author | Alex Lam S.L <alexlamsl@gmail.com> | 2020-12-10 07:45:39 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-10 15:45:39 +0800 |
commit | a59593cac8b5acbefa07daf1fca3af6a72c9d262 (patch) | |
tree | 06c805beca2673bedad14c47f15190a08d8fd1a7 /lib/compress.js | |
parent | 046bbde9d44a131ec60952e369ec4b22b9718def (diff) | |
download | tracifyjs-a59593cac8b5acbefa07daf1fca3af6a72c9d262.tar.gz tracifyjs-a59593cac8b5acbefa07daf1fca3af6a72c9d262.zip |
fix corner case in `loops` & `unused` (#4356)
fixes #4355
Diffstat (limited to 'lib/compress.js')
-rw-r--r-- | lib/compress.js | 37 |
1 files changed, 33 insertions, 4 deletions
diff --git a/lib/compress.js b/lib/compress.js index 4da13758..1bb6d542 100644 --- a/lib/compress.js +++ b/lib/compress.js @@ -4542,7 +4542,7 @@ merge(Compressor.prototype, { var def = node.definition(); if (member(def.scope, scopes)) return true; if (scope && !def.redefined()) { - var scope_def = scope.find_variable(node); + var scope_def = scope.find_variable(node.name); if (def.undeclared ? !scope_def : scope_def === def) { result = "f"; return true; @@ -4842,7 +4842,9 @@ merge(Compressor.prototype, { if (!(ref instanceof AST_SymbolRef)) return; var def = ref.definition(); var ldef = node.variables.get(ref.name); - if (ldef && (ldef === def || def.undeclared || node.parent_scope.find_variable(ref) === def)) { + if (ldef && (ldef === def + || def.undeclared + || node.parent_scope.find_variable(ref.name) === def)) { references[def.id] = false; references[ldef.id] = false; } else { @@ -5468,7 +5470,7 @@ merge(Compressor.prototype, { }) : def.value === def.name.fixed_value()) && (!def.value.name || (old_def = def.value.name.definition()).assignments == 0 && (old_def.name == def.name.name || all(old_def.references, function(ref) { - return ref.scope.find_variable(def.name) === def.name.definition(); + return ref.scope.find_variable(def.name.name) === def.name.definition(); }))) && can_declare_defun() && can_rename(def.value, def.name.name)) { @@ -5759,6 +5761,33 @@ merge(Compressor.prototype, { if (init instanceof AST_SymbolRef) return init; } + function find_variable(name) { + var level = 0; + var scope = self; + while (true) { + var in_arg = false; + do { + var parent = compressor.parent(level++); + if (parent instanceof AST_DestructuredKeyVal && parent.key === scope) { + var fn = compressor.parent(level + 1); + if (fn instanceof AST_Lambda) { + in_arg = fn.argnames.indexOf(compressor.parent(level)) >= 0; + level += 2; + scope = fn; + break; + } + } + if (!parent) return; + scope = parent; + } while (!(scope instanceof AST_BlockScope)); + var def = scope.variables.get(name); + if (!def) continue; + if (!in_arg) return def; + var sym = def.orig[0]; + if (sym instanceof AST_SymbolFunarg || sym instanceof AST_SymbolLambda) return def; + } + } + function scan_ref_scoped(node, descend, init) { if (node instanceof AST_Assign && node.left instanceof AST_SymbolRef) { var def = node.left.definition(); @@ -5807,7 +5836,7 @@ merge(Compressor.prototype, { if (!sym) return; var def = sym.definition(); if (def.scope !== self) { - var d = self.find_variable(sym); + var d = find_variable(sym.name); if ((d && d.redefined() || d) === def) return; } node.object.walk(tw); |