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

The following examples show how to use com.google.javascript.rhino.jstype.FunctionType#isReturnTypeInferred() . 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: Nopol2017_0027_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * If a variable is assigned a function literal in the global scope,
 * make that a declared type (even if there's no doc info).
 * There's only one exception to this rule:
 * if the return type is inferred, and we're in a local
 * scope, we should assume the whole function is inferred.
 */
private boolean shouldUseFunctionLiteralType(
    FunctionType type, JSDocInfo info, Node lValue) {
  if (info != null) {
    return true;
  }
  if (lValue != null &&
      NodeUtil.isObjectLitKey(lValue, lValue.getParent())) {
    return false;
  }
  return scope.isGlobal() || !type.isReturnTypeInferred();
}
 
Example 2
Source File: Closure_17_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * If a variable is assigned a function literal in the global scope,
 * make that a declared type (even if there's no doc info).
 * There's only one exception to this rule:
 * if the return type is inferred, and we're in a local
 * scope, we should assume the whole function is inferred.
 */
private boolean shouldUseFunctionLiteralType(
    FunctionType type, JSDocInfo info, Node lValue) {
  if (info != null) {
    return true;
  }
  if (lValue != null &&
      NodeUtil.isObjectLitKey(lValue, lValue.getParent())) {
    return false;
  }
  return scope.isGlobal() || !type.isReturnTypeInferred();
}
 
Example 3
Source File: Closure_17_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * If a variable is assigned a function literal in the global scope,
 * make that a declared type (even if there's no doc info).
 * There's only one exception to this rule:
 * if the return type is inferred, and we're in a local
 * scope, we should assume the whole function is inferred.
 */
private boolean shouldUseFunctionLiteralType(
    FunctionType type, JSDocInfo info, Node lValue) {
  if (info != null) {
    return true;
  }
  if (lValue != null &&
      NodeUtil.isObjectLitKey(lValue, lValue.getParent())) {
    return false;
  }
  return scope.isGlobal() || !type.isReturnTypeInferred();
}
 
Example 4
Source File: Nopol2017_0027_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * If a variable is assigned a function literal in the global scope,
 * make that a declared type (even if there's no doc info).
 * There's only one exception to this rule:
 * if the return type is inferred, and we're in a local
 * scope, we should assume the whole function is inferred.
 */
private boolean shouldUseFunctionLiteralType(
    FunctionType type, JSDocInfo info, Node lValue) {
  if (info != null) {
    return true;
  }
  if (lValue != null &&
      NodeUtil.isObjectLitKey(lValue, lValue.getParent())) {
    return false;
  }
  return scope.isGlobal() || !type.isReturnTypeInferred();
}
 
Example 5
Source File: TypedScopeCreator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * If a variable is assigned a function literal in the global scope,
 * make that a declared type (even if there's no doc info).
 * There's only one exception to this rule:
 * if the return type is inferred, and we're in a local
 * scope, we should assume the whole function is inferred.
 */
private boolean shouldUseFunctionLiteralType(
    FunctionType type, JSDocInfo info, Node lValue) {
  if (info != null) {
    return true;
  }
  if (lValue != null &&
      NodeUtil.isObjectLitKey(lValue, lValue.getParent())) {
    return false;
  }
  return scope.isGlobal() || !type.isReturnTypeInferred();
}
 
Example 6
Source File: Closure_70_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * If a variable is assigned a function literal in the global scope,
 * make that a declared type (even if there's no doc info).
 * There's only one exception to this rule:
 * if the return type is inferred, and we're in a local
 * scope, we should assume the whole function is inferred.
 */
private boolean shouldUseFunctionLiteralType(
    FunctionType type, JSDocInfo info, Node lValue) {
  if (info != null) {
    return true;
  }
  if (lValue != null &&
      NodeUtil.isObjectLitKey(lValue, lValue.getParent())) {
    return false;
  }
  return scope.isGlobal() || !type.isReturnTypeInferred();
}
 
Example 7
Source File: Closure_70_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * If a variable is assigned a function literal in the global scope,
 * make that a declared type (even if there's no doc info).
 * There's only one exception to this rule:
 * if the return type is inferred, and we're in a local
 * scope, we should assume the whole function is inferred.
 */
private boolean shouldUseFunctionLiteralType(
    FunctionType type, JSDocInfo info, Node lValue) {
  if (info != null) {
    return true;
  }
  if (lValue != null &&
      NodeUtil.isObjectLitKey(lValue, lValue.getParent())) {
    return false;
  }
  return scope.isGlobal() || !type.isReturnTypeInferred();
}
 
Example 8
Source File: Closure_48_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * If a variable is assigned a function literal in the global scope,
 * make that a declared type (even if there's no doc info).
 * There's only one exception to this rule:
 * if the return type is inferred, and we're in a local
 * scope, we should assume the whole function is inferred.
 */
private boolean shouldUseFunctionLiteralType(
    FunctionType type, JSDocInfo info, Node lValue) {
  if (info != null) {
    return true;
  }
  if (lValue != null &&
      NodeUtil.isObjectLitKey(lValue, lValue.getParent())) {
    return false;
  }
  return scope.isGlobal() || !type.isReturnTypeInferred();
}
 
Example 9
Source File: Closure_48_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * If a variable is assigned a function literal in the global scope,
 * make that a declared type (even if there's no doc info).
 * There's only one exception to this rule:
 * if the return type is inferred, and we're in a local
 * scope, we should assume the whole function is inferred.
 */
private boolean shouldUseFunctionLiteralType(
    FunctionType type, JSDocInfo info, Node lValue) {
  if (info != null) {
    return true;
  }
  if (lValue != null &&
      NodeUtil.isObjectLitKey(lValue, lValue.getParent())) {
    return false;
  }
  return scope.isGlobal() || !type.isReturnTypeInferred();
}
 
Example 10
Source File: Closure_95_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Defines a variable based on the {@link Token#NAME} node passed.
 * @param name The {@link Token#NAME} node.
 * @param var The parent of the {@code name} node, which must be a
 *     {@link Token#VAR} node.
 * @param parent {@code var}'s parent.
 * @param info the {@link JSDocInfo} information relating to this
 *     {@code name} node.
 */
private void defineName(Node name, Node var, Node parent, JSDocInfo info) {
  Node value = name.getFirstChild();

  if (value != null && value.getType() == Token.FUNCTION) {
    // function
    String functionName = name.getString();
    FunctionType functionType =
        getFunctionType(functionName, value, info, null);
    if (functionType.isReturnTypeInferred() &&
        scope.isLocal()) {
      defineSlot(name, var, null);
    } else {
      defineSlot(name, var, functionType);
    }
  } else {
    // variable's type
    JSType type = null;
    if (info == null) {
      // the variable's type will be inferred
      CompilerInput input = compiler.getInput(sourceName);
      Preconditions.checkNotNull(input, sourceName);
      type = input.isExtern() ?
          getNativeType(UNKNOWN_TYPE) : null;
    } else if (info.hasEnumParameterType()) {
      type = getEnumType(name.getString(), var, value,
          info.getEnumParameterType().evaluate(scope, typeRegistry));
    } else if (info.isConstructor()) {
      type = getFunctionType(name.getString(), value, info, name);
    } else {
      type = getDeclaredTypeInAnnotation(sourceName, name, info);
    }

    defineSlot(name, var, type);
  }
}
 
Example 11
Source File: Closure_95_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Defines a variable based on the {@link Token#NAME} node passed.
 * @param name The {@link Token#NAME} node.
 * @param var The parent of the {@code name} node, which must be a
 *     {@link Token#VAR} node.
 * @param parent {@code var}'s parent.
 * @param info the {@link JSDocInfo} information relating to this
 *     {@code name} node.
 */
private void defineName(Node name, Node var, Node parent, JSDocInfo info) {
  Node value = name.getFirstChild();

  if (value != null && value.getType() == Token.FUNCTION) {
    // function
    String functionName = name.getString();
    FunctionType functionType =
        getFunctionType(functionName, value, info, null);
    if (functionType.isReturnTypeInferred() &&
        scope.isLocal()) {
      defineSlot(name, var, null);
    } else {
      defineSlot(name, var, functionType);
    }
  } else {
    // variable's type
    JSType type = null;
    if (info == null) {
      // the variable's type will be inferred
      CompilerInput input = compiler.getInput(sourceName);
      Preconditions.checkNotNull(input, sourceName);
      type = input.isExtern() ?
          getNativeType(UNKNOWN_TYPE) : null;
    } else if (info.hasEnumParameterType()) {
      type = getEnumType(name.getString(), var, value,
          info.getEnumParameterType().evaluate(scope, typeRegistry));
    } else if (info.isConstructor()) {
      type = getFunctionType(name.getString(), value, info, name);
    } else {
      type = getDeclaredTypeInAnnotation(sourceName, name, info);
    }

    defineSlot(name, var, type);
  }
}
 
Example 12
Source File: Closure_43_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * If a variable is assigned a function literal in the global scope,
 * make that a declared type (even if there's no doc info).
 * There's only one exception to this rule:
 * if the return type is inferred, and we're in a local
 * scope, we should assume the whole function is inferred.
 */
private boolean shouldUseFunctionLiteralType(
    FunctionType type, JSDocInfo info, Node lValue) {
  if (info != null) {
    return true;
  }
  if (lValue != null &&
      NodeUtil.isObjectLitKey(lValue, lValue.getParent())) {
    return false;
  }
  return scope.isGlobal() || !type.isReturnTypeInferred();
}
 
Example 13
Source File: Closure_43_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * If a variable is assigned a function literal in the global scope,
 * make that a declared type (even if there's no doc info).
 * There's only one exception to this rule:
 * if the return type is inferred, and we're in a local
 * scope, we should assume the whole function is inferred.
 */
private boolean shouldUseFunctionLiteralType(
    FunctionType type, JSDocInfo info, Node lValue) {
  if (info != null) {
    return true;
  }
  if (lValue != null &&
      NodeUtil.isObjectLitKey(lValue, lValue.getParent())) {
    return false;
  }
  return scope.isGlobal() || !type.isReturnTypeInferred();
}
 
Example 14
Source File: Closure_54_TypedScopeCreator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * If a variable is assigned a function literal in the global scope,
 * make that a declared type (even if there's no doc info).
 * There's only one exception to this rule:
 * if the return type is inferred, and we're in a local
 * scope, we should assume the whole function is inferred.
 */
private boolean shouldUseFunctionLiteralType(
    FunctionType type, JSDocInfo info, Node lValue) {
  if (info != null) {
    return true;
  }
  if (lValue != null &&
      NodeUtil.isObjectLitKey(lValue, lValue.getParent())) {
    return false;
  }
  return scope.isGlobal() || !type.isReturnTypeInferred();
}
 
Example 15
Source File: Closure_54_TypedScopeCreator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * If a variable is assigned a function literal in the global scope,
 * make that a declared type (even if there's no doc info).
 * There's only one exception to this rule:
 * if the return type is inferred, and we're in a local
 * scope, we should assume the whole function is inferred.
 */
private boolean shouldUseFunctionLiteralType(
    FunctionType type, JSDocInfo info, Node lValue) {
  if (info != null) {
    return true;
  }
  if (lValue != null &&
      NodeUtil.isObjectLitKey(lValue, lValue.getParent())) {
    return false;
  }
  return scope.isGlobal() || !type.isReturnTypeInferred();
}
 
Example 16
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 17
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 18
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 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 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;
}