Skip to content

Commit

Permalink
small cleanups and clarifications
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=145841380
  • Loading branch information
tbreisacher authored and blickly committed Jan 28, 2017
1 parent 35e1e1b commit 99e883b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
29 changes: 13 additions & 16 deletions src/com/google/javascript/jscomp/MakeDeclaredNamesUnique.java
Expand Up @@ -42,8 +42,7 @@
* TODO(johnlenz): Try to merge this with the ScopeCreator. * TODO(johnlenz): Try to merge this with the ScopeCreator.
* TODO(moz): Handle more ES6 features, such as default parameters. * TODO(moz): Handle more ES6 features, such as default parameters.
*/ */
class MakeDeclaredNamesUnique class MakeDeclaredNamesUnique implements NodeTraversal.ScopedCallback {
implements NodeTraversal.ScopedCallback {


// Arguments is special cased to handle cases where a local name shadows // Arguments is special cased to handle cases where a local name shadows
// the arguments declaration. // the arguments declaration.
Expand Down Expand Up @@ -86,12 +85,11 @@ public void enterScope(NodeTraversal t) {
// If the contextual renamer is being used, the starting context can not // If the contextual renamer is being used, the starting context can not
// be a function. // be a function.
Preconditions.checkState( Preconditions.checkState(
!declarationRoot.isFunction() || !declarationRoot.isFunction() || !(rootRenamer instanceof ContextualRenamer));
!(rootRenamer instanceof ContextualRenamer));
Preconditions.checkState(t.inGlobalScope()); Preconditions.checkState(t.inGlobalScope());
renamer = rootRenamer; renamer = rootRenamer;
} else { } else {
renamer = nameStack.peek().forChildScope(!NodeUtil.createsBlockScope(declarationRoot)); renamer = nameStack.peek().createForChildScope(!NodeUtil.createsBlockScope(declarationRoot));
} }


if (!declarationRoot.isFunction()) { if (!declarationRoot.isFunction()) {
Expand All @@ -118,12 +116,11 @@ public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
case FUNCTION: { case FUNCTION: {
// Add recursive function name, if needed. // Add recursive function name, if needed.
// NOTE: "enterScope" is called after we need to pick up this name. // NOTE: "enterScope" is called after we need to pick up this name.
Renamer renamer = nameStack.peek().forChildScope(false); Renamer renamer = nameStack.peek().createForChildScope(false);


// If needed, add the function recursive name. // If needed, add the function recursive name.
String name = n.getFirstChild().getString(); String name = n.getFirstChild().getString();
if (name != null && !name.isEmpty() && parent != null if (!name.isEmpty() && parent != null && !NodeUtil.isFunctionDeclaration(n)) {
&& !NodeUtil.isFunctionDeclaration(n)) {
renamer.addDeclaredName(name, false); renamer.addDeclaredName(name, false);
} }


Expand All @@ -132,7 +129,7 @@ public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
} }


case PARAM_LIST: { case PARAM_LIST: {
Renamer renamer = nameStack.peek().forChildScope(true); Renamer renamer = nameStack.peek().createForChildScope(true);


// Add the function parameters // Add the function parameters
for (Node c = n.getFirstChild(); c != null; c = c.getNext()) { for (Node c = n.getFirstChild(); c != null; c = c.getNext()) {
Expand All @@ -148,7 +145,7 @@ public boolean shouldTraverse(NodeTraversal t, Node n, Node parent) {
} }


case CATCH: { case CATCH: {
Renamer renamer = nameStack.peek().forChildScope(false); Renamer renamer = nameStack.peek().createForChildScope(false);


String name = n.getFirstChild().getString(); String name = n.getFirstChild().getString();
renamer.addDeclaredName(name, false); renamer.addDeclaredName(name, false);
Expand Down Expand Up @@ -264,7 +261,7 @@ interface Renamer {
/** /**
* @return A Renamer for a scope within the scope of the current Renamer. * @return A Renamer for a scope within the scope of the current Renamer.
*/ */
Renamer forChildScope(boolean hoisted); Renamer createForChildScope(boolean hoisted);


/** /**
* @return The closest hoisting target for var and function declarations. * @return The closest hoisting target for var and function declarations.
Expand Down Expand Up @@ -482,7 +479,7 @@ private ContextualRenamer(
* Create a ContextualRenamer * Create a ContextualRenamer
*/ */
@Override @Override
public Renamer forChildScope(boolean hoistingTargetScope) { public Renamer createForChildScope(boolean hoistingTargetScope) {
return new ContextualRenamer(nameUsage, hoistingTargetScope, this); return new ContextualRenamer(nameUsage, hoistingTargetScope, this);
} }


Expand Down Expand Up @@ -624,7 +621,7 @@ public String getReplacementName(String oldName) {
} }


@Override @Override
public Renamer forChildScope(boolean hoistingTargetScope) { public Renamer createForChildScope(boolean hoistingTargetScope) {
return new InlineRenamer( return new InlineRenamer(
convention, uniqueIdSupplier, idPrefix, removeConstness, hoistingTargetScope, this); convention, uniqueIdSupplier, idPrefix, removeConstness, hoistingTargetScope, this);
} }
Expand Down Expand Up @@ -659,7 +656,7 @@ static class BoilerplateRenamer extends ContextualRenamer {
} }


@Override @Override
public Renamer forChildScope(boolean hoisted) { public Renamer createForChildScope(boolean hoisted) {
return new InlineRenamer(convention, uniqueIdSupplier, idPrefix, false, hoisted, this); return new InlineRenamer(convention, uniqueIdSupplier, idPrefix, false, hoisted, this);
} }
} }
Expand Down Expand Up @@ -693,8 +690,8 @@ public boolean stripConstIfReplaced() {
} }


@Override @Override
public Renamer forChildScope(boolean hoistingTargetScope) { public Renamer createForChildScope(boolean hoistingTargetScope) {
return new WhitelistedRenamer(delegate.forChildScope(hoistingTargetScope), whitelist); return new WhitelistedRenamer(delegate.createForChildScope(hoistingTargetScope), whitelist);
} }


@Override @Override
Expand Down
8 changes: 4 additions & 4 deletions test/com/google/javascript/jscomp/NormalizeTest.java
Expand Up @@ -410,10 +410,10 @@ public void testRemoveDuplicateVarDeclarations1() {
"function f() { var a = 1; a = 2 }"); "function f() { var a = 1; a = 2 }");
test("var a = 1; function f(){ var a = 2 }", test("var a = 1; function f(){ var a = 2 }",
"var a = 1; function f(){ var a$jscomp$1 = 2 }"); "var a = 1; function f(){ var a$jscomp$1 = 2 }");
test("function f() { var a = 1; lable1:var a = 2 }", test("function f() { var a = 1; label1:var a = 2 }",
"function f() { var a = 1; lable1:{a = 2}}"); "function f() { var a = 1; label1:{a = 2}}");
test("function f() { var a = 1; lable1:var a }", test("function f() { var a = 1; label1:var a }",
"function f() { var a = 1; lable1:{} }"); "function f() { var a = 1; label1:{} }");
test("function f() { var a = 1; for(var a in b); }", test("function f() { var a = 1; for(var a in b); }",
"function f() { var a = 1; for(a in b); }"); "function f() { var a = 1; for(a in b); }");
} }
Expand Down

0 comments on commit 99e883b

Please sign in to comment.