Skip to content

Commit

Permalink
Make TypedScopeCreator a little more readable.
Browse files Browse the repository at this point in the history
* Move the call to Node#setJSType out of the DeferredSetType constructor (which is a surprising side effect) and directly into the deferredSetType() method.
* Factor the call to NodeTraversal#traverseTyped into AbstractScopeBuilder#build and consolidate how global and local scopes are treated.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=180769654
  • Loading branch information
shicks authored and blickly committed Jan 4, 2018
1 parent 6bb1e6f commit 572f393
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/com/google/javascript/jscomp/TypedScopeCreator.java
Expand Up @@ -179,10 +179,6 @@ private class DeferredSetType {
checkNotNull(type);
this.node = node;
this.type = type;

// Other parts of this pass may read off the node.
// (like when we set the LHS of an assign with a typed RHS function.)
node.setJSType(type);
}

void resolve(TypedScope scope) {
Expand Down Expand Up @@ -241,16 +237,12 @@ public TypedScope createScope(Node root, Scope parent) {

// Find all the classes in the global scope.
newScope = createInitialScope(root);

GlobalScopeBuilder globalScopeBuilder = new GlobalScopeBuilder(newScope);
scopeBuilder = globalScopeBuilder;
NodeTraversal.traverseTyped(compiler, root, scopeBuilder);
scopeBuilder = new GlobalScopeBuilder(newScope);
} else {
newScope = new TypedScope(typedParent, root);
LocalScopeBuilder localScopeBuilder = new LocalScopeBuilder(newScope);
scopeBuilder = localScopeBuilder;
localScopeBuilder.build();
scopeBuilder = new LocalScopeBuilder(newScope);
}
scopeBuilder.build();

scopeBuilder.resolveStubDeclarations();

Expand Down Expand Up @@ -470,7 +462,16 @@ private AbstractScopeBuilder(TypedScope scope) {
this.scope = scope;
}

/** Traverse the scope root and build it. */
void build() {
NodeTraversal.traverseTyped(compiler, scope.getRootNode(), this);
}

/** Set the type for a node now, and enqueue it to be updated with a resolved type later. */
void setDeferredType(Node node, JSType type) {
// Other parts of this pass may read the not-yet-resolved type off the node.
// (like when we set the LHS of an assign with a typed RHS function.)
node.setJSType(type);
deferredSetTypes.add(new DeferredSetType(node, type));
}

Expand Down Expand Up @@ -1942,8 +1943,9 @@ private LocalScopeBuilder(TypedScope scope) {
/**
* Traverse the scope root and build it.
*/
@Override
void build() {
NodeTraversal.traverseTyped(compiler, scope.getRootNode(), this);
super.build();

AstFunctionContents contents =
getFunctionAnalysisResults(scope.getRootNode());
Expand Down

0 comments on commit 572f393

Please sign in to comment.