com.google.javascript.rhino.jstype.EnumType Java Examples

The following examples show how to use com.google.javascript.rhino.jstype.EnumType. 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: InferJSDocInfo.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Handle cases #1 and #3 in the class doc.
 */
private void attachJSDocInfoToNominalTypeOrShape(
    ObjectType objType, JSDocInfo docInfo, @Nullable String qName) {
  if (objType.isConstructor() ||
      objType.isEnumType() ||
      objType.isInterface()) {
    // Named types.
    if (objType.hasReferenceName() &&
        objType.getReferenceName().equals(qName)) {
      objType.setJSDocInfo(docInfo);

      if (objType.isConstructor() || objType.isInterface()) {
        JSType.toMaybeFunctionType(objType).getInstanceType().setJSDocInfo(
            docInfo);
      } else if (objType instanceof EnumType) {
        ((EnumType) objType).getElementsType().setJSDocInfo(docInfo);
      }
    }
  } else if (!objType.isNativeObjectType() &&
      objType.isFunctionType()) {
    // Structural functions.
    objType.setJSDocInfo(docInfo);
  }
}
 
Example #2
Source File: Closure_66_TypeCheck_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example #3
Source File: Closure_66_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example #4
Source File: Closure_125_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * <p>Checks enum aliases.
 *
 * <p>We verify that the enum element type of the enum used
 * for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Example:</p>
 * <pre>var myEnum = myOtherEnum;</pre>
 *
 * <p>Enum aliases are irregular, so we need special code for this :(</p>
 *
 * @param value the value used for initialization of the enum
 */
private void checkEnumAlias(
    NodeTraversal t, JSDocInfo declInfo, Node value) {
  if (declInfo == null || !declInfo.hasEnumParameterType()) {
    return;
  }

  JSType valueType = getJSType(value);
  if (!valueType.isEnumType()) {
    return;
  }

  EnumType valueEnumType = valueType.toMaybeEnumType();
  JSType valueEnumPrimitiveType =
      valueEnumType.getElementsType().getPrimitiveType();
  validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
      declInfo.getEnumParameterType().evaluate(t.getScope(), typeRegistry),
      "incompatible enum element types");
}
 
Example #5
Source File: Closure_11_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * <p>Checks enum aliases.
 *
 * <p>We verify that the enum element type of the enum used
 * for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Example:</p>
 * <pre>var myEnum = myOtherEnum;</pre>
 *
 * <p>Enum aliases are irregular, so we need special code for this :(</p>
 *
 * @param value the value used for initialization of the enum
 */
private void checkEnumAlias(
    NodeTraversal t, JSDocInfo declInfo, Node value) {
  if (declInfo == null || !declInfo.hasEnumParameterType()) {
    return;
  }

  JSType valueType = getJSType(value);
  if (!valueType.isEnumType()) {
    return;
  }

  EnumType valueEnumType = valueType.toMaybeEnumType();
  JSType valueEnumPrimitiveType =
      valueEnumType.getElementsType().getPrimitiveType();
  validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
      declInfo.getEnumParameterType().evaluate(t.getScope(), typeRegistry),
      "incompatible enum element types");
}
 
Example #6
Source File: Closure_125_TypeCheck_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * <p>Checks enum aliases.
 *
 * <p>We verify that the enum element type of the enum used
 * for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Example:</p>
 * <pre>var myEnum = myOtherEnum;</pre>
 *
 * <p>Enum aliases are irregular, so we need special code for this :(</p>
 *
 * @param value the value used for initialization of the enum
 */
private void checkEnumAlias(
    NodeTraversal t, JSDocInfo declInfo, Node value) {
  if (declInfo == null || !declInfo.hasEnumParameterType()) {
    return;
  }

  JSType valueType = getJSType(value);
  if (!valueType.isEnumType()) {
    return;
  }

  EnumType valueEnumType = valueType.toMaybeEnumType();
  JSType valueEnumPrimitiveType =
      valueEnumType.getElementsType().getPrimitiveType();
  validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
      declInfo.getEnumParameterType().evaluate(t.getScope(), typeRegistry),
      "incompatible enum element types");
}
 
