Class JSTypeRegistry

java.lang.Object
com.google.javascript.rhino.jstype.JSTypeRegistry

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

This class is not thread-safe.

  • Field Details

  • Constructor Details

    • JSTypeRegistry

      public JSTypeRegistry(ErrorReporter reporter)
    • JSTypeRegistry

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

    • 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.
    • getIterableValueTemplate

      public TemplateType getIterableValueTemplate()
      Returns the value template variable for the Iterable interface.
    • getIteratorIterableValueTemplate

      public TemplateType getIteratorIterableValueTemplate()
      Returns the value template variable for the IteratorIterable interface.
    • getIIterableResultValueTemplate

      public TemplateType getIIterableResultValueTemplate()
      Returns the value template variable for the IIterableResult interface.
    • getIteratorValueTemplate

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

      public TemplateType getGeneratorValueTemplate()
      Returns the value template variable for the Generator interface.
    • getAsyncIterableValueTemplate

      public TemplateType getAsyncIterableValueTemplate()
      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, String fnName)
      Returns an immutable list of template types of the given builtin.
    • getErrorReporter

      public ErrorReporter getErrorReporter()
    • resolveViaClosureNamespace

      public @Nullable JSType resolveViaClosureNamespace(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(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, String propertyName)
      Returns whether the given property can possibly be set on the given type.
    • getEachReferenceTypeWithProperty

      public Iterable<ObjectType> getEachReferenceTypeWithProperty(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, 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, 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, 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(String name)
      Whether this is a forward-declared type name.
    • getReadableTypeName

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

      public String getReadableTypeNameNoDeref(Node n)
    • getGlobalType

      public JSType getGlobalType(String jsTypeName)
    • getType

      public JSType getType(@Nullable StaticScope scope, 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, String jsTypeName, 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.
    • getNativeType

      public JSType getNativeType(JSTypeNative typeId)
    • getNativeObjectType

      public ObjectType getNativeObjectType(JSTypeNative typeId)
    • getNativeFunctionType

      public FunctionType getNativeFunctionType(JSTypeNative typeId)
    • getResolver

      public JSTypeResolver getResolver()
    • 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(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, List<FunctionType.Parameter> parameters)
      Parameters:
      returnType - the function's return type or null to indicate that the return type is unknown.
      parameters - the function's parameters or null to indicate that the parameter types are 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, 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(Map<String,? extends JSType> props)
    • createObjectType

      public ObjectType createObjectType(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(String name, Node source, 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(String name)
    • createTemplateType

      public TemplateType createTemplateType(String name, JSType bound)
    • createTemplateTypeWithTransformation

      public TemplateType createTemplateTypeWithTransformation(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, 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, String reference, String sourceName, int lineno, int charno)
      Creates a named type.
    • identifyNonNullableName

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

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

      public JSType evaluateTypeExpression(JSTypeExpression expr, StaticTypedScope scope)
    • createTypeFromCommentNode

      public JSType createTypeFromCommentNode(Node n)
    • createTypeFromCommentNode

      public JSType createTypeFromCommentNode(Node n, 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(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, Iterable<TemplateType> templates)
      Returns a new scope that includes the given template names for type resolution purposes.
    • registerNonLegacyClosureNamespace

      public void registerNonLegacyClosureNamespace(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(String moduleName)
      Registers a goog.provide or legacy goog.module namespace with the type registry