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

The following examples show how to use com.google.javascript.rhino.jstype.JSType#isEnumElementType() . 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: DeclarationGenerator.java    From clutz with MIT License 6 votes vote down vote up
private boolean isPrivate(JSType type) {
  // Due to https://github.com/google/closure-compiler/issues/1975 we cannot obtain the JSDoc
  // for a typedef. Assume non-private as it is more common.
  // Closure creates a NamedType when the typedef is used in an union, eg: T | null.
  // Dereference the named type before checking if it is a typedef.
  NamedType nType = type.toMaybeNamedType();
  if (typedefs.containsKey(type)
      || (nType != null && typedefs.containsKey(nType.getReferencedType()))) {
    return false;
  }

  // For unknown reasons, enum types do not keep their defining jsdoc info.
  if (type.isEnumType() || type.isEnumElementType()) {
    return isPrivate(type.getDisplayName());
  } else {
    return isPrivate(type.getJSDocInfo());
  }
}
 
Example 2
Source File: Closure_118_DisambiguateProperties_s.java    From coming with MIT License 6 votes vote down vote up
private ConcreteType maybeAddAutoboxes(
    ConcreteType cType, JSType jsType, String prop) {
  jsType = jsType.restrictByNotNullOrUndefined();
  if (jsType.isUnionType()) {
    for (JSType alt : jsType.toMaybeUnionType().getAlternates()) {
      cType = maybeAddAutoboxes(cType, alt, prop);
    }
    return cType;
  } else if (jsType.isEnumElementType()) {
    return maybeAddAutoboxes(
        cType, jsType.toMaybeEnumElementType().getPrimitiveType(), prop);
  }

  if (jsType.autoboxesTo() != null) {
    JSType autoboxed = jsType.autoboxesTo();
    return cType.unionWith(tt.getConcreteInstance((ObjectType) autoboxed));
  } else if (jsType.unboxesTo() != null) {
    return cType.unionWith(tt.getConcreteInstance((ObjectType) jsType));
  }

  return cType;
}
 
Example 3
Source File: Closure_118_DisambiguateProperties_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Invalidates the given type, so that no properties on it will be renamed.
 */
private void addInvalidatingType(JSType type, JSError error) {
  type = type.restrictByNotNullOrUndefined();
  if (type.isUnionType()) {
    for (JSType alt : type.toMaybeUnionType().getAlternates()) {
      addInvalidatingType(alt, error);
    }
  } else if (type.isEnumElementType()) {
    addInvalidatingType(
        type.toMaybeEnumElementType().getPrimitiveType(), error);
  } else {
    typeSystem.addInvalidatingType(type);
    recordInvalidationError(type, error);
    ObjectType objType = ObjectType.cast(type);
    if (objType != null && objType.getImplicitPrototype() != null) {
      typeSystem.addInvalidatingType(objType.getImplicitPrototype());
      recordInvalidationError(objType.getImplicitPrototype(), error);
    }
  }
}
 
Example 4
Source File: Closure_118_DisambiguateProperties_t.java    From coming with MIT License 6 votes vote down vote up
private ConcreteType maybeAddAutoboxes(
    ConcreteType cType, JSType jsType, String prop) {
  jsType = jsType.restrictByNotNullOrUndefined();
  if (jsType.isUnionType()) {
    for (JSType alt : jsType.toMaybeUnionType().getAlternates()) {
      cType = maybeAddAutoboxes(cType, alt, prop);
    }
    return cType;
  } else if (jsType.isEnumElementType()) {
    return maybeAddAutoboxes(
        cType, jsType.toMaybeEnumElementType().getPrimitiveType(), prop);
  }

  if (jsType.autoboxesTo() != null) {
    JSType autoboxed = jsType.autoboxesTo();
    return cType.unionWith(tt.getConcreteInstance((ObjectType) autoboxed));
  } else if (jsType.unboxesTo() != null) {
    return cType.unionWith(tt.getConcreteInstance((ObjectType) jsType));
  }

  return cType;
}
 
Example 5
Source File: DisambiguateProperties.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private ConcreteType maybeAddAutoboxes(
    ConcreteType cType, JSType jsType, String prop) {
  jsType = jsType.restrictByNotNullOrUndefined();
  if (jsType.isUnionType()) {
    for (JSType alt : jsType.toMaybeUnionType().getAlternates()) {
      cType = maybeAddAutoboxes(cType, alt, prop);
    }
    return cType;
  } else if (jsType.isEnumElementType()) {
    return maybeAddAutoboxes(
        cType, jsType.toMaybeEnumElementType().getPrimitiveType(), prop);
  }

  if (jsType.autoboxesTo() != null) {
    JSType autoboxed = jsType.autoboxesTo();
    return cType.unionWith(tt.getConcreteInstance((ObjectType) autoboxed));
  } else if (jsType.unboxesTo() != null) {
    return cType.unionWith(tt.getConcreteInstance((ObjectType) jsType));
  }

  return cType;
}
 
