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

The following examples show how to use com.google.javascript.rhino.jstype.ObjectType#isEquivalentTo() . 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_6_TypeValidator_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Expect that the first type is the direct superclass of the second type.
 *
 * @param t The node traversal.
 * @param n The node where warnings should point to.
 * @param superObject The expected super instance type.
 * @param subObject The sub instance type.
 */
void expectSuperType(NodeTraversal t, Node n, ObjectType superObject,
    ObjectType subObject) {
  FunctionType subCtor = subObject.getConstructor();
  ObjectType implicitProto = subObject.getImplicitPrototype();
  ObjectType declaredSuper =
      implicitProto == null ? null : implicitProto.getImplicitPrototype();
  if (declaredSuper != null &&
      !(superObject instanceof UnknownType) &&
      !declaredSuper.isEquivalentTo(superObject)) {
    if (declaredSuper.isEquivalentTo(getNativeType(OBJECT_TYPE))) {
      registerMismatch(superObject, declaredSuper, report(
          t.makeError(n, MISSING_EXTENDS_TAG_WARNING, subObject.toString())));
    } else {
      mismatch(t.getSourceName(), n,
          "mismatch in declaration of superclass type",
          superObject, declaredSuper);
    }

    // Correct the super type.
    if (!subCtor.hasCachedValues()) {
      subCtor.setPrototypeBasedOn(superObject);
    }
  }
}
 
Example 2
Source File: Closure_6_TypeValidator_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Expect that the first type is the direct superclass of the second type.
 *
 * @param t The node traversal.
 * @param n The node where warnings should point to.
 * @param superObject The expected super instance type.
 * @param subObject The sub instance type.
 */
void expectSuperType(NodeTraversal t, Node n, ObjectType superObject,
    ObjectType subObject) {
  FunctionType subCtor = subObject.getConstructor();
  ObjectType implicitProto = subObject.getImplicitPrototype();
  ObjectType declaredSuper =
      implicitProto == null ? null : implicitProto.getImplicitPrototype();
  if (declaredSuper != null &&
      !(superObject instanceof UnknownType) &&
      !declaredSuper.isEquivalentTo(superObject)) {
    if (declaredSuper.isEquivalentTo(getNativeType(OBJECT_TYPE))) {
      registerMismatch(superObject, declaredSuper, report(
          t.makeError(n, MISSING_EXTENDS_TAG_WARNING, subObject.toString())));
    } else {
      mismatch(t.getSourceName(), n,
          "mismatch in declaration of superclass type",
          superObject, declaredSuper);
    }

    // Correct the super type.
    if (!subCtor.hasCachedValues()) {
      subCtor.setPrototypeBasedOn(superObject);
    }
  }
}
 
Example 3
Source File: TypeValidator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Expect that the first type is the direct superclass of the second type.
 *
 * @param t The node traversal.
 * @param n The node where warnings should point to.
 * @param superObject The expected super instance type.
 * @param subObject The sub instance type.
 */
void expectSuperType(NodeTraversal t, Node n, ObjectType superObject,
    ObjectType subObject) {
  FunctionType subCtor = subObject.getConstructor();
  ObjectType implicitProto = subObject.getImplicitPrototype();
  ObjectType declaredSuper =
      implicitProto == null ? null : implicitProto.getImplicitPrototype();
  if (declaredSuper != null &&
      !(superObject instanceof UnknownType) &&
      !declaredSuper.isEquivalentTo(superObject)) {
    if (declaredSuper.isEquivalentTo(getNativeType(OBJECT_TYPE))) {
      registerMismatch(superObject, declaredSuper, report(
          t.makeError(n, MISSING_EXTENDS_TAG_WARNING, subObject.toString())));
    } else {
      mismatch(t.getSourceName(), n,
          "mismatch in declaration of superclass type",
          superObject, declaredSuper);
    }

    // Correct the super type.
    if (!subCtor.hasCachedValues()) {
      subCtor.setPrototypeBasedOn(superObject);
    }
  }
}
 
