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

The following examples show how to use com.google.javascript.rhino.jstype.ObjectType#isPropertyTypeInferred() . 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: Nopol2017_0051_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits an object literal field definition <code>key : value</code>.
 *
 * If the <code>lvalue</code> is a prototype modification, we change the
 * schema of the object type it is referring to.
 *
 * @param t the traversal
 * @param key the assign node
 */
private void visitObjLitKey(NodeTraversal t, Node key, Node objlit) {
  // TODO(johnlenz): Validate get and set function declarations are valid
  // as is the functions can have "extraneous" bits.

  // For getter and setter property definitions the
  // rvalue type != the property type.
  Node rvalue = key.getFirstChild();
  JSType rightType = NodeUtil.getObjectLitKeyTypeFromValueType(
      key, getJSType(rvalue));
  if (rightType == null) {
    rightType = getNativeType(UNKNOWN_TYPE);
  }

  Node owner = objlit;

  // Validate value is assignable to the key type.

  JSType keyType = getJSType(key);
  boolean valid = validator.expectCanAssignToPropertyOf(t, key,
      rightType, keyType,
      owner, NodeUtil.getObjectLitKeyName(key));
  if (valid) {
    ensureTyped(t, key, rightType);
  } else {
    ensureTyped(t, key);
  }

  // Validate that the key type is assignable to the object property type.
  // This is necessary as the objlit may have been cast to a non-literal
  // object type.
  // TODO(johnlenz): consider introducing a CAST node to the AST (or
  // perhaps a parentheses node).

  JSType objlitType = getJSType(objlit);
  ObjectType type = ObjectType.cast(
      objlitType.restrictByNotNullOrUndefined());
  if (type != null) {
    String property = NodeUtil.getObjectLitKeyName(key);
    if (type.hasProperty(property) &&
        !type.isPropertyTypeInferred(property) &&
        !propertyIsImplicitCast(type, property)) {
      validator.expectCanAssignToPropertyOf(
          t, key, keyType,
          type.getPropertyType(property), owner, property);
    }
    return;
  }
}
 
Example 2
Source File: Nopol2017_0051_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits an object literal field definition <code>key : value</code>.
 *
 * If the <code>lvalue</code> is a prototype modification, we change the
 * schema of the object type it is referring to.
 *
 * @param t the traversal
 * @param key the assign node
 */
private void visitObjLitKey(NodeTraversal t, Node key, Node objlit) {
  // TODO(johnlenz): Validate get and set function declarations are valid
  // as is the functions can have "extraneous" bits.

  // For getter and setter property definitions the
  // rvalue type != the property type.
  Node rvalue = key.getFirstChild();
  JSType rightType = NodeUtil.getObjectLitKeyTypeFromValueType(
      key, getJSType(rvalue));
  if (rightType == null) {
    rightType = getNativeType(UNKNOWN_TYPE);
  }

  Node owner = objlit;

  // Validate value is assignable to the key type.

  JSType keyType = getJSType(key);
  boolean valid = validator.expectCanAssignToPropertyOf(t, key,
      rightType, keyType,
      owner, NodeUtil.getObjectLitKeyName(key));
  if (valid) {
    ensureTyped(t, key, rightType);
  } else {
    ensureTyped(t, key);
  }

  // Validate that the key type is assignable to the object property type.
  // This is necessary as the objlit may have been cast to a non-literal
  // object type.
  // TODO(johnlenz): consider introducing a CAST node to the AST (or
  // perhaps a parentheses node).

  JSType objlitType = getJSType(objlit);
  ObjectType type = ObjectType.cast(
      objlitType.restrictByNotNullOrUndefined());
  if (type != null) {
    String property = NodeUtil.getObjectLitKeyName(key);
    if (type.hasProperty(property) &&
        !type.isPropertyTypeInferred(property) &&
        !propertyIsImplicitCast(type, property)) {
      validator.expectCanAssignToPropertyOf(
          t, key, keyType,
          type.getPropertyType(property), owner, property);
    }
    return;
  }
}
 
Example 3
Source File: Nopol2017_0029_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits an object literal field definition <code>key : value</code>.
 *
 * If the <code>lvalue</code> is a prototype modification, we change the
 * schema of the object type it is referring to.
 *
 * @param t the traversal
 * @param key the assign node
 */
