Java Code Examples for com.google.javascript.rhino.jstype.ObjectType#getPropertyType()

The following examples show how to use com.google.javascript.rhino.jstype.ObjectType#getPropertyType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Closure_48_TypedScopeCreator_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Find the function that's being overridden on this type, if any.
 */
private FunctionType findOverriddenFunction(
    ObjectType ownerType, String propName) {
  // First, check to see if the property is implemented
  // on a superclass.
  JSType propType = ownerType.getPropertyType(propName);
  if (propType != null && propType.isFunctionType()) {
    return propType.toMaybeFunctionType();
  } else {
    // If it's not, then check to see if it's implemented
    // on an implemented interface.
    for (ObjectType iface :
             ownerType.getCtorImplementedInterfaces()) {
      propType = iface.getPropertyType(propName);
      if (propType != null && propType.isFunctionType()) {
        return propType.toMaybeFunctionType();
      }
    }
  }

  return null;
}
 
Example 2
Source File: InferJSDocInfoTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testAbstractMethod() {
  testSame(
      "/** Abstract method. \n * @type {!Function} */ var abstractMethod;" +
      "/** @constructor */ function Foo() {}" +
      "/** Block description. \n * @param {number} x */" +
      "Foo.prototype.bar = abstractMethod;");
  FunctionType abstractMethod =
      (FunctionType) findGlobalNameType("abstractMethod");
  assertNull(abstractMethod.getJSDocInfo());

  FunctionType ctor = (FunctionType) findGlobalNameType("Foo");
  ObjectType proto = ctor.getInstanceType().getImplicitPrototype();
  FunctionType method = (FunctionType) proto.getPropertyType("bar");
  assertEquals(
      "Block description.",
      method.getJSDocInfo().getBlockDescription());
  assertEquals(
      "Block description.",
      proto.getOwnPropertyJSDocInfo("bar").getBlockDescription());
}
 
Example 3
Source File: Closure_95_TypedScopeCreator_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Find the function that's being overridden on this type, if any.
 */
private FunctionType findOverriddenFunction(
    ObjectType ownerType, String propName) {
  // First, check to see if the property is implemented
  // on a superclass.
  JSType propType = ownerType.getPropertyType(propName);
  if (propType instanceof FunctionType) {
    return (FunctionType) propType;
  } else {
    // If it's not, then check to see if it's implemented
    // on an implemented interface.
    for (ObjectType iface :
             ownerType.getCtorImplementedInterfaces()) {
      propType = iface.getPropertyType(propName);
      if (propType instanceof FunctionType) {
        return (FunctionType) propType;
      }
    }
  }

  return null;
}
 
Example 4
Source File: Closure_95_TypedScopeCreator_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Find the function that's being overridden on this type, if any.
 */
private FunctionType findOverriddenFunction(
    ObjectType ownerType, String propName) {
  // First, check to see if the property is implemented
  // on a superclass.
  JSType propType = ownerType.getPropertyType(propName);
  if (propType instanceof FunctionType) {
    return (FunctionType) propType;
  } else {
    // If it's not, then check to see if it's implemented
    // on an implemented interface.
    for (ObjectType iface :
             ownerType.getCtorImplementedInterfaces()) {
      propType = iface.getPropertyType(propName);
      if (propType instanceof FunctionType) {
        return (FunctionType) propType;
      }
    }
  }

  return null;
}
 
Example 5
Source File: Closure_35_TypeInference_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Suppose X is an object with inferred properties.
 * Suppose also that X is used in a way where it would only type-check
 * correctly if some of those properties are widened.
 * Then we should be polite and automatically widen X's properties for him.
 *
 * For a concrete example, consider:
 * param x {{prop: (number|undefined)}}
 * function f(x) {}
 * f({});
 *
 * If we give the anonymous object an inferred property of (number|undefined),
 * then this code will type-check appropriately.
 */
