Some simplifications of the Es6SyntaticScopeCreator.
@@ -35,8 +35,6 @@ import com.google.javascript.rhino.Node;
*/
public final class Es6SyntacticScopeCreator implements ScopeCreator {
private final AbstractCompiler compiler;
- private Scope scope;
- private InputId inputId;
private final RedeclarationHandler redeclarationHandler;
// The arguments variable is special, in that it's declared for every function,
@@ -54,31 +52,42 @@ public final class Es6SyntacticScopeCreator implements ScopeCreator {
this.redeclarationHandler = redeclarationHandler;
}
+ @Override
+ public boolean hasBlockScope() {
+ return true;
+ }
+
@Override
public Scope createScope(Node n, Scope parent) {
- inputId = null;
+ Scope scope;
if (parent == null) {
scope = Scope.createGlobalScope(n);
} else {
scope = new Scope(parent, n);
}
- scanRoot(n);
+ new ScopeScanner(scope).populate();
- inputId = null;
- Scope returnedScope = scope;
- scope = null;
- return returnedScope;
+ return scope;
}
- private void scanRoot(Node n) {
- if (n.isFunction()) {
- if (inputId == null) {
+ private class ScopeScanner {
+ private final Scope scope;
+ private InputId inputId;
+
+ ScopeScanner(Scope scope) {
+ this.scope = scope;
+ }
+
+ void populate() {
+ Node n = scope.getRootNode();
+ // If we are populating the global scope, inputId will be null, and need to be set
+ // as we enter each SCRIPT node.
inputId = NodeUtil.getInputId(n);
+ if (n.isFunction()) {
// TODO(johnlenz): inputId maybe null if the FUNCTION node is detached
// from the AST.
// Is it meaningful to build a scope for detached FUNCTION node?
- }
final Node fnNameNode = n.getFirstChild();
final Node args = fnNameNode.getNext();
@@ -96,10 +105,6 @@ public final class Es6SyntacticScopeCreator implements ScopeCreator {
// Since we create a separate scope for body, stop scanning here
} else if (n.isClass()) {
- if (scope.getParent() != null) {
- inputId = NodeUtil.getInputId(n);
- }
-
final Node classNameNode = n.getFirstChild();
// Bleed the class name into the scope, if it hasn't
// been declared in the outer scope.
@@ -113,9 +118,6 @@ public final class Es6SyntacticScopeCreator implements ScopeCreator {
|| NodeUtil.isAnyFor(n)
|| n.isSwitch()
|| n.isModuleBody()) {
- if (scope.getParent() != null) {
- inputId = NodeUtil.getInputId(n);
- }
boolean scanInnerBlocks =
n.isRoot() || NodeUtil.isFunctionBlock(n) || n.isModuleBody();
scanVars(n, scanInnerBlocks, true);
@@ -249,7 +251,8 @@ public final class Es6SyntacticScopeCreator implements ScopeCreator {
CompilerInput input = compiler.getInput(inputId);
if (v != null
|| isShadowingDisallowed(name)
- || ((scope.isFunctionScope() || scope.isFunctionBlockScope()) && name.equals(ARGUMENTS))) {
+ || ((scope.isFunctionScope()
+ || scope.isFunctionBlockScope()) && name.equals(ARGUMENTS))) {
redeclarationHandler.onRedeclaration(scope, name, n, input);
} else {
scope.declare(name, n, input);
@@ -302,9 +305,5 @@ public final class Es6SyntacticScopeCreator implements ScopeCreator {
throw new RuntimeException("Unsupported node parent: " + parent);
}
}
-
- @Override
- public boolean hasBlockScope() {
- return true;
}
}
