Class TypedScope

  • All Implemented Interfaces:
    StaticTypedScope, StaticScope, java.io.Serializable

    public class TypedScope
    extends AbstractScope<TypedScope,​TypedVar>
    implements StaticTypedScope
    TypedScope contains information about variables and their types. Scopes can be nested; a scope points back to its parent scope.

    TypedScope 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.

    TypedScopes also have a concept of a set of 'reserved' names. These are names that will be declared as actual TypedVars later in the compilation. Looking up any of these reserved names will always return null, even if the name is available in a parent scope.

    See Also:
    NodeTraversal, Several methods in this class, such as throw an exception when called. The reason for this is that we want to shadow methods from the parent class, to avoid calling them accidentally., Serialized Form
    • Method Detail

      • hasOwnImplicitSlot

        protected boolean hasOwnImplicitSlot​(@Nullable
                                             com.google.javascript.jscomp.AbstractScope.ImplicitVar name)
        Description copied from class: AbstractScope
        Returns true iff this scope implies a slot with the given name.
        Overrides:
        hasOwnImplicitSlot in class AbstractScope<TypedScope,​TypedVar>
      • getDeclarativelyUnboundVarsWithoutTypes

        public java.lang.Iterable<TypedVar> getDeclarativelyUnboundVarsWithoutTypes()
        Returns the variables in this scope that have been declared with 'var' and not yet declared with a known type.

        These variables can safely be set to undefined (rather than unknown) at the start of type inference, and will be reset to the correct type when analyzing the first assignment to them. Parameters and externs are excluded because they are not initialized in the function body, and lexically-bound variables (let and const) are excluded because they are initialized when inferring the LET/CONST node, which is guaranteed to occur before any use, since they are not hoisted.

      • getNamespaceOrTypedefType

        public JSType getNamespaceOrTypedefType​(java.lang.String typeName)
      • getJsdocOfTypeDeclaration

        public JSDocInfo getJsdocOfTypeDeclaration​(java.lang.String typeName)
      • getVar

        public TypedVar getVar​(java.lang.String name)
        Returns the slot for name, considering shadowing of qualified names.

        The superclass method does not handle shadowing.

        Lookup of qualified names (i.e. names with dots) executes against scopes in the following precedence:

        1. This Scope.
        2. The first ancestor Scope, if any, that declares the root of the qualified name.
        3. The global Scope.

        An example of where this is necessary: say the global scope contains "a" and "a.b" and a function scope contains "a". When looking up "a.b" in the function scope, AbstractScope::getVar returns "a.b". This method returns null because the global "a" is shadowed.

        Overrides:
        getVar in class AbstractScope<TypedScope,​TypedVar>
      • getTopmostScopeOfEventualDeclaration

        public StaticScope getTopmostScopeOfEventualDeclaration​(java.lang.String name)
        Description copied from interface: StaticScope
        Returns the topmost slot containing this name, or null if no slots do. May return true in cases where StaticScope.getSlot(java.lang.String) returns null, though. Do not rely on this method if you need an actual slot.

        This method is intended for use while scopes are still being built, hence the name 'eventual' declaration. Once scope building is complete, the scope returned from this method must be equivalent to "getSlot(name).getScope()" or null

        Specified by:
        getTopmostScopeOfEventualDeclaration in interface StaticScope