Skip to content

Commit

Permalink
Another small readability improvement in MakeDeclaredNamesUnique.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=167645485
  • Loading branch information
tbreisacher authored and blickly committed Sep 8, 2017
1 parent f4376bc commit 2d2988f
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/com/google/javascript/jscomp/MakeDeclaredNamesUnique.java
Expand Up @@ -100,11 +100,12 @@ public void enterScope(NodeTraversal t) {
renamer = renamerStack.peek().createForChildScope(t.getScopeRoot(), hoist);
}

renamerStack.push(renamer);

if (!declarationRoot.isFunction()) {
// Add the block declarations
findDeclaredNames(t, declarationRoot, renamer, false);
findDeclaredNames(t, declarationRoot);
}
renamerStack.push(renamer);
}

@Override
Expand Down Expand Up @@ -145,9 +146,9 @@ public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
}

Node functionBody = n.getNext();
findDeclaredNames(t, functionBody, renamer, false);

renamerStack.push(renamer);

findDeclaredNames(t, functionBody);
break;
}

Expand Down Expand Up @@ -232,11 +233,15 @@ private String getReplacementName(String oldName) {
}

/**
* Traverses the current scope and collects declared names.
*
* @param recursive Whether this is being called recursively.
* Traverses the current scope and collects declared names by calling {@code addDeclaredName} on
* the {@code Renamer} that is at the top of the {@code renamerStack}.
*/
private void findDeclaredNames(NodeTraversal t, Node n, Renamer renamer, boolean recursive) {
private void findDeclaredNames(NodeTraversal t, Node n) {
findDeclaredNamesHelper(t, n, false);
}

private void findDeclaredNamesHelper(NodeTraversal t, Node n, boolean recursive) {
Renamer renamer = renamerStack.peek();
Node parent = n.getParent();

// Do a shallow traversal: Don't traverse into the function param list or body; just its name.
Expand All @@ -261,7 +266,7 @@ private void findDeclaredNames(NodeTraversal t, Node n, Renamer renamer, boolean
}

for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
findDeclaredNames(t, c, renamer, true);
findDeclaredNamesHelper(t, c, true);
}
}

Expand Down Expand Up @@ -303,8 +308,7 @@ interface Renamer {
/**
* Inverts the transformation by {@link ContextualRenamer}, when possible.
*/
static class ContextualRenameInverter
implements ScopedCallback, CompilerPass {
static class ContextualRenameInverter implements ScopedCallback, CompilerPass {
private final AbstractCompiler compiler;

// The set of names referenced in the current scope.
Expand Down Expand Up @@ -388,7 +392,7 @@ public void exitScope(NodeTraversal t) {
* values.
*/
void handleScopeVar(Var v) {
String name = v.getName();
String name = v.getName();
if (containsSeparator(name) && !getOriginalName(name).isEmpty()) {
String newName = findReplacementName(name);
referencedNames.remove(name);
Expand Down

0 comments on commit 2d2988f

Please sign in to comment.