Java Code Examples for com.google.javascript.rhino.Node#isVarArgs()

The following examples show how to use com.google.javascript.rhino.Node#isVarArgs() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Closure_112_TypeInference_t.java    From coming with MIT License 6 votes vote down vote up
private void maybeResolveTemplateTypeFromNodes(
    Iterator<Node> declParams,
    Iterator<Node> callParams,
    Map<TemplateType, JSType> resolvedTypes) {
  while (declParams.hasNext() && callParams.hasNext()) {
    Node declParam = declParams.next();
    maybeResolveTemplatedType(
        getJSType(declParam),
        getJSType(callParams.next()),
        resolvedTypes);
    if (declParam.isVarArgs()) {
      while (callParams.hasNext()) {
        maybeResolveTemplatedType(
            getJSType(declParam),
            getJSType(callParams.next()),
            resolvedTypes);
      }
    }
  }
}
 
Example 2
Source File: TypeInference.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void maybeResolveTemplateTypeFromNodes(
    Iterator<Node> declParams,
    Iterator<Node> callParams,
    Map<TemplateType, JSType> resolvedTypes) {
  while (declParams.hasNext() && callParams.hasNext()) {
    Node declParam = declParams.next();
    maybeResolveTemplatedType(
        getJSType(declParam),
        getJSType(callParams.next()),
        resolvedTypes);
    if (declParam.isVarArgs()) {
      while (callParams.hasNext()) {
        maybeResolveTemplatedType(
            getJSType(declParam),
            getJSType(callParams.next()),
            resolvedTypes);
      }
    }
  }
}
 
Example 3
Source File: Closure_90_FunctionType_s.java    From coming with MIT License 5 votes vote down vote up
/** Gets the minimum number of arguments that this function requires. */
public int getMinArguments() {
  // NOTE(nicksantos): There are some native functions that have optional
  // parameters before required parameters. This algorithm finds the position
  // of the last required parameter.
  int i = 0;
  int min = 0;
  for (Node n : getParameters()) {
    i++;
    if (!n.isOptionalArg() && !n.isVarArgs()) {
      min = i;
    }
  }
  return min;
}
 
Example 4
Source File: FunctionType.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Notice that "call" and "bind" have the same argument signature,
 * except that all the arguments of "bind" (except the first)
 * are optional.
 */
private FunctionType getCallOrBindSignature(boolean isCall) {
  boolean isBind = !isCall;
  FunctionBuilder builder = new FunctionBuilder(registry)
      .withReturnType(isCall ? getReturnType() : getBindReturnType(-1))
      .withTemplateKeys(getTemplateKeys());

  Node origParams = getParametersNode();
  if (origParams != null) {
    Node params = origParams.cloneTree();

    Node thisTypeNode = Node.newString(Token.NAME, "thisType");
    thisTypeNode.setJSType(
        registry.createOptionalNullableType(getTypeOfThis()));
    params.addChildToFront(thisTypeNode);

    if (isBind) {
      // The arguments of bind() are unique in that they are all
      // optional but not undefinable.
      for (Node current = thisTypeNode.getNext();
           current != null; current = current.getNext()) {
        current.setOptionalArg(true);
      }
    } else if (isCall) {
      // The first argument of call() is optional iff all the arguments
      // are optional. It's sufficient to check the first argument.
      Node firstArg = thisTypeNode.getNext();
      if (firstArg == null
          || firstArg.isOptionalArg()
          || firstArg.isVarArgs()) {
        thisTypeNode.setOptionalArg(true);
      }
    }

    builder.withParamsNode(params);
  }

  return builder.build();
}
 
Example 5
Source File: Closure_66_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits the parameters of a CALL or a NEW node.
 */