Example 6
Source File: DisambiguateProperties.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override public ImmutableSet<JSType> getTypesToSkipForType(JSType type) {
  type = type.restrictByNotNullOrUndefined();
  if (type.isUnionType()) {
    Set<JSType> types = Sets.newHashSet(type);
    for (JSType alt : type.toMaybeUnionType().getAlternates()) {
      types.addAll(getTypesToSkipForTypeNonUnion(type));
    }
    return ImmutableSet.copyOf(types);
  } else if (type.isEnumElementType()) {
    return getTypesToSkipForType(
        type.toMaybeEnumElementType().getPrimitiveType());
  }
  return ImmutableSet.copyOf(getTypesToSkipForTypeNonUnion(type));
}
 
Example 7
Source File: TypeCollectionPass.java    From js-dossier with Apache License 2.0 5 votes vote down vote up
private static boolean isPrimitive(JSType type) {
  return !type.isEnumElementType()
      && (type.isBooleanValueType()
          || type.isBooleanObjectType()
          || type.isNumber()
          || type.isNumberValueType()
          || type.isNumberObjectType()
          || type.isString()
          || type.isStringObjectType()
          || type.isStringValueType()
          || type.isVoidType()
          || type.isArrayType());
}
 
Example 8
Source File: Closure_118_DisambiguateProperties_s.java    From coming with MIT License 5 votes vote down vote up
@Override public ImmutableSet<JSType> getTypesToSkipForType(JSType type) {
  type = type.restrictByNotNullOrUndefined();
  if (type.isUnionType()) {
    Set<JSType> types = Sets.newHashSet(type);
    for (JSType alt : type.toMaybeUnionType().getAlternates()) {
      types.addAll(getTypesToSkipForTypeNonUnion(alt));
    }
    return ImmutableSet.copyOf(types);
  } else if (type.isEnumElementType()) {
    return getTypesToSkipForType(
        type.toMaybeEnumElementType().getPrimitiveType());
  }
  return ImmutableSet.copyOf(getTypesToSkipForTypeNonUnion(type));
}
 
Example 9
Source File: Closure_118_DisambiguateProperties_t.java    From coming with MIT License 5 votes vote down vote up
@Override public ImmutableSet<JSType> getTypesToSkipForType(JSType type) {
  type = type.restrictByNotNullOrUndefined();
  if (type.isUnionType()) {
    Set<JSType> types = Sets.newHashSet(type);
    for (JSType alt : type.toMaybeUnionType().getAlternates()) {
      types.addAll(getTypesToSkipForTypeNonUnion(alt));
    }
    return ImmutableSet.copyOf(types);
  } else if (type.isEnumElementType()) {
    return getTypesToSkipForType(
        type.toMaybeEnumElementType().getPrimitiveType());
  }
  return ImmutableSet.copyOf(getTypesToSkipForTypeNonUnion(type));
}
 
Example 10
Source File: Closure_118_DisambiguateProperties_t.java    From coming with MIT License 4 votes vote down vote up
@Override public ObjectType getTypeWithProperty(String field, JSType type) {
  if (type == null) {
    return null;
  }

  if (type.isEnumElementType()) {
    return getTypeWithProperty(
        field, type.toMaybeEnumElementType().getPrimitiveType());
  }

  if (!(type instanceof ObjectType)) {
    if (type.autoboxesTo() != null) {
      type = type.autoboxesTo();
    } else {
      return null;
    }
  }

  // Ignore the prototype itself at all times.
  if ("prototype".equals(field)) {
    return null;
  }

  // We look up the prototype chain to find the highest place (if any) that
  // this appears.  This will make references to overridden properties look
  // like references to the initial property, so they are renamed alike.
  ObjectType foundType = null;
  ObjectType objType = ObjectType.cast(type);
  if (objType != null && objType.getConstructor() != null
      && objType.getConstructor().isInterface()) {
    ObjectType topInterface = FunctionType.getTopDefiningInterface(
        objType, field);
    if (topInterface != null && topInterface.getConstructor() != null) {
      foundType = topInterface.getConstructor().getPrototype();
    }
  } else {
    while (objType != null && objType.getImplicitPrototype() != objType) {
      if (objType.hasOwnProperty(field)) {
        foundType = objType;
      }
      objType = objType.getImplicitPrototype();
    }
  }

  // If the property does not exist on the referenced type but the original
  // type is an object type, see if any subtype has the property.
  if (foundType == null) {
    ObjectType maybeType = ObjectType.cast(
        registry.getGreatestSubtypeWithProperty(type, field));
    // getGreatestSubtypeWithProperty does not guarantee that the property
    // is defined on the returned type, it just indicates that it might be,
    // so we have to double check.
    if (maybeType != null && maybeType.hasOwnProperty(field)) {
      foundType = maybeType;
    }
  }
  return foundType;
}
 
