Package com.google.javascript.jscomp
Class Scope
- java.lang.Object
-
- com.google.javascript.jscomp.Scope
-
- All Implemented Interfaces:
StaticScope<JSType>
,StaticSymbolTable<Scope.Var,Scope.Var>
public class Scope extends java.lang.Object implements StaticScope<JSType>, StaticSymbolTable<Scope.Var,Scope.Var>
Scope contains information about a variable scope in JavaScript. Scopes can be nested, a scope points back to its parent scope. A Scope contains information about variables defined in that scope.A Scope is also used as a lattice element for flow-sensitive type inference. As a lattice element, a Scope is viewed as a map from names to types. A name not in the map is considered to have the bottom type. The join of two maps m1 and m2 is the map of the union of names with
JSType.getLeastSupertype(com.google.javascript.rhino.jstype.JSType)
to meet the m1 type and m2 type.- See Also:
NodeTraversal
,DataFlowAnalysis
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
Scope.Arguments
A special subclass of Var used to distinguish "arguments" in the current scope.static class
Scope.Var
Stores info about a variable
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Iterable<Scope.Var>
getAllSymbols()
Returns all variables in this symbol table.Scope.Var
getArgumentsVar()
Get a unique VAR object to represents "arguments" within this scopejava.util.Iterator<Scope.Var>
getDeclarativelyUnboundVarsWithoutTypes()
Gets all variables declared with "var" but without declared types attached.Scope.Var
getOwnSlot(java.lang.String name)
LikegetSlot
but does not recurse into parent scopes.Scope
getParent()
StaticScope<JSType>
getParentScope()
Returns the scope enclosing this one or null if none.java.lang.Iterable<Scope.Var>
getReferences(Scope.Var var)
Returns the references that point to the given symbol.Node
getRootNode()
Gets the container node of the scope.StaticScope<JSType>
getScope(Scope.Var var)
Returns the scope for a given symbol.Scope.Var
getSlot(java.lang.String name)
Returns any defined slot within this scope for this name.JSType
getTypeOfThis()
Gets the type ofthis
in the current scope.Scope.Var
getVar(java.lang.String name)
Returns the variable, may be nullint
getVarCount()
Returns number of variables in this scopejava.util.Iterator<Scope.Var>
getVars()
Return an iterator over all of the variables declared in this scope.boolean
isDeclared(java.lang.String name, boolean recurse)
Returns true if a variable is declared.boolean
isGlobal()
Returns whether this is the global scope.boolean
isLocal()
Returns whether this is a local scope (i.e.
-
-
-
Method Detail
-
getRootNode
public Node 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<JSType>
-
getParent
public Scope getParent()
-
getParentScope
public StaticScope<JSType> getParentScope()
Description copied from interface:StaticScope
Returns the scope enclosing this one or null if none.- Specified by:
getParentScope
in interfaceStaticScope<JSType>
-
getTypeOfThis
public JSType getTypeOfThis()
Gets the type ofthis
in the current scope.- Specified by:
getTypeOfThis
in interfaceStaticScope<JSType>
-
getSlot
public Scope.Var getSlot(java.lang.String name)
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<JSType>
- Parameters:
name
- The name of the variable slot to look up.- Returns:
- The defined slot for the variable, or
null
if no definition exists.
-
getOwnSlot
public Scope.Var getOwnSlot(java.lang.String name)
Description copied from interface:StaticScope
LikegetSlot
but does not recurse into parent scopes.- Specified by:
getOwnSlot
in interfaceStaticScope<JSType>
-
getVar
public Scope.Var getVar(java.lang.String name)
Returns the variable, may be null
-
getArgumentsVar
public Scope.Var getArgumentsVar()
Get a unique VAR object to represents "arguments" within this scope
-
isDeclared
public boolean isDeclared(java.lang.String name, boolean recurse)
Returns true if a variable is declared.
-
getVars
public java.util.Iterator<Scope.Var> getVars()
Return an iterator over all of the variables declared in this scope.
-
getReferences
public java.lang.Iterable<Scope.Var> getReferences(Scope.Var var)
Description copied from interface:StaticSymbolTable
Returns the references that point to the given symbol.- Specified by:
getReferences
in interfaceStaticSymbolTable<Scope.Var,Scope.Var>
-
getScope
public StaticScope<JSType> getScope(Scope.Var var)
Description copied from interface:StaticSymbolTable
Returns the scope for a given symbol.- Specified by:
getScope
in interfaceStaticSymbolTable<Scope.Var,Scope.Var>
-
getAllSymbols
public java.lang.Iterable<Scope.Var> getAllSymbols()
Description copied from interface:StaticSymbolTable
Returns all variables in this symbol table.- Specified by:
getAllSymbols
in interfaceStaticSymbolTable<Scope.Var,Scope.Var>
-
getVarCount
public int getVarCount()
Returns number of variables in this scope
-
isGlobal
public boolean isGlobal()
Returns whether this is the global scope.
-
isLocal
public boolean isLocal()
Returns whether this is a local scope (i.e. not the global scope).
-
getDeclarativelyUnboundVarsWithoutTypes
public java.util.Iterator<Scope.Var> getDeclarativelyUnboundVarsWithoutTypes()
Gets all variables declared with "var" but without declared types attached.
-
-