private void visitParameterList(NodeTraversal t, Node call,
    FunctionType functionType) {
  Iterator<Node> arguments = call.children().iterator();
  arguments.next(); // skip the function name

  Iterator<Node> parameters = functionType.getParameters().iterator();
  int ordinal = 0;
  Node parameter = null;
  Node argument = null;
  while (arguments.hasNext() &&
         (parameters.hasNext() ||
          parameter != null && parameter.isVarArgs())) {
    // If there are no parameters left in the list, then the while loop
    // above implies that this must be a var_args function.
    if (parameters.hasNext()) {
      parameter = parameters.next();
    }
    argument = arguments.next();
    ordinal++;

    validator.expectArgumentMatchesParameter(t, argument,
        getJSType(argument), getJSType(parameter), call, ordinal);
  }

  int numArgs = call.getChildCount() - 1;
  int minArgs = functionType.getMinArguments();
  int maxArgs = functionType.getMaxArguments();
  if (minArgs > numArgs || maxArgs < numArgs) {
    report(t, call, WRONG_ARGUMENT_COUNT,
            validator.getReadableJSTypeName(call.getFirstChild(), false),
            String.valueOf(numArgs), String.valueOf(minArgs),
            maxArgs != Integer.MAX_VALUE ?
            " and no more than " + maxArgs + " argument(s)" : "");
  }
}
 
Example 6
Source File: Closure_54_FunctionType_t.java    From coming with MIT License 5 votes vote down vote up
/** Gets the minimum number of arguments that this function requires. */
public int getMinArguments() {
  // NOTE(nicksantos): There are some native functions that have optional
  // parameters before required parameters. This algorithm finds the position
  // of the last required parameter.
  int i = 0;
  int min = 0;
  for (Node n : getParameters()) {
    i++;
    if (!n.isOptionalArg() && !n.isVarArgs()) {
      min = i;
    }
  }
  return min;
}
 
Example 7
Source File: Closure_125_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits the parameters of a CALL or a NEW node.
 */
private void visitParameterList(NodeTraversal t, Node call,
    FunctionType functionType) {
  Iterator<Node> arguments = call.children().iterator();
  arguments.next(); // skip the function name

  Iterator<Node> parameters = functionType.getParameters().iterator();
  int ordinal = 0;
  Node parameter = null;
  Node argument = null;
  while (arguments.hasNext() &&
         (parameters.hasNext() ||
          parameter != null && parameter.isVarArgs())) {
    // If there are no parameters left in the list, then the while loop
    // above implies that this must be a var_args function.
    if (parameters.hasNext()) {
      parameter = parameters.next();
    }
    argument = arguments.next();
    ordinal++;

    validator.expectArgumentMatchesParameter(t, argument,
        getJSType(argument), getJSType(parameter), call, ordinal);
  }

  int numArgs = call.getChildCount() - 1;
  int minArgs = functionType.getMinArguments();
  int maxArgs = functionType.getMaxArguments();
  if (minArgs > numArgs || maxArgs < numArgs) {
    report(t, call, WRONG_ARGUMENT_COUNT,
            validator.getReadableJSTypeName(call.getFirstChild(), false),
            String.valueOf(numArgs), String.valueOf(minArgs),
            maxArgs != Integer.MAX_VALUE ?
            " and no more than " + maxArgs + " argument(s)" : "");
  }
}
 
Example 8
Source File: FunctionParamBuilder.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Copies the parameter specification from the given node,
 * but makes sure it's optional.
 */
public Node newOptionalParameterFromNode(Node n) {
  Node newParam = newParameterFromNode(n);
  if (!newParam.isVarArgs() && !newParam.isOptionalArg()) {
    newParam.setOptionalArg(true);
  }
  return newParam;
}
 
Example 9
Source File: ArrowType.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return True if our parameter spec is equal to {@code that}'s parameter
 *     spec.
 */
boolean hasEqualParameters(ArrowType that, EquivalenceMethod eqMethod) {
  Node thisParam = parameters.getFirstChild();
  Node otherParam = that.parameters.getFirstChild();
  while (thisParam != null && otherParam != null) {
    JSType thisParamType = thisParam.getJSType();
    JSType otherParamType = otherParam.getJSType();
    if (thisParamType != null) {
      // Both parameter lists give a type for this param, it should be equal
      if (otherParamType != null &&
          !thisParamType.checkEquivalenceHelper(
              otherParamType, eqMethod)) {
        return false;
      }
    } else {
      if (otherParamType != null) {
        return false;
      }
    }

    // Check var_args/optionality
    if (thisParam.isOptionalArg() != otherParam.isOptionalArg()) {
      return false;
    }

    if (thisParam.isVarArgs() != otherParam.isVarArgs()) {
      return false;
    }

    thisParam = thisParam.getNext();
    otherParam = otherParam.getNext();
  }
  // One of the parameters is null, so the types are only equal if both
  // parameter lists are null (they are equal).
  return thisParam == otherParam;
}
 
