Skip to content

Commit

Permalink
Make more explicit which scope roots are possible in populate()
Browse files Browse the repository at this point in the history
This makes the code slightly longer, but I find it easier to follow.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=162537031
  • Loading branch information
blickly committed Jul 20, 2017
1 parent 6a69f8b commit 9218de8
Showing 1 changed file with 58 additions and 37 deletions.
95 changes: 58 additions & 37 deletions src/com/google/javascript/jscomp/Es6SyntacticScopeCreator.java
Expand Up @@ -122,46 +122,67 @@ void populate() {
// 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();

// Bleed the function name into the scope, if it hasn't
// been declared in the outer scope.
String fnName = fnNameNode.getString();
if (!fnName.isEmpty() && NodeUtil.isFunctionExpression(n)) {
declareVar(scope, fnNameNode);
switch (n.getToken()) {
case FUNCTION: {
// 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();

// Bleed the function name into the scope, if it hasn't
// been declared in the outer scope.
String fnName = fnNameNode.getString();
if (!fnName.isEmpty() && NodeUtil.isFunctionExpression(n)) {
declareVar(scope, fnNameNode);
}

// Args: Declare function variables
checkState(args.isParamList());
declareLHS(scope, args);
// Since we create a separate scope for body, stop scanning here
return;
}

// Args: Declare function variables
checkState(args.isParamList());
declareLHS(scope, args);
// Since we create a separate scope for body, stop scanning here

} else if (n.isClass()) {
final Node classNameNode = n.getFirstChild();
// Bleed the class name into the scope, if it hasn't
// been declared in the outer scope.
if (!classNameNode.isEmpty() && NodeUtil.isClassExpression(n)) {
declareVar(scope, classNameNode);
case CLASS: {
final Node classNameNode = n.getFirstChild();
// Bleed the class name into the scope, if it hasn't
// been declared in the outer scope.
if (!classNameNode.isEmpty() && NodeUtil.isClassExpression(n)) {
declareVar(scope, classNameNode);
}
return;
}
} else if (n.isRoot()
|| n.isNormalBlock()
|| NodeUtil.isAnyFor(n)
|| n.isSwitch()
|| n.isModuleBody()) {
boolean isHoistScope =
n.isRoot() || NodeUtil.isFunctionBlock(n) || n.isModuleBody();
Scope hoistScope = isHoistScope ? scope : null;
scanVars(n, hoistScope, scope);
} else {
// n is the global scope
checkState(scope.isGlobal(), scope);
scanVars(n, scope, scope);

case ROOT:
case SCRIPT:
// n is the global scope
checkState(scope.isGlobal(), scope);
scanVars(n, scope, scope);
return;

case MODULE_BODY:
scanVars(n, scope, scope);
return;

case FOR:
case FOR_OF:
case FOR_IN:
case SWITCH:
scanVars(n, null, scope);
return;

case BLOCK:
if (NodeUtil.isFunctionBlock(n)) {
scanVars(n, scope, scope);
} else {
scanVars(n, null, scope);
}
return;

default:
throw new RuntimeException("Illegal scope root: " + n);
}
}

Expand Down

0 comments on commit 9218de8

Please sign in to comment.