Java Code Examples for com.google.javascript.rhino.jstype.FunctionType#getReturnType()

The following examples show how to use com.google.javascript.rhino.jstype.FunctionType#getReturnType() . 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: DeclarationGenerator.java    From clutz with MIT License 6 votes vote down vote up
private void visitFunctionDeclaration(FunctionType ftype, List<String> skipTemplateParams) {
  visitFunctionParameters(ftype, true, skipTemplateParams);
  JSType type = ftype.getReturnType();
  final JSType typeOfThis = ftype.getTypeOfThis();

  if (type == null) return;
  emit(":");
  // Closure conflates 'undefined' and 'void', and in general visitType always emits `undefined`
  // for that type.
  // In idiomatic TypeScript, `void` is used for function return types, and the "void",
  // "undefined" types are not the same.
  if (type.isVoidType()) {
    emit("void");
  } else if (typeOfThis != null && typeOfThis.isTemplateType() && typeOfThis.equals(type)) {
    // Special case: prefer polymorphic `this` type to templatized `this` param
    emit("this");
  } else {
    visitType(type);
  }
}
 
Example 2
Source File: CheckMissingReturn.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Determines if the given scope should explicitly return. All functions
 * with non-void or non-unknown return types must have explicit returns.
 * @return If a return type is expected, returns it. Otherwise, returns null.
 */
private JSType explicitReturnExpected(Node scope) {
  FunctionType scopeType = JSType.toMaybeFunctionType(scope.getJSType());

  if (scopeType == null) {
    return null;
  }

  if (isEmptyFunction(scope)) {
    return null;
  }

  JSType returnType = scopeType.getReturnType();

  if (returnType == null) {
    return null;
  }

  if (!isVoidOrUnknown(returnType)) {
    return returnType;
  }

  return null;
}
 
Example 3
Source File: DevirtualizePrototypeMethods.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates a new JSType based on the original function type by
 * adding the original this pointer type to the beginning of the
 * argument type list and replacing the this pointer type with
 * NO_TYPE.
 */
private void fixFunctionType(Node functionNode) {
  FunctionType type = JSType.toMaybeFunctionType(functionNode.getJSType());
  if (type != null) {
    JSTypeRegistry typeRegistry = compiler.getTypeRegistry();

    List<JSType> parameterTypes = Lists.newArrayList();
    parameterTypes.add(type.getTypeOfThis());

    for (Node param : type.getParameters()) {
      parameterTypes.add(param.getJSType());
    }

    ObjectType thisType =
        typeRegistry.getNativeObjectType(JSTypeNative.UNKNOWN_TYPE);
    JSType returnType = type.getReturnType();

    JSType newType = typeRegistry.createFunctionType(
        thisType, returnType, parameterTypes);
    functionNode.setJSType(newType);
  }
}
 
Example 4
Source File: Closure_96_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a RETURN node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitReturn(NodeTraversal t, Node n) {
  Node function = t.getEnclosingFunction();

  // This is a misplaced return, but the real JS will fail to compile,
  // so let it go.
  if (function == null) {
    return;
  }
  JSType jsType = getJSType(function);

  if (jsType instanceof FunctionType) {
    FunctionType functionType = (FunctionType) jsType;

    JSType returnType = functionType.getReturnType();

    // if no return type is specified, undefined must be returned
    // (it's a void function)
    if (returnType == null) {
      returnType = getNativeType(VOID_TYPE);
    }

    // fetching the returned value's type
    Node valueNode = n.getFirstChild();
    JSType actualReturnType;
    if (valueNode == null) {
      actualReturnType = getNativeType(VOID_TYPE);
      valueNode = n;
    } else {
      actualReturnType = getJSType(valueNode);
    }

    // verifying
    validator.expectCanAssignTo(t, valueNode, actualReturnType, returnType,
        "inconsistent return type");
  }
}
 
Example 5
Source File: TypeCheck.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Visits a RETURN node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitReturn(NodeTraversal t, Node n) {
  JSType jsType = getJSType(t.getEnclosingFunction());

  if (jsType.isFunctionType()) {
    FunctionType functionType = jsType.toMaybeFunctionType();

    JSType returnType = functionType.getReturnType();

    // if no return type is specified, undefined must be returned
    // (it's a void function)
    if (returnType == null) {
      returnType = getNativeType(VOID_TYPE);
    }

    // fetching the returned value's type
    Node valueNode = n.getFirstChild();
    JSType actualReturnType;
    if (valueNode == null) {
      actualReturnType = getNativeType(VOID_TYPE);
      valueNode = n;
    } else {
      actualReturnType = getJSType(valueNode);
    }

    // verifying
    validator.expectCanAssignTo(t, valueNode, actualReturnType, returnType,
        "inconsistent return type");
  }
}
 
