public abstract class AbstractScope<S extends AbstractScope<S,V>,V extends AbstractVar<S,V>> extends java.lang.Object implements StaticScope, java.io.Serializable
ES 2015 introduces new scoping rules, which adds some complexity to this class. In particular, scopes fall into two mutually exclusive categories: block and container. Block scopes are all scopes whose roots are blocks, as well as any control structures whose optional blocks are omitted. These scopes did not exist at all prior to ES 2015. Container scopes comprise function scopes, global scopes, and module scopes, and (aside from modules, which didn't exist in ES5) corresponds to the ES5 scope rules. This corresponds roughly to one container scope per CFG root (but not exactly, due to SCRIPT-level CFGs).
All container scopes, plus the outermost block scope within a function (i.e. the function block scope) are considered hoist scopes. All functions thus have two hoist scopes: the function scope and the function block scope. Hoist scopes are relevant because "var" declarations are hoisted to the closest hoist scope, as opposed to "let" and "const" which always apply to the specific scope in which they occur.
Note that every function actually has two distinct hoist scopes: a container scope on the FUNCTION node, and a block-scope on the top-level BLOCK in the function (the "function block"). Local variables are declared on the function block, while parameters and optionally the function name (if it bleeds, i.e. from a named function expression) are declared on the container scope. This is required so that default parameter initializers can refer to names from outside the function that could possibly be shadowed in the function block. But these scopes are not fully independent of one another, since the language does not allow a top-level local variable to shadow a parameter name - so in some situations these scopes must be treated as a single scope.
NodeTraversal
,
Serialized FormModifier and Type | Method and Description |
---|---|
java.lang.Iterable<V> |
getAllAccessibleVariables()
Return an iterable over all of the variables accessible to this scope (i.e.
|
java.lang.Iterable<V> |
getAllSymbols() |
V |
getArgumentsVar()
Get a unique Var object to represent "arguments" within this scope
|
S |
getClosestContainerScope()
Returns the closest container scope.
|
S |
getClosestHoistScope()
If a var were declared in this scope, return the scope it would be hoisted to.
|
abstract int |
getDepth()
The depth of the scope.
|
S |
getGlobalScope()
Walks up the tree to find the global scope.
|
V |
getOwnSlot(java.lang.String name)
Like
getSlot but does not recurse into parent scopes. |
abstract S |
getParent()
Returns the parent scope, or null if this is the global scope.
|
S |
getParentScope()
Returns the scope enclosing this one or null if none.
|
Node |
getRootNode()
Gets the container node of the scope.
|
V |
getSlot(java.lang.String name)
Returns any defined slot within this scope for this name.
|
V |
getVar(java.lang.String name)
Returns the variable, may be null
|
int |
getVarCount()
Returns number of variables in this scope (excluding the special 'arguments' variable)
|
java.lang.Iterable<V> |
getVarIterable()
Return an iterable over all of the variables declared in this scope
(except the special 'arguments' variable).
|
boolean |
hasOwnSlot(java.lang.String name)
Returns true if a variable is declared in this scope, with no recursion.
|
boolean |
hasSlot(java.lang.String name)
Returns true if a variable is declared in this or any parent scope.
|
boolean |
isBlockScope() |
boolean |
isCatchScope() |
boolean |
isFunctionBlockScope() |
boolean |
isFunctionScope() |
boolean |
isGlobal()
Returns whether this is the global scope.
|
boolean |
isLocal()
Returns whether this is a local scope (i.e.
|
boolean |
isModuleScope() |
java.lang.String |
toString() |
TypedScope |
typed() |
Scope |
untyped() |
public abstract int getDepth()
public abstract S getParent()
public final java.lang.String toString()
toString
in class java.lang.Object
public Scope untyped()
public TypedScope typed()
public Node getRootNode()
getRootNode
in interface StaticScope
public final S getGlobalScope()
public final S getParentScope()
StaticScope
getParentScope
in interface StaticScope
public final V getSlot(java.lang.String name)
StaticScope
getSlot
in interface StaticScope
name
- The name of the variable slot to look up.null
if no
definition exists.public final V getOwnSlot(java.lang.String name)
StaticScope
getSlot
but does not recurse into parent scopes.getOwnSlot
in interface StaticScope
public V getVar(java.lang.String name)
public final V getArgumentsVar()
public final boolean hasOwnSlot(java.lang.String name)
public final boolean hasSlot(java.lang.String name)
public java.lang.Iterable<V> getVarIterable()
public final java.lang.Iterable<V> getAllAccessibleVariables()
The iterable contains variables from inner scopes before adding variables from outer parent scopes.
We do not include the special 'arguments' variable.
public java.lang.Iterable<V> getAllSymbols()
public int getVarCount()
public boolean isGlobal()
public boolean isLocal()
public final boolean isBlockScope()
public final boolean isFunctionBlockScope()
public final boolean isFunctionScope()
public final boolean isModuleScope()
public final boolean isCatchScope()
public final S getClosestHoistScope()
For function scopes, we return back the scope itself, since even though there is no way to declare a var inside function parameters, it would make even less sense to say that such declarations would be "hoisted" somewhere else.
public final S getClosestContainerScope()
Copyright © 2009-2019 Google. All Rights Reserved.