private void visitObjLitKey(
    NodeTraversal t, Node key, Node objlit, JSType litType) {
  // Do not validate object lit value types in externs. We don't really care,
  // and it makes it easier to generate externs.
  if (objlit.isFromExterns()) {
    ensureTyped(t, key);
    return;
  }

  // Structs must have unquoted keys and dicts must have quoted keys
  if (litType.isStruct() && key.isQuotedString()) {
    report(t, key, ILLEGAL_OBJLIT_KEY, "struct");
  } else if (litType.isDict() && !key.isQuotedString()) {
    report(t, key, ILLEGAL_OBJLIT_KEY, "dict");
  }

  // TODO(johnlenz): Validate get and set function declarations are valid
  // as is the functions can have "extraneous" bits.

  // For getter and setter property definitions the
  // r-value type != the property type.
  Node rvalue = key.getFirstChild();
  JSType rightType = NodeUtil.getObjectLitKeyTypeFromValueType(
      key, getJSType(rvalue));
  if (rightType == null) {
    rightType = getNativeType(UNKNOWN_TYPE);
  }

  Node owner = objlit;

  // Validate value is assignable to the key type.

  JSType keyType = getJSType(key);

  JSType allowedValueType = keyType;
  if (allowedValueType.isEnumElementType()) {
    allowedValueType =
        allowedValueType.toMaybeEnumElementType().getPrimitiveType();
  }

  boolean valid = validator.expectCanAssignToPropertyOf(t, key,
      rightType, allowedValueType,
      owner, NodeUtil.getObjectLitKeyName(key));
  if (valid) {
    ensureTyped(t, key, rightType);
  } else {
    ensureTyped(t, key);
  }

  // Validate that the key type is assignable to the object property type.
  // This is necessary as the objlit may have been cast to a non-literal
  // object type.
  // TODO(johnlenz): consider introducing a CAST node to the AST (or
  // perhaps a parentheses node).

  JSType objlitType = getJSType(objlit);
  ObjectType type = ObjectType.cast(
      objlitType.restrictByNotNullOrUndefined());
  if (type != null) {
    String property = NodeUtil.getObjectLitKeyName(key);
    if (type.hasProperty(property) &&
        !type.isPropertyTypeInferred(property) &&
        !propertyIsImplicitCast(type, property)) {
      validator.expectCanAssignToPropertyOf(
          t, key, keyType,
          type.getPropertyType(property), owner, property);
    }
    return;
  }
}
 
Example 4
Source File: Nopol2017_0029_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits an object literal field definition <code>key : value</code>.
 *
 * If the <code>lvalue</code> is a prototype modification, we change the
 * schema of the object type it is referring to.
 *
 * @param t the traversal
 * @param key the assign node
 */
private void visitObjLitKey(
    NodeTraversal t, Node key, Node objlit, JSType litType) {
  // Do not validate object lit value types in externs. We don't really care,
  // and it makes it easier to generate externs.
  if (objlit.isFromExterns()) {
    ensureTyped(t, key);
    return;
  }

  // Structs must have unquoted keys and dicts must have quoted keys
  if (litType.isStruct() && key.isQuotedString()) {
    report(t, key, ILLEGAL_OBJLIT_KEY, "struct");
  } else if (litType.isDict() && !key.isQuotedString()) {
    report(t, key, ILLEGAL_OBJLIT_KEY, "dict");
  }

  // TODO(johnlenz): Validate get and set function declarations are valid
  // as is the functions can have "extraneous" bits.

  // For getter and setter property definitions the
  // r-value type != the property type.
  Node rvalue = key.getFirstChild();
  JSType rightType = NodeUtil.getObjectLitKeyTypeFromValueType(
      key, getJSType(rvalue));
  if (rightType == null) {
    rightType = getNativeType(UNKNOWN_TYPE);
  }

  Node owner = objlit;

  // Validate value is assignable to the key type.

  JSType keyType = getJSType(key);

  JSType allowedValueType = keyType;
  if (allowedValueType.isEnumElementType()) {
    allowedValueType =
        allowedValueType.toMaybeEnumElementType().getPrimitiveType();
  }

  boolean valid = validator.expectCanAssignToPropertyOf(t, key,
      rightType, allowedValueType,
      owner, NodeUtil.getObjectLitKeyName(key));
  if (valid) {
    ensureTyped(t, key, rightType);
  } else {
    ensureTyped(t, key);
  }

  // Validate that the key type is assignable to the object property type.
  // This is necessary as the objlit may have been cast to a non-literal
  // object type.
  // TODO(johnlenz): consider introducing a CAST node to the AST (or
  // perhaps a parentheses node).

  JSType objlitType = getJSType(objlit);
  ObjectType type = ObjectType.cast(
      objlitType.restrictByNotNullOrUndefined());
  if (type != null) {
    String property = NodeUtil.getObjectLitKeyName(key);
    if (type.hasProperty(property) &&
        !type.isPropertyTypeInferred(property) &&
        !propertyIsImplicitCast(type, property)) {
      validator.expectCanAssignToPropertyOf(
          t, key, keyType,
          type.getPropertyType(property), owner, property);
    }
    return;
  }
}
 