Example #7
Source File: Closure_11_TypeCheck_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * <p>Checks enum aliases.
 *
 * <p>We verify that the enum element type of the enum used
 * for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Example:</p>
 * <pre>var myEnum = myOtherEnum;</pre>
 *
 * <p>Enum aliases are irregular, so we need special code for this :(</p>
 *
 * @param value the value used for initialization of the enum
 */
private void checkEnumAlias(
    NodeTraversal t, JSDocInfo declInfo, Node value) {
  if (declInfo == null || !declInfo.hasEnumParameterType()) {
    return;
  }

  JSType valueType = getJSType(value);
  if (!valueType.isEnumType()) {
    return;
  }

  EnumType valueEnumType = valueType.toMaybeEnumType();
  JSType valueEnumPrimitiveType =
      valueEnumType.getElementsType().getPrimitiveType();
  validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
      declInfo.getEnumParameterType().evaluate(t.getScope(), typeRegistry),
      "incompatible enum element types");
}
 
Example #8
Source File: Closure_69_TypeCheck_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example #9
Source File: Closure_96_TypeCheck_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example #10
Source File: Closure_69_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example #11
Source File: Nopol2017_0029_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * <p>Checks enum aliases.
 *
 * <p>We verify that the enum element type of the enum used
 * for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Example:</p>
 * <pre>var myEnum = myOtherEnum;</pre>
 *
 * <p>Enum aliases are irregular, so we need special code for this :(</p>
 *
 * @param value the value used for initialization of the enum
 */
private void checkEnumAlias(
    NodeTraversal t, JSDocInfo declInfo, Node value) {
  if (declInfo == null || !declInfo.hasEnumParameterType()) {
    return;
  }

  JSType valueType = getJSType(value);
  if (!valueType.isEnumType()) {
    return;
  }

  EnumType valueEnumType = valueType.toMaybeEnumType();
  JSType valueEnumPrimitiveType =
      valueEnumType.getElementsType().getPrimitiveType();
  validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
      declInfo.getEnumParameterType().evaluate(t.getScope(), typeRegistry),
      "incompatible enum element types");
}
 
Example #12
Source File: Nopol2017_0029_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * <p>Checks enum aliases.
 *
 * <p>We verify that the enum element type of the enum used
 * for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Example:</p>
 * <pre>var myEnum = myOtherEnum;</pre>
 *
 * <p>Enum aliases are irregular, so we need special code for this :(</p>
 *
 * @param value the value used for initialization of the enum
 */
private void checkEnumAlias(
    NodeTraversal t, JSDocInfo declInfo, Node value) {
  if (declInfo == null || !declInfo.hasEnumParameterType()) {
    return;
  }

  JSType valueType = getJSType(value);
  if (!valueType.isEnumType()) {
    return;
  }

  EnumType valueEnumType = valueType.toMaybeEnumType();
  JSType valueEnumPrimitiveType =
      valueEnumType.getElementsType().getPrimitiveType();
  validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
      declInfo.getEnumParameterType().evaluate(t.getScope(), typeRegistry),
      "incompatible enum element types");
}
 
Example #13
Source File: Closure_96_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example #14
Source File: Nopol2017_0051_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example #15
Source File: Nopol2017_0051_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Make sure that the access of this property is ok.
 */
private void checkPropertyAccess(JSType childType, String propName,
    NodeTraversal t, Node n) {
  ObjectType objectType = childType.dereference();
  if (objectType != null) {
    JSType propType = getJSType(n);
    if ((!objectType.hasProperty(propName) ||
         objectType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) &&
        propType.equals(typeRegistry.getNativeType(UNKNOWN_TYPE))) {
      if (objectType instanceof EnumType) {
        report(t, n, INEXISTENT_ENUM_ELEMENT, propName);
      } else if (!objectType.isEmptyType() &&
          reportMissingProperties && !isPropertyTest(n)) {
        if (!typeRegistry.canPropertyBeDefined(objectType, propName)) {
          report(t, n, INEXISTENT_PROPERTY, propName,
              validator.getReadableJSTypeName(n.getFirstChild(), true));
        }
      }
    }
  } else {
    // TODO(nicksantos): might want to flag the access on a non object when
    // it's impossible to get a property from this type.
  }
}
 