Example 11
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 12
Source File: TypeCollectionPass.java    From js-dossier with Apache License 2.0 4 votes vote down vote up
private void processSymbol(Symbol symbol) {
  final JSType globalThis =
      compiler.getTypeRegistry().getNativeObjectType(JSTypeNative.GLOBAL_THIS);

  if (typeRegistry.isType(symbol.getName())) {
    return;
  }

  JSType type = null;
  boolean isFromCompilerRegistry = false;

  if (symbol.getReferencedSymbol() != null
      && typeRegistry.isType(symbol.getReferencedSymbol())) {
    type = typeRegistry.getType(symbol.getReferencedSymbol()).getType();
  }

  if (type == null) {
    type = compiler.getTypeRegistry().getGlobalType(symbol.getName());
    isFromCompilerRegistry = type != null;
  }

  if (type == null) {
    type = globalThis;
    for (String part : Splitter.on('.').split(symbol.getName())) {
      type = type.findPropertyType(part);
      if (type == null) {
        return;
      }
    }
  }

  if (type != null) {
    if (type.isInstanceType()
        && (isFromCompilerRegistry
            || shouldConvertToConstructor(type)
            || shouldConvertToConstructor(symbol))) {
      type = type.toObjectType().getConstructor();
    } else if (type.isEnumElementType()) {
      type = type.toMaybeEnumElementType().getEnumType();
    }
  }

  if (type == null) {
    log.warning("skipping " + symbol.getName() + "; no type");
    return;
  } else if (globalThis.equals(type)) {
    log.warning("skipping " + symbol.getName() + "; references global this");
    return;
  } else if (type.isOrdinaryFunction() && !typeRegistry.isProvided(symbol.getName())) {
    log.info("skipping " + symbol.getName() + "; is ordinary function");
    return;
  }
  collectTypes(symbol.getName(), type, symbol.getNode(), symbol.getJSDocInfo());
}
 
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_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 15
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 16
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 17
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 18
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;
  }
}
 
Example 19
Source File: DisambiguateProperties.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Override public ObjectType getTypeWithProperty(String field, JSType type) {
  if (type == null) {
    return null;
  }

  if (type.isEnumElementType()) {
    return getTypeWithProperty(
        field, type.toMaybeEnumElementType().getPrimitiveType());
  }

  if (!(type instanceof ObjectType)) {
    if (type.autoboxesTo() != null) {
      type = type.autoboxesTo();
    } else {
      return null;
    }
  }

  // Ignore the prototype itself at all times.
  if ("prototype".equals(field)) {
    return null;
  }

  // We look up the prototype chain to find the highest place (if any) that
  // this appears.  This will make references to overridden properties look
  // like references to the initial property, so they are renamed alike.
  ObjectType foundType = null;
  ObjectType objType = ObjectType.cast(type);
  if (objType != null && objType.getConstructor() != null
      && objType.getConstructor().isInterface()) {
    ObjectType topInterface = FunctionType.getTopDefiningInterface(
        objType, field);
    if (topInterface != null && topInterface.getConstructor() != null) {
      foundType = topInterface.getConstructor().getPrototype();
    }
  } else {
    while (objType != null && objType.getImplicitPrototype() != objType) {
      if (objType.hasOwnProperty(field)) {
        foundType = objType;
      }
      objType = objType.getImplicitPrototype();
    }
  }

  // If the property does not exist on the referenced type but the original
  // type is an object type, see if any subtype has the property.
  if (foundType == null) {
    ObjectType maybeType = ObjectType.cast(
        registry.getGreatestSubtypeWithProperty(type, field));
    // getGreatestSubtypeWithProperty does not guarantee that the property
    // is defined on the returned type, it just indicates that it might be,
    // so we have to double check.
    if (maybeType != null && maybeType.hasOwnProperty(field)) {
      foundType = maybeType;
    }
  }
  return foundType;
}
 
Example 20
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;
  }
}