Example 5
Source File: Closure_69_TypeCheck_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits an object literal field definition <code>key : value</code>.
 *
 * If the <code>lvalue</code> is a prototype modification, we change the
 * schema of the object type it is referring to.
 *
 * @param t the traversal
 * @param key the assign node
 */
private void visitObjLitKey(NodeTraversal t, Node key, Node objlit) {
  // TODO(johnlenz): Validate get and set function declarations are valid
  // as is the functions can have "extraneous" bits.

  // For getter and setter property definitions the
  // rvalue type != the property type.
  Node rvalue = key.getFirstChild();
  JSType rightType = NodeUtil.getObjectLitKeyTypeFromValueType(
      key, getJSType(rvalue));
  if (rightType == null) {
    rightType = getNativeType(UNKNOWN_TYPE);
  }

  Node owner = objlit;

  // Validate value is assignable to the key type.

  JSType keyType = getJSType(key);
  boolean valid = validator.expectCanAssignToPropertyOf(t, key,
      rightType, keyType,
      owner, NodeUtil.getObjectLitKeyName(key));
  if (valid) {
    ensureTyped(t, key, rightType);
  } else {
    ensureTyped(t, key);
  }

  // Validate that the key type is assignable to the object property type.
  // This is necessary as the objlit may have been cast to a non-literal
  // object type.
  // TODO(johnlenz): consider introducing a CAST node to the AST (or
  // perhaps a parentheses node).

  JSType objlitType = getJSType(objlit);
  ObjectType type = ObjectType.cast(
      objlitType.restrictByNotNullOrUndefined());
  if (type != null) {
    String property = NodeUtil.getObjectLitKeyName(key);
    if (type.hasProperty(property) &&
        !type.isPropertyTypeInferred(property) &&
        !propertyIsImplicitCast(type, property)) {
      validator.expectCanAssignToPropertyOf(
          t, key, keyType,
          type.getPropertyType(property), owner, property);
    }
    return;
  }
}
 
Example 6
Source File: Closure_69_TypeCheck_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits an object literal field definition <code>key : value</code>.
 *
 * If the <code>lvalue</code> is a prototype modification, we change the
 * schema of the object type it is referring to.
 *
 * @param t the traversal
 * @param key the assign node
 */
private void visitObjLitKey(NodeTraversal t, Node key, Node objlit) {
  // TODO(johnlenz): Validate get and set function declarations are valid
  // as is the functions can have "extraneous" bits.

  // For getter and setter property definitions the
  // rvalue type != the property type.
  Node rvalue = key.getFirstChild();
  JSType rightType = NodeUtil.getObjectLitKeyTypeFromValueType(
      key, getJSType(rvalue));
  if (rightType == null) {
    rightType = getNativeType(UNKNOWN_TYPE);
  }

  Node owner = objlit;

  // Validate value is assignable to the key type.

  JSType keyType = getJSType(key);
  boolean valid = validator.expectCanAssignToPropertyOf(t, key,
      rightType, keyType,
      owner, NodeUtil.getObjectLitKeyName(key));
  if (valid) {
    ensureTyped(t, key, rightType);
  } else {
    ensureTyped(t, key);
  }

  // Validate that the key type is assignable to the object property type.
  // This is necessary as the objlit may have been cast to a non-literal
  // object type.
  // TODO(johnlenz): consider introducing a CAST node to the AST (or
  // perhaps a parentheses node).

  JSType objlitType = getJSType(objlit);
  ObjectType type = ObjectType.cast(
      objlitType.restrictByNotNullOrUndefined());
  if (type != null) {
    String property = NodeUtil.getObjectLitKeyName(key);
    if (type.hasProperty(property) &&
        !type.isPropertyTypeInferred(property) &&
        !propertyIsImplicitCast(type, property)) {
      validator.expectCanAssignToPropertyOf(
          t, key, keyType,
          type.getPropertyType(property), owner, property);
    }
    return;
  }
}
 