Example 6
Source File: Closure_96_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a RETURN node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitReturn(NodeTraversal t, Node n) {
  Node function = t.getEnclosingFunction();

  // This is a misplaced return, but the real JS will fail to compile,
  // so let it go.
  if (function == null) {
    return;
  }
  JSType jsType = getJSType(function);

  if (jsType instanceof FunctionType) {
    FunctionType functionType = (FunctionType) jsType;

    JSType returnType = functionType.getReturnType();

    // if no return type is specified, undefined must be returned
    // (it's a void function)
    if (returnType == null) {
      returnType = getNativeType(VOID_TYPE);
    }

    // fetching the returned value's type
    Node valueNode = n.getFirstChild();
    JSType actualReturnType;
    if (valueNode == null) {
      actualReturnType = getNativeType(VOID_TYPE);
      valueNode = n;
    } else {
      actualReturnType = getJSType(valueNode);
    }

    // verifying
    validator.expectCanAssignTo(t, valueNode, actualReturnType, returnType,
        "inconsistent return type");
  }
}
 
Example 7
Source File: Closure_11_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a RETURN node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitReturn(NodeTraversal t, Node n) {
  JSType jsType = getJSType(t.getEnclosingFunction());

  if (jsType.isFunctionType()) {
    FunctionType functionType = jsType.toMaybeFunctionType();

    JSType returnType = functionType.getReturnType();

    // if no return type is specified, undefined must be returned
    // (it's a void function)
    if (returnType == null) {
      returnType = getNativeType(VOID_TYPE);
    }

    // fetching the returned value's type
    Node valueNode = n.getFirstChild();
    JSType actualReturnType;
    if (valueNode == null) {
      actualReturnType = getNativeType(VOID_TYPE);
      valueNode = n;
    } else {
      actualReturnType = getJSType(valueNode);
    }

    // verifying
    validator.expectCanAssignTo(t, valueNode, actualReturnType, returnType,
        "inconsistent return type");
  }
}
 
Example 8
Source File: Closure_66_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a RETURN node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitReturn(NodeTraversal t, Node n) {
  Node function = t.getEnclosingFunction();

  // This is a misplaced return, but the real JS will fail to compile,
  // so let it go.
  if (function == null) {
    return;
  }
  JSType jsType = getJSType(function);

  if (jsType instanceof FunctionType) {
    FunctionType functionType = (FunctionType) jsType;

    JSType returnType = functionType.getReturnType();

    // if no return type is specified, undefined must be returned
    // (it's a void function)
    if (returnType == null) {
      returnType = getNativeType(VOID_TYPE);
    }

    // fetching the returned value's type
    Node valueNode = n.getFirstChild();
    JSType actualReturnType;
    if (valueNode == null) {
      actualReturnType = getNativeType(VOID_TYPE);
      valueNode = n;
    } else {
      actualReturnType = getJSType(valueNode);
    }

    // verifying
    validator.expectCanAssignTo(t, valueNode, actualReturnType, returnType,
        "inconsistent return type");
  }
}
 
Example 9
Source File: Closure_125_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a RETURN node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitReturn(NodeTraversal t, Node n) {
  JSType jsType = getJSType(t.getEnclosingFunction());

  if (jsType.isFunctionType()) {
    FunctionType functionType = jsType.toMaybeFunctionType();

    JSType returnType = functionType.getReturnType();

    // if no return type is specified, undefined must be returned
    // (it's a void function)
    if (returnType == null) {
      returnType = getNativeType(VOID_TYPE);
    }

    // fetching the returned value's type
    Node valueNode = n.getFirstChild();
    JSType actualReturnType;
    if (valueNode == null) {
      actualReturnType = getNativeType(VOID_TYPE);
      valueNode = n;
    } else {
      actualReturnType = getJSType(valueNode);
    }

    // verifying
    validator.expectCanAssignTo(t, valueNode, actualReturnType, returnType,
        "inconsistent return type");
  }
}
 
