Java Code Examples for com.google.javascript.rhino.jstype.JSTypeRegistry#createFunctionType()

The following examples show how to use com.google.javascript.rhino.jstype.JSTypeRegistry#createFunctionType() . 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: 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 2
Source File: TypeValidatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testFunctionMismatch() throws Exception {
  testSame(
      "/** \n" +
      " * @param {function(string): number} x \n" +
      " * @return {function(boolean): string} \n" +
      " */ function f(x) { return x; }",
      TYPE_MISMATCH_WARNING);

  JSTypeRegistry registry = compiler.getTypeRegistry();
  JSType string = registry.getNativeType(STRING_TYPE);
  JSType bool = registry.getNativeType(BOOLEAN_TYPE);
  JSType number = registry.getNativeType(NUMBER_TYPE);
  JSType firstFunction = registry.createFunctionType(number, string);
  JSType secondFunction = registry.createFunctionType(string, bool);

  assertMismatches(
      Lists.newArrayList(
          new TypeMismatch(firstFunction, secondFunction, null),
          fromNatives(STRING_TYPE, BOOLEAN_TYPE),
          fromNatives(NUMBER_TYPE, STRING_TYPE)));
}
 
Example 3
Source File: TypeValidatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testFunctionMismatch2() throws Exception {
  testSame(
      "/** \n" +
      " * @param {function(string): number} x \n" +
      " * @return {function(boolean): number} \n" +
      " */ function f(x) { return x; }",
      TYPE_MISMATCH_WARNING);

  JSTypeRegistry registry = compiler.getTypeRegistry();
  JSType string = registry.getNativeType(STRING_TYPE);
  JSType bool = registry.getNativeType(BOOLEAN_TYPE);
  JSType number = registry.getNativeType(NUMBER_TYPE);
  JSType firstFunction = registry.createFunctionType(number, string);
  JSType secondFunction = registry.createFunctionType(number, bool);

  assertMismatches(
      Lists.newArrayList(
          new TypeMismatch(firstFunction, secondFunction, null),
          fromNatives(STRING_TYPE, BOOLEAN_TYPE)));
}