Example 7
Source File: Closure_125_TypeCheck_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits an object literal field definition <code>key : value</code>.
 *
 * If the <code>lvalue</code> is a prototype modification, we change the
 * schema of the object type it is referring to.
 *
 * @param t the traversal
 * @param key the assign node
 */
private void visitObjLitKey(
    NodeTraversal t, Node key, Node objlit, JSType litType) {
  // Do not validate object lit value types in externs. We don't really care,
  // and it makes it easier to generate externs.
  if (objlit.isFromExterns()) {
    ensureTyped(t, key);
    return;
  }

  // Structs must have unquoted keys and dicts must have quoted keys
  if (litType.isStruct() && key.isQuotedString()) {
    report(t, key, ILLEGAL_OBJLIT_KEY, "struct");
  } else if (litType.isDict() && !key.isQuotedString()) {
    report(t, key, ILLEGAL_OBJLIT_KEY, "dict");
  }

  // TODO(johnlenz): Validate get and set function declarations are valid
  // as is the functions can have "extraneous" bits.

  // For getter and setter property definitions the
  // r-value type != the property type.
  Node rvalue = key.getFirstChild();
  JSType rightType = NodeUtil.getObjectLitKeyTypeFromValueType(
      key, getJSType(rvalue));
  if (rightType == null) {
    rightType = getNativeType(UNKNOWN_TYPE);
  }

  Node owner = objlit;

  // Validate value is assignable to the key type.

  JSType keyType = getJSType(key);

  JSType allowedValueType = keyType;
  if (allowedValueType.isEnumElementType()) {
    allowedValueType =
        allowedValueType.toMaybeEnumElementType().getPrimitiveType();
  }

  boolean valid = validator.expectCanAssignToPropertyOf(t, key,
      rightType, allowedValueType,
      owner, NodeUtil.getObjectLitKeyName(key));
  if (valid) {
    ensureTyped(t, key, rightType);
  } else {
    ensureTyped(t, key);
  }

  // Validate that the key type is assignable to the object property type.
  // This is necessary as the objlit may have been cast to a non-literal
  // object type.
  // TODO(johnlenz): consider introducing a CAST node to the AST (or
  // perhaps a parentheses node).

  JSType objlitType = getJSType(objlit);
  ObjectType type = ObjectType.cast(
      objlitType.restrictByNotNullOrUndefined());
  if (type != null) {
    String property = NodeUtil.getObjectLitKeyName(key);
    if (type.hasProperty(property) &&
        !type.isPropertyTypeInferred(property) &&
        !propertyIsImplicitCast(type, property)) {
      validator.expectCanAssignToPropertyOf(
          t, key, keyType,
          type.getPropertyType(property), owner, property);
    }
    return;
  }
}
 
Example 8
Source File: Closure_125_TypeCheck_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits an object literal field definition <code>key : value</code>.
 *
 * If the <code>lvalue</code> is a prototype modification, we change the
 * schema of the object type it is referring to.
 *
 * @param t the traversal
 * @param key the assign node
 */
private void visitObjLitKey(
    NodeTraversal t, Node key, Node objlit, JSType litType) {
  // Do not validate object lit value types in externs. We don't really care,
  // and it makes it easier to generate externs.
  if (objlit.isFromExterns()) {
    ensureTyped(t, key);
    return;
  }

  // Structs must have unquoted keys and dicts must have quoted keys
  if (litType.isStruct() && key.isQuotedString()) {
    report(t, key, ILLEGAL_OBJLIT_KEY, "struct");
  } else if (litType.isDict() && !key.isQuotedString()) {
    report(t, key, ILLEGAL_OBJLIT_KEY, "dict");
  }

  // TODO(johnlenz): Validate get and set function declarations are valid
  // as is the functions can have "extraneous" bits.

  // For getter and setter property definitions the
  // r-value type != the property type.
  Node rvalue = key.getFirstChild();
  JSType rightType = NodeUtil.getObjectLitKeyTypeFromValueType(
      key, getJSType(rvalue));
  if (rightType == null) {
    rightType = getNativeType(UNKNOWN_TYPE);
  }

  Node owner = objlit;

  // Validate value is assignable to the key type.

  JSType keyType = getJSType(key);

  JSType allowedValueType = keyType;
  if (allowedValueType.isEnumElementType()) {
    allowedValueType =
        allowedValueType.toMaybeEnumElementType().getPrimitiveType();
  }

  boolean valid = validator.expectCanAssignToPropertyOf(t, key,
      rightType, allowedValueType,
      owner, NodeUtil.getObjectLitKeyName(key));
  if (valid) {
    ensureTyped(t, key, rightType);
  } else {
    ensureTyped(t, key);
  }

  // Validate that the key type is assignable to the object property type.
  // This is necessary as the objlit may have been cast to a non-literal
  // object type.
  // TODO(johnlenz): consider introducing a CAST node to the AST (or
  // perhaps a parentheses node).

  JSType objlitType = getJSType(objlit);
  ObjectType type = ObjectType.cast(
      objlitType.restrictByNotNullOrUndefined());
  if (type != null) {
    String property = NodeUtil.getObjectLitKeyName(key);
    if (type.hasProperty(property) &&
        !type.isPropertyTypeInferred(property) &&
        !propertyIsImplicitCast(type, property)) {
      validator.expectCanAssignToPropertyOf(
          t, key, keyType,
          type.getPropertyType(property), owner, property);
    }
    return;
  }
}
 
