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);
}
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;
}
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);
}
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);
}
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;
}
Aggregations