Example 10
Source File: Closure_69_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a RETURN node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitReturn(NodeTraversal t, Node n) {
  Node function = t.getEnclosingFunction();

  // This is a misplaced return, but the real JS will fail to compile,
  // so let it go.
  if (function == null) {
    return;
  }
  JSType jsType = getJSType(function);

  if (jsType instanceof FunctionType) {
    FunctionType functionType = (FunctionType) jsType;

    JSType returnType = functionType.getReturnType();

    // if no return type is specified, undefined must be returned
    // (it's a void function)
    if (returnType == null) {
      returnType = getNativeType(VOID_TYPE);
    }

    // fetching the returned value's type
    Node valueNode = n.getFirstChild();
    JSType actualReturnType;
    if (valueNode == null) {
      actualReturnType = getNativeType(VOID_TYPE);
      valueNode = n;
    } else {
      actualReturnType = getJSType(valueNode);
    }

    // verifying
    validator.expectCanAssignTo(t, valueNode, actualReturnType, returnType,
        "inconsistent return type");
  }
}
 
Example 11
Source File: Closure_69_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a RETURN node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitReturn(NodeTraversal t, Node n) {
  Node function = t.getEnclosingFunction();

  // This is a misplaced return, but the real JS will fail to compile,
  // so let it go.
  if (function == null) {
    return;
  }
  JSType jsType = getJSType(function);

  if (jsType instanceof FunctionType) {
    FunctionType functionType = (FunctionType) jsType;

    JSType returnType = functionType.getReturnType();

    // if no return type is specified, undefined must be returned
    // (it's a void function)
    if (returnType == null) {
      returnType = getNativeType(VOID_TYPE);
    }

    // fetching the returned value's type
    Node valueNode = n.getFirstChild();
    JSType actualReturnType;
    if (valueNode == null) {
      actualReturnType = getNativeType(VOID_TYPE);
      valueNode = n;
    } else {
      actualReturnType = getJSType(valueNode);
    }

    // verifying
    validator.expectCanAssignTo(t, valueNode, actualReturnType, returnType,
        "inconsistent return type");
  }
}
 
Example 12
Source File: Closure_2_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a RETURN node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitReturn(NodeTraversal t, Node n) {
  JSType jsType = getJSType(t.getEnclosingFunction());

  if (jsType.isFunctionType()) {
    FunctionType functionType = jsType.toMaybeFunctionType();

    JSType returnType = functionType.getReturnType();

    // if no return type is specified, undefined must be returned
    // (it's a void function)
    if (returnType == null) {
      returnType = getNativeType(VOID_TYPE);
    }

    // fetching the returned value's type
    Node valueNode = n.getFirstChild();
    JSType actualReturnType;
    if (valueNode == null) {
      actualReturnType = getNativeType(VOID_TYPE);
      valueNode = n;
    } else {
      actualReturnType = getJSType(valueNode);
    }

    // verifying
    validator.expectCanAssignTo(t, valueNode, actualReturnType, returnType,
        "inconsistent return type");
  }
}
 
Example 13
Source File: Nopol2017_0029_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a RETURN node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitReturn(NodeTraversal t, Node n) {
  JSType jsType = getJSType(t.getEnclosingFunction());

  if (jsType.isFunctionType()) {
    FunctionType functionType = jsType.toMaybeFunctionType();

    JSType returnType = functionType.getReturnType();

    // if no return type is specified, undefined must be returned
    // (it's a void function)
    if (returnType == null) {
      returnType = getNativeType(VOID_TYPE);
    }

    // fetching the returned value's type
    Node valueNode = n.getFirstChild();
    JSType actualReturnType;
    if (valueNode == null) {
      actualReturnType = getNativeType(VOID_TYPE);
      valueNode = n;
    } else {
      actualReturnType = getJSType(valueNode);
    }

    // verifying
    validator.expectCanAssignTo(t, valueNode, actualReturnType, returnType,
        "inconsistent return type");
  }
}
 