Example #16
Source File: Closure_2_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * <p>Checks enum aliases.
 *
 * <p>We verify that the enum element type of the enum used
 * for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Example:</p>
 * <pre>var myEnum = myOtherEnum;</pre>
 *
 * <p>Enum aliases are irregular, so we need special code for this :(</p>
 *
 * @param value the value used for initialization of the enum
 */
private void checkEnumAlias(
    NodeTraversal t, JSDocInfo declInfo, Node value) {
  if (declInfo == null || !declInfo.hasEnumParameterType()) {
    return;
  }

  JSType valueType = getJSType(value);
  if (!valueType.isEnumType()) {
    return;
  }

  EnumType valueEnumType = valueType.toMaybeEnumType();
  JSType valueEnumPrimitiveType =
      valueEnumType.getElementsType().getPrimitiveType();
  validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
      declInfo.getEnumParameterType().evaluate(t.getScope(), typeRegistry),
      "incompatible enum element types");
}
 
Example #17
Source File: Closure_2_TypeCheck_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * <p>Checks enum aliases.
 *
 * <p>We verify that the enum element type of the enum used
 * for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Example:</p>
 * <pre>var myEnum = myOtherEnum;</pre>
 *
 * <p>Enum aliases are irregular, so we need special code for this :(</p>
 *
 * @param value the value used for initialization of the enum
 */
private void checkEnumAlias(
    NodeTraversal t, JSDocInfo declInfo, Node value) {
  if (declInfo == null || !declInfo.hasEnumParameterType()) {
    return;
  }

  JSType valueType = getJSType(value);
  if (!valueType.isEnumType()) {
    return;
  }

  EnumType valueEnumType = valueType.toMaybeEnumType();
  JSType valueEnumPrimitiveType =
      valueEnumType.getElementsType().getPrimitiveType();
  validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
      declInfo.getEnumParameterType().evaluate(t.getScope(), typeRegistry),
      "incompatible enum element types");
}
 
Example #18
Source File: TypeCheck.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * <p>Checks enum aliases.
 *
 * <p>We verify that the enum element type of the enum used
 * for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Example:</p>
 * <pre>var myEnum = myOtherEnum;</pre>
 *
 * <p>Enum aliases are irregular, so we need special code for this :(</p>
 *
 * @param value the value used for initialization of the enum
 */
private void checkEnumAlias(
    NodeTraversal t, JSDocInfo declInfo, Node value) {
  if (declInfo == null || !declInfo.hasEnumParameterType()) {
    return;
  }

  JSType valueType = getJSType(value);
  if (!valueType.isEnumType()) {
    return;
  }

  EnumType valueEnumType = valueType.toMaybeEnumType();
  JSType valueEnumPrimitiveType =
      valueEnumType.getElementsType().getPrimitiveType();
  validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
      declInfo.getEnumParameterType().evaluate(t.getScope(), typeRegistry),
      "incompatible enum element types");
}
 
Example #19
Source File: Closure_96_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Checks the initializer of an enum. An enum can be initialized with an
 * object literal whose values must be subtypes of the declared enum element
 * type, or by copying another enum.</p>
 *
 * <p>In the case of an enum copy, we verify that the enum element type of the
 * enum used for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Examples:</p>
 * <pre>var myEnum = {FOO: ..., BAR: ...};
 * var myEnum = myOtherEnum;</pre>
 *
 * @param value the value used for initialization of the enum
 * @param primitiveType The type of each element of the enum.
 */