Example 10
Source File: Nopol2017_0051_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits the parameters of a CALL or a NEW node.
 */
private void visitParameterList(NodeTraversal t, Node call,
    FunctionType functionType) {
  Iterator<Node> arguments = call.children().iterator();
  arguments.next(); // skip the function name

  Iterator<Node> parameters = functionType.getParameters().iterator();
  int ordinal = 0;
  Node parameter = null;
  Node argument = null;
  while (arguments.hasNext() &&
         (parameters.hasNext() ||
          parameter != null && parameter.isVarArgs())) {
    // If there are no parameters left in the list, then the while loop
    // above implies that this must be a var_args function.
    if (parameters.hasNext()) {
      parameter = parameters.next();
    }
    argument = arguments.next();
    ordinal++;

    validator.expectArgumentMatchesParameter(t, argument,
        getJSType(argument), getJSType(parameter), call, ordinal);
  }

  int numArgs = call.getChildCount() - 1;
  int minArgs = functionType.getMinArguments();
  int maxArgs = functionType.getMaxArguments();
  if (minArgs > numArgs || maxArgs < numArgs) {
    report(t, call, WRONG_ARGUMENT_COUNT,
            validator.getReadableJSTypeName(call.getFirstChild(), false),
            String.valueOf(numArgs), String.valueOf(minArgs),
            maxArgs != Integer.MAX_VALUE ?
            " and no more than " + maxArgs + " argument(s)" : "");
  }
}
 
Example 11
Source File: TypeCheck.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Visits the parameters of a CALL or a NEW node.
 */
private void visitParameterList(NodeTraversal t, Node call,
    FunctionType functionType) {
  Iterator<Node> arguments = call.children().iterator();
  arguments.next(); // skip the function name

  Iterator<Node> parameters = functionType.getParameters().iterator();
  int ordinal = 0;
  Node parameter = null;
  Node argument = null;
  while (arguments.hasNext() &&
         (parameters.hasNext() ||
          parameter != null && parameter.isVarArgs())) {
    // If there are no parameters left in the list, then the while loop
    // above implies that this must be a var_args function.
    if (parameters.hasNext()) {
      parameter = parameters.next();
    }
    argument = arguments.next();
    ordinal++;

    validator.expectArgumentMatchesParameter(t, argument,
        getJSType(argument), getJSType(parameter), call, ordinal);
  }

  int numArgs = call.getChildCount() - 1;
  int minArgs = functionType.getMinArguments();
  int maxArgs = functionType.getMaxArguments();
  if (minArgs > numArgs || maxArgs < numArgs) {
    report(t, call, WRONG_ARGUMENT_COUNT,
            validator.getReadableJSTypeName(call.getFirstChild(), false),
            String.valueOf(numArgs), String.valueOf(minArgs),
            maxArgs != Integer.MAX_VALUE ?
            " and no more than " + maxArgs + " argument(s)" : "");
  }
}
 
Example 12
Source File: Closure_69_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits the parameters of a CALL or a NEW node.
 */
private void visitParameterList(NodeTraversal t, Node call,
    FunctionType functionType) {
  Iterator<Node> arguments = call.children().iterator();
  arguments.next(); // skip the function name

  Iterator<Node> parameters = functionType.getParameters().iterator();
  int ordinal = 0;
  Node parameter = null;
  Node argument = null;
  while (arguments.hasNext() &&
         (parameters.hasNext() ||
          parameter != null && parameter.isVarArgs())) {
    // If there are no parameters left in the list, then the while loop
    // above implies that this must be a var_args function.
    if (parameters.hasNext()) {
      parameter = parameters.next();
    }
    argument = arguments.next();
    ordinal++;

    validator.expectArgumentMatchesParameter(t, argument,
        getJSType(argument), getJSType(parameter), call, ordinal);
  }

  int numArgs = call.getChildCount() - 1;
  int minArgs = functionType.getMinArguments();
  int maxArgs = functionType.getMaxArguments();
  if (minArgs > numArgs || maxArgs < numArgs) {
    report(t, call, WRONG_ARGUMENT_COUNT,
            validator.getReadableJSTypeName(call.getFirstChild(), false),
            String.valueOf(numArgs), String.valueOf(minArgs),
            maxArgs != Integer.MAX_VALUE ?
            " and no more than " + maxArgs + " argument(s)" : "");
  }
}
 
