Class JSTypeRegistry


  • public final class JSTypeRegistry
    extends java.lang.Object
    The type registry is used to resolve named types.

    This class is not thread-safe.

    • Field Detail

      • OBJECT_ELEMENT_TEMPLATE

        @Deprecated
        public static final java.lang.String OBJECT_ELEMENT_TEMPLATE
        Deprecated.
        See Also:
        Constant Field Values
    • Constructor Detail

      • JSTypeRegistry

        public JSTypeRegistry​(ErrorReporter reporter)
      • JSTypeRegistry

        public JSTypeRegistry​(ErrorReporter reporter,
                              java.util.Set<java.lang.String> forwardDeclaredTypes)
        Constructs a new type registry populated with the built-in types.
    • Method Detail

      • getArrayElementKey

        public TemplateType getArrayElementKey()
        Returns the template variable for the element type of Arrays.
      • getReadonlyArrayElementKey

        public TemplateType getReadonlyArrayElementKey()
        Returns the template variable for the element type of ReadonlyArrays.
      • getReadonlyMapKey

        public TemplateType getReadonlyMapKey()
        Returns the template variable for the key type of ReadonlyMaps.
      • getReadonlyMapValue

        public TemplateType getReadonlyMapValue()
        Returns the template variable for the value type of ReadonlyMaps.
      • getObjectElementKey

        public TemplateType getObjectElementKey()
        Returns:
        The template variable corresponding to the property value type for Javascript Objects and Arrays.
      • getObjectIndexKey

        public TemplateType getObjectIndexKey()
        Returns:
        The template variable corresponding to the property key type of the built-in Javascript object.
      • getIterableTemplate

        public TemplateType getIterableTemplate()
        Returns:
        The template variable for the Iterable interface.
      • getIteratorIterableTemplateKey

        public TemplateType getIteratorIterableTemplateKey()
        Returns:
        The template variable for the IteratorIterable interface.
      • getIIterableResultTemplateKey

        public TemplateType getIIterableResultTemplateKey()
        Returns:
        The template variable for the IteratorIterable interface.
      • getIteratorValueTemplate

        public TemplateType getIteratorValueTemplate()
        Return the value template variable for the Iterator interface.
      • getGeneratorValueTemplate

        public TemplateType getGeneratorValueTemplate()
        Return the value template variable for the Generator interface.
      • getAsyncIterableTemplate

        public TemplateType getAsyncIterableTemplate()
        Returns the template variable for the AsyncIterable interface.
      • getAsyncIteratorValueTemplate

        public TemplateType getAsyncIteratorValueTemplate()
        Returns the template variable for the AsyncIterator interface.
      • getIThenableTemplate

        public TemplateType getIThenableTemplate()
        Returns:
        The template variable for the IThenable interface.
      • maybeGetTemplateTypesOfBuiltin

        public @Nullable com.google.common.collect.ImmutableList<TemplateType> maybeGetTemplateTypesOfBuiltin​(StaticScope scope,
                                                                                                              java.lang.String fnName)
        Returns an immutable list of template types of the given builtin.
      • resolveViaClosureNamespace

        public @Nullable JSType resolveViaClosureNamespace​(java.lang.String reference)
        Resolves a named type by checking for the longest prefix that matches some Closure namespace, if any, then attempting to resolve via properties based on the type of the `exports` object in that namespace.
      • registerPropertyOnType

        public void registerPropertyOnType​(java.lang.String propertyName,
                                           JSType type)
        Tells the type system that owner may have a property named propertyName. This allows the registry to keep track of what types a property is defined upon.

        This is NOT the same as saying that owner must have a property named type. ObjectType#hasProperty attempts to minimize false positives ("if we're not sure, then don't type check this property"). The type registry, on the other hand, should attempt to minimize false negatives ("if this property is assigned anywhere in the program, it must show up in the type registry").

      • canPropertyBeDefined

        public JSTypeRegistry.PropDefinitionKind canPropertyBeDefined​(JSType type,
                                                                      java.lang.String propertyName)
        Returns whether the given property can possibly be set on the given type.
      • getEachReferenceTypeWithProperty

        public java.lang.Iterable<ObjectType> getEachReferenceTypeWithProperty​(java.lang.String propertyName)
        Returns each reference type that has a property propertyName defined on it.

        Unlike most types in our type system, the collection of types returned will not be collapsed. This means that if a type is defined on Object and on Array, this method must return [Object, Array]. It would not be correct to collapse them to [Object].

      • declareType

        public boolean declareType​(StaticScope scope,
                                   java.lang.String name,
                                   JSType type)
        Records declared global type names. This makes resolution faster and more robust in the common case.
        Parameters:
        name - The name of the type to be recorded.
        type - The actual type being associated with the name.
        Returns:
        True if this name is not already defined, false otherwise.
      • declareTypeForExactScope

        public boolean declareTypeForExactScope​(StaticScope scope,
                                                java.lang.String name,
                                                JSType type)
        Records declared global type names. This makes resolution faster and more robust in the common case.
        Parameters:
        name - The name of the type to be recorded.
        type - The actual type being associated with the name.
        Returns:
        True if this name is not already defined, false otherwise.
      • overwriteDeclaredType

        public void overwriteDeclaredType​(StaticScope scope,
                                          java.lang.String name,
                                          JSType type)
        Overrides a declared global type name. Throws an exception if this type name hasn't been declared yet.
      • isForwardDeclaredType

        public boolean isForwardDeclaredType​(java.lang.String name)
        Whether this is a forward-declared type name.
      • getReadableTypeName

        public java.lang.String getReadableTypeName​(Node n)
        First dereferences the JSType to remove null/undefined then returns a human-readable type name
      • getReadableTypeNameNoDeref

        public java.lang.String getReadableTypeNameNoDeref​(Node n)
      • getGlobalType

        public JSType getGlobalType​(java.lang.String jsTypeName)
      • getType

        public JSType getType​(@Nullable StaticScope scope,
                              java.lang.String jsTypeName)
        Looks up a native type by name.
        Parameters:
        jsTypeName - The name string.
        Returns:
        the corresponding JSType object or null it cannot be found
      • getType

        public JSType getType​(StaticTypedScope scope,
                              java.lang.String jsTypeName,
                              java.lang.String sourceName,
                              int lineno,
                              int charno)
        Looks up a type by name. To allow for forward references to types, an unrecognized string has to be bound to a NamedType object that will be resolved later.
        Parameters:
        scope - A scope for doing type name resolution.
        jsTypeName - The name string.
        sourceName - The name of the source file where this reference appears.
        lineno - The line number of the reference.
        Returns:
        a NamedType if the string argument is not one of the known types, otherwise the corresponding JSType object.
      • evaluateTypeExpressionInGlobalScope

        public JSType evaluateTypeExpressionInGlobalScope​(JSTypeExpression expr)
      • createOptionalType

        public JSType createOptionalType​(JSType type)
        Creates a type representing optional values of the given type.
        Returns:
        the union of the type and the void type
      • createNullableType

        public JSType createNullableType​(JSType type)
        Creates a type representing nullable values of the given type.
        Returns:
        the union of the type and the Null type
      • createOptionalNullableType

        public JSType createOptionalNullableType​(JSType type)
        Creates a nullable and undefine-able value of the given type.
        Returns:
        The union of the type and null and undefined.
      • createUnionType

        public JSType createUnionType​(JSType... variants)
        Creates a union type whose variants are the arguments.
      • createUnionType

        public JSType createUnionType​(java.util.List<? extends JSType> variants)
      • createUnionType

        public JSType createUnionType​(JSTypeNative... variants)
        Creates a union type whose variants are the built-in types specified by the arguments.
      • createFunctionType

        public FunctionType createFunctionType​(JSType returnType,
                                               JSType... parameterTypes)
        Creates a function type.
        Parameters:
        returnType - the function's return type
        parameterTypes - the parameters' types
      • createFunctionType

        public FunctionType createFunctionType​(JSType returnType,
                                               java.util.List<FunctionType.Parameter> parameters)
        Parameters:
        parameters - the function's parameters or null to indicate that the parameter types are unknown.
        returnType - the function's return type or null to indicate that the return type is unknown.
      • createFunctionTypeWithVarArgs

        public FunctionType createFunctionTypeWithVarArgs​(JSType returnType,
                                                          JSType... parameterTypes)
        Creates a function type. The last parameter type of the function is considered a variable length argument.
        Parameters:
        returnType - the function's return type
        parameterTypes - the parameters' types
      • createFunctionTypeWithInstanceType

        public JSType createFunctionTypeWithInstanceType​(ObjectType instanceType,
                                                         JSType returnType,
                                                         java.util.List<JSType> parameterTypes)
        Creates a function type in which this refers to an object instance.
        Parameters:
        instanceType - the type of this
        returnType - the function's return type
        parameterTypes - the parameters' types
      • createParameters

        public com.google.common.collect.ImmutableList<FunctionType.Parameter> createParameters​(JSType... parameterTypes)
        Creates a tree hierarchy representing a typed argument list.
        Parameters:
        parameterTypes - the parameter types.
        Returns:
        a tree hierarchy representing a typed argument list.
      • createParametersWithVarArgs

        public com.google.common.collect.ImmutableList<FunctionType.Parameter> createParametersWithVarArgs​(JSType... parameterTypes)
        Creates a tree hierarchy representing a typed argument list. The last parameter type is considered a variable length argument.
        Parameters:
        parameterTypes - the parameter types. The last element of this array is considered a variable length argument.
        Returns:
        a tree hierarchy representing a typed argument list.
      • createOptionalParameters

        public com.google.common.collect.ImmutableList<FunctionType.Parameter> createOptionalParameters​(JSType... parameterTypes)
        Creates a tree hierarchy representing a typed parameter list in which every parameter is optional.
      • createFunctionTypeWithNewReturnType

        public FunctionType createFunctionTypeWithNewReturnType​(FunctionType existingFunctionType,
                                                                JSType returnType)
        Creates a new function type based on an existing function type but with a new return type.
        Parameters:
        existingFunctionType - the existing function type.
        returnType - the new return type.
      • buildRecordTypeFromObject

        public JSType buildRecordTypeFromObject​(ObjectType objType)
      • createRecordType

        public JSType createRecordType​(java.util.Map<java.lang.String,​? extends JSType> props)
      • createObjectType

        public ObjectType createObjectType​(java.lang.String name,
                                           ObjectType implicitPrototype)
        Create an object type.
      • createAnonymousObjectType

        public ObjectType createAnonymousObjectType​(@Nullable JSDocInfo info)
        Create an anonymous object type.
        Parameters:
        info - Used to mark object literals as structs; can be null
      • resetImplicitPrototype

        public void resetImplicitPrototype​(JSType type,
                                           ObjectType newImplicitProto)
        Set the implicit prototype if it's possible to do so. There are a few different reasons why this could be a no-op: for example, numbers can't be implicit prototypes, and we don't want to change the implicit prototype if other classes have already subclassed this one.
      • createConstructorType

        public FunctionType createConstructorType​(java.lang.String name,
                                                  Node source,
                                                  java.util.List<FunctionType.Parameter> parameters,
                                                  JSType returnType,
                                                  @Nullable com.google.common.collect.ImmutableList<TemplateType> templateKeys,
                                                  boolean isAbstract)
        Creates a constructor function type.
        Parameters:
        name - the function's name or null to indicate that the function is anonymous.
        source - the node defining this function. Its type (Node.getToken() ()}) must be Token.FUNCTION.
        parameters - the function's parameters or null to indicate that the parameter types are unknown.
        returnType - the function's return type or null to indicate that the return type is unknown.
        templateKeys - the templatized types for the class.
        isAbstract - whether the function type represents an abstract class
      • createTemplateType

        public TemplateType createTemplateType​(java.lang.String name)
      • createTemplateType

        public TemplateType createTemplateType​(java.lang.String name,
                                               JSType bound)
      • createTemplateTypeWithTransformation

        public TemplateType createTemplateTypeWithTransformation​(java.lang.String name,
                                                                 Node expr)
      • getEmptyTemplateTypeMap

        public TemplateTypeMap getEmptyTemplateTypeMap()
      • createTemplatizedType

        public TemplatizedType createTemplatizedType​(ObjectType baseType,
                                                     com.google.common.collect.ImmutableList<JSType> templatizedTypes)
        Creates a templatized instance of the specified type. Only ObjectTypes can currently be templatized; extend the logic in this function when more types can be templatized.
        Parameters:
        baseType - the type to be templatized.
        templatizedTypes - a list of the template JSTypes. Will be matched by list order to the template keys on the base type.
      • createTemplatizedType

        public TemplatizedType createTemplatizedType​(ObjectType baseType,
                                                     java.util.Map<TemplateType,​JSType> templatizedTypes)
        Creates a templatized instance of the specified type. Only ObjectTypes can currently be templatized; extend the logic in this function when more types can be templatized.
        Parameters:
        baseType - the type to be templatized.
        templatizedTypes - a map from TemplateType to corresponding JSType value. Any unfilled TemplateTypes on the baseType that are *not* contained in this map will have UNKNOWN_TYPE used as their value.
      • createTemplatizedType

        public TemplatizedType createTemplatizedType​(ObjectType baseType,
                                                     JSType... templatizedTypes)
        Creates a templatized instance of the specified type. Only ObjectTypes can currently be templatized; extend the logic in this function when more types can be templatized.
        Parameters:
        baseType - the type to be templatized.
        templatizedTypes - a list of the template JSTypes. Will be matched by list order to the template keys on the base type.
      • createNamedType

        public NamedType createNamedType​(StaticTypedScope scope,
                                         java.lang.String reference,
                                         java.lang.String sourceName,
                                         int lineno,
                                         int charno)
        Creates a named type.
      • identifyNonNullableName

        public void identifyNonNullableName​(@Nullable StaticScope scope,
                                            java.lang.String name)
        Identifies the name of a typedef or enum before we actually declare it.
      • isNonNullableName

        public boolean isNonNullableName​(StaticScope scope,
                                         java.lang.String name)
        Identifies the name of a typedef or enum before we actually declare it.
      • createTypeFromCommentNode

        public JSType createTypeFromCommentNode​(Node n)
      • createTypeFromCommentNode

        public JSType createTypeFromCommentNode​(Node n,
                                                java.lang.String sourceName,
                                                @Nullable StaticTypedScope scope)
        Creates a JSType from the nodes representing a type.
        Parameters:
        n - The node with type info.
        sourceName - The source file name.
        scope - A scope for doing type name lookups.
      • registerTemplateTypeNamesInScope

        public void registerTemplateTypeNamesInScope​(java.lang.Iterable<TemplateType> keys,
                                                     Node scopeRoot)
        Registers template types on the given scope root. This takes a Node rather than a StaticScope because at the time it is called, the scope has not yet been created.
      • createScopeWithTemplates

        public StaticTypedScope createScopeWithTemplates​(StaticTypedScope scope,
                                                         java.lang.Iterable<TemplateType> templates)
        Returns a new scope that includes the given template names for type resolution purposes.
      • registerNonLegacyClosureNamespace

        public void registerNonLegacyClosureNamespace​(java.lang.String moduleName,
                                                      Node definitionNode,
                                                      JSType type)
        Registers a goog.module namespace (that does not have goog.module.declareLegacyNamespace)

        This allows JSTypeRegistry to resolve types that refer to goog.modules by namespace. These have unique handling because they exist only in the type space and do not have a corresponding value space value.

      • registerLegacyClosureNamespace

        public void registerLegacyClosureNamespace​(java.lang.String moduleName)
        Registers a goog.provide or legacy goog.module namespace with the type registry