@Override public void enterScope(NodeTraversal t) { Node declarationRoot = t.getScopeRoot(); // ES6 function blocks are handled along with PARAM_LIST if (NodeUtil.isFunctionBlock(declarationRoot)) { return; } Renamer renamer; if (nameStack.isEmpty()) { // If the contextual renamer is being used, the starting context can not // be a function. Preconditions.checkState( !declarationRoot.isFunction() || !(rootRenamer instanceof ContextualRenamer)); Preconditions.checkState(t.inGlobalScope()); renamer = rootRenamer; } else { renamer = nameStack.peek().forChildScope(!NodeUtil.createsBlockScope(declarationRoot)); } if (!declarationRoot.isFunction()) { // Add the block declarations findDeclaredNames(declarationRoot, null, renamer); } nameStack.push(renamer); }
@Override public void enterScope(NodeTraversal t) { checkState( t.getScopeCreator().hasBlockScope(), "MakeDeclaredNamesUnique requires an ES6-compatible scope creator. %s is not compatible.", t.getScopeCreator()); Node declarationRoot = t.getScopeRoot(); Renamer renamer; if (renamerStack.isEmpty()) { // If the contextual renamer is being used, the starting context can not // be a function. checkState(!declarationRoot.isFunction() || !(rootRenamer instanceof ContextualRenamer)); renamer = rootRenamer; } else { boolean hoist = !declarationRoot.isFunction() && !NodeUtil.createsBlockScope(declarationRoot); renamer = renamerStack.peek().createForChildScope(t.getScopeRoot(), hoist); } renamerStack.push(renamer); findDeclaredNames(t, declarationRoot); }
findDeclaredNames(functionBody, null, renamer);
/** * Traverses the current scope and collects declared names. Does not * decent into functions or add CATCH exceptions. */ private void findDeclaredNames(Node n, Node parent, Renamer renamer) { // Do a shallow traversal, so don't traverse into function declarations, // except for the name of the function itself. if (parent == null || !parent.isFunction() || n == parent.getFirstChild()) { if (NodeUtil.isVarDeclaration(n)) { renamer.addDeclaredName(n.getString(), true); } else if (NodeUtil.isBlockScopedDeclaration(n) && !parent.isCatch()) { renamer.addDeclaredName(n.getString(), false); } else if (NodeUtil.isFunctionDeclaration(n)) { Node nameNode = n.getFirstChild(); renamer.addDeclaredName(nameNode.getString(), true); } for (Node c = n.getFirstChild(); c != null; c = c.getNext()) { findDeclaredNames(c, n, renamer); } } }