Example 13
Source File: Closure_69_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits the parameters of a CALL or a NEW node.
 */
private void visitParameterList(NodeTraversal t, Node call,
    FunctionType functionType) {
  Iterator<Node> arguments = call.children().iterator();
  arguments.next(); // skip the function name

  Iterator<Node> parameters = functionType.getParameters().iterator();
  int ordinal = 0;
  Node parameter = null;
  Node argument = null;
  while (arguments.hasNext() &&
         (parameters.hasNext() ||
          parameter != null && parameter.isVarArgs())) {
    // If there are no parameters left in the list, then the while loop
    // above implies that this must be a var_args function.
    if (parameters.hasNext()) {
      parameter = parameters.next();
    }
    argument = arguments.next();
    ordinal++;

    validator.expectArgumentMatchesParameter(t, argument,
        getJSType(argument), getJSType(parameter), call, ordinal);
  }

  int numArgs = call.getChildCount() - 1;
  int minArgs = functionType.getMinArguments();
  int maxArgs = functionType.getMaxArguments();
  if (minArgs > numArgs || maxArgs < numArgs) {
    report(t, call, WRONG_ARGUMENT_COUNT,
            validator.getReadableJSTypeName(call.getFirstChild(), false),
            String.valueOf(numArgs), String.valueOf(minArgs),
            maxArgs != Integer.MAX_VALUE ?
            " and no more than " + maxArgs + " argument(s)" : "");
  }
}
 
Example 14
Source File: Closure_2_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits the parameters of a CALL or a NEW node.
 */
private void visitParameterList(NodeTraversal t, Node call,
    FunctionType functionType) {
  Iterator<Node> arguments = call.children().iterator();
  arguments.next(); // skip the function name

  Iterator<Node> parameters = functionType.getParameters().iterator();
  int ordinal = 0;
  Node parameter = null;
  Node argument = null;
  while (arguments.hasNext() &&
         (parameters.hasNext() ||
          parameter != null && parameter.isVarArgs())) {
    // If there are no parameters left in the list, then the while loop
    // above implies that this must be a var_args function.
    if (parameters.hasNext()) {
      parameter = parameters.next();
    }
    argument = arguments.next();
    ordinal++;

    validator.expectArgumentMatchesParameter(t, argument,
        getJSType(argument), getJSType(parameter), call, ordinal);
  }

  int numArgs = call.getChildCount() - 1;
  int minArgs = functionType.getMinArguments();
  int maxArgs = functionType.getMaxArguments();
  if (minArgs > numArgs || maxArgs < numArgs) {
    report(t, call, WRONG_ARGUMENT_COUNT,
            validator.getReadableJSTypeName(call.getFirstChild(), false),
            String.valueOf(numArgs), String.valueOf(minArgs),
            maxArgs != Integer.MAX_VALUE ?
            " and no more than " + maxArgs + " argument(s)" : "");
  }
}
 
Example 15
Source File: FunctionTypeBuilder.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Infer the parameter types from the list of argument names and
 * the doc info.
 */
