Search in sources :

Example 1 with TemplateType

use of com.google.javascript.rhino.jstype.TemplateType in project closure-compiler by google.

the class RewriteDynamicImports method injectWrappingFunctionExtern.

/**
 * For a given module, return a reference to the module namespace export
 */
private void injectWrappingFunctionExtern() {
    JSTypeRegistry registry = compiler.getTypeRegistry();
    TemplateType templateT = registry.createTemplateType("T");
    final Node wrappingFunctionDefinition = astFactory.createFunction(DYNAMIC_IMPORT_CALLBACK_FN, astFactory.createParamList("importCallback"), astFactory.createBlock(), type(registry.createFunctionType(templateT, templateT)));
    Node externsRoot = compiler.getSynthesizedExternsInput().getAstRoot(compiler);
    wrappingFunctionDefinition.srcrefTree(externsRoot);
    externsRoot.addChildToBack(wrappingFunctionDefinition);
    compiler.reportChangeToEnclosingScope(wrappingFunctionDefinition);
}
Also used : JSTypeRegistry(com.google.javascript.rhino.jstype.JSTypeRegistry) Node(com.google.javascript.rhino.Node) TemplateType(com.google.javascript.rhino.jstype.TemplateType)

Example 2 with TemplateType

use of com.google.javascript.rhino.jstype.TemplateType in project closure-compiler by google.

the class FunctionTypeBuilder method buildTemplateTypesFromJSDocInfo.

private ImmutableList<TemplateType> buildTemplateTypesFromJSDocInfo(JSDocInfo info, boolean allowTypeTransformations) {
    ImmutableMap<String, JSTypeExpression> infoTypeKeys = info.getTemplateTypes();
    ImmutableMap<String, Node> infoTypeTransformations = info.getTypeTransformations();
    if (infoTypeKeys.isEmpty() && infoTypeTransformations.isEmpty()) {
        return ImmutableList.of();
    }
    // Temporarily bootstrap the template environment with unbound (unknown bound) template types
    List<TemplateType> unboundedTemplates = new ArrayList<>();
    for (String templateKey : infoTypeKeys.keySet()) {
        unboundedTemplates.add(typeRegistry.createTemplateType(templateKey));
    }
    this.templateScope = typeRegistry.createScopeWithTemplates(templateScope, unboundedTemplates);
    // Evaluate template type bounds with bootstrapped environment and reroute the bounds to these
    ImmutableList.Builder<TemplateType> templates = ImmutableList.builder();
    Map<TemplateType, JSType> templatesToBounds = new LinkedHashMap<>();
    for (Map.Entry<String, JSTypeExpression> entry : infoTypeKeys.entrySet()) {
        JSTypeExpression expr = entry.getValue();
        JSType typeBound = typeRegistry.evaluateTypeExpression(entry.getValue(), templateScope);
        // treatment in the future, since "unknown" is currently used as a proxy for "implicit".
        if (expr.isExplicitUnknownTemplateBound()) {
            reportError(TEMPLATE_TYPE_ILLEGAL_BOUND, String.valueOf(typeBound), entry.getKey());
        }
        TemplateType template = typeRegistry.getType(templateScope, entry.getKey()).toMaybeTemplateType();
        if (template != null) {
            templatesToBounds.put(template, typeBound);
        } else {
            templatesToBounds.put(typeRegistry.createTemplateType(entry.getKey(), typeBound), typeBound);
        }
    }
    for (Map.Entry<TemplateType, JSType> entry : templatesToBounds.entrySet()) {
        TemplateType template = entry.getKey();
        JSType bound = entry.getValue();
        template.setBound(bound);
        templates.add(template);
    }
    for (Map.Entry<String, Node> entry : infoTypeTransformations.entrySet()) {
        if (allowTypeTransformations) {
            templates.add(typeRegistry.createTemplateTypeWithTransformation(entry.getKey(), entry.getValue()));
        } else {
            reportWarning(TEMPLATE_TRANSFORMATION_ON_CLASS, entry.getKey());
        }
    }
    ImmutableList<TemplateType> builtTemplates = templates.build();
    for (TemplateType template : builtTemplates) {
        if (template.containsCycle()) {
            reportError(RhinoErrorReporter.PARSE_ERROR, "Cycle detected in inheritance chain of type " + template.getReferenceName());
        }
    }
    return builtTemplates;
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) ImmutableList(com.google.common.collect.ImmutableList) Node(com.google.javascript.rhino.Node) ArrayList(java.util.ArrayList) TemplateType(com.google.javascript.rhino.jstype.TemplateType) LinkedHashMap(java.util.LinkedHashMap) JSTypeExpression(com.google.javascript.rhino.JSTypeExpression) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 3 with TemplateType

