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

The following examples show how to use com.google.javascript.rhino.jstype.FunctionType#getTopMostDefiningType() . 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_96_TypeCheck_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Given a constructor type and a property name, check that the property has
 * the JSDoc annotation @override iff the property is declared on a
 * superclass. Several checks regarding inheritance correctness are also
 * performed.
 */
private void checkDeclaredPropertyInheritance(
    NodeTraversal t, Node n, FunctionType ctorType, String propertyName,
    JSDocInfo info, JSType propertyType) {
  // TODO(user): We're not 100% confident that type-checking works,
  // so we return quietly if the unknown type is a superclass of this type.
  // Remove this check as we become more confident. We should flag a warning
  // when the unknown type is on the inheritance chain, as it is likely
  // because of a programmer error.
  if (ctorType.hasUnknownSupertype()) {
    return;
  }

  FunctionType superClass = ctorType.getSuperClassConstructor();
  boolean superClassHasProperty = superClass != null &&
      superClass.getPrototype().hasProperty(propertyName);
  boolean declaredOverride = info != null && info.isOverride();

  boolean foundInterfaceProperty = false;
  if (ctorType.isConstructor()) {
    for (JSType implementedInterface : ctorType.getImplementedInterfaces()) {
      if (implementedInterface.isUnknownType()) {
        continue;
      }
      FunctionType interfaceType =
          implementedInterface.toObjectType().getConstructor();
      boolean interfaceHasProperty =
          interfaceType.getPrototype().hasProperty(propertyName);
      foundInterfaceProperty = foundInterfaceProperty || interfaceHasProperty;
      if (reportMissingOverride.isOn() && !declaredOverride &&
          interfaceHasProperty) {
        // @override not present, but the property does override an interface
        // property
        compiler.report(t.makeError(n, reportMissingOverride,
            HIDDEN_INTERFACE_PROPERTY, propertyName,
            interfaceType.getTopMostDefiningType(propertyName).toString()));
      }
      if (!declaredOverride) {
        continue;
      }
      // @override is present and we have to check that it is ok
      if (interfaceHasProperty) {
        JSType interfacePropType =
            interfaceType.getPrototype().getPropertyType(propertyName);
        if (!propertyType.canAssignTo(interfacePropType)) {
          compiler.report(t.makeError(n,
              HIDDEN_INTERFACE_PROPERTY_MISMATCH, propertyName,
              interfaceType.getTopMostDefiningType(propertyName).toString(),
              interfacePropType.toString(), propertyType.toString()));
        }
      }
    }
  }

  if (!declaredOverride && !superClassHasProperty) {
    // nothing to do here, it's just a plain new property
    return;
  }

  JSType topInstanceType = superClassHasProperty ?
      superClass.getTopMostDefiningType(propertyName) : null;
  if (reportMissingOverride.isOn() && ctorType.isConstructor() &&
      !declaredOverride && superClassHasProperty) {
    // @override not present, but the property does override a superclass
    // property
    compiler.report(t.makeError(n, reportMissingOverride,
        HIDDEN_SUPERCLASS_PROPERTY, propertyName,
        topInstanceType.toString()));
  }
  if (!declaredOverride) {
    // there's no @override to check
    return;
  }
  // @override is present and we have to check that it is ok
  if (superClassHasProperty) {
    // there is a superclass implementation
    JSType superClassPropType =
        superClass.getPrototype().getPropertyType(propertyName);
    if (!propertyType.canAssignTo(superClassPropType)) {
      compiler.report(
          t.makeError(n, HIDDEN_SUPERCLASS_PROPERTY_MISMATCH,
              propertyName, topInstanceType.toString(),
              superClassPropType.toString(), propertyType.toString()));
    }
  } else if (!foundInterfaceProperty) {
    // there is no superclass nor interface implementation
    compiler.report(
        t.makeError(n, UNKNOWN_OVERRIDE,
            propertyName, ctorType.getInstanceType().toString()));
  }
}
 