private void inferPropertyTypesToMatchConstraint(
    JSType type, JSType constraint) {
  if (type == null || constraint == null) {
    return;
  }

  ObjectType constraintObj =
      ObjectType.cast(constraint.restrictByNotNullOrUndefined());
  if (constraintObj != null && constraintObj.isRecordType()) {
    ObjectType objType = ObjectType.cast(type.restrictByNotNullOrUndefined());
    if (objType != null) {
      for (String prop : constraintObj.getOwnPropertyNames()) {
        JSType propType = constraintObj.getPropertyType(prop);
        if (!objType.isPropertyTypeDeclared(prop)) {
          JSType typeToInfer = propType;
          if (!objType.hasProperty(prop)) {
            typeToInfer =
                getNativeType(VOID_TYPE).getLeastSupertype(propType);
          }
          objType.defineInferredProperty(prop, typeToInfer, null);
        }
      }
    }
  }
}
 
Example 6
Source File: Closure_54_TypedScopeCreator_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Find the function that's being overridden on this type, if any.
 */
private FunctionType findOverriddenFunction(
    ObjectType ownerType, String propName) {
  // First, check to see if the property is implemented
  // on a superclass.
  JSType propType = ownerType.getPropertyType(propName);
  if (propType != null && propType.isFunctionType()) {
    return propType.toMaybeFunctionType();
  } else {
    // If it's not, then check to see if it's implemented
    // on an implemented interface.
    for (ObjectType iface :
             ownerType.getCtorImplementedInterfaces()) {
      propType = iface.getPropertyType(propName);
      if (propType != null && propType.isFunctionType()) {
        return propType.toMaybeFunctionType();
      }
    }
  }

  return null;
}
 
Example 7
Source File: Closure_54_TypedScopeCreator_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Find the function that's being overridden on this type, if any.
 */
private FunctionType findOverriddenFunction(
    ObjectType ownerType, String propName) {
  // First, check to see if the property is implemented
  // on a superclass.
  JSType propType = ownerType.getPropertyType(propName);
  if (propType != null && propType.isFunctionType()) {
    return propType.toMaybeFunctionType();
  } else {
    // If it's not, then check to see if it's implemented
    // on an implemented interface.
    for (ObjectType iface :
             ownerType.getCtorImplementedInterfaces()) {
      propType = iface.getPropertyType(propName);
      if (propType != null && propType.isFunctionType()) {
        return propType.toMaybeFunctionType();
      }
    }
  }

  return null;
}
 
Example 8
Source File: Closure_43_TypedScopeCreator_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Find the function that's being overridden on this type, if any.
 */
private FunctionType findOverriddenFunction(
    ObjectType ownerType, String propName) {
  // First, check to see if the property is implemented
  // on a superclass.
  JSType propType = ownerType.getPropertyType(propName);
  if (propType != null && propType.isFunctionType()) {
    return propType.toMaybeFunctionType();
  } else {
    // If it's not, then check to see if it's implemented
    // on an implemented interface.
    for (ObjectType iface :
             ownerType.getCtorImplementedInterfaces()) {
      propType = iface.getPropertyType(propName);
      if (propType != null && propType.isFunctionType()) {
        return propType.toMaybeFunctionType();
      }
    }
  }

  return null;
}
 
Example 9
Source File: Closure_43_TypedScopeCreator_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Find the function that's being overridden on this type, if any.
 */
private FunctionType findOverriddenFunction(
    ObjectType ownerType, String propName) {
  // First, check to see if the property is implemented
  // on a superclass.
  JSType propType = ownerType.getPropertyType(propName);
  if (propType != null && propType.isFunctionType()) {
    return propType.toMaybeFunctionType();
  } else {
    // If it's not, then check to see if it's implemented
    // on an implemented interface.
    for (ObjectType iface :
             ownerType.getCtorImplementedInterfaces()) {
      propType = iface.getPropertyType(propName);
      if (propType != null && propType.isFunctionType()) {
        return propType.toMaybeFunctionType();
      }
    }
  }

  return null;
}
 
Example 10
Source File: Closure_48_TypedScopeCreator_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Find the function that's being overridden on this type, if any.
 */
private FunctionType findOverriddenFunction(
    ObjectType ownerType, String propName) {
  // First, check to see if the property is implemented
  // on a superclass.
  JSType propType = ownerType.getPropertyType(propName);
  if (propType != null && propType.isFunctionType()) {
    return propType.toMaybeFunctionType();
  } else {
    // If it's not, then check to see if it's implemented
    // on an implemented interface.
    for (ObjectType iface :
             ownerType.getCtorImplementedInterfaces()) {
      propType = iface.getPropertyType(propName);
      if (propType != null && propType.isFunctionType()) {
        return propType.toMaybeFunctionType();
      }
    }
  }

  return null;
}
 