Example 9
Source File: Closure_66_TypeCheck_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits an object literal field definition <code>key : value</code>.
 *
 * If the <code>lvalue</code> is a prototype modification, we change the
 * schema of the object type it is referring to.
 *
 * @param t the traversal
 * @param key the assign node
 */
private void visitObjLitKey(NodeTraversal t, Node key, Node objlit) {
  // TODO(johnlenz): Validate get and set function declarations are valid
  // as is the functions can have "extraneous" bits.

  // For getter and setter property definitions the
  // rvalue type != the property type.
  Node rvalue = key.getFirstChild();
  JSType rightType = NodeUtil.getObjectLitKeyTypeFromValueType(
      key, getJSType(rvalue));
  if (rightType == null) {
    rightType = getNativeType(UNKNOWN_TYPE);
  }

  Node owner = objlit;

  // Validate value is assignable to the key type.

  JSType keyType = getJSType(key);
  boolean valid = validator.expectCanAssignToPropertyOf(t, key,
      rightType, keyType,
      owner, NodeUtil.getObjectLitKeyName(key));
  if (valid) {
    ensureTyped(t, key, rightType);
  } else {
    ensureTyped(t, key);
  }

  // Validate that the key type is assignable to the object property type.
  // This is necessary as the objlit may have been cast to a non-literal
  // object type.
  // TODO(johnlenz): consider introducing a CAST node to the AST (or
  // perhaps a parentheses node).

  JSType objlitType = getJSType(objlit);
  ObjectType type = ObjectType.cast(
      objlitType.restrictByNotNullOrUndefined());
  if (type != null) {
    String property = NodeUtil.getObjectLitKeyName(key);
    if (type.hasProperty(property) &&
        !type.isPropertyTypeInferred(property) &&
        !propertyIsImplicitCast(type, property)) {
      validator.expectCanAssignToPropertyOf(
          t, key, keyType,
          type.getPropertyType(property), owner, property);
    }
    return;
  }
}
 
Example 10
Source File: Closure_66_TypeCheck_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits an object literal field definition <code>key : value</code>.
 *
 * If the <code>lvalue</code> is a prototype modification, we change the
 * schema of the object type it is referring to.
 *
 * @param t the traversal
 * @param key the assign node
 */
private void visitObjLitKey(NodeTraversal t, Node key, Node objlit) {
  // TODO(johnlenz): Validate get and set function declarations are valid
  // as is the functions can have "extraneous" bits.

  // For getter and setter property definitions the
  // rvalue type != the property type.
  Node rvalue = key.getFirstChild();
  JSType rightType = NodeUtil.getObjectLitKeyTypeFromValueType(
      key, getJSType(rvalue));
  if (rightType == null) {
    rightType = getNativeType(UNKNOWN_TYPE);
  }

  Node owner = objlit;

  // Validate value is assignable to the key type.

  JSType keyType = getJSType(key);
  boolean valid = validator.expectCanAssignToPropertyOf(t, key,
      rightType, keyType,
      owner, NodeUtil.getObjectLitKeyName(key));
  if (valid) {
    ensureTyped(t, key, rightType);
  } else {
    ensureTyped(t, key);
  }

  // Validate that the key type is assignable to the object property type.
  // This is necessary as the objlit may have been cast to a non-literal
  // object type.
  // TODO(johnlenz): consider introducing a CAST node to the AST (or
  // perhaps a parentheses node).

  JSType objlitType = getJSType(objlit);
  ObjectType type = ObjectType.cast(
      objlitType.restrictByNotNullOrUndefined());
  if (type != null) {
    String property = NodeUtil.getObjectLitKeyName(key);
    if (type.hasProperty(property) &&
        !type.isPropertyTypeInferred(property) &&
        !propertyIsImplicitCast(type, property)) {
      validator.expectCanAssignToPropertyOf(
          t, key, keyType,
          type.getPropertyType(property), owner, property);
    }
    return;
  }
}
 