Example 2
Source File: Closure_96_TypeCheck_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Given a constructor type and a property name, check that the property has
 * the JSDoc annotation @override iff the property is declared on a
 * superclass. Several checks regarding inheritance correctness are also
 * performed.
 */
private void checkDeclaredPropertyInheritance(
    NodeTraversal t, Node n, FunctionType ctorType, String propertyName,
    JSDocInfo info, JSType propertyType) {
  // TODO(user): We're not 100% confident that type-checking works,
  // so we return quietly if the unknown type is a superclass of this type.
  // Remove this check as we become more confident. We should flag a warning
  // when the unknown type is on the inheritance chain, as it is likely
  // because of a programmer error.
  if (ctorType.hasUnknownSupertype()) {
    return;
  }

  FunctionType superClass = ctorType.getSuperClassConstructor();
  boolean superClassHasProperty = superClass != null &&
      superClass.getPrototype().hasProperty(propertyName);
  boolean declaredOverride = info != null && info.isOverride();

  boolean foundInterfaceProperty = false;
  if (ctorType.isConstructor()) {
    for (JSType implementedInterface : ctorType.getImplementedInterfaces()) {
      if (implementedInterface.isUnknownType()) {
        continue;
      }
      FunctionType interfaceType =
          implementedInterface.toObjectType().getConstructor();
      boolean interfaceHasProperty =
          interfaceType.getPrototype().hasProperty(propertyName);
      foundInterfaceProperty = foundInterfaceProperty || interfaceHasProperty;
      if (reportMissingOverride.isOn() && !declaredOverride &&
          interfaceHasProperty) {
        // @override not present, but the property does override an interface
        // property
        compiler.report(t.makeError(n, reportMissingOverride,
            HIDDEN_INTERFACE_PROPERTY, propertyName,
            interfaceType.getTopMostDefiningType(propertyName).toString()));
      }
      if (!declaredOverride) {
        continue;
      }
      // @override is present and we have to check that it is ok
      if (interfaceHasProperty) {
        JSType interfacePropType =
            interfaceType.getPrototype().getPropertyType(propertyName);
        if (!propertyType.canAssignTo(interfacePropType)) {
          compiler.report(t.makeError(n,
              HIDDEN_INTERFACE_PROPERTY_MISMATCH, propertyName,
              interfaceType.getTopMostDefiningType(propertyName).toString(),
              interfacePropType.toString(), propertyType.toString()));
        }
      }
    }
  }

  if (!declaredOverride && !superClassHasProperty) {
    // nothing to do here, it's just a plain new property
    return;
  }

  JSType topInstanceType = superClassHasProperty ?
      superClass.getTopMostDefiningType(propertyName) : null;
  if (reportMissingOverride.isOn() && ctorType.isConstructor() &&
      !declaredOverride && superClassHasProperty) {
    // @override not present, but the property does override a superclass
    // property
    compiler.report(t.makeError(n, reportMissingOverride,
        HIDDEN_SUPERCLASS_PROPERTY, propertyName,
        topInstanceType.toString()));
  }
  if (!declaredOverride) {
    // there's no @override to check
    return;
  }
  // @override is present and we have to check that it is ok
  if (superClassHasProperty) {
    // there is a superclass implementation
    JSType superClassPropType =
        superClass.getPrototype().getPropertyType(propertyName);
    if (!propertyType.canAssignTo(superClassPropType)) {
      compiler.report(
          t.makeError(n, HIDDEN_SUPERCLASS_PROPERTY_MISMATCH,
              propertyName, topInstanceType.toString(),
              superClassPropType.toString(), propertyType.toString()));
    }
  } else if (!foundInterfaceProperty) {
    // there is no superclass nor interface implementation
    compiler.report(
        t.makeError(n, UNKNOWN_OVERRIDE,
            propertyName, ctorType.getInstanceType().toString()));
  }
}