use of com.google.javascript.rhino.jstype.FunctionType.Parameter in project closure-compiler by google.
the class FunctionTypeBuilder method inferParameterTypes.
/**
* Infer the parameter types from the list of parameter names and the JSDoc info.
*/
FunctionTypeBuilder inferParameterTypes(@Nullable Node paramsParent, @Nullable JSDocInfo info) {
if (paramsParent == null) {
if (info == null) {
return this;
} else {
return inferParameterTypes(info);
}
}
// arguments
final Iterator<Parameter> oldParameters;
Parameter oldParameterType = null;
if (parameters != null) {
oldParameters = parameters.iterator();
oldParameterType = oldParameters.hasNext() ? oldParameters.next() : null;
} else {
oldParameters = Collections.emptyIterator();
}
FunctionParamBuilder builder = new FunctionParamBuilder(typeRegistry);
boolean warnedAboutArgList = false;
Set<String> allJsDocParams = (info == null) ? new HashSet<>() : new HashSet<>(info.getParameterNames());
boolean isVarArgs = false;
int paramIndex = 0;
for (Node param = paramsParent.getFirstChild(); param != null; param = param.getNext()) {
boolean isOptionalParam = false;
final Node paramLhs;
if (param.isRest()) {
isVarArgs = true;
paramLhs = param.getOnlyChild();
} else if (param.isDefaultValue()) {
// The first child is the actual positional parameter
paramLhs = checkNotNull(param.getFirstChild(), param);
isOptionalParam = true;
} else {
isVarArgs = isVarArgsParameterByConvention(param);
isOptionalParam = isOptionalParameterByConvention(param);
paramLhs = param;
}
String paramName = null;
if (paramLhs.isName()) {
paramName = paramLhs.getString();
} else {
checkState(paramLhs.isDestructuringPattern());
// third JSDoc parameter.
if (info != null) {
paramName = info.getParameterNameAt(paramIndex);
}
}
allJsDocParams.remove(paramName);
// type from JSDocInfo
JSType parameterType = null;
if (info != null && info.hasParameterType(paramName)) {
JSTypeExpression parameterTypeExpression = info.getParameterType(paramName);
parameterType = parameterTypeExpression.evaluate(templateScope, typeRegistry);
isOptionalParam = isOptionalParam || parameterTypeExpression.isOptionalArg();
isVarArgs = isVarArgs || parameterTypeExpression.isVarArgs();
} else if (paramLhs.getJSDocInfo() != null && paramLhs.getJSDocInfo().hasType()) {
JSTypeExpression parameterTypeExpression = paramLhs.getJSDocInfo().getType();
parameterType = parameterTypeExpression.evaluate(templateScope, typeRegistry);
isOptionalParam = parameterTypeExpression.isOptionalArg();
isVarArgs = parameterTypeExpression.isVarArgs();
} else if (oldParameterType != null && oldParameterType.getJSType() != null) {
parameterType = oldParameterType.getJSType();
isOptionalParam = oldParameterType.isOptional();
isVarArgs = oldParameterType.isVariadic();
} else {
parameterType = typeRegistry.getNativeType(UNKNOWN_TYPE);
}
warnedAboutArgList |= addParameter(builder, parameterType, warnedAboutArgList, isOptionalParam, isVarArgs);
oldParameterType = oldParameters.hasNext() ? oldParameters.next() : null;
paramIndex++;
}
// Copy over any old parameters that aren't in the param list.
if (!isVarArgs) {
while (oldParameterType != null && !isVarArgs) {
builder.newParameterFrom(oldParameterType);
oldParameterType = oldParameters.hasNext() ? oldParameters.next() : null;
}
}
for (String inexistentName : allJsDocParams) {
reportWarning(INEXISTENT_PARAM, inexistentName, formatFnName());
}
parameters = builder.build();
return this;
}
use of com.google.javascript.rhino.jstype.FunctionType.Parameter in project closure-compiler by google.
the class CheckMissingOverrideTypes method recordMissingParamAnnotations.
private void recordMissingParamAnnotations(Node fnNode, JSDocInfo jsDoc, FunctionType fnType, JSDocInfo.Builder builder) {
checkState(fnNode.isFunction(), fnNode);
Set<String> jsDocParamNames = jsDoc.getParameterNames();
List<String> astParamNames = getFunctionParamNamesOrPlaceholder(fnNode);
List<Parameter> fnTypeParams = fnType.getParameters();
for (int paramIndex = 0; paramIndex < astParamNames.size(); paramIndex++) {
String astName = astParamNames.get(paramIndex);
if (jsDocParamNames.contains(astName)) {
continue;
}
// missing annotation for `paramName`
Parameter fnTypeParam = fnTypeParams.get(paramIndex);
JSType paramType = fnTypeParam.getJSType();
if (fnTypeParam.isOptional()) {
paramType = paramType.restrictByNotUndefined();
}
Node paramTypeAst = typeToTypeAst(paramType);
if (fnTypeParam.isOptional()) {
paramTypeAst = new Node(Token.EQUALS, paramTypeAst);
}
builder.recordParameter(astName, new JSTypeExpression(paramTypeAst, JSDOC_FILE_NAME));
}
}
use of com.google.javascript.rhino.jstype.FunctionType.Parameter in project closure-compiler by google.
the class TypeCheck method getObjectLitKeyTypeFromValueType.
/**
* @param key A OBJECTLIT key node.
* @return The type expected when using the key.
*/
static JSType getObjectLitKeyTypeFromValueType(Node key, JSType valueType) {
if (valueType != null) {
switch(key.getToken()) {
case GETTER_DEF:
// GET must always return a function type.
if (valueType.isFunctionType()) {
FunctionType fntype = valueType.toMaybeFunctionType();
valueType = fntype.getReturnType();
} else {
return null;
}
break;
case SETTER_DEF:
if (valueType.isFunctionType()) {
// SET must always return a function type.
FunctionType fntype = valueType.toMaybeFunctionType();
Parameter param = fntype.getParameters().get(0);
// SET function must always have one parameter.
valueType = param.getJSType();
} else {
return null;
}
break;
default:
break;
}
}
return valueType;
}
use of com.google.javascript.rhino.jstype.FunctionType.Parameter in project closure-compiler by google.
the class TypeCheck method visitTaggedTemplateLit.
private void visitTaggedTemplateLit(Node n) {
Node tag = n.getFirstChild();
JSType tagType = tag.getJSType().restrictByNotNullOrUndefined();
if (!tagType.canBeCalled()) {
report(n, NOT_CALLABLE, tagType.toString());
return;
} else if (!tagType.isFunctionType()) {
// functions. Return if we have one of those types that is not actually a known function.
return;
}
FunctionType tagFnType = tagType.toMaybeFunctionType();
Iterator<Parameter> parameters = tagFnType.getParameters().iterator();
// Validate that the tag function takes at least one parameter
if (!parameters.hasNext()) {
report(n, WRONG_ARGUMENT_COUNT, typeRegistry.getReadableTypeNameNoDeref(tag), String.valueOf(NodeUtil.getInvocationArgsCount(n)), "0", " and no more than 0 argument(s)");
return;
}
// Validate that the first parameter is a supertype of ITemplateArray
Parameter firstParameter = parameters.next();
JSType parameterType = firstParameter.getJSType().restrictByNotNullOrUndefined();
if (parameterType != null) {
validator.expectITemplateArraySupertype(tag, parameterType, "Invalid type for the first parameter of tag function");
}
// Validate the remaining parameters (the template literal substitutions)
checkArgumentsMatchParameters(n, tagFnType, NodeUtil.getInvocationArgsAsIterable(n).iterator(), parameters, 1);
}
use of com.google.javascript.rhino.jstype.FunctionType.Parameter in project closure-compiler by google.
the class EqualityChecker method areArrowParameterEqual.
private boolean areArrowParameterEqual(ArrowType left, ArrowType right) {
if (left.getParameterList().size() != right.getParameterList().size()) {
return false;
}
for (int i = 0; i < left.getParameterList().size(); i++) {
Parameter leftParam = left.getParameterList().get(i);
Parameter rightParam = right.getParameterList().get(i);
JSType leftParamType = leftParam.getJSType();
JSType rightParamType = rightParam.getJSType();
if (leftParamType != null) {
// Both parameter lists give a type for this param, it should be equal
if (rightParamType != null && !this.areEqualCaching(leftParamType, rightParamType)) {
return false;
}
} else {
if (rightParamType != null) {
return false;
}
}
// Check var_args/optionality
if (leftParam.isOptional() != rightParam.isOptional()) {
return false;
}
if (leftParam.isVariadic() != rightParam.isVariadic()) {
return false;
}
}
return true;
}
Aggregations