Example 11
Source File: Closure_11_TypeCheck_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits an object literal field definition <code>key : value</code>.
 *
 * If the <code>lvalue</code> is a prototype modification, we change the
 * schema of the object type it is referring to.
 *
 * @param t the traversal
 * @param key the assign node
 */
private void visitObjLitKey(NodeTraversal t, Node key, Node objlit) {
  // Do not validate object lit value types in externs. We don't really care,
  // and it makes it easier to generate externs.
  if (objlit.isFromExterns()) {
    ensureTyped(t, key);
    return;
  }

  // TODO(johnlenz): Validate get and set function declarations are valid
  // as is the functions can have "extraneous" bits.

  // For getter and setter property definitions the
  // r-value type != the property type.
  Node rvalue = key.getFirstChild();
  JSType rightType = NodeUtil.getObjectLitKeyTypeFromValueType(
      key, getJSType(rvalue));
  if (rightType == null) {
    rightType = getNativeType(UNKNOWN_TYPE);
  }

  Node owner = objlit;

  // Validate value is assignable to the key type.

  JSType keyType = getJSType(key);

  JSType allowedValueType = keyType;
  if (allowedValueType.isEnumElementType()) {
    allowedValueType =
        allowedValueType.toMaybeEnumElementType().getPrimitiveType();
  }

  boolean valid = validator.expectCanAssignToPropertyOf(t, key,
      rightType, allowedValueType,
      owner, NodeUtil.getObjectLitKeyName(key));
  if (valid) {
    ensureTyped(t, key, rightType);
  } else {
    ensureTyped(t, key);
  }

  // Validate that the key type is assignable to the object property type.
  // This is necessary as the objlit may have been cast to a non-literal
  // object type.
  // TODO(johnlenz): consider introducing a CAST node to the AST (or
  // perhaps a parentheses node).

  JSType objlitType = getJSType(objlit);
  ObjectType type = ObjectType.cast(
      objlitType.restrictByNotNullOrUndefined());
  if (type != null) {
    String property = NodeUtil.getObjectLitKeyName(key);
    if (type.hasProperty(property) &&
        !type.isPropertyTypeInferred(property) &&
        !propertyIsImplicitCast(type, property)) {
      validator.expectCanAssignToPropertyOf(
          t, key, keyType,
          type.getPropertyType(property), owner, property);
    }
    return;
  }
}
 
Example 12
Source File: Closure_11_TypeCheck_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits an object literal field definition <code>key : value</code>.
 *
 * If the <code>lvalue</code> is a prototype modification, we change the
 * schema of the object type it is referring to.
 *
 * @param t the traversal
 * @param key the assign node
 */
private void visitObjLitKey(NodeTraversal t, Node key, Node objlit) {
  // Do not validate object lit value types in externs. We don't really care,
  // and it makes it easier to generate externs.
  if (objlit.isFromExterns()) {
    ensureTyped(t, key);
    return;
  }

  // TODO(johnlenz): Validate get and set function declarations are valid
  // as is the functions can have "extraneous" bits.

  // For getter and setter property definitions the
  // r-value type != the property type.
  Node rvalue = key.getFirstChild();
  JSType rightType = NodeUtil.getObjectLitKeyTypeFromValueType(
      key, getJSType(rvalue));
  if (rightType == null) {
    rightType = getNativeType(UNKNOWN_TYPE);
  }

  Node owner = objlit;

  // Validate value is assignable to the key type.

  JSType keyType = getJSType(key);

  JSType allowedValueType = keyType;
  if (allowedValueType.isEnumElementType()) {
    allowedValueType =
        allowedValueType.toMaybeEnumElementType().getPrimitiveType();
  }

  boolean valid = validator.expectCanAssignToPropertyOf(t, key,
      rightType, allowedValueType,
      owner, NodeUtil.getObjectLitKeyName(key));
  if (valid) {
    ensureTyped(t, key, rightType);
  } else {
    ensureTyped(t, key);
  }

  // Validate that the key type is assignable to the object property type.
  // This is necessary as the objlit may have been cast to a non-literal
  // object type.
  // TODO(johnlenz): consider introducing a CAST node to the AST (or
  // perhaps a parentheses node).

  JSType objlitType = getJSType(objlit);
  ObjectType type = ObjectType.cast(
      objlitType.restrictByNotNullOrUndefined());
  if (type != null) {
    String property = NodeUtil.getObjectLitKeyName(key);
    if (type.hasProperty(property) &&
        !type.isPropertyTypeInferred(property) &&
        !propertyIsImplicitCast(type, property)) {
      validator.expectCanAssignToPropertyOf(
          t, key, keyType,
          type.getPropertyType(property), owner, property);
    }
    return;
  }
}
 