Example 4
Source File: Nopol2017_0029_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Emit a warning if we can prove that a property cannot possibly be
 * defined on an object. Note the difference between JS and a strictly
 * statically typed language: we're checking if the property
 * *cannot be defined*, whereas a java compiler would check if the
 * property *can be undefined*.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  // If the property type is unknown, check the object type to see if it
  // can ever be defined. We explicitly exclude CHECKED_UNKNOWN (for
  // properties where we've checked that it exists, or for properties on
  // objects that aren't in this binary).
  JSType propType = getJSType(n);
  if (propType.isEquivalentTo(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
    childType = childType.autobox();
    ObjectType objectType = ObjectType.cast(childType);
    if (objectType != null) {
      // We special-case object types so that checks on enums can be
      // much stricter, and so that we can use hasProperty (which is much
      // faster in most cases).
      if (!objectType.hasProperty(propName) ||
          objectType.isEquivalentTo(
              typeRegistry.getNativeType(UNKNOWN_TYPE))) {
        if (objectType instanceof EnumType) {
          report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
        } else {
          checkPropertyAccessHelper(objectType, propName, t, n);
        }
      }

    } else {
      checkPropertyAccessHelper(childType, propName, t, n);
    }
  }
}
 
Example 5
Source File: Nopol2017_0029_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Emit a warning if we can prove that a property cannot possibly be
 * defined on an object. Note the difference between JS and a strictly
 * statically typed language: we're checking if the property
 * *cannot be defined*, whereas a java compiler would check if the
 * property *can be undefined*.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  // If the property type is unknown, check the object type to see if it
  // can ever be defined. We explicitly exclude CHECKED_UNKNOWN (for
  // properties where we've checked that it exists, or for properties on
  // objects that aren't in this binary).
  JSType propType = getJSType(n);
  if (propType.isEquivalentTo(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
    childType = childType.autobox();
    ObjectType objectType = ObjectType.cast(childType);
    if (objectType != null) {
      // We special-case object types so that checks on enums can be
      // much stricter, and so that we can use hasProperty (which is much
      // faster in most cases).
      if (!objectType.hasProperty(propName) ||
          objectType.isEquivalentTo(
              typeRegistry.getNativeType(UNKNOWN_TYPE))) {
        if (objectType instanceof EnumType) {
          report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
        } else {
          checkPropertyAccessHelper(objectType, propName, t, n);
        }
      }

    } else {
      checkPropertyAccessHelper(childType, propName, t, n);
    }
  }
}
 
Example 6
Source File: Closure_125_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Emit a warning if we can prove that a property cannot possibly be
 * defined on an object. Note the difference between JS and a strictly
 * statically typed language: we're checking if the property
 * *cannot be defined*, whereas a java compiler would check if the
 * property *can be undefined*.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  // If the property type is unknown, check the object type to see if it
  // can ever be defined. We explicitly exclude CHECKED_UNKNOWN (for
  // properties where we've checked that it exists, or for properties on
  // objects that aren't in this binary).
  JSType propType = getJSType(n);
  if (propType.isEquivalentTo(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
    childType = childType.autobox();
    ObjectType objectType = ObjectType.cast(childType);
    if (objectType != null) {
      // We special-case object types so that checks on enums can be
      // much stricter, and so that we can use hasProperty (which is much
      // faster in most cases).
      if (!objectType.hasProperty(propName) ||
          objectType.isEquivalentTo(
              typeRegistry.getNativeType(UNKNOWN_TYPE))) {
        if (objectType instanceof EnumType) {
          report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
        } else {
          checkPropertyAccessHelper(objectType, propName, t, n);
        }
      }

    } else {
      checkPropertyAccessHelper(childType, propName, t, n);
    }
  }
}
 
Example 7
Source File: Closure_125_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Emit a warning if we can prove that a property cannot possibly be
 * defined on an object. Note the difference between JS and a strictly
 * statically typed language: we're checking if the property
 * *cannot be defined*, whereas a java compiler would check if the
 * property *can be undefined*.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  // If the property type is unknown, check the object type to see if it
  // can ever be defined. We explicitly exclude CHECKED_UNKNOWN (for
  // properties where we've checked that it exists, or for properties on
  // objects that aren't in this binary).
  JSType propType = getJSType(n);
  if (propType.isEquivalentTo(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
    childType = childType.autobox();
    ObjectType objectType = ObjectType.cast(childType);
    if (objectType != null) {
      // We special-case object types so that checks on enums can be
      // much stricter, and so that we can use hasProperty (which is much
      // faster in most cases).
      if (!objectType.hasProperty(propName) ||
          objectType.isEquivalentTo(
              typeRegistry.getNativeType(UNKNOWN_TYPE))) {
        if (objectType instanceof EnumType) {
          report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
        } else {
          checkPropertyAccessHelper(objectType, propName, t, n);
        }
      }

    } else {
      checkPropertyAccessHelper(childType, propName, t, n);
    }
  }
}
 