FunctionTypeBuilder inferParameterTypes(@Nullable Node argsParent,
    @Nullable JSDocInfo info) {
  if (argsParent == null) {
    if (info == null) {
      return this;
    } else {
      return inferParameterTypes(info);
    }
  }

  // arguments
  Node oldParameterType = null;
  if (parametersNode != null) {
    oldParameterType = parametersNode.getFirstChild();
  }

  FunctionParamBuilder builder = new FunctionParamBuilder(typeRegistry);
  boolean warnedAboutArgList = false;
  Set<String> allJsDocParams = (info == null) ?
      Sets.<String>newHashSet() :
      Sets.newHashSet(info.getParameterNames());
  boolean foundTemplateType = false;
  boolean isVarArgs = false;
  for (Node arg : argsParent.children()) {
    String argumentName = arg.getString();
    allJsDocParams.remove(argumentName);

    // type from JSDocInfo
    JSType parameterType = null;
    boolean isOptionalParam = isOptionalParameter(arg, info);
    isVarArgs = isVarArgsParameter(arg, info);

    if (info != null && info.hasParameterType(argumentName)) {
      parameterType =
          info.getParameterType(argumentName).evaluate(scope, typeRegistry);
    } else if (oldParameterType != null &&
        oldParameterType.getJSType() != null) {
      parameterType = oldParameterType.getJSType();
      isOptionalParam = oldParameterType.isOptionalArg();
      isVarArgs = oldParameterType.isVarArgs();
    } else {
      parameterType = typeRegistry.getNativeType(UNKNOWN_TYPE);
    }

    warnedAboutArgList |= addParameter(
        builder, parameterType, warnedAboutArgList,
        isOptionalParam,
        isVarArgs);

    if (oldParameterType != null) {
      oldParameterType = oldParameterType.getNext();
    }
  }

  // Copy over any old parameters that aren't in the param list.
  if (!isVarArgs) {
    while (oldParameterType != null && !isVarArgs) {
      builder.newParameterFromNode(oldParameterType);
      oldParameterType = oldParameterType.getNext();
    }
  }

  for (String inexistentName : allJsDocParams) {
    reportWarning(INEXISTANT_PARAM, inexistentName, fnName);
  }

  parametersNode = builder.build();
  return this;
}
 
Example 16
Source File: Closure_54_FunctionType_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Informally, a function is represented by
 * {@code function (params): returnType} where the {@code params} is a comma
 * separated list of types, the first one being a special
 * {@code this:T} if the function expects a known type for {@code this}.
 */
@Override
public String toString() {
  if (this == registry.getNativeType(JSTypeNative.FUNCTION_INSTANCE_TYPE)) {
    return "Function";
  }

  StringBuilder b = new StringBuilder(32);
  b.append("function (");
  int paramNum = call.parameters.getChildCount();
  boolean hasKnownTypeOfThis = !typeOfThis.isUnknownType();
  if (hasKnownTypeOfThis) {
    if (isConstructor()) {
      b.append("new:");
    } else {
      b.append("this:");
    }
    b.append(typeOfThis.toString());
  }
  if (paramNum > 0) {
    if (hasKnownTypeOfThis) {
      b.append(", ");
    }
    Node p = call.parameters.getFirstChild();
    if (p.isVarArgs()) {
      appendVarArgsString(b, p.getJSType());
    } else {
      b.append(p.getJSType().toString());
    }
    p = p.getNext();
    while (p != null) {
      b.append(", ");
      if (p.isVarArgs()) {
        appendVarArgsString(b, p.getJSType());
      } else {
        b.append(p.getJSType().toString());
      }
      p = p.getNext();
    }
  }
  b.append("): ");
  b.append(call.returnType);
  return b.toString();
}
 
Example 17
Source File: Closure_90_FunctionTypeBuilder_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Infer the parameter types from the list of argument names and
 * the doc info.
 */
FunctionTypeBuilder inferParameterTypes(@Nullable Node argsParent,
    @Nullable JSDocInfo info) {
  if (argsParent == null) {
    if (info == null) {
      return this;
    } else {
      return inferParameterTypes(info);
    }
  }

  // arguments
  Node oldParameterType = null;
  if (parametersNode != null) {
    oldParameterType = parametersNode.getFirstChild();
  }

  FunctionParamBuilder builder = new FunctionParamBuilder(typeRegistry);
  boolean warnedAboutArgList = false;
  Set<String> allJsDocParams = (info == null) ?
      Sets.<String>newHashSet() :
      Sets.newHashSet(info.getParameterNames());
  boolean foundTemplateType = false;
  for (Node arg : argsParent.children()) {
    String argumentName = arg.getString();
    allJsDocParams.remove(argumentName);

    // type from JSDocInfo
    JSType parameterType = null;
    boolean isOptionalParam = isOptionalParameter(arg, info);
    boolean isVarArgs = isVarArgsParameter(arg, info);
    if (info != null && info.hasParameterType(argumentName)) {
      parameterType =
          info.getParameterType(argumentName).evaluate(scope, typeRegistry);
    } else if (oldParameterType != null &&
        oldParameterType.getJSType() != null) {
      parameterType = oldParameterType.getJSType();
      isOptionalParam = oldParameterType.isOptionalArg();
      isVarArgs = oldParameterType.isVarArgs();
    } else {
      parameterType = typeRegistry.getNativeType(UNKNOWN_TYPE);
    }

    if (templateTypeName != null &&
        parameterType.restrictByNotNullOrUndefined().isTemplateType()) {
      if (foundTemplateType) {
        reportError(TEMPLATE_TYPE_DUPLICATED, fnName);
      }
      foundTemplateType = true;
    }
    warnedAboutArgList |= addParameter(
        builder, parameterType, warnedAboutArgList,
        isOptionalParam,
        isVarArgs);

    if (oldParameterType != null) {
      oldParameterType = oldParameterType.getNext();
    }
  }

  if (templateTypeName != null && !foundTemplateType) {
    reportError(TEMPLATE_TYPE_EXPECTED, fnName);
  }

  for (String inexistentName : allJsDocParams) {
    reportWarning(INEXISTANT_PARAM, inexistentName, fnName);
  }

  parametersNode = builder.build();
  return this;
}
 