Example 14
Source File: Nopol2017_0051_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Visits a RETURN node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 */
private void visitReturn(NodeTraversal t, Node n) {
  Node function = t.getEnclosingFunction();

  // This is a misplaced return, but the real JS will fail to compile,
  // so let it go.
  if (function == null) {
    return;
  }
  JSType jsType = getJSType(function);

  if (jsType instanceof FunctionType) {
    FunctionType functionType = (FunctionType) jsType;

    JSType returnType = functionType.getReturnType();

    // if no return type is specified, undefined must be returned
    // (it's a void function)
    if (returnType == null) {
      returnType = getNativeType(VOID_TYPE);
    }

    // fetching the returned value's type
    Node valueNode = n.getFirstChild();
    JSType actualReturnType;
    if (valueNode == null) {
      actualReturnType = getNativeType(VOID_TYPE);
      valueNode = n;
    } else {
      actualReturnType = getJSType(valueNode);
    }

    // verifying
    validator.expectCanAssignTo(t, valueNode, actualReturnType, returnType,
        "inconsistent return type");
  }
}
 
Example 15
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 16
Source File: TypeExpressionParser.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
@Override
public Void caseFunctionType(FunctionType type) {
  if ("Function".equals(type.getReferenceName())) {
    currentExpression().getNamedTypeBuilder().setName("Function");
    return null;
  }

  com.github.jsdossier.proto.FunctionType.Builder functionType =
      currentExpression().getFunctionTypeBuilder();

  if (type.isConstructor()) {
    functionType.setIsConstructor(true);
    expressions.addLast(functionType.getInstanceTypeBuilder());
    type.getTypeOfThis().visit(this);
    expressions.removeLast();

  } else if (!type.getTypeOfThis().isUnknownType()
      || type.getTypeOfThis() instanceof NamedType) {
    expressions.addLast(functionType.getInstanceTypeBuilder());
    type.getTypeOfThis().visit(this);
    expressions.removeLast();
  }

  for (Node node : type.getParameters()) {
    TypeExpression.Builder parameterType = functionType.addParameterBuilder();
    expressions.addLast(parameterType);

    if (node.isVarArgs()) {
      parameterType.setIsVarargs(true);
    }

    if (node.getJSType() != null) {
      if (node.getJSType().isUnionType()) {
        caseUnionType((UnionType) node.getJSType(), node.isOptionalArg());
      } else {
        node.getJSType().visit(this);
      }
    }

    if (node.isOptionalArg()) {
      // Not sure if this is possible, but varargs implies optional and we only permit one
      // bit to be set.
      if (!parameterType.getIsVarargs()) {
        parameterType.setIsOptional(true);
      }
    }

    expressions.removeLast();
  }

  if (type.getReturnType() != null && !type.isConstructor()) {
    expressions.addLast(functionType.getReturnTypeBuilder());
    type.getReturnType().visit(this);
    expressions.removeLast();
  }
  return null;
}
 
Example 17
Source File: ExternExportsPass.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Warn the user if there is an exported function for which a parameter
 * or return type is unknown.
 */
private void checkForFunctionsWithUnknownTypes(Node function) {
  Preconditions.checkArgument(function.isFunction());

  FunctionType functionType =
      JSType.toMaybeFunctionType(function.getJSType());

  if (functionType == null) {
    // No type information is available (CheckTypes was probably not run)
    // so just bail.
    return;
  }

  /* We must get the JSDocInfo from the function's type since the function
   * itself does not have an associated JSDocInfo node.
   */
  JSDocInfo functionJSDocInfo = functionType.getJSDocInfo();

  JSType returnType = functionType.getReturnType();

  /* It is OK if a constructor doesn't have a return type */
  if (!functionType.isConstructor() &&
      (returnType == null || returnType.isUnknownType())) {
    reportUnknownReturnType(function);
  }

  /* We can't just use the function's type's getParameters() to get the
   * parameter nodes because the nodes returned from that method
   * do not have names or locations. Similarly, the function's AST parameter
   * nodes do not have JSTypes(). So we walk both lists of parameter nodes
   * in lock step getting parameter names from the first and types from the
   * second.
   */
  Node astParameterIterator = NodeUtil.getFunctionParameters(function)
    .getFirstChild();

  Node typeParameterIterator = functionType.getParametersNode()
    .getFirstChild();

  while (astParameterIterator != null) {
    JSType parameterType = typeParameterIterator.getJSType();

    if (parameterType == null || parameterType.isUnknownType()) {
      reportUnknownParameterType(function, astParameterIterator);
    }

    astParameterIterator = astParameterIterator.getNext();
    typeParameterIterator = typeParameterIterator.getNext();
  }
}
 
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_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 20
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;
}