private void checkEnumInitializer(
    NodeTraversal t, Node value, JSType primitiveType) {
  if (value.getType() == Token.OBJECTLIT) {
    // re-using value as the value of the object literal and advancing twice
    value = value.getFirstChild();
    value = (value == null) ? null : value.getNext();
    while (value != null) {
      // the value's type must be assignable to the enum's primitive type
      validator.expectCanAssignTo(t, value, getJSType(value), primitiveType,
          "element type must match enum's type");

      // advancing twice
      value = value.getNext();
      value = (value == null) ? null : value.getNext();
    }
  } else if (value.getJSType() instanceof EnumType) {
    // TODO(user): Remove the instanceof check in favor
    // of a type.isEnumType() predicate. Currently, not all enum types are
    // implemented by the EnumClass, e.g. the unknown type and the any
    // type. The types need to be defined by interfaces such that an
    // implementation can implement multiple types interface.
    EnumType valueEnumType = (EnumType) value.getJSType();
    JSType valueEnumPrimitiveType =
        valueEnumType.getElementsType().getPrimitiveType();
    validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
        primitiveType, "incompatible enum element types");
  } else {
    // The error condition is handled in TypedScopeCreator.
  }
}
 
Example #20
Source File: Closure_96_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Checks the initializer of an enum. An enum can be initialized with an
 * object literal whose values must be subtypes of the declared enum element
 * type, or by copying another enum.</p>
 *
 * <p>In the case of an enum copy, we verify that the enum element type of the
 * enum used for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Examples:</p>
 * <pre>var myEnum = {FOO: ..., BAR: ...};
 * var myEnum = myOtherEnum;</pre>
 *
 * @param value the value used for initialization of the enum
 * @param primitiveType The type of each element of the enum.
 */
private void checkEnumInitializer(
    NodeTraversal t, Node value, JSType primitiveType) {
  if (value.getType() == Token.OBJECTLIT) {
    // re-using value as the value of the object literal and advancing twice
    value = value.getFirstChild();
    value = (value == null) ? null : value.getNext();
    while (value != null) {
      // the value's type must be assignable to the enum's primitive type
      validator.expectCanAssignTo(t, value, getJSType(value), primitiveType,
          "element type must match enum's type");

      // advancing twice
      value = value.getNext();
      value = (value == null) ? null : value.getNext();
    }
  } else if (value.getJSType() instanceof EnumType) {
    // TODO(user): Remove the instanceof check in favor
    // of a type.isEnumType() predicate. Currently, not all enum types are
    // implemented by the EnumClass, e.g. the unknown type and the any
    // type. The types need to be defined by interfaces such that an
    // implementation can implement multiple types interface.
    EnumType valueEnumType = (EnumType) value.getJSType();
    JSType valueEnumPrimitiveType =
        valueEnumType.getElementsType().getPrimitiveType();
    validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
        primitiveType, "incompatible enum element types");
  } else {
    // The error condition is handled in TypedScopeCreator.
  }
}
 
Example #21
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 #22
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 #23
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 #24
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);
    }
  }
}
 
Example #25
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 #26
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 #27
Source File: Closure_66_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Checks the initializer of an enum. An enum can be initialized with an
 * object literal whose values must be subtypes of the declared enum element
 * type, or by copying another enum.</p>
 *
 * <p>In the case of an enum copy, we verify that the enum element type of the
 * enum used for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Examples:</p>
 * <pre>var myEnum = {FOO: ..., BAR: ...};
 * var myEnum = myOtherEnum;</pre>
 *
 * @param value the value used for initialization of the enum
 * @param primitiveType The type of each element of the enum.
 */
private void checkEnumInitializer(
    NodeTraversal t, Node value, JSType primitiveType) {
  if (value.getType() == Token.OBJECTLIT) {
    for (Node key = value.getFirstChild();
         key != null; key = key.getNext()) {
      Node propValue = key.getFirstChild();

      // the value's type must be assignable to the enum's primitive type
      validator.expectCanAssignTo(
          t, propValue, getJSType(propValue), primitiveType,
          "element type must match enum's type");
    }
  } else if (value.getJSType() instanceof EnumType) {
    // TODO(user): Remove the instanceof check in favor
    // of a type.isEnumType() predicate. Currently, not all enum types are
    // implemented by the EnumClass, e.g. the unknown type and the any
    // type. The types need to be defined by interfaces such that an
    // implementation can implement multiple types interface.
    EnumType valueEnumType = (EnumType) value.getJSType();
    JSType valueEnumPrimitiveType =
        valueEnumType.getElementsType().getPrimitiveType();
    validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
        primitiveType, "incompatible enum element types");
  } else {
    // The error condition is handled in TypedScopeCreator.
  }
}
 