Example 8
Source File: Closure_11_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Emit a warning if we can prove that a property cannot possibly be
 * defined on an object. Note the difference between JS and a strictly
 * statically typed language: we're checking if the property
 * *cannot be defined*, whereas a java compiler would check if the
 * property *can be undefined*.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  // If the property type is unknown, check the object type to see if it
  // can ever be defined. We explicitly exclude CHECKED_UNKNOWN (for
  // properties where we've checked that it exists, or for properties on
  // objects that aren't in this binary).
  JSType propType = getJSType(n);
  if (propType.isEquivalentTo(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
    childType = childType.autobox();
    ObjectType objectType = ObjectType.cast(childType);
    if (objectType != null) {
      // We special-case object types so that checks on enums can be
      // much stricter, and so that we can use hasProperty (which is much
      // faster in most cases).
      if (!objectType.hasProperty(propName) ||
          objectType.isEquivalentTo(
              typeRegistry.getNativeType(UNKNOWN_TYPE))) {
        if (objectType instanceof EnumType) {
          report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
        } else {
          checkPropertyAccessHelper(objectType, propName, t, n);
        }
      }

    } else {
      checkPropertyAccessHelper(childType, propName, t, n);
    }
  }
}
 
Example 9
Source File: Closure_11_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Emit a warning if we can prove that a property cannot possibly be
 * defined on an object. Note the difference between JS and a strictly
 * statically typed language: we're checking if the property
 * *cannot be defined*, whereas a java compiler would check if the
 * property *can be undefined*.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  // If the property type is unknown, check the object type to see if it
  // can ever be defined. We explicitly exclude CHECKED_UNKNOWN (for
  // properties where we've checked that it exists, or for properties on
  // objects that aren't in this binary).
  JSType propType = getJSType(n);
  if (propType.isEquivalentTo(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
    childType = childType.autobox();
    ObjectType objectType = ObjectType.cast(childType);
    if (objectType != null) {
      // We special-case object types so that checks on enums can be
      // much stricter, and so that we can use hasProperty (which is much
      // faster in most cases).
      if (!objectType.hasProperty(propName) ||
          objectType.isEquivalentTo(
              typeRegistry.getNativeType(UNKNOWN_TYPE))) {
        if (objectType instanceof EnumType) {
          report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
        } else {
          checkPropertyAccessHelper(objectType, propName, t, n);
        }
      }

    } else {
      checkPropertyAccessHelper(childType, propName, t, n);
    }
  }
}
 
Example 10
Source File: Closure_117_TypeValidator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect that the first type is the direct superclass of the second type.
 *
 * @param t The node traversal.
 * @param n The node where warnings should point to.
 * @param superObject The expected super instance type.
 * @param subObject The sub instance type.
 */
void expectSuperType(NodeTraversal t, Node n, ObjectType superObject,
    ObjectType subObject) {
  FunctionType subCtor = subObject.getConstructor();
  ObjectType implicitProto = subObject.getImplicitPrototype();
  ObjectType declaredSuper =
      implicitProto == null ? null : implicitProto.getImplicitPrototype();
  if (declaredSuper != null && declaredSuper.isTemplatizedType()) {
    declaredSuper =
        declaredSuper.toMaybeTemplatizedType().getReferencedType();
  }
  if (declaredSuper != null &&
      !(superObject instanceof UnknownType) &&
      !declaredSuper.isEquivalentTo(superObject)) {
    if (declaredSuper.isEquivalentTo(getNativeType(OBJECT_TYPE))) {
      registerMismatch(superObject, declaredSuper, report(
          t.makeError(n, MISSING_EXTENDS_TAG_WARNING, subObject.toString())));
    } else {
      mismatch(t.getSourceName(), n,
          "mismatch in declaration of superclass type",
          superObject, declaredSuper);
    }

    // Correct the super type.
    if (!subCtor.hasCachedValues()) {
      subCtor.setPrototypeBasedOn(superObject);
    }
  }
}
 
Example 11
Source File: Closure_117_TypeValidator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect that the first type is the direct superclass of the second type.
 *
 * @param t The node traversal.
 * @param n The node where warnings should point to.
 * @param superObject The expected super instance type.
 * @param subObject The sub instance type.
 */