Example 13
Source File: Closure_2_TypeCheck_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits an object literal field definition <code>key : value</code>.
 *
 * If the <code>lvalue</code> is a prototype modification, we change the
 * schema of the object type it is referring to.
 *
 * @param t the traversal
 * @param key the assign node
 */
private void visitObjLitKey(
    NodeTraversal t, Node key, Node objlit, JSType litType) {
  // Do not validate object lit value types in externs. We don't really care,
  // and it makes it easier to generate externs.
  if (objlit.isFromExterns()) {
    ensureTyped(t, key);
    return;
  }

  // Structs must have unquoted keys and dicts must have quoted keys
  if (litType.isStruct() && key.isQuotedString()) {
    report(t, key, ILLEGAL_OBJLIT_KEY, "struct");
  } else if (litType.isDict() && !key.isQuotedString()) {
    report(t, key, ILLEGAL_OBJLIT_KEY, "dict");
  }

  // TODO(johnlenz): Validate get and set function declarations are valid
  // as is the functions can have "extraneous" bits.

  // For getter and setter property definitions the
  // r-value type != the property type.
  Node rvalue = key.getFirstChild();
  JSType rightType = NodeUtil.getObjectLitKeyTypeFromValueType(
      key, getJSType(rvalue));
  if (rightType == null) {
    rightType = getNativeType(UNKNOWN_TYPE);
  }

  Node owner = objlit;

  // Validate value is assignable to the key type.

  JSType keyType = getJSType(key);

  JSType allowedValueType = keyType;
  if (allowedValueType.isEnumElementType()) {
    allowedValueType =
        allowedValueType.toMaybeEnumElementType().getPrimitiveType();
  }

  boolean valid = validator.expectCanAssignToPropertyOf(t, key,
      rightType, allowedValueType,
      owner, NodeUtil.getObjectLitKeyName(key));
  if (valid) {
    ensureTyped(t, key, rightType);
  } else {
    ensureTyped(t, key);
  }

  // Validate that the key type is assignable to the object property type.
  // This is necessary as the objlit may have been cast to a non-literal
  // object type.
  // TODO(johnlenz): consider introducing a CAST node to the AST (or
  // perhaps a parentheses node).

  JSType objlitType = getJSType(objlit);
  ObjectType type = ObjectType.cast(
      objlitType.restrictByNotNullOrUndefined());
  if (type != null) {
    String property = NodeUtil.getObjectLitKeyName(key);
    if (type.hasProperty(property) &&
        !type.isPropertyTypeInferred(property) &&
        !propertyIsImplicitCast(type, property)) {
      validator.expectCanAssignToPropertyOf(
          t, key, keyType,
          type.getPropertyType(property), owner, property);
    }
    return;
  }
}
 
Example 14
Source File: Closure_2_TypeCheck_t.java    From coming with MIT License 4 votes vote down vote up
/**
 * Visits an object literal field definition <code>key : value</code>.
 *
 * If the <code>lvalue</code> is a prototype modification, we change the
 * schema of the object type it is referring to.
 *
 * @param t the traversal
 * @param key the assign node
 */