Example 18
Source File: FunctionTypeBuilder.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Infer the parameter and return types of a function from
 * the parameter and return types of the function it is overriding.
 *
 * @param oldType The function being overridden. Does nothing if this is null.
 * @param paramsParent The LP node of the function that we're assigning to.
 *     If null, that just means we're not initializing this to a function
 *     literal.
 */
FunctionTypeBuilder inferFromOverriddenFunction(
    @Nullable FunctionType oldType, @Nullable Node paramsParent) {
  if (oldType == null) {
    return this;
  }

  returnType = oldType.getReturnType();
  returnTypeInferred = oldType.isReturnTypeInferred();
  if (paramsParent == null) {
    // Not a function literal.
    parametersNode = oldType.getParametersNode();
    if (parametersNode == null) {
      parametersNode = new FunctionParamBuilder(typeRegistry).build();
    }
  } else {
    // We're overriding with a function literal. Apply type information
    // to each parameter of the literal.
    FunctionParamBuilder paramBuilder =
        new FunctionParamBuilder(typeRegistry);
    Iterator<Node> oldParams = oldType.getParameters().iterator();
    boolean warnedAboutArgList = false;
    boolean oldParamsListHitOptArgs = false;
    for (Node currentParam = paramsParent.getFirstChild();
         currentParam != null; currentParam = currentParam.getNext()) {
      if (oldParams.hasNext()) {
        Node oldParam = oldParams.next();
        Node newParam = paramBuilder.newParameterFromNode(oldParam);

        oldParamsListHitOptArgs = oldParamsListHitOptArgs ||
            oldParam.isVarArgs() ||
            oldParam.isOptionalArg();

        // The subclass method might write its var_args as individual
        // arguments.
        if (currentParam.getNext() != null && newParam.isVarArgs()) {
          newParam.setVarArgs(false);
          newParam.setOptionalArg(true);
        }
      } else {
        warnedAboutArgList |= addParameter(
            paramBuilder,
            typeRegistry.getNativeType(UNKNOWN_TYPE),
            warnedAboutArgList,
            codingConvention.isOptionalParameter(currentParam) ||
                oldParamsListHitOptArgs,
            codingConvention.isVarArgsParameter(currentParam));
      }
    }

    // Clone any remaining params that aren't in the function literal,
    // but make them optional.
    while (oldParams.hasNext()) {
      paramBuilder.newOptionalParameterFromNode(oldParams.next());
    }

    parametersNode = paramBuilder.build();
  }
  return this;
}
 
Example 19
Source File: Closure_90_FunctionTypeBuilder_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Infer the parameter and return types of a function from
 * the parameter and return types of the function it is overriding.
 *
 * @param oldType The function being overridden. Does nothing if this is null.
 * @param paramsParent The LP node of the function that we're assigning to.
 *     If null, that just means we're not initializing this to a function
 *     literal.
 */
