Java Code Examples for com.google.javascript.rhino.jstype.JSType#isDict()

The following examples show how to use com.google.javascript.rhino.jstype.JSType#isDict() . 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_0029_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Visits a GETPROP node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 * @param parent The parent of <code>n</code>
 */
private void visitGetProp(NodeTraversal t, Node n, Node parent) {
  // obj.prop or obj.method()
  // Lots of types can appear on the left, a call to a void function can
  // never be on the left. getPropertyType will decide what is acceptable
  // and what isn't.
  Node property = n.getLastChild();
  Node objNode = n.getFirstChild();
  JSType childType = getJSType(objNode);

  if (childType.isDict()) {
    report(t, property, TypeValidator.ILLEGAL_PROPERTY_ACCESS, "'.'", "dict");
  } else if (validator.expectNotNullOrUndefined(t, n, childType,
      "No properties on this expression", getNativeType(OBJECT_TYPE))) {
    checkPropertyAccess(childType, property.getString(), t, n);
  }
  ensureTyped(t, n);
}
 
Example 2
Source File: Nopol2017_0029_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Visits a GETPROP node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 * @param parent The parent of <code>n</code>
 */
private void visitGetProp(NodeTraversal t, Node n, Node parent) {
  // obj.prop or obj.method()
  // Lots of types can appear on the left, a call to a void function can
  // never be on the left. getPropertyType will decide what is acceptable
  // and what isn't.
  Node property = n.getLastChild();
  Node objNode = n.getFirstChild();
  JSType childType = getJSType(objNode);

  if (childType.isDict()) {
    report(t, property, TypeValidator.ILLEGAL_PROPERTY_ACCESS, "'.'", "dict");
  } else if (validator.expectNotNullOrUndefined(t, n, childType,
      "No properties on this expression", getNativeType(OBJECT_TYPE))) {
    checkPropertyAccess(childType, property.getString(), t, n);
  }
  ensureTyped(t, n);
}
 
Example 3
Source File: Closure_125_TypeCheck_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Visits a GETPROP node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 * @param parent The parent of <code>n</code>
 */
private void visitGetProp(NodeTraversal t, Node n, Node parent) {
  // obj.prop or obj.method()
  // Lots of types can appear on the left, a call to a void function can
  // never be on the left. getPropertyType will decide what is acceptable
  // and what isn't.
  Node property = n.getLastChild();
  Node objNode = n.getFirstChild();
  JSType childType = getJSType(objNode);

  if (childType.isDict()) {
    report(t, property, TypeValidator.ILLEGAL_PROPERTY_ACCESS, "'.'", "dict");
  } else if (validator.expectNotNullOrUndefined(t, n, childType,
      "No properties on this expression", getNativeType(OBJECT_TYPE))) {
    checkPropertyAccess(childType, property.getString(), t, n);
  }
  ensureTyped(t, n);
}
 
Example 4
Source File: Closure_125_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Visits a GETPROP node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 * @param parent The parent of <code>n</code>
 */
private void visitGetProp(NodeTraversal t, Node n, Node parent) {
  // obj.prop or obj.method()
  // Lots of types can appear on the left, a call to a void function can
  // never be on the left. getPropertyType will decide what is acceptable
  // and what isn't.
  Node property = n.getLastChild();
  Node objNode = n.getFirstChild();
  JSType childType = getJSType(objNode);

  if (childType.isDict()) {
    report(t, property, TypeValidator.ILLEGAL_PROPERTY_ACCESS, "'.'", "dict");
  } else if (validator.expectNotNullOrUndefined(t, n, childType,
      "No properties on this expression", getNativeType(OBJECT_TYPE))) {
    checkPropertyAccess(childType, property.getString(), t, n);
  }
  ensureTyped(t, n);
}
 
Example 5
Source File: Closure_11_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Visits a GETPROP node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 * @param parent The parent of <code>n</code>
 */
private void visitGetProp(NodeTraversal t, Node n, Node parent) {
  // obj.prop or obj.method()
  // Lots of types can appear on the left, a call to a void function can
  // never be on the left. getPropertyType will decide what is acceptable
  // and what isn't.
  Node property = n.getLastChild();
  Node objNode = n.getFirstChild();
  JSType childType = getJSType(objNode);

  if (childType.isDict()) {
    report(t, property, TypeValidator.ILLEGAL_PROPERTY_ACCESS, "'.'", "dict");
  } else if (n.getJSType() != null && parent.isAssign()) {
    return;
  } else if (validator.expectNotNullOrUndefined(t, n, childType,
      "No properties on this expression", getNativeType(OBJECT_TYPE))) {
    checkPropertyAccess(childType, property.getString(), t, n);
  }
  ensureTyped(t, n);
}
 
