Class JSTypeRegistry
- java.lang.Object
-
- com.google.javascript.rhino.jstype.JSTypeRegistry
-
public final class JSTypeRegistry extends java.lang.ObjectThe type registry is used to resolve named types.This class is not thread-safe.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classJSTypeRegistry.PropDefinitionKindA tristate value returned from canPropertyBeDefined.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringOBJECT_ELEMENT_TEMPLATEDeprecated.
-
Constructor Summary
Constructors Constructor Description JSTypeRegistry(ErrorReporter reporter)JSTypeRegistry(ErrorReporter reporter, java.util.Set<java.lang.String> forwardDeclaredTypes)Constructs a new type registry populated with the built-in types.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JSTypebuildRecordTypeFromObject(ObjectType objType)JSTypeRegistry.PropDefinitionKindcanPropertyBeDefined(JSType type, java.lang.String propertyName)Returns whether the given property can possibly be set on the given type.ObjectTypecreateAnonymousObjectType(@Nullable JSDocInfo info)Create an anonymous object type.FunctionTypecreateConstructorType(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.FunctionTypecreateFunctionType(JSType returnType, JSType... parameterTypes)Creates a function type.FunctionTypecreateFunctionType(JSType returnType, java.util.List<FunctionType.Parameter> parameters)JSTypecreateFunctionTypeWithInstanceType(ObjectType instanceType, JSType returnType, java.util.List<JSType> parameterTypes)Creates a function type in whichthisrefers to an object instance.FunctionTypecreateFunctionTypeWithNewReturnType(FunctionType existingFunctionType, JSType returnType)Creates a new function type based on an existing function type but with a new return type.FunctionTypecreateFunctionTypeWithVarArgs(JSType returnType, JSType... parameterTypes)Creates a function type.NamedTypecreateNamedType(StaticTypedScope scope, java.lang.String reference, java.lang.String sourceName, int lineno, int charno)Creates a named type.JSTypecreateNullableType(JSType type)Creates a type representing nullable values of the given type.ObjectTypecreateObjectType(java.lang.String name, ObjectType implicitPrototype)Create an object type.JSTypecreateOptionalNullableType(JSType 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.JSTypecreateOptionalType(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.JSTypecreateRecordType(java.util.Map<java.lang.String,? extends JSType> props)StaticTypedScopecreateScopeWithTemplates(StaticTypedScope scope, java.lang.Iterable<TemplateType> templates)Returns a new scope that includes the given template names for type resolution purposes.TemplateTypecreateTemplateType(java.lang.String name)TemplateTypecreateTemplateType(java.lang.String name, JSType bound)TemplateTypecreateTemplateTypeWithTransformation(java.lang.String name, Node expr)TemplatizedTypecreateTemplatizedType(ObjectType baseType, com.google.common.collect.ImmutableList<JSType> templatizedTypes)Creates a templatized instance of the specified type.TemplatizedTypecreateTemplatizedType(ObjectType baseType, JSType... templatizedTypes)Creates a templatized instance of the specified type.TemplatizedTypecreateTemplatizedType(ObjectType baseType, java.util.Map<TemplateType,JSType> templatizedTypes)Creates a templatized instance of the specified type.JSTypecreateTypeFromCommentNode(Node n)JSTypecreateTypeFromCommentNode(Node n, java.lang.String sourceName, @Nullable StaticTypedScope scope)Creates a JSType from the nodes representing a type.JSTypecreateUnionType(JSType... variants)Creates a union type whose variants are the arguments.JSTypecreateUnionType(JSTypeNative... variants)Creates a union type whose variants are the built-in types specified by the arguments.JSTypecreateUnionType(java.util.List<? extends JSType> variants)booleandeclareType(StaticScope scope, java.lang.String name, JSType type)Records declared global type names.booleandeclareTypeForExactScope(StaticScope scope, java.lang.String name, JSType type)Records declared global type names.JSTypeevaluateTypeExpression(JSTypeExpression expr, StaticTypedScope scope)JSTypeevaluateTypeExpressionInGlobalScope(JSTypeExpression expr)TemplateTypegetArrayElementKey()Returns the template variable for the element type of Arrays.TemplateTypegetAsyncIterableTemplate()Returns the template variable for the AsyncIterable interface.TemplateTypegetAsyncIteratorValueTemplate()Returns the template variable for the AsyncIterator interface.java.lang.Iterable<ObjectType>getEachReferenceTypeWithProperty(java.lang.String propertyName)Returns each reference type that has a propertypropertyNamedefined on it.TemplateTypeMapgetEmptyTemplateTypeMap()ErrorReportergetErrorReporter()TemplateTypegetGeneratorValueTemplate()Return the value template variable for the Generator interface.JSTypegetGlobalType(java.lang.String jsTypeName)TemplateTypegetIIterableResultTemplateKey()TemplateTypegetIterableTemplate()TemplateTypegetIteratorIterableTemplateKey()TemplateTypegetIteratorValueTemplate()Return the value template variable for the Iterator interface.TemplateTypegetIThenableTemplate()FunctionTypegetNativeFunctionType(JSTypeNative typeId)ObjectTypegetNativeObjectType(JSTypeNative typeId)JSTypegetNativeType(JSTypeNative typeId)TemplateTypegetObjectElementKey()TemplateTypegetObjectIndexKey()java.lang.StringgetReadableTypeName(Node n)First dereferences the JSType to remove null/undefined then returns a human-readable type namejava.lang.StringgetReadableTypeNameNoDeref(Node n)TemplateTypegetReadonlyArrayElementKey()Returns the template variable for the element type of ReadonlyArrays.JSTypeResolvergetResolver()JSTypegetType(@Nullable StaticScope scope, java.lang.String jsTypeName)Looks up a native type by name.JSTypegetType(StaticTypedScope scope, java.lang.String jsTypeName, java.lang.String sourceName, int lineno, int charno)Looks up a type by name.voididentifyNonNullableName(@Nullable StaticScope scope, java.lang.String name)Identifies the name of a typedef or enum before we actually declare it.booleanisForwardDeclaredType(java.lang.String name)Whether this is a forward-declared type name.booleanisNonNullableName(StaticScope scope, java.lang.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, java.lang.String fnName)Returns an immutable list of template types of the given builtin.voidoverwriteDeclaredType(StaticScope scope, java.lang.String name, JSType type)Overrides a declared global type name.voidregisterLegacyClosureNamespace(java.lang.String moduleName)Registers a goog.provide or legacy goog.module namespace with the type registryvoidregisterNonLegacyClosureNamespace(java.lang.String moduleName, Node definitionNode, JSType type)Registers a goog.module namespace (that does not have goog.module.declareLegacyNamespace)voidregisterPropertyOnType(java.lang.String propertyName, JSType type)Tells the type system thatownermay have a property namedpropertyName.voidregisterTemplateTypeNamesInScope(java.lang.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(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.
-
-
-
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.
-
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.
-
getErrorReporter
public ErrorReporter getErrorReporter()
-
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 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
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 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
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
nullit 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.
-
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(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 typeparameterTypes- the parameters' types
-
createFunctionType
public FunctionType createFunctionType(JSType returnType, java.util.List<FunctionType.Parameter> parameters)
- Parameters:
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.
-
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 typeparameterTypes- the parameters' types
-
createFunctionTypeWithInstanceType
public JSType createFunctionTypeWithInstanceType(ObjectType instanceType, JSType returnType, java.util.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
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 benull
-
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 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
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.
-
evaluateTypeExpression
public JSType evaluateTypeExpression(JSTypeExpression expr, StaticTypedScope scope)
-
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
-
-