Example 11
Source File: TypedScopeCreatorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void testPrototypePropertyMethodWithoutAnnotation() {
  testSame("var Foo = function Foo() {};" +
           "var proto = Foo.prototype = {" +
           "   bar: function(a, b){}" +
           "};" +
           "proto.baz = function(c) {};" +
           "(function() { proto.baz = function() {}; })();");
  ObjectType foo = (ObjectType) findNameType("Foo", globalScope);
  assertTrue(foo.hasProperty("prototype"));

  ObjectType fooProto = (ObjectType) foo.getPropertyType("prototype");
  assertTrue(fooProto.hasProperty("bar"));
  assertEquals("function (?, ?): undefined",
      fooProto.getPropertyType("bar").toString());

  assertTrue(fooProto.hasProperty("baz"));
  assertEquals("function (?): undefined",
      fooProto.getPropertyType("baz").toString());
}
 
Example 12
Source File: Closure_70_TypedScopeCreator_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Find the function that's being overridden on this type, if any.
 */
private FunctionType findOverriddenFunction(
    ObjectType ownerType, String propName) {
  // First, check to see if the property is implemented
  // on a superclass.
  JSType propType = ownerType.getPropertyType(propName);
  if (propType instanceof FunctionType) {
    return (FunctionType) propType;
  } else {
    // If it's not, then check to see if it's implemented
    // on an implemented interface.
    for (ObjectType iface :
             ownerType.getCtorImplementedInterfaces()) {
      propType = iface.getPropertyType(propName);
      if (propType instanceof FunctionType) {
        return (FunctionType) propType;
      }
    }
  }

  return null;
}
 
Example 13
Source File: Closure_17_TypedScopeCreator_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Find the function that's being overridden on this type, if any.
 */
private FunctionType findOverriddenFunction(
    ObjectType ownerType, String propName) {
  // First, check to see if the property is implemented
  // on a superclass.
  JSType propType = ownerType.getPropertyType(propName);
  if (propType != null && propType.isFunctionType()) {
    return propType.toMaybeFunctionType();
  } else {
    // If it's not, then check to see if it's implemented
    // on an implemented interface.
    for (ObjectType iface :
             ownerType.getCtorImplementedInterfaces()) {
      propType = iface.getPropertyType(propName);
      if (propType != null && propType.isFunctionType()) {
        return propType.toMaybeFunctionType();
      }
    }
  }

  return null;
}
 
Example 14
Source File: Closure_17_TypedScopeCreator_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Find the function that's being overridden on this type, if any.
 */
private FunctionType findOverriddenFunction(
    ObjectType ownerType, String propName) {
  // First, check to see if the property is implemented
  // on a superclass.
  JSType propType = ownerType.getPropertyType(propName);
  if (propType != null && propType.isFunctionType()) {
    return propType.toMaybeFunctionType();
  } else {
    // If it's not, then check to see if it's implemented
    // on an implemented interface.
    for (ObjectType iface :
             ownerType.getCtorImplementedInterfaces()) {
      propType = iface.getPropertyType(propName);
      if (propType != null && propType.isFunctionType()) {
        return propType.toMaybeFunctionType();
      }
    }
  }

  return null;
}
 
Example 15
Source File: Nopol2017_0027_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Find the function that's being overridden on this type, if any.
 */
private FunctionType findOverriddenFunction(
    ObjectType ownerType, String propName) {
  // First, check to see if the property is implemented
  // on a superclass.
  JSType propType = ownerType.getPropertyType(propName);
  if (propType != null && propType.isFunctionType()) {
    return propType.toMaybeFunctionType();
  } else {
    // If it's not, then check to see if it's implemented
    // on an implemented interface.
    for (ObjectType iface :
             ownerType.getCtorImplementedInterfaces()) {
      propType = iface.getPropertyType(propName);
      if (propType != null && propType.isFunctionType()) {
        return propType.toMaybeFunctionType();
      }
    }
  }

  return null;
}
 