private void visitObjLitKey(
    NodeTraversal t, Node key, Node objlit, JSType litType) {
  // Do not validate object lit value types in externs. We don't really care,
  // and it makes it easier to generate externs.
  if (objlit.isFromExterns()) {
    ensureTyped(t, key);
    return;
  }

  // Structs must have unquoted keys and dicts must have quoted keys
  if (litType.isStruct() && key.isQuotedString()) {
    report(t, key, ILLEGAL_OBJLIT_KEY, "struct");
  } else if (litType.isDict() && !key.isQuotedString()) {
    report(t, key, ILLEGAL_OBJLIT_KEY, "dict");
  }

  // TODO(johnlenz): Validate get and set function declarations are valid
  // as is the functions can have "extraneous" bits.

  // For getter and setter property definitions the
  // r-value type != the property type.
  Node rvalue = key.getFirstChild();
  JSType rightType = NodeUtil.getObjectLitKeyTypeFromValueType(
      key, getJSType(rvalue));
  if (rightType == null) {
    rightType = getNativeType(UNKNOWN_TYPE);
  }

  Node owner = objlit;

  // Validate value is assignable to the key type.

  JSType keyType = getJSType(key);

  JSType allowedValueType = keyType;
  if (allowedValueType.isEnumElementType()) {
    allowedValueType =
        allowedValueType.toMaybeEnumElementType().getPrimitiveType();
  }

  boolean valid = validator.expectCanAssignToPropertyOf(t, key,
      rightType, allowedValueType,
      owner, NodeUtil.getObjectLitKeyName(key));
  if (valid) {
    ensureTyped(t, key, rightType);
  } else {
    ensureTyped(t, key);
  }

  // Validate that the key type is assignable to the object property type.
  // This is necessary as the objlit may have been cast to a non-literal
  // object type.
  // TODO(johnlenz): consider introducing a CAST node to the AST (or
  // perhaps a parentheses node).

  JSType objlitType = getJSType(objlit);
  ObjectType type = ObjectType.cast(
      objlitType.restrictByNotNullOrUndefined());
  if (type != null) {
    String property = NodeUtil.getObjectLitKeyName(key);
    if (type.hasProperty(property) &&
        !type.isPropertyTypeInferred(property) &&
        !propertyIsImplicitCast(type, property)) {
      validator.expectCanAssignToPropertyOf(
          t, key, keyType,
          type.getPropertyType(property), owner, property);
    }
    return;
  }
}
 
Example 15
Source File: TypeCheck.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Visits an object literal field definition <code>key : value</code>.
 *
 * If the <code>lvalue</code> is a prototype modification, we change the
 * schema of the object type it is referring to.
 *
 * @param t the traversal
 * @param key the assign node
 */
private void visitObjLitKey(
    NodeTraversal t, Node key, Node objlit, JSType litType) {
  // Do not validate object lit value types in externs. We don't really care,
  // and it makes it easier to generate externs.
  if (objlit.isFromExterns()) {
    ensureTyped(t, key);
    return;
  }

  // Structs must have unquoted keys and dicts must have quoted keys
  if (litType.isStruct() && key.isQuotedString()) {
    report(t, key, ILLEGAL_OBJLIT_KEY, "struct");
  } else if (litType.isDict() && !key.isQuotedString()) {
    report(t, key, ILLEGAL_OBJLIT_KEY, "dict");
  }

  // TODO(johnlenz): Validate get and set function declarations are valid
  // as is the functions can have "extraneous" bits.

  // For getter and setter property definitions the
  // r-value type != the property type.
  Node rvalue = key.getFirstChild();
  JSType rightType = NodeUtil.getObjectLitKeyTypeFromValueType(
      key, getJSType(rvalue));
  if (rightType == null) {
    rightType = getNativeType(UNKNOWN_TYPE);
  }

  Node owner = objlit;

  // Validate value is assignable to the key type.

  JSType keyType = getJSType(key);

  JSType allowedValueType = keyType;
  if (allowedValueType.isEnumElementType()) {
    allowedValueType =
        allowedValueType.toMaybeEnumElementType().getPrimitiveType();
  }

  boolean valid = validator.expectCanAssignToPropertyOf(t, key,
      rightType, allowedValueType,
      owner, NodeUtil.getObjectLitKeyName(key));
  if (valid) {
    ensureTyped(t, key, rightType);
  } else {
    ensureTyped(t, key);
  }

  // Validate that the key type is assignable to the object property type.
  // This is necessary as the objlit may have been cast to a non-literal
  // object type.
  // TODO(johnlenz): consider introducing a CAST node to the AST (or
  // perhaps a parentheses node).

  JSType objlitType = getJSType(objlit);
  ObjectType type = ObjectType.cast(
      objlitType.restrictByNotNullOrUndefined());
  if (type != null) {
    String property = NodeUtil.getObjectLitKeyName(key);
    if (type.hasProperty(property) &&
        !type.isPropertyTypeInferred(property) &&
        !propertyIsImplicitCast(type, property)) {
      validator.expectCanAssignToPropertyOf(
          t, key, keyType,
          type.getPropertyType(property), owner, property);
    }
    return;
  }
}