Example 6
Source File: Closure_11_TypeCheck_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Visits a GETPROP node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 * @param parent The parent of <code>n</code>
 */
private void visitGetProp(NodeTraversal t, Node n, Node parent) {
  // obj.prop or obj.method()
  // Lots of types can appear on the left, a call to a void function can
  // never be on the left. getPropertyType will decide what is acceptable
  // and what isn't.
  Node property = n.getLastChild();
  Node objNode = n.getFirstChild();
  JSType childType = getJSType(objNode);

  if (childType.isDict()) {
    report(t, property, TypeValidator.ILLEGAL_PROPERTY_ACCESS, "'.'", "dict");
  } else if (validator.expectNotNullOrUndefined(t, n, childType,
      "No properties on this expression", getNativeType(OBJECT_TYPE))) {
    checkPropertyAccess(childType, property.getString(), t, n);
  }
  ensureTyped(t, n);
}
 
Example 7
Source File: Closure_2_TypeCheck_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Visits a GETPROP node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 * @param parent The parent of <code>n</code>
 */
private void visitGetProp(NodeTraversal t, Node n, Node parent) {
  // obj.prop or obj.method()
  // Lots of types can appear on the left, a call to a void function can
  // never be on the left. getPropertyType will decide what is acceptable
  // and what isn't.
  Node property = n.getLastChild();
  Node objNode = n.getFirstChild();
  JSType childType = getJSType(objNode);

  if (childType.isDict()) {
    report(t, property, TypeValidator.ILLEGAL_PROPERTY_ACCESS, "'.'", "dict");
  } else if (validator.expectNotNullOrUndefined(t, n, childType,
      "No properties on this expression", getNativeType(OBJECT_TYPE))) {
    checkPropertyAccess(childType, property.getString(), t, n);
  }
  ensureTyped(t, n);
}
 
Example 8
Source File: Closure_2_TypeCheck_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Visits a GETPROP node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 * @param parent The parent of <code>n</code>
 */
private void visitGetProp(NodeTraversal t, Node n, Node parent) {
  // obj.prop or obj.method()
  // Lots of types can appear on the left, a call to a void function can
  // never be on the left. getPropertyType will decide what is acceptable
  // and what isn't.
  Node property = n.getLastChild();
  Node objNode = n.getFirstChild();
  JSType childType = getJSType(objNode);

  if (childType.isDict()) {
    report(t, property, TypeValidator.ILLEGAL_PROPERTY_ACCESS, "'.'", "dict");
  } else if (validator.expectNotNullOrUndefined(t, n, childType,
      "No properties on this expression", getNativeType(OBJECT_TYPE))) {
    checkPropertyAccess(childType, property.getString(), t, n);
  }
  ensureTyped(t, n);
}
 
Example 9
Source File: TypeCheck.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Visits a GETPROP node.
 *
 * @param t The node traversal object that supplies context, such as the
 * scope chain to use in name lookups as well as error reporting.
 * @param n The node being visited.
 * @param parent The parent of <code>n</code>
 */
private void visitGetProp(NodeTraversal t, Node n, Node parent) {
  // obj.prop or obj.method()
  // Lots of types can appear on the left, a call to a void function can
  // never be on the left. getPropertyType will decide what is acceptable
  // and what isn't.
  Node property = n.getLastChild();
  Node objNode = n.getFirstChild();
  JSType childType = getJSType(objNode);

  if (childType.isDict()) {
    report(t, property, TypeValidator.ILLEGAL_PROPERTY_ACCESS, "'.'", "dict");
  } else if (validator.expectNotNullOrUndefined(t, n, childType,
      "No properties on this expression", getNativeType(OBJECT_TYPE))) {
    checkPropertyAccess(childType, property.getString(), t, n);
  }
  ensureTyped(t, n);
}
 
Example 10
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 11
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 12
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 13
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 14
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 15
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 16
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;
  }
}