Class JSTypeRegistry
This class is not thread-safe.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumA tristate value returned from canPropertyBeDefined. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionJSTypeRegistry(ErrorReporter reporter) JSTypeRegistry(ErrorReporter reporter, Set<String> forwardDeclaredTypes) Constructs a new type registry populated with the built-in types. -
Method Summary
Modifier and TypeMethodDescriptionbuildRecordTypeFromObject(ObjectType objType) canPropertyBeDefined(JSType type, String propertyName) Returns whether the given property can possibly be set on the given type.createAnonymousObjectType(@Nullable JSDocInfo info) Create an anonymous object type.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.createFunctionType(JSType returnType, JSType... parameterTypes) Creates a function type.createFunctionType(JSType returnType, List<FunctionType.Parameter> parameters) createFunctionTypeWithInstanceType(ObjectType instanceType, JSType returnType, List<JSType> parameterTypes) Creates a function type in whichthisrefers to an object instance.createFunctionTypeWithNewReturnType(FunctionType existingFunctionType, JSType returnType) Creates a new function type based on an existing function type but with a new return type.createFunctionTypeWithVarArgs(JSType returnType, JSType... parameterTypes) Creates a function type.createNamedType(StaticTypedScope scope, String reference, String sourceName, int lineno, int charno) Creates a named type.createNullableType(JSType type) Creates a type representing nullable values of the given type.createObjectType(String name, ObjectType implicitPrototype) Create an object type.Creates a nullable and undefine-able value of the given type.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.createOptionalType(JSType type) Creates a type representing optional values of the given type.com.google.common.collect.ImmutableList<FunctionType.Parameter> createParameters(JSType... parameterTypes) Creates a tree hierarchy representing a typed argument list.com.google.common.collect.ImmutableList<FunctionType.Parameter> createParametersWithVarArgs(JSType... parameterTypes) Creates a tree hierarchy representing a typed argument list.createRecordType(Map<String, ? extends JSType> props) createScopeWithTemplates(StaticTypedScope scope, Iterable<TemplateType> templates) Returns a new scope that includes the given template names for type resolution purposes.createTemplateType(String name) createTemplateType(String name, JSType bound) createTemplateTypeWithTransformation(String name, Node expr) createTemplatizedType(ObjectType baseType, com.google.common.collect.ImmutableList<JSType> templatizedTypes) Creates a templatized instance of the specified type.createTemplatizedType(ObjectType baseType, JSType... templatizedTypes) Creates a templatized instance of the specified type.createTemplatizedType(ObjectType baseType, Map<TemplateType, JSType> templatizedTypes) Creates a templatized instance of the specified type.createTypeFromCommentNode(Node n, String sourceName, @Nullable StaticTypedScope scope) Creates a JSType from the nodes representing a type.createUnionType(JSType... variants) Creates a union type whose variants are the arguments.createUnionType(JSTypeNative... variants) Creates a union type whose variants are the built-in types specified by the arguments.createUnionType(List<? extends JSType> variants) booleandeclareType(StaticScope scope, String name, JSType type) Records declared global type names.booleandeclareTypeForExactScope(StaticScope scope, String name, JSType type) Records declared global type names.evaluateTypeExpression(JSTypeExpression expr, StaticTypedScope scope) Returns the template variable for the element type of Arrays.Returns the template variable for the AsyncIterable interface.Returns the template variable for the AsyncIterator interface.getEachReferenceTypeWithProperty(String propertyName) Returns each reference type that has a propertypropertyNamedefined on it.Returns the value template variable for the Generator interface.getGlobalType(String jsTypeName) Returns the value template variable for the IIterableResult interface.Returns the value template variable for the Iterable interface.Returns the value template variable for the IteratorIterable interface.Returns the value template variable for the Iterator interface.getNativeFunctionType(JSTypeNative typeId) getNativeObjectType(JSTypeNative typeId) getNativeType(JSTypeNative typeId) First dereferences the JSType to remove null/undefined then returns a human-readable type nameReturns the template variable for the element type of ReadonlyArrays.Returns the template variable for the key type of ReadonlyMaps.Returns the template variable for the value type of ReadonlyMaps.getType(@Nullable StaticScope scope, String jsTypeName) Looks up a native type by name.getType(StaticTypedScope scope, String jsTypeName, String sourceName, int lineno, int charno) Looks up a type by name.voididentifyNonNullableName(@Nullable StaticScope scope, String name) Identifies the name of a typedef or enum before we actually declare it.booleanisForwardDeclaredType(String name) Whether this is a forward-declared type name.booleanisNonNullableName(StaticScope scope, String name) Identifies the name of a typedef or enum before we actually declare it.@Nullable com.google.common.collect.ImmutableList<TemplateType> maybeGetTemplateTypesOfBuiltin(StaticScope scope, String fnName) Returns an immutable list of template types of the given builtin.voidoverwriteDeclaredType(StaticScope scope, String name, JSType type) Overrides a declared global type name.voidregisterLegacyClosureNamespace(String moduleName) Registers a goog.provide or legacy goog.module namespace with the type registryvoidregisterNonLegacyClosureNamespace(String moduleName, Node definitionNode, JSType type) Registers a goog.module namespace (that does not have goog.module.declareLegacyNamespace)voidregisterPropertyOnType(String propertyName, JSType type) Tells the type system thatownermay have a property namedpropertyName.voidregisterTemplateTypeNamesInScope(Iterable<TemplateType> keys, Node scopeRoot) Registers template types on the given scope root.voidresetImplicitPrototype(JSType type, ObjectType newImplicitProto) Set the implicit prototype if it's possible to do so.@Nullable JSTyperesolveViaClosureNamespace(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.
-
Field Details
-
OBJECT_ELEMENT_TEMPLATE
Deprecated.- See Also:
-
-
Constructor Details
-
JSTypeRegistry
-
JSTypeRegistry
Constructs a new type registry populated with the built-in types.
-
-
Method Details
-
getArrayElementKey
Returns the template variable for the element type of Arrays. -
getReadonlyArrayElementKey
Returns the template variable for the element type of ReadonlyArrays. -
getReadonlyMapKey
Returns the template variable for the key type of ReadonlyMaps. -
getReadonlyMapValue
Returns the template variable for the value type of ReadonlyMaps. -
getObjectElementKey
- Returns:
- The template variable corresponding to the property value type for Javascript Objects and Arrays.
-
getObjectIndexKey
- Returns:
- The template variable corresponding to the property key type of the built-in Javascript object.
-
getIterableValueTemplate
Returns the value template variable for the Iterable interface. -
getIteratorIterableValueTemplate
Returns the value template variable for the IteratorIterable interface. -
getIIterableResultValueTemplate
Returns the value template variable for the IIterableResult interface. -
getIteratorValueTemplate
Returns the value template variable for the Iterator interface. -
getGeneratorValueTemplate
Returns the value template variable for the Generator interface. -
getAsyncIterableValueTemplate
Returns the template variable for the AsyncIterable interface. -
getAsyncIteratorValueTemplate
Returns the template variable for the AsyncIterator interface. -
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
-
resolveViaClosureNamespace
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
Tells the type system thatownermay have a property namedpropertyName. This allows the registry to keep track of what types a property is defined upon.This is NOT the same as saying that
ownermust 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
Returns whether the given property can possibly be set on the given type. -
getEachReferenceTypeWithProperty
Returns each reference type that has a propertypropertyNamedefined 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
Objectand onArray, this method must return[Object, Array]. It would not be correct to collapse them to[Object]. -
declareType
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
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
Overrides a declared global type name. Throws an exception if this type name hasn't been declared yet. -
isForwardDeclaredType
Whether this is a forward-declared type name. -
getReadableTypeName
First dereferences the JSType to remove null/undefined then returns a human-readable type name -
getReadableTypeNameNoDeref
-
getGlobalType
-
getType
Looks up a native type by name.- Parameters:
jsTypeName- The name string.- Returns:
- the corresponding JSType object or
nullit 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
-
getNativeObjectType
-
getNativeFunctionType
-
getResolver
-
evaluateTypeExpressionInGlobalScope
-
createOptionalType
Creates a type representing optional values of the given type.- Returns:
- the union of the type and the void type
-
createNullableType
Creates a type representing nullable values of the given type.- Returns:
- the union of the type and the Null type
-
createOptionalNullableType
Creates a nullable and undefine-able value of the given type.- Returns:
- The union of the type and null and undefined.
-
createUnionType
Creates a union type whose variants are the arguments. -
createUnionType
-
createUnionType
Creates a union type whose variants are the built-in types specified by the arguments. -
createFunctionType
Creates a function type.- Parameters:
returnType- the function's return typeparameterTypes- the parameters' types
-
createFunctionType
- Parameters:
returnType- the function's return type ornullto indicate that the return type is unknown.parameters- the function's parameters ornullto indicate that the parameter types are unknown.
-
createFunctionTypeWithVarArgs
Creates a function type. The last parameter type of the function is considered a variable length argument.- Parameters:
returnType- the function's return typeparameterTypes- the parameters' types
-
createFunctionTypeWithInstanceType
public JSType createFunctionTypeWithInstanceType(ObjectType instanceType, JSType returnType, List<JSType> parameterTypes) Creates a function type in whichthisrefers to an object instance.- Parameters:
instanceType- the type ofthisreturnType- the function's return typeparameterTypes- 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
-
createRecordType
-
createObjectType
Create an object type. -
createAnonymousObjectType
Create an anonymous object type.- Parameters:
info- Used to mark object literals as structs; can benull
-
resetImplicitPrototype
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 ornullto indicate that the function is anonymous.source- the node defining this function. Its type (Node.getToken()()}) must beToken.FUNCTION.parameters- the function's parameters ornullto indicate that the parameter types are unknown.returnType- the function's return type ornullto indicate that the return type is unknown.templateKeys- the templatized types for the class.isAbstract- whether the function type represents an abstract class
-
createTemplateType
-
createTemplateType
-
createTemplateTypeWithTransformation
-
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
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
Identifies the name of a typedef or enum before we actually declare it. -
isNonNullableName
Identifies the name of a typedef or enum before we actually declare it. -
evaluateTypeExpression
-
createTypeFromCommentNode
-
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
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
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
Registers a goog.provide or legacy goog.module namespace with the type registry
-