com.google.javascript.rhino.jstype.TemplateType Java Examples
The following examples show how to use
com.google.javascript.rhino.jstype.TemplateType.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TypeInference.java From astor with GNU General Public License v2.0 | 6 votes |
private Map<TemplateType, JSType> inferTemplateTypesFromParameters( FunctionType fnType, Node call) { if (fnType.getTemplateKeys().isEmpty()) { return Collections.emptyMap(); } Map<TemplateType, JSType> resolvedTypes = Maps.newIdentityHashMap(); Node callTarget = call.getFirstChild(); if (NodeUtil.isGet(callTarget)) { Node obj = callTarget.getFirstChild(); maybeResolveTemplatedType( fnType.getTypeOfThis(), getJSType(obj), resolvedTypes); } if (call.hasMoreThanOneChild()) { maybeResolveTemplateTypeFromNodes( fnType.getParameters(), call.getChildAtIndex(1).siblings(), resolvedTypes); } return resolvedTypes; }
Example #2
Source File: Closure_112_TypeInference_t.java From coming with MIT License | 6 votes |
private void maybeResolveTemplateTypeFromNodes( Iterator<Node> declParams, Iterator<Node> callParams, Map<TemplateType, JSType> resolvedTypes) { while (declParams.hasNext() && callParams.hasNext()) { Node declParam = declParams.next(); maybeResolveTemplatedType( getJSType(declParam), getJSType(callParams.next()), resolvedTypes); if (declParam.isVarArgs()) { while (callParams.hasNext()) { maybeResolveTemplatedType( getJSType(declParam), getJSType(callParams.next()), resolvedTypes); } } } }
Example #3
Source File: ClosureTypeRegistry.java From jsinterop-generator with Apache License 2.0 | 6 votes |
private TypeReference createMethodDefiningTypeReferenceFrom(TemplateType templateType) { JSType jsType = jsTypeByThisTemplateType.get(templateType); checkNotNull(jsType, "%s is not used a method receiver.", templateType); TypeReference typeReference = resolveTypeReference(jsType); List<TemplateType> templateKeys = (jsType instanceof ObjectType ? ((ObjectType) jsType).getConstructor() : jsType) .getTemplateTypeMap() .getTemplateKeys(); // Create a ParametrizedTypeReference if the replacing type has template type. The JSType // stored in the map is never a TemplatizedType because it's the type definition not a type // reference. if (templateKeys.isEmpty()) { return typeReference; } else if (jsType.isArrayType()) { checkState(templateKeys.size() == 1, templateKeys); return new ArrayTypeReference(resolveTypeReference(templateKeys.get(0))); } else { return createParametrizedTypeReference(jsType, templateKeys); } }
Example #4
Source File: TypeInference.java From astor with GNU General Public License v2.0 | 6 votes |
private void maybeResolveTemplateTypeFromNodes( Iterator<Node> declParams, Iterator<Node> callParams, Map<TemplateType, JSType> resolvedTypes) { while (declParams.hasNext() && callParams.hasNext()) { Node declParam = declParams.next(); maybeResolveTemplatedType( getJSType(declParam), getJSType(callParams.next()), resolvedTypes); if (declParam.isVarArgs()) { while (callParams.hasNext()) { maybeResolveTemplatedType( getJSType(declParam), getJSType(callParams.next()), resolvedTypes); } } } }
Example #5
Source File: Closure_112_TypeInference_t.java From coming with MIT License | 6 votes |
private Map<TemplateType, JSType> inferTemplateTypesFromParameters( FunctionType fnType, Node call) { if (fnType.getTemplateTypeMap().getTemplateKeys().isEmpty()) { return Collections.emptyMap(); } Map<TemplateType, JSType> resolvedTypes = Maps.newIdentityHashMap(); Node callTarget = call.getFirstChild(); if (NodeUtil.isGet(callTarget)) { Node obj = callTarget.getFirstChild(); maybeResolveTemplatedType( fnType.getTypeOfThis(), getJSType(obj), resolvedTypes); } if (call.hasMoreThanOneChild()) { maybeResolveTemplateTypeFromNodes( fnType.getParameters(), call.getChildAtIndex(1).siblings(), resolvedTypes); } return resolvedTypes; }
Example #6
Source File: Closure_112_TypeInference_s.java From coming with MIT License | 6 votes |
private Map<TemplateType, JSType> inferTemplateTypesFromParameters( FunctionType fnType, Node call) { if (fnType.getTemplateTypeMap().getTemplateKeys().isEmpty()) { return Collections.emptyMap(); } Map<TemplateType, JSType> resolvedTypes = Maps.newIdentityHashMap(); Node callTarget = call.getFirstChild(); if (NodeUtil.isGet(callTarget)) { Node obj = callTarget.getFirstChild(); maybeResolveTemplatedType( fnType.getTypeOfThis(), getJSType(obj), resolvedTypes); } if (call.hasMoreThanOneChild()) { maybeResolveTemplateTypeFromNodes( fnType.getParameters(), call.getChildAtIndex(1).siblings(), resolvedTypes); } return resolvedTypes; }
Example #7
Source File: DeclarationGenerator.java From clutz with MIT License | 6 votes |
/** * Special handling for simple typing returning polymorphic this type in TypeScript. Prefer * `func(): this` instead of `func<T>(this: T): T` when any params are not templatized. */ private boolean shouldEmitThisParam(FunctionType ftype) { final JSType typeOfThis = ftype.getTypeOfThis(); if (typeOfThis == null || !typeOfThis.isTemplateType() || !typeOfThis.equals(ftype.getReturnType())) { return true; } for (Parameter parameter : ftype.getParameters()) { JSType paramType = parameter.getJSType(); if (!paramType.isTemplatizedType()) { continue; } final TemplateTypeMap templateTypeMap = paramType.getTemplateTypeMap(); for (TemplateType key : templateTypeMap.getTemplateKeys()) { if (templateTypeMap.getResolvedTemplateType(key).equals(typeOfThis)) { return true; } } } return false; }
Example #8
Source File: Closure_112_TypeInference_s.java From coming with MIT License | 6 votes |
private void maybeResolveTemplateTypeFromNodes( Iterator<Node> declParams, Iterator<Node> callParams, Map<TemplateType, JSType> resolvedTypes) { while (declParams.hasNext() && callParams.hasNext()) { Node declParam = declParams.next(); maybeResolveTemplatedType( getJSType(declParam), getJSType(callParams.next()), resolvedTypes); if (declParam.isVarArgs()) { while (callParams.hasNext()) { maybeResolveTemplatedType( getJSType(declParam), getJSType(callParams.next()), resolvedTypes); } } } }
Example #9
Source File: TypeInference.java From astor with GNU General Public License v2.0 | 6 votes |
/** * For functions with function(this: T, ...) and T as parameters, type * inference will set the type of this on a function literal argument to the * the actual type of T. */ private boolean inferTemplatedTypesForCall( Node n, FunctionType fnType) { if (fnType.getTemplateKeys().isEmpty()) { return false; } // Try to infer the template types Map<TemplateType, JSType> inferred = inferTemplateTypesFromParameters( fnType, n); // Replace all template types. If we couldn't find a replacement, we // replace it with UNKNOWN. TemplateTypeReplacer replacer = new TemplateTypeReplacer( registry, inferred); Node callTarget = n.getFirstChild(); FunctionType replacementFnType = fnType.visit(replacer) .toMaybeFunctionType(); Preconditions.checkNotNull(replacementFnType); callTarget.setJSType(replacementFnType); n.setJSType(replacementFnType.getReturnType()); return replacer.madeChanges; }
Example #10
Source File: Closure_112_TypeInference_s.java From coming with MIT License | 5 votes |
private void maybeResolveTemplateTypeFromNodes( Iterable<Node> declParams, Iterable<Node> callParams, Map<TemplateType, JSType> resolvedTypes) { maybeResolveTemplateTypeFromNodes( declParams.iterator(), callParams.iterator(), resolvedTypes); }
Example #11
Source File: Closure_112_TypeInference_s.java From coming with MIT License | 5 votes |
/** * For functions with function(this: T, ...) and T as parameters, type * inference will set the type of this on a function literal argument to the * the actual type of T. */ private boolean inferTemplatedTypesForCall( Node n, FunctionType fnType) { final ImmutableList<TemplateType> keys = fnType.getTemplateTypeMap() .getTemplateKeys(); if (keys.isEmpty()) { return false; } // Try to infer the template types Map<TemplateType, JSType> inferred = inferTemplateTypesFromParameters(fnType, n); // Replace all template types. If we couldn't find a replacement, we // replace it with UNKNOWN. TemplateTypeReplacer replacer = new TemplateTypeReplacer( registry, inferred); Node callTarget = n.getFirstChild(); FunctionType replacementFnType = fnType.visit(replacer) .toMaybeFunctionType(); Preconditions.checkNotNull(replacementFnType); callTarget.setJSType(replacementFnType); n.setJSType(replacementFnType.getReturnType()); return replacer.madeChanges; }
Example #12
Source File: Closure_112_TypeInference_s.java From coming with MIT License | 5 votes |
@Override public JSType caseTemplateType(TemplateType type) { madeChanges = true; JSType replacement = replacements.get(type); return replacement != null ? replacement : registry.getNativeType(UNKNOWN_TYPE); }
Example #13
Source File: Closure_112_TypeInference_s.java From coming with MIT License | 5 votes |
private static void resolvedTemplateType( Map<TemplateType, JSType> map, TemplateType template, JSType resolved) { JSType previous = map.get(template); if (!resolved.isUnknownType()) { if (previous == null) { map.put(template, resolved); } else { JSType join = previous.getLeastSupertype(resolved); map.put(template, join); } } }
Example #14
Source File: TypeInference.java From astor with GNU General Public License v2.0 | 5 votes |
private void maybeResolveTemplateTypeFromNodes( Iterable<Node> declParams, Iterable<Node> callParams, Map<TemplateType, JSType> resolvedTypes) { maybeResolveTemplateTypeFromNodes( declParams.iterator(), callParams.iterator(), resolvedTypes); }
Example #15
Source File: Closure_112_TypeInference_t.java From coming with MIT License | 5 votes |
/** * For functions with function(this: T, ...) and T as parameters, type * inference will set the type of this on a function literal argument to the * the actual type of T. */ private boolean inferTemplatedTypesForCall( Node n, FunctionType fnType) { final ImmutableList<TemplateType> keys = fnType.getTemplateTypeMap() .getTemplateKeys(); if (keys.isEmpty()) { return false; } // Try to infer the template types Map<TemplateType, JSType> inferred = Maps.filterKeys( inferTemplateTypesFromParameters(fnType, n), new Predicate<TemplateType>() { @Override public boolean apply(TemplateType key) { return keys.contains(key); }} ); // Replace all template types. If we couldn't find a replacement, we // replace it with UNKNOWN. TemplateTypeReplacer replacer = new TemplateTypeReplacer( registry, inferred); Node callTarget = n.getFirstChild(); FunctionType replacementFnType = fnType.visit(replacer) .toMaybeFunctionType(); Preconditions.checkNotNull(replacementFnType); callTarget.setJSType(replacementFnType); n.setJSType(replacementFnType.getReturnType()); return replacer.madeChanges; }
Example #16
Source File: Closure_112_TypeInference_t.java From coming with MIT License | 5 votes |
@Override public JSType caseTemplateType(TemplateType type) { madeChanges = true; JSType replacement = replacements.get(type); return replacement != null ? replacement : registry.getNativeType(UNKNOWN_TYPE); }
Example #17
Source File: Closure_112_TypeInference_t.java From coming with MIT License | 5 votes |
private void maybeResolveTemplateTypeFromNodes( Iterable<Node> declParams, Iterable<Node> callParams, Map<TemplateType, JSType> resolvedTypes) { maybeResolveTemplateTypeFromNodes( declParams.iterator(), callParams.iterator(), resolvedTypes); }
Example #18
Source File: Closure_112_TypeInference_t.java From coming with MIT License | 5 votes |
private static void resolvedTemplateType( Map<TemplateType, JSType> map, TemplateType template, JSType resolved) { JSType previous = map.get(template); if (!resolved.isUnknownType()) { if (previous == null) { map.put(template, resolved); } else { JSType join = previous.getLeastSupertype(resolved); map.put(template, join); } } }
Example #19
Source File: TypeInference.java From astor with GNU General Public License v2.0 | 5 votes |
private void resolvedTemplateType( Map<TemplateType, JSType> map, TemplateType template, JSType resolved) { JSType previous = map.get(template); if (!resolved.isUnknownType()) { if (previous == null) { map.put(template, resolved); } else { JSType join = previous.getLeastSupertype(resolved); map.put(template, join); } } }
Example #20
Source File: ClosureTypeRegistry.java From jsinterop-generator with Apache License 2.0 | 5 votes |
@Override public TypeReference caseTemplateType(TemplateType templateType) { if (jsTypeByThisTemplateType.containsKey(templateType)) { // The templateType is used in @this annotations in order to allow method chaining. // Replace it by the type where the method is defined. return createMethodDefiningTypeReferenceFrom(templateType); } return new TypeVariableReference(templateType.getReferenceName(), null); }
Example #21
Source File: ThisTemplateTypeVisitor.java From jsinterop-generator with Apache License 2.0 | 5 votes |
@Override protected boolean visitMethod(FunctionType method, boolean isStatic) { Optional<TemplateType> thisTemplateType = getThisTemplateType(method); thisTemplateType.ifPresent( templateType -> getJavaTypeRegistry() .registerThisTemplateType(templateType, currentJsTypeStack.peek())); return true; }
Example #22
Source File: TypeInference.java From astor with GNU General Public License v2.0 | 5 votes |
@Override public JSType caseTemplateType(TemplateType type) { madeChanges = true; JSType replacement = replacements.get(type); return replacement != null ? replacement : registry.getNativeType(UNKNOWN_TYPE); }
Example #23
Source File: TypeRegistry.java From js-dossier with Apache License 2.0 | 5 votes |
private JSType getSuperInstance( ObjectType instance, FunctionType ctor, StaticTypedScope globalScope, JSTypeRegistry jsRegistry) { JSType superInstance; if (ctor.getJSDocInfo() != null && ctor.getJSDocInfo().getBaseType() != null) { List<TemplateType> templateTypes = instance.getTemplateTypeMap().getTemplateKeys(); StaticTypedScope scope = templateTypes.isEmpty() ? globalScope : jsRegistry.createScopeWithTemplates(globalScope, templateTypes); JSTypeExpression baseTypeExpression = ctor.getJSDocInfo().getBaseType(); superInstance = Types.evaluate(baseTypeExpression, scope, jsRegistry); // The type expression will resolve to a named type if it is an aliased reference to // a module's exported type. Compensate by checking dossier's type registry, which // tracks exported types by their exported name (whereas the compiler tracks them by // their initially declared name from within the module). if (superInstance.isNamedType() && isType(superInstance.toMaybeNamedType().getReferenceName())) { superInstance = getType(superInstance.toMaybeNamedType().getReferenceName()).getType(); if (superInstance.isConstructor() || superInstance.isInterface()) { superInstance = superInstance.toMaybeFunctionType().getTypeOfThis(); } } } else { FunctionType superCtor = ctor.getSuperClassConstructor(); if (superCtor == null) { return null; } superInstance = superCtor.getTypeOfThis(); } return superInstance; }
Example #24
Source File: DeclarationGenerator.java From clutz with MIT License | 5 votes |
/** * Emits template types for a given type. For example for <code>T<A,B></code>, this method will * emit <code><A,B></code>. * * @param type the type in question * @param alreadyEmittedTemplateType when visiting methods the class template types will be * reported by closure, but should not be emitted. * @param isDeclaration isDeclaration {@pre true} if this type declares the template types, * {@pre false} if it instantiates a generic type. In the former case, Clutz emits defaults * for the template parameters. */ private void visitTemplateTypes( ObjectType type, List<String> alreadyEmittedTemplateType, boolean isDeclaration) { if (type.hasAnyTemplateTypes() && !type.getTemplateTypeMap().isEmpty()) { List<String> realTemplateType = new ArrayList<>(); for (TemplateType templateType : type.getTemplateTypeMap().getTemplateKeys()) { String displayName = templateType.getDisplayName(); // Some template variables can be already defined at the class definition. // Closure and TypeScript disagree in that case, in closure redeclaring a class template // variable at a method does nothing, but in Typescript it introduces a new variable. // To preserve the semantics from closure we skip emitting redeclared variables. if (alreadyEmittedTemplateType.contains(displayName)) { continue; } if (displayName.contains("IObject#")) { displayName = normalizeIObjectTemplateName(type, displayName); } // Because we emit partial programs, we cannot differentiate whether Foo // is a plain type or a generic type for which closure infers '?' as // all type arguments. // To support this usecase we emit ' = any' for all generic args. if (isDeclaration) { displayName += " = any"; } if (displayName != null) { realTemplateType.add(displayName); } } if (!realTemplateType.isEmpty()) { emit("<"); emit(Joiner.on(" , ").join(realTemplateType)); emit(">"); } } }
Example #25
Source File: Closure_112_TypeInference_s.java From coming with MIT License | 4 votes |
private void maybeResolveTemplatedType( JSType paramType, JSType argType, Map<TemplateType, JSType> resolvedTypes) { if (paramType.isTemplateType()) { // @param {T} resolvedTemplateType( resolvedTypes, paramType.toMaybeTemplateType(), argType); } else if (paramType.isUnionType()) { // @param {Array.<T>|NodeList|Arguments|{length:number}} UnionType unionType = paramType.toMaybeUnionType(); for (JSType alernative : unionType.getAlternates()) { maybeResolveTemplatedType(alernative, argType, resolvedTypes); } } else if (paramType.isFunctionType()) { FunctionType paramFunctionType = paramType.toMaybeFunctionType(); FunctionType argFunctionType = argType .restrictByNotNullOrUndefined() .collapseUnion() .toMaybeFunctionType(); if (argFunctionType != null && argFunctionType.isSubtype(paramType)) { // infer from return type of the function type maybeResolveTemplatedType( paramFunctionType.getTypeOfThis(), argFunctionType.getTypeOfThis(), resolvedTypes); // infer from return type of the function type maybeResolveTemplatedType( paramFunctionType.getReturnType(), argFunctionType.getReturnType(), resolvedTypes); // infer from parameter types of the function type maybeResolveTemplateTypeFromNodes( paramFunctionType.getParameters(), argFunctionType.getParameters(), resolvedTypes); } } else if (paramType.isTemplatizedType()) { // @param {Array.<T>} ObjectType referencedParamType = paramType .toMaybeTemplatizedType() .getReferencedType(); JSType argObjectType = argType .restrictByNotNullOrUndefined() .collapseUnion(); if (argObjectType.isSubtype(referencedParamType)) { // If the argument type is a subtype of the parameter type, resolve any // template types amongst their templatized types. TemplateTypeMap paramTypeMap = paramType.getTemplateTypeMap(); TemplateTypeMap argTypeMap = argObjectType.getTemplateTypeMap(); for (TemplateType key : paramTypeMap.getTemplateKeys()) { maybeResolveTemplatedType( paramTypeMap.getTemplateType(key), argTypeMap.getTemplateType(key), resolvedTypes); } } } }
Example #26
Source File: AbstractNoOpVisitor.java From jsinterop-generator with Apache License 2.0 | 4 votes |
@Override public T caseTemplateType(TemplateType templateType) { return null; }
Example #27
Source File: Closure_112_TypeInference_t.java From coming with MIT License | 4 votes |
TemplateTypeReplacer( JSTypeRegistry registry, Map<TemplateType, JSType> replacements) { super(registry); this.registry = registry; this.replacements = replacements; }
Example #28
Source File: TypeCollectionPass.java From js-dossier with Apache License 2.0 | 4 votes |
@Override public Object caseTemplateType(TemplateType templateType) { return null; }
Example #29
Source File: ChainableReverseAbstractInterpreter.java From astor with GNU General Public License v2.0 | 4 votes |
@Override public JSType caseTemplateType(TemplateType templateType) { return caseObjectType(templateType); }
Example #30
Source File: TypeInference.java From astor with GNU General Public License v2.0 | 4 votes |
private void maybeResolveTemplatedType( JSType paramType, JSType argType, Map<TemplateType, JSType> resolvedTypes) { if (paramType.isTemplateType()) { // @param {T} resolvedTemplateType( resolvedTypes, paramType.toMaybeTemplateType(), argType); } else if (paramType.isUnionType()) { // @param {Array.<T>|NodeList|Arguments|{length:number}} UnionType unionType = paramType.toMaybeUnionType(); for (JSType alernative : unionType.getAlternates()) { maybeResolveTemplatedType(alernative, argType, resolvedTypes); } } else if (paramType.isFunctionType()) { FunctionType paramFunctionType = paramType.toMaybeFunctionType(); FunctionType argFunctionType = argType .restrictByNotNullOrUndefined() .collapseUnion() .toMaybeFunctionType(); if (argFunctionType != null && argFunctionType.isSubtype(paramType)) { // infer from return type of the function type maybeResolveTemplatedType( paramFunctionType.getTypeOfThis(), argFunctionType.getTypeOfThis(), resolvedTypes); // infer from return type of the function type maybeResolveTemplatedType( paramFunctionType.getReturnType(), argFunctionType.getReturnType(), resolvedTypes); // infer from parameter types of the function type maybeResolveTemplateTypeFromNodes( paramFunctionType.getParameters(), argFunctionType.getParameters(), resolvedTypes); } } else if (paramType.isParameterizedType()) { ParameterizedType paramObjectType = paramType.toMaybeParameterizedType(); JSType typeParameter = paramObjectType.getParameterType(); Preconditions.checkNotNull(typeParameter); if (typeParameter != null) { // @param {Array.<T>} ObjectType argObjectType = argType .restrictByNotNullOrUndefined() .collapseUnion() .toMaybeParameterizedType(); if (argObjectType != null && argObjectType.isSubtype(paramType)) { JSType argTypeParameter = argObjectType.getParameterType(); Preconditions.checkNotNull(argTypeParameter); maybeResolveTemplatedType( typeParameter, argTypeParameter, resolvedTypes); } } } }