Class AbstractScope<S extends AbstractScope<S,V>,V extends AbstractVar<S,V>>
- All Implemented Interfaces:
StaticScope
,Serializable
- Direct Known Subclasses:
Scope
,TypedScope
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.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionReturn an iterable over all of the variables accessible to this scope (i.e.final V
Get a unique Var object to represent "arguments" within this scope.final S
final S
Returns the closest container scope.final S
If a var were declared in this scope, return the scope it would be hoisted to.abstract int
getDepth()
The depth of the scope.final S
Walks up the tree to find the global scope.final V
getOwnSlot
(String name) LikegetSlot
but does not recurse into parent scopes.abstract S
Returns the parent scope, or null if this is the global scope.final S
Returns the scope enclosing this one or null if none.final Node
Gets the container node of the scope.final V
Returns any defined slot within this scope for this name.Returns the variable, may be nullfinal int
Returns number of variables in this scope (excluding the special 'arguments' variable)Return an iterable over all of the variables declared in this scope (except the special 'arguments' variable).protected boolean
hasOwnImplicitSlot
(@Nullable com.google.javascript.jscomp.AbstractScope.ImplicitVar name) Returns true iff this scope implies a slot with the given name.final boolean
hasOwnSlot
(String name) Returns true if a variable is declared in this scope, with no recursion.final boolean
Returns true if a variable is declared in this or any parent scope.final boolean
final boolean
final boolean
final boolean
final boolean
final boolean
final boolean
isGlobal()
Returns whether this is the global scope.final boolean
isLocal()
Returns whether this is a local scope (i.e.final boolean
final boolean
final boolean
final String
toString()
typed()
untyped()
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.google.javascript.rhino.StaticScope
getTopmostScopeOfEventualDeclaration
-
Method Details
-
getDepth
public abstract int getDepth()The depth of the scope. The global scope has depth 0. -
getParent
Returns the parent scope, or null if this is the global scope. -
toString
-
untyped
-
typed
-
getRootNode
Gets the container node of the scope. This is typically the FUNCTION node or the global BLOCK/SCRIPT node.- Specified by:
getRootNode
in interfaceStaticScope
-
getGlobalScope
Walks up the tree to find the global scope. -
getParentScope
Description copied from interface:StaticScope
Returns the scope enclosing this one or null if none.- Specified by:
getParentScope
in interfaceStaticScope
-
hasOwnImplicitSlot
protected boolean hasOwnImplicitSlot(@Nullable com.google.javascript.jscomp.AbstractScope.ImplicitVar name) Returns true iff this scope implies a slot with the given name. -
hasOwnSlot
Returns true if a variable is declared in this scope, with no recursion. -
hasSlot
Returns true if a variable is declared in this or any parent scope. -
getOwnSlot
Description copied from interface:StaticScope
LikegetSlot
but does not recurse into parent scopes.- Specified by:
getOwnSlot
in interfaceStaticScope
-
getSlot
Description copied from interface:StaticScope
Returns any defined slot within this scope for this name. This call continues searching through parent scopes if a slot with this name is not found in the current scope.- Specified by:
getSlot
in interfaceStaticScope
- Parameters:
name
- The name of the variable slot to look up.- Returns:
- The defined slot for the variable, or
null
if no definition exists.
-
getVar
Returns the variable, may be nullNon-final for
TypedScope
which needs to handle qualified names. -
getArgumentsVar
Get a unique Var object to represent "arguments" within this scope.This explicitly excludes user declared variables that are names "arguments". It only returns special "arguments" variable that is inherent to a function.
-
getVarIterable
Return an iterable over all of the variables declared in this scope (except the special 'arguments' variable). -
getAllAccessibleVariables
Return an iterable over all of the variables accessible to this scope (i.e. the variables in this scope and its parent scopes). Any variables declared in the local scope with the same name as a variable declared in a parent scope gain precedence - if let x exists in the block scope, a declaration let x from the parent scope would not be included because the parent scope's variable gets shadowed.The iterable contains variables from inner scopes before adding variables from outer parent scopes.
We do not include the special 'arguments' variable.
-
getAllSymbols
-
getVarCount
public final int getVarCount()Returns number of variables in this scope (excluding the special 'arguments' variable) -
isGlobal
public final boolean isGlobal()Returns whether this is the global scope. -
isLocal
public final boolean isLocal()Returns whether this is a local scope (i.e. not the global scope). -
isBlockScope
public final boolean isBlockScope() -
isStaticBlockScope
public final boolean isStaticBlockScope() -
isFunctionBlockScope
public final boolean isFunctionBlockScope() -
isFunctionScope
public final boolean isFunctionScope() -
isModuleScope
public final boolean isModuleScope() -
isMemberFieldDefScope
public final boolean isMemberFieldDefScope() -
isComputedFieldDefRhsScope
public final boolean isComputedFieldDefRhsScope() -
isCatchScope
public final boolean isCatchScope() -
isCfgRootScope
public final boolean isCfgRootScope() -
getClosestHoistScope
If a var were declared in this scope, return the scope it would be hoisted to.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.
-
getClosestCfgRootScope
-
getClosestContainerScope
Returns the closest container scope. This is equivalent to what the current scope would have been for non-ES6 scope creators, and is thus useful for migrating code to use block scopes.
-