FunctionTypeBuilder inferFromOverriddenFunction(
    @Nullable FunctionType oldType, @Nullable Node paramsParent) {
  if (oldType == null) {
    return this;
  }

  returnType = oldType.getReturnType();
  returnTypeInferred = oldType.isReturnTypeInferred();
  if (paramsParent == null) {
    // Not a function literal.
    parametersNode = oldType.getParametersNode();
    if (parametersNode == null) {
      parametersNode = new FunctionParamBuilder(typeRegistry).build();
    }
  } else {
    // We're overriding with a function literal. Apply type information
    // to each parameter of the literal.
    FunctionParamBuilder paramBuilder =
        new FunctionParamBuilder(typeRegistry);
    Iterator<Node> oldParams = oldType.getParameters().iterator();
    boolean warnedAboutArgList = false;
    boolean oldParamsListHitOptArgs = false;
    for (Node currentParam = paramsParent.getFirstChild();
         currentParam != null; currentParam = currentParam.getNext()) {
      if (oldParams.hasNext()) {
        Node oldParam = oldParams.next();
        Node newParam = paramBuilder.newParameterFromNode(oldParam);

        oldParamsListHitOptArgs = oldParamsListHitOptArgs ||
            oldParam.isVarArgs() ||
            oldParam.isOptionalArg();

        // The subclass method might right its var_args as individual
        // arguments.
        if (currentParam.getNext() != null && newParam.isVarArgs()) {
          newParam.setVarArgs(false);
          newParam.setOptionalArg(true);
        }
      } else {
        warnedAboutArgList |= addParameter(
            paramBuilder,
            typeRegistry.getNativeType(UNKNOWN_TYPE),
            warnedAboutArgList,
            codingConvention.isOptionalParameter(currentParam) ||
                oldParamsListHitOptArgs,
            codingConvention.isVarArgsParameter(currentParam));
      }
    }
    parametersNode = paramBuilder.build();
  }
  return this;
}
 
Example 20
Source File: Closure_41_FunctionTypeBuilder_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Infer the parameter types from the list of argument names and
 * the doc info.
 */
FunctionTypeBuilder inferParameterTypes(@Nullable Node argsParent,
    @Nullable JSDocInfo info) {
  if (argsParent == null) {
    if (info == null) {
      return this;
    } else {
      return inferParameterTypes(info);
    }
  }

  // arguments
  Node oldParameterType = null;
  if (parametersNode != null) {
    oldParameterType = parametersNode.getFirstChild();
  }

  FunctionParamBuilder builder = new FunctionParamBuilder(typeRegistry);
  boolean warnedAboutArgList = false;
  Set<String> allJsDocParams = (info == null) ?
      Sets.<String>newHashSet() :
      Sets.newHashSet(info.getParameterNames());
  boolean foundTemplateType = false;
  boolean isVarArgs = false;
  for (Node arg : argsParent.children()) {
    String argumentName = arg.getString();
    allJsDocParams.remove(argumentName);

    // type from JSDocInfo
    JSType parameterType = null;
    boolean isOptionalParam = isOptionalParameter(arg, info);
    isVarArgs = isVarArgsParameter(arg, info);

    if (info != null && info.hasParameterType(argumentName)) {
      parameterType =
          info.getParameterType(argumentName).evaluate(scope, typeRegistry);
    } else if (oldParameterType != null &&
        oldParameterType.getJSType() != null) {
      parameterType = oldParameterType.getJSType();
      isOptionalParam = oldParameterType.isOptionalArg();
      isVarArgs = oldParameterType.isVarArgs();
    } else {
      parameterType = typeRegistry.getNativeType(UNKNOWN_TYPE);
    }

    if (templateTypeName != null &&
        parameterType.restrictByNotNullOrUndefined().isTemplateType()) {
      if (foundTemplateType) {
        reportError(TEMPLATE_TYPE_DUPLICATED, fnName);
      }
      foundTemplateType = true;
    }
    warnedAboutArgList |= addParameter(
        builder, parameterType, warnedAboutArgList,
        isOptionalParam,
        isVarArgs);

    if (oldParameterType != null) {
      oldParameterType = oldParameterType.getNext();
    }
  }

  // Copy over any old parameters that aren't in the param list.

  if (templateTypeName != null && !foundTemplateType) {
    reportError(TEMPLATE_TYPE_EXPECTED, fnName);
  }

  for (String inexistentName : allJsDocParams) {
    reportWarning(INEXISTANT_PARAM, inexistentName, fnName);
  }

  parametersNode = builder.build();
  return this;
}