@Override protected CompilerPass create(AbstractCompiler compiler) { return MakeDeclaredNamesUnique.getContextualRenameInverter(compiler); } };
@Override public void visit(NodeTraversal t, Node n, Node parent) { switch (n.getToken()) { case NAME: case IMPORT_STAR: visitName(t, n, parent); break; case STRING_KEY: { String newName = getReplacementName(n.getString()); if (newName != null && !n.hasChildren()) { Node name = IR.name(n.getString()).useSourceInfoFrom(n); n.addChildToBack(name); visitName(t, name, n); } break; } default: break; } }
/** * Rename any local shadows of namespaces. * This should be a very rare occurrence, so only do this traversal * if we know that we need it. */ private void renameNamespaceShadows(NodeTraversal t) { checkState(NodeUtil.isFunctionBlock(t.getScopeRoot()), t.getScopeRoot()); if (hasNamespaceShadows) { MakeDeclaredNamesUnique.Renamer renamer = new MakeDeclaredNamesUnique.WhitelistedRenamer( new MakeDeclaredNamesUnique.ContextualRenamer(), forbiddenLocals); for (String s : forbiddenLocals) { renamer.addDeclaredName(s, false); } MakeDeclaredNamesUnique uniquifier = new MakeDeclaredNamesUnique(renamer); NodeTraversal.traverseScopeRoots( compiler, null, ImmutableList.of(t.getScopeRoot()), uniquifier, true); } }
@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); }
switch (n.getType()) { case Token.NAME: String newName = getReplacementName(n.getString()); if (newName != null) { Renamer renamer = nameStack.peek();
static void normalizeSyntheticCode( AbstractCompiler compiler, Node js, String prefix) { NodeTraversal.traverseEs6(compiler, js, new Normalize.NormalizeStatements(compiler, false)); NodeTraversal.traverseEs6( compiler, js, new MakeDeclaredNamesUnique( new BoilerplateRenamer( compiler.getCodingConvention(), compiler.getUniqueNameIdSupplier(), prefix))); }
@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); }
private void visitName(NodeTraversal t, Node n, Node parent) { // Don't rename the exported name foo in export {a as foo}; or import {foo as b}; if (NodeUtil.isNonlocalModuleExportName(n)) { return; } String newName = getReplacementName(n.getString()); if (newName != null) { Renamer renamer = renamerStack.peek(); if (renamer.stripConstIfReplaced()) { // TODO(johnlenz): Do we need to do anything about the Javadoc? n.removeProp(Node.IS_CONSTANT_NAME); } n.setString(newName); if (markChanges) { t.reportCodeChange(); // If we are renaming a function declaration, make sure the containing scope // has the opporunity to act on the change. if (parent.isFunction() && NodeUtil.isFunctionDeclaration(parent)) { t.getCompiler().reportChangeToEnclosingScope(parent); } } } }
static void normalizeSyntheticCode( AbstractCompiler compiler, Node js, String prefix) { NodeTraversal.traverse(compiler, js, new Normalize.NormalizeStatements(compiler, false)); NodeTraversal.traverse( compiler, js, new MakeDeclaredNamesUnique( new BoilerplateRenamer( compiler.getCodingConvention(), compiler.getUniqueNameIdSupplier(), prefix))); }
findDeclaredNames(functionBody, null, renamer);
@Override protected CompilerPass create(AbstractCompiler compiler) { return MakeDeclaredNamesUnique.getContextualRenameInverter(compiler); }
/** * Fix-up all local names to be unique for this subtree. * @param fnNode A mutable instance of the function to be inlined. */ private void makeLocalNamesUnique(Node fnNode, boolean isCallInLoop) { Supplier<String> idSupplier = compiler.getUniqueNameIdSupplier(); // Make variable names unique to this instance. NodeTraversal.traverseScopeRoots( compiler, null, ImmutableList.of(fnNode), new MakeDeclaredNamesUnique( new InlineRenamer( compiler.getCodingConvention(), idSupplier, "inline_", isCallInLoop, true, null), false), true); // Make label names unique to this instance. new RenameLabels(compiler, new LabelNameSupplier(idSupplier), false, false) .process(null, fnNode); }
/** * 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); } } }
/** * Rename any local shadows of namespaces. * This should be a very rare occurrence, so only do this traversal * if we know that we need it. */ private void renameNamespaceShadows(NodeTraversal t) { if (hasNamespaceShadows) { MakeDeclaredNamesUnique.Renamer renamer = new MakeDeclaredNamesUnique.WhitelistedRenamer( new MakeDeclaredNamesUnique.ContextualRenamer(), forbiddenLocals); for (String s : forbiddenLocals) { renamer.addDeclaredName(s, false); } MakeDeclaredNamesUnique uniquifier = new MakeDeclaredNamesUnique(renamer); Node parent = t.getScopeRoot().getParent(); NodeTraversal.traverseEs6(compiler, parent, uniquifier); } }
/** * Fix-up all local names to be unique for this subtree. * @param fnNode A mutable instance of the function to be inlined. */ private void makeLocalNamesUnique(Node fnNode, boolean isCallInLoop) { Supplier<String> idSupplier = compiler.getUniqueNameIdSupplier(); // Make variable names unique to this instance. NodeTraversal.traverseEs6( compiler, fnNode, new MakeDeclaredNamesUnique( new InlineRenamer( compiler.getCodingConvention(), idSupplier, "inline_", isCallInLoop, true, null))); // Make label names unique to this instance. new RenameLabels(compiler, new LabelNameSupplier(idSupplier), false) .process(null, fnNode); }
@Override public void process(Node externs, Node root) { NodeTraversal.traverse(compiler, root, new RemoveEmptyClassMembers()); NodeTraversal.traverseRoots( compiler, new NormalizeStatements(compiler, assertOnChange), externs, root); removeDuplicateDeclarations(externs, root); MakeDeclaredNamesUnique renamer = new MakeDeclaredNamesUnique(); NodeTraversal.traverseRoots(compiler, renamer, externs, root); new PropagateConstantAnnotationsOverVars(compiler, assertOnChange) .process(externs, root); FindExposeAnnotations findExposeAnnotations = new FindExposeAnnotations(); NodeTraversal.traverse(compiler, root, findExposeAnnotations); if (!findExposeAnnotations.exposedProperties.isEmpty()) { NodeTraversal.traverse(compiler, root, new RewriteExposedProperties( findExposeAnnotations.exposedProperties)); } if (!compiler.getLifeCycleStage().isNormalized()) { compiler.setLifeCycleStage(LifeCycleStage.NORMALIZED); } }
.traverseRoots(externs, root); if (MAKE_LOCAL_NAMES_UNIQUE) { MakeDeclaredNamesUnique renamer = new MakeDeclaredNamesUnique(); NodeTraversal t = new NodeTraversal(compiler, renamer); t.traverseRoots(externs, root);