Example #28
Source File: Closure_66_TypeCheck_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Checks the initializer of an enum. An enum can be initialized with an
 * object literal whose values must be subtypes of the declared enum element
 * type, or by copying another enum.</p>
 *
 * <p>In the case of an enum copy, we verify that the enum element type of the
 * enum used for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Examples:</p>
 * <pre>var myEnum = {FOO: ..., BAR: ...};
 * var myEnum = myOtherEnum;</pre>
 *
 * @param value the value used for initialization of the enum
 * @param primitiveType The type of each element of the enum.
 */
private void checkEnumInitializer(
    NodeTraversal t, Node value, JSType primitiveType) {
  if (value.getType() == Token.OBJECTLIT) {
    for (Node key = value.getFirstChild();
         key != null; key = key.getNext()) {
      Node propValue = key.getFirstChild();

      // the value's type must be assignable to the enum's primitive type
      validator.expectCanAssignTo(
          t, propValue, getJSType(propValue), primitiveType,
          "element type must match enum's type");
    }
  } else if (value.getJSType() instanceof EnumType) {
    // TODO(user): Remove the instanceof check in favor
    // of a type.isEnumType() predicate. Currently, not all enum types are
    // implemented by the EnumClass, e.g. the unknown type and the any
    // type. The types need to be defined by interfaces such that an
    // implementation can implement multiple types interface.
    EnumType valueEnumType = (EnumType) value.getJSType();
    JSType valueEnumPrimitiveType =
        valueEnumType.getElementsType().getPrimitiveType();
    validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
        primitiveType, "incompatible enum element types");
  } else {
    // The error condition is handled in TypedScopeCreator.
  }
}
 
Example #29
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 #30
Source File: Closure_69_TypeCheck_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * <p>Checks the initializer of an enum. An enum can be initialized with an
 * object literal whose values must be subtypes of the declared enum element
 * type, or by copying another enum.</p>
 *
 * <p>In the case of an enum copy, we verify that the enum element type of the
 * enum used for initialization is a subtype of the enum element type of
 * the enum the value is being copied in.</p>
 *
 * <p>Examples:</p>
 * <pre>var myEnum = {FOO: ..., BAR: ...};
 * var myEnum = myOtherEnum;</pre>
 *
 * @param value the value used for initialization of the enum
 * @param primitiveType The type of each element of the enum.
 */
private void checkEnumInitializer(
    NodeTraversal t, Node value, JSType primitiveType) {
  if (value.getType() == Token.OBJECTLIT) {
    for (Node key = value.getFirstChild();
         key != null; key = key.getNext()) {
      Node propValue = key.getFirstChild();

      // the value's type must be assignable to the enum's primitive type
      validator.expectCanAssignTo(
          t, propValue, getJSType(propValue), primitiveType,
          "element type must match enum's type");
    }
  } else if (value.getJSType() instanceof EnumType) {
    // TODO(user): Remove the instanceof check in favor
    // of a type.isEnumType() predicate. Currently, not all enum types are
    // implemented by the EnumClass, e.g. the unknown type and the any
    // type. The types need to be defined by interfaces such that an
    // implementation can implement multiple types interface.
    EnumType valueEnumType = (EnumType) value.getJSType();
    JSType valueEnumPrimitiveType =
        valueEnumType.getElementsType().getPrimitiveType();
    validator.expectCanAssignTo(t, value, valueEnumPrimitiveType,
        primitiveType, "incompatible enum element types");
  } else {
    // The error condition is handled in TypedScopeCreator.
  }
}