use of com.google.javascript.rhino.jstype.TemplateTypeReplacer in project closure-compiler by google.
the class AstFactory method replaceTemplate.
private JSType replaceTemplate(JSType templatedType, ImmutableList<JSType> templateTypes) {
TemplateTypeMap typeMap = registry.getEmptyTemplateTypeMap().copyWithExtension(templatedType.getTemplateTypeMap().getTemplateKeys(), templateTypes);
TemplateTypeReplacer replacer = TemplateTypeReplacer.forPartialReplacement(registry, typeMap);
return templatedType.visit(replacer);
}
use of com.google.javascript.rhino.jstype.TemplateTypeReplacer in project closure-compiler by google.
the class TypeInference method inferTemplatedTypesForCall.
/**
* For functions that use template types, specialize the function type for the call target based
* on the call-site specific arguments. Specifically, this enables inference to set the type of
* any function literal parameters based on these inferred types.
*/
private boolean inferTemplatedTypesForCall(Node n, FunctionType fnType, FlowScope scope) {
ImmutableList<TemplateType> keys = fnType.getTemplateTypeMap().getTemplateKeys();
if (keys.isEmpty()) {
return false;
}
// Try to infer the template types
Map<TemplateType, JSType> bindings = new InvocationTemplateTypeMatcher(this.registry, fnType, scope.getTypeOfThis(), n).match();
Map<TemplateType, JSType> inferred = new LinkedHashMap<>();
for (TemplateType key : keys) {
inferred.put(key, bindings.getOrDefault(key, unknownType));
}
// If the inferred type doesn't satisfy the template bound, swap to using the bound. This
// ensures errors will be reported in type-checking.
inferred.replaceAll((k, v) -> v.isSubtypeOf(k.getBound()) ? v : k.getBound());
// Try to infer the template types using the type transformations
Map<TemplateType, JSType> typeTransformations = evaluateTypeTransformations(keys, inferred, scope);
if (typeTransformations != null) {
inferred.putAll(typeTransformations);
}
// Replace all template types. If we couldn't find a replacement, we
// replace it with UNKNOWN.
TemplateTypeReplacer replacer = TemplateTypeReplacer.forInference(registry, inferred);
Node callTarget = n.getFirstChild();
FunctionType replacementFnType = fnType.visit(replacer).toMaybeFunctionType();
checkNotNull(replacementFnType);
callTarget.setJSType(replacementFnType);
n.setJSType(replacementFnType.getReturnType());
return replacer.hasMadeReplacement();
}
Aggregations