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

The following examples show how to use com.google.javascript.rhino.Node#isOptionalArg() . 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: 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 2
Source File: FunctionType.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void appendArgString(
    StringBuilder b, Node p, boolean forAnnotations) {
  if (p.isVarArgs()) {
    appendVarArgsString(b, p.getJSType(), forAnnotations);
  } else if (p.isOptionalArg()) {
    appendOptionalArgString(b, p.getJSType(), forAnnotations);
  } else {
    b.append(p.getJSType().toStringHelper(forAnnotations));
  }
}
 
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: Closure_90_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 5
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 6
Source File: FunctionType.java    From astor with GNU General Public License v2.0 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: 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 8
Source File: Closure_54_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 9
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 10
Source File: ArrowType.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public boolean isSubtype(JSType other) {
  if (!(other instanceof ArrowType)) {
    return false;
  }

  ArrowType that = (ArrowType) other;

  // This is described in Draft 2 of the ES4 spec,
  // Section 3.4.7: Subtyping Function Types.

  // this.returnType <: that.returnType (covariant)
  if (!this.returnType.isSubtype(that.returnType)) {
    return false;
  }

  // that.paramType[i] <: this.paramType[i] (contravariant)
  //
  // If this.paramType[i] is required,
  // then that.paramType[i] is required.
  //
  // In theory, the "required-ness" should work in the other direction as
  // well. In other words, if we have
  //
  // function f(number, number) {}
  // function g(number) {}
  //
  // Then f *should* not be a subtype of g, and g *should* not be
  // a subtype of f. But in practice, we do not implement it this way.
  // We want to support the use case where you can pass g where f is
  // expected, and pretend that g ignores the second argument.
  // That way, you can have a single "no-op" function, and you don't have
  // to create a new no-op function for every possible type signature.
  //
  // So, in this case, g < f, but f !< g
  Node thisParam = parameters.getFirstChild();
  Node thatParam = that.parameters.getFirstChild();
  while (thisParam != null && thatParam != null) {
    JSType thisParamType = thisParam.getJSType();
    JSType thatParamType = thatParam.getJSType();
    if (thisParamType != null) {
      if (thatParamType == null ||
          !thatParamType.isSubtype(thisParamType)) {
        return false;
      }
    }

    boolean thisIsVarArgs = thisParam.isVarArgs();
    boolean thatIsVarArgs = thatParam.isVarArgs();
    boolean thisIsOptional = thisIsVarArgs || thisParam.isOptionalArg();
    boolean thatIsOptional = thatIsVarArgs || thatParam.isOptionalArg();

    // "that" can't be a supertype, because it's missing a required argument.
    if (!thisIsOptional && thatIsOptional) {
      // NOTE(nicksantos): In our type system, we use {function(...?)} and
      // {function(...NoType)} to to indicate that arity should not be
      // checked. Strictly speaking, this is not a correct formulation,
      // because now a sub-function can required arguments that are var_args
      // in the super-function. So we special-case this.
      boolean isTopFunction =
          thatIsVarArgs &&
          (thatParamType == null ||
           thatParamType.isUnknownType() ||
           thatParamType.isNoType());
      if (!isTopFunction) {
        return false;
      }
    }

    // don't advance if we have variable arguments
    if (!thisIsVarArgs) {
      thisParam = thisParam.getNext();
    }
    if (!thatIsVarArgs) {
      thatParam = thatParam.getNext();
    }

    // both var_args indicates the end
    if (thisIsVarArgs && thatIsVarArgs) {
      thisParam = null;
      thatParam = null;
    }
  }

  // "that" can't be a supertype, because it's missing a required argument.
  if (thisParam != null
      && !thisParam.isOptionalArg() && !thisParam.isVarArgs()
      && thatParam == null) {
    return false;
  }

  return true;
}
 
Example 11
Source File: FunctionParamBuilder.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private boolean hasOptionalOrVarArgs() {
  Node lastChild = root.getLastChild();
  return lastChild != null &&
      (lastChild.isOptionalArg() || lastChild.isVarArgs());
}
 
Example 12
Source File: ModificationVisitor.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override
public JSType caseFunctionType(FunctionType type) {
  if (isNativeFunctionType(type)) {
    return type;
  }

  // TODO(johnlenz): remove this simplifying assumption...
  if (!type.isOrdinaryFunction()) {
    return type;
  }

  boolean changed = false;

  JSType beforeThis = type.getTypeOfThis();
  JSType afterThis = coerseToThisType(beforeThis.visit(this));
  if (beforeThis != afterThis) {
    changed = true;
  }

  JSType beforeReturn = type.getReturnType();
  JSType afterReturn = beforeReturn.visit(this);
  if (beforeReturn != afterReturn) {
    changed = true;
  }

  FunctionParamBuilder paramBuilder = new FunctionParamBuilder(registry);
  for (Node paramNode : type.getParameters()) {
    JSType beforeParamType = paramNode.getJSType();
    JSType afterParamType = beforeParamType.visit(this);
    if (beforeParamType != afterParamType) {
      changed = true;
    }
    if (paramNode.isOptionalArg()) {
      paramBuilder.addOptionalParams(afterParamType);
    } else if (paramNode.isVarArgs()) {
      paramBuilder.addVarArgs(afterParamType);
    } else {
      paramBuilder.addRequiredParams(afterParamType);
    }
  }

  if (changed) {
    // TODO(johnlenz): should we support preserving template keys?
    FunctionBuilder builder = new FunctionBuilder(registry);
    builder.withParams(paramBuilder);
    builder.withReturnType(afterReturn);
    builder.withTypeOfThis(afterThis);
    return builder.build();
  }

  return type;
}
 
Example 13
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 14
Source File: Closure_41_FunctionTypeBuilder_t.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 (!isVarArgs) {
    while (oldParameterType != null && !isVarArgs) {
      builder.newParameterFromNode(oldParameterType);
      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 15
Source File: Closure_41_FunctionTypeBuilder_t.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 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.
    while (oldParams.hasNext()) {
      paramBuilder.newParameterFromNode(oldParams.next());
    }

    parametersNode = paramBuilder.build();
  }
  return this;
}
 
Example 16
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;
}
 
Example 17
Source File: Closure_41_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 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.

    parametersNode = paramBuilder.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 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 19
Source File: Closure_90_FunctionTypeBuilder_t.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_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;
}