Example 16
Source File: Nopol2017_0027_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Find the function that's being overridden on this type, if any.
 */
private FunctionType findOverriddenFunction(
    ObjectType ownerType, String propName) {
  // First, check to see if the property is implemented
  // on a superclass.
  JSType propType = ownerType.getPropertyType(propName);
  if (propType != null && propType.isFunctionType()) {
    return propType.toMaybeFunctionType();
  } else {
    // If it's not, then check to see if it's implemented
    // on an implemented interface.
    for (ObjectType iface :
             ownerType.getCtorImplementedInterfaces()) {
      propType = iface.getPropertyType(propName);
      if (propType != null && propType.isFunctionType()) {
        return propType.toMaybeFunctionType();
      }
    }
  }

  return null;
}
 
Example 17
Source File: DeclarationGenerator.java    From clutz with MIT License 6 votes vote down vote up
private void visitProperty(
    String propName,
    ObjectType objType,
    boolean isStatic,
    boolean forcePropDeclaration,
    boolean isNamespace,
    List<String> classTemplateTypeNames) {
  JSType propertyType = objType.getPropertyType(propName);
  // The static methods from the function prototype are provided by lib.d.ts.
  if (isStatic && isFunctionPrototypeProp(propName)) return;
  JSDocInfo jsdoc = objType.getOwnPropertyJSDocInfo(propName);
  maybeEmitJsDoc(jsdoc, /* ignoreParams */ false);
  boolean isProtected = isProtectedProperty(objType, propName);
  JSDocInfo jsDocInfo = objType.getOwnPropertyJSDocInfo(propName);
  boolean isAbstract = jsDocInfo != null && jsDocInfo.isAbstract();
  emitProperty(
      propName,
      propertyType,
      isStatic,
      isProtected,
      isAbstract,
      forcePropDeclaration,
      isNamespace,
      classTemplateTypeNames);
}
 
Example 18
Source File: DeclarationGenerator.java    From clutz with MIT License 6 votes vote down vote up
/**
 * Emits the given set of properties.
 *
 * @param isInNamespace if true, emit the properties in a form suitable for namespace members
 *     (e.g. with "let" and "function" prefixes). If false, emit suitable for properties
 *     declared in a class or interface.
 */
private void visitStaticProperties(
    ObjectType objType, Set<String> propNames, boolean isInNamespace) {
  for (String propName : propNames) {
    if (isInNamespace) {
      JSType propType = objType.getPropertyType(propName);
      if (propType.toMaybeFunctionType() == null) {
        emit("let");
      } else {
        emit("function");
      }
    }
    visitProperty(
        propName,
        objType,
        !isInNamespace,
        false,
        isInNamespace,
        Collections.<String>emptyList());
  }
}
 
Example 19
Source File: TypedScopeCreatorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testEnumProperty() {
  testSame("var foo = {}; /** @enum */ foo.Bar = {XXX: 'xxx'};");
  ObjectType foo = (ObjectType) findNameType("foo", globalScope);
  assertTrue(foo.hasProperty("Bar"));
  assertFalse(foo.isPropertyTypeInferred("Bar"));
  assertTrue(foo.isPropertyTypeDeclared("Bar"));

  JSType fooBar = foo.getPropertyType("Bar");
  assertEquals("enum{foo.Bar}", fooBar.toString());
  Asserts.assertTypeCollectionEquals(
      Lists.newArrayList(foo), registry.getTypesWithProperty("Bar"));
}
 
Example 20
Source File: TypedScopeCreatorTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void testConstructorProperty() {
  testSame("var foo = {}; /** @constructor */ foo.Bar = function() {};");
  ObjectType foo = (ObjectType) findNameType("foo", globalScope);
  assertTrue(foo.hasProperty("Bar"));
  assertFalse(foo.isPropertyTypeInferred("Bar"));

  JSType fooBar = foo.getPropertyType("Bar");
  assertEquals("function (new:foo.Bar): undefined", fooBar.toString());
  Asserts.assertTypeCollectionEquals(
      Lists.newArrayList(foo), registry.getTypesWithProperty("Bar"));
}