void expectSuperType(NodeTraversal t, Node n, ObjectType superObject,
    ObjectType subObject) {
  FunctionType subCtor = subObject.getConstructor();
  ObjectType implicitProto = subObject.getImplicitPrototype();
  ObjectType declaredSuper =
      implicitProto == null ? null : implicitProto.getImplicitPrototype();
  if (declaredSuper != null && declaredSuper.isTemplatizedType()) {
    declaredSuper =
        declaredSuper.toMaybeTemplatizedType().getReferencedType();
  }
  if (declaredSuper != null &&
      !(superObject instanceof UnknownType) &&
      !declaredSuper.isEquivalentTo(superObject)) {
    if (declaredSuper.isEquivalentTo(getNativeType(OBJECT_TYPE))) {
      registerMismatch(superObject, declaredSuper, report(
          t.makeError(n, MISSING_EXTENDS_TAG_WARNING, subObject.toString())));
    } else {
      mismatch(t.getSourceName(), n,
          "mismatch in declaration of superclass type",
          superObject, declaredSuper);
    }

    // Correct the super type.
    if (!subCtor.hasCachedValues()) {
      subCtor.setPrototypeBasedOn(superObject);
    }
  }
}
 
Example 12
Source File: Closure_2_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Emit a warning if we can prove that a property cannot possibly be
 * defined on an object. Note the difference between JS and a strictly
 * statically typed language: we're checking if the property
 * *cannot be defined*, whereas a java compiler would check if the
 * property *can be undefined*.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  // If the property type is unknown, check the object type to see if it
  // can ever be defined. We explicitly exclude CHECKED_UNKNOWN (for
  // properties where we've checked that it exists, or for properties on
  // objects that aren't in this binary).
  JSType propType = getJSType(n);
  if (propType.isEquivalentTo(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
    childType = childType.autobox();
    ObjectType objectType = ObjectType.cast(childType);
    if (objectType != null) {
      // We special-case object types so that checks on enums can be
      // much stricter, and so that we can use hasProperty (which is much
      // faster in most cases).
      if (!objectType.hasProperty(propName) ||
          objectType.isEquivalentTo(
              typeRegistry.getNativeType(UNKNOWN_TYPE))) {
        if (objectType instanceof EnumType) {
          report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
        } else {
          checkPropertyAccessHelper(objectType, propName, t, n);
        }
      }

    } else {
      checkPropertyAccessHelper(childType, propName, t, n);
    }
  }
}
 
Example 13
Source File: Closure_2_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Emit a warning if we can prove that a property cannot possibly be
 * defined on an object. Note the difference between JS and a strictly
 * statically typed language: we're checking if the property
 * *cannot be defined*, whereas a java compiler would check if the
 * property *can be undefined*.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  // If the property type is unknown, check the object type to see if it
  // can ever be defined. We explicitly exclude CHECKED_UNKNOWN (for
  // properties where we've checked that it exists, or for properties on
  // objects that aren't in this binary).
  JSType propType = getJSType(n);
  if (propType.isEquivalentTo(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
    childType = childType.autobox();
    ObjectType objectType = ObjectType.cast(childType);
    if (objectType != null) {
      // We special-case object types so that checks on enums can be
      // much stricter, and so that we can use hasProperty (which is much
      // faster in most cases).
      if (!objectType.hasProperty(propName) ||
          objectType.isEquivalentTo(
              typeRegistry.getNativeType(UNKNOWN_TYPE))) {
        if (objectType instanceof EnumType) {
          report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
        } else {
          checkPropertyAccessHelper(objectType, propName, t, n);
        }
      }

    } else {
      checkPropertyAccessHelper(childType, propName, t, n);
    }
  }
}
 
Example 14
Source File: TypeCheck.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Emit a warning if we can prove that a property cannot possibly be
 * defined on an object. Note the difference between JS and a strictly
 * statically typed language: we're checking if the property
 * *cannot be defined*, whereas a java compiler would check if the
 * property *can be undefined*.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  // If the property type is unknown, check the object type to see if it
  // can ever be defined. We explicitly exclude CHECKED_UNKNOWN (for
  // properties where we've checked that it exists, or for properties on
  // objects that aren't in this binary).
  JSType propType = getJSType(n);
  if (propType.isEquivalentTo(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
    childType = childType.autobox();
    ObjectType objectType = ObjectType.cast(childType);
    if (objectType != null) {
      // We special-case object types so that checks on enums can be
      // much stricter, and so that we can use hasProperty (which is much
      // faster in most cases).
      if (!objectType.hasProperty(propName) ||
          objectType.isEquivalentTo(
              typeRegistry.getNativeType(UNKNOWN_TYPE))) {
        if (objectType instanceof EnumType) {
          report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
        } else {
          checkPropertyAccessHelper(objectType, propName, t, n);
        }
      }

    } else {
      checkPropertyAccessHelper(childType, propName, t, n);
    }
  }
}