Search in sources :

Example 1 with TemplateTypeReplacer

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);
}
Also used : TemplateTypeMap(com.google.javascript.rhino.jstype.TemplateTypeMap) TemplateTypeReplacer(com.google.javascript.rhino.jstype.TemplateTypeReplacer)

Example 2 with TemplateTypeReplacer

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();
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) TemplateTypeReplacer(com.google.javascript.rhino.jstype.TemplateTypeReplacer) Node(com.google.javascript.rhino.Node) FunctionType(com.google.javascript.rhino.jstype.FunctionType) TemplateType(com.google.javascript.rhino.jstype.TemplateType) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

TemplateTypeReplacer (com.google.javascript.rhino.jstype.TemplateTypeReplacer)2 Node (com.google.javascript.rhino.Node)1 FunctionType (com.google.javascript.rhino.jstype.FunctionType)1 JSType (com.google.javascript.rhino.jstype.JSType)1 TemplateType (com.google.javascript.rhino.jstype.TemplateType)1 TemplateTypeMap (com.google.javascript.rhino.jstype.TemplateTypeMap)1 LinkedHashMap (java.util.LinkedHashMap)1