From a59593cac8b5acbefa07daf1fca3af6a72c9d262 Mon Sep 17 00:00:00 2001 From: "Alex Lam S.L" Date: Thu, 10 Dec 2020 07:45:39 +0000 Subject: fix corner case in `loops` & `unused` (#4356) fixes #4355 --- lib/scope.js | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) (limited to 'lib/scope.js') diff --git a/lib/scope.js b/lib/scope.js index 6c521bcc..ff0cb58c 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -190,7 +190,7 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) { function entangle(defun, scope) { if (defun === scope) return; node.mark_enclosed(options); - var def = scope.find_variable(node); + var def = scope.find_variable(node.name); if (node.thedef === def) return; node.thedef = def; def.orig.push(node); @@ -219,6 +219,21 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) { if (node.label) node.label.thedef.references.push(node); return true; } + // ensure mangling works if `catch` reuses a scope variable + if (node instanceof AST_SymbolCatch) { + var def = node.definition().redefined(); + if (def) for (var s = node.scope; s; s = s.parent_scope) { + push_uniq(s.enclosed, def); + if (s === def.scope) break; + } + return true; + } + // ensure compression works if `const` reuses a scope variable + if (node instanceof AST_SymbolConst) { + var redef = node.definition().redefined(); + if (redef) redef.const_redefs = true; + return true; + } if (node instanceof AST_SymbolRef) { var name = node.name; var sym = node.scope.find_variable(name); @@ -258,21 +273,6 @@ AST_Toplevel.DEFMETHOD("figure_out_scope", function(options) { node.reference(options); return true; } - // ensure mangling works if `catch` reuses a scope variable - if (node instanceof AST_SymbolCatch) { - var def = node.definition().redefined(); - if (def) for (var s = node.scope; s; s = s.parent_scope) { - push_uniq(s.enclosed, def); - if (s === def.scope) break; - } - return true; - } - // ensure compression works if `const` reuses a scope variable - if (node instanceof AST_SymbolConst) { - var redef = node.definition().redefined(); - if (redef) redef.const_redefs = true; - return true; - } }); self.walk(tw); @@ -390,9 +390,8 @@ AST_Symbol.DEFMETHOD("reference", function(options) { }); AST_BlockScope.DEFMETHOD("find_variable", function(name) { - if (name instanceof AST_Symbol) name = name.name; return this.variables.get(name) - || (this.parent_scope && this.parent_scope.find_variable(name)); + || this.parent_scope && this.parent_scope.find_variable(name); }); AST_BlockScope.DEFMETHOD("def_function", function(symbol, init) { -- cgit v1.2.3