use of com.google.javascript.rhino.jstype.TemplateType in project closure-compiler by google.

the class TypeInferenceTest method includeAssertInstanceof.

/**
 * Adds goog.asserts.assertInstanceof to the scope, to do fine-grained assertion testing
 */
private void includeAssertInstanceof() {
    String fullName = "goog.asserts.assertInstanceof";
    TemplateType templateType = registry.createTemplateType("T");
    // Create the function type `function(new:T)`
    FunctionType templateTypeCtor = FunctionType.builder(registry).forConstructor().withTypeOfThis(templateType).build();
    // Create the function type `function(?, function(new:T)): T`
    // This matches the JSDoc for goog.asserts.assertInstanceof:
    // /**
    // * @param {?} value The value to check
    // * @param {function(new:T)) type A user-defined ctor
    // * @return {T}
    // * @template T
    // */
    FunctionType fnType = FunctionType.builder(registry).withParameters(registry.createParameters(getNativeType(UNKNOWN_TYPE), templateTypeCtor)).withTemplateKeys(templateType).withReturnType(templateType).withName(fullName).build();
    assuming(fullName, fnType);
}
Also used : FunctionType(com.google.javascript.rhino.jstype.FunctionType) TemplateType(com.google.javascript.rhino.jstype.TemplateType)

Example 4 with TemplateType

use of com.google.javascript.rhino.jstype.TemplateType in project closure-compiler by google.

the class TypeInferenceTest method includePrimitiveTruthyAssertionFunction.

/**
 * Adds a function with {@link ClosurePrimitive#ASSERTS_TRUTHY} and the given name
 */
private void includePrimitiveTruthyAssertionFunction(String fnName) {
    TemplateType t = registry.createTemplateType("T");
    FunctionType assertType = FunctionType.builder(registry).withName(fnName).withClosurePrimitiveId(ClosurePrimitive.ASSERTS_TRUTHY).withReturnType(t).withParameters(registry.createParameters(t)).withTemplateKeys(t).build();
    assuming(fnName, assertType);
}
Also used : FunctionType(com.google.javascript.rhino.jstype.FunctionType) TemplateType(com.google.javascript.rhino.jstype.TemplateType)

Example 5 with TemplateType

use of com.google.javascript.rhino.jstype.TemplateType in project closure-compiler by google.

the class TypeInference method evaluateTypeTransformations.

/**
 * This function will evaluate the type transformations associated to the template types
 */
private Map<TemplateType, JSType> evaluateTypeTransformations(ImmutableList<TemplateType> templateTypes, Map<TemplateType, JSType> inferredTypes, FlowScope scope) {
    Map<String, JSType> typeVars = null;
    Map<TemplateType, JSType> result = null;
    TypeTransformation ttlObj = null;
    for (TemplateType type : templateTypes) {
        if (type.isTypeTransformation()) {
            // Lazy initialization when the first type transformation is found
            if (ttlObj == null) {
                ttlObj = new TypeTransformation(compiler, scope.getDeclarationScope());
                typeVars = buildTypeVariables(inferredTypes);
                result = new LinkedHashMap<>();
            }
            // Evaluate the type transformation expression using the current
            // known types for the template type variables
            JSType transformedType = ttlObj.eval(type.getTypeTransformation(), ImmutableMap.copyOf(typeVars));
            result.put(type, transformedType);
            // Add the transformed type to the type variables
            typeVars.put(type.getReferenceName(), transformedType);
        }
    }
    return result;
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) TemplateType(com.google.javascript.rhino.jstype.TemplateType)

Aggregations

TemplateType (com.google.javascript.rhino.jstype.TemplateType)17 JSType (com.google.javascript.rhino.jstype.JSType)13 FunctionType (com.google.javascript.rhino.jstype.FunctionType)8 Node (com.google.javascript.rhino.Node)7 ObjectType (com.google.javascript.rhino.jstype.ObjectType)3 TemplateTypeMap (com.google.javascript.rhino.jstype.TemplateTypeMap)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 JSTypeExpression (com.google.javascript.rhino.JSTypeExpression)2 TemplatizedType (com.google.javascript.rhino.jstype.TemplatizedType)2 UnionType (com.google.javascript.rhino.jstype.UnionType)2 ArrayList (java.util.ArrayList)2 LinkedHashMap (java.util.LinkedHashMap)2 Set (java.util.Set)2 FlowScope (com.google.javascript.jscomp.type.FlowScope)1 BooleanLiteralSet (com.google.javascript.rhino.jstype.BooleanLiteralSet)1 JSTypeRegistry (com.google.javascript.rhino.jstype.JSTypeRegistry)1 TemplateTypeReplacer (com.google.javascript.rhino.jstype.TemplateTypeReplacer)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1