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

The following examples show how to use com.google.javascript.rhino.jstype.JSType#isSubtype() . 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: TypeValidator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void registerMismatch(JSType found, JSType required, JSError error) {
  // Don't register a mismatch for differences in null or undefined or if the
  // code didn't downcast.
  found = found.restrictByNotNullOrUndefined();
  required = required.restrictByNotNullOrUndefined();
  if (found.isSubtype(required) || required.isSubtype(found)) {
    return;
  }

  mismatches.add(new TypeMismatch(found, required, error));
  if (found.isFunctionType() &&
      required.isFunctionType()) {
    FunctionType fnTypeA = found.toMaybeFunctionType();
    FunctionType fnTypeB = required.toMaybeFunctionType();
    Iterator<Node> paramItA = fnTypeA.getParameters().iterator();
    Iterator<Node> paramItB = fnTypeB.getParameters().iterator();
    while (paramItA.hasNext() && paramItB.hasNext()) {
      registerIfMismatch(paramItA.next().getJSType(),
          paramItB.next().getJSType(), error);
    }

    registerIfMismatch(
        fnTypeA.getReturnType(), fnTypeB.getReturnType(), error);
  }
}
 
Example 2
Source File: Closure_6_TypeValidator_t.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect the type to contain an object sometimes. If the expectation is
 * not met, issue a warning at the provided node's source code position.
 */
void expectAnyObject(NodeTraversal t, Node n, JSType type, String msg) {
  JSType anyObjectType = getNativeType(NO_OBJECT_TYPE);
  if (!anyObjectType.isSubtype(type) && !type.isEmptyType()) {
    mismatch(t, n, msg, type, anyObjectType);
  }
}
 
Example 3
Source File: TypeValidator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Expect the type to be anything but the null or void type. If the
 * expectation is not met, issue a warning at the provided node's
 * source code position. Note that a union type that includes the
 * void type and at least one other type meets the expectation.
 * @return Whether the expectation was met.
 */
boolean expectNotNullOrUndefined(
    NodeTraversal t, Node n, JSType type, String msg, JSType expectedType) {
  if (!type.isNoType() && !type.isUnknownType() &&
      type.isSubtype(nullOrUndefined) &&
      !containsForwardDeclaredUnresolvedName(type)) {

    // There's one edge case right now that we don't handle well, and
    // that we don't want to warn about.
    // if (this.x == null) {
    //   this.initializeX();
    //   this.x.foo();
    // }
    // In this case, we incorrectly type x because of how we
    // infer properties locally. See issue 109.
    // http://code.google.com/p/closure-compiler/issues/detail?id=109
    //
    // We do not do this inference globally.
    if (n.isGetProp() &&
        !t.inGlobalScope() && type.isNullType()) {
      return true;
    }

    mismatch(t, n, msg, type, expectedType);
    return false;
  }
  return true;
}
 
Example 4
Source File: Closure_117_TypeValidator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect the type to contain an object sometimes. If the expectation is
 * not met, issue a warning at the provided node's source code position.
 */
void expectAnyObject(NodeTraversal t, Node n, JSType type, String msg) {
  JSType anyObjectType = getNativeType(NO_OBJECT_TYPE);
  if (!anyObjectType.isSubtype(type) && !type.isEmptyType()) {
    mismatch(t, n, msg, type, anyObjectType);
  }
}
 
Example 5
Source File: Closure_19_ChainableReverseAbstractInterpreter_s.java    From coming with MIT License 5 votes vote down vote up
@Override
public JSType caseObjectType(ObjectType type) {
  if (value.equals("function")) {
    JSType ctorType = getNativeType(U2U_CONSTRUCTOR_TYPE);
    return resultEqualsValue && ctorType.isSubtype(type) ? ctorType : null;
  }
  return matchesExpectation("object") ? type : null;
}
 
Example 6
Source File: Closure_90_FunctionTypeBuilder_s.java    From coming with MIT License 5 votes vote down vote up
@Override
public boolean apply(JSType type) {
  // TODO(user): Doing an instanceof check here is too
  // restrictive as (Date,Error) is, for instance, an object type
  // even though its implementation is a UnionType. Would need to
  // create interfaces JSType, ObjectType, FunctionType etc and have
  // separate implementation instead of the class hierarchy, so that
  // union types can also be object types, etc.
  if (!type.isSubtype(
          typeRegistry.getNativeType(OBJECT_TYPE))) {
    reportWarning(THIS_TYPE_NON_OBJECT, type.toString());
    return false;
  }
  return true;
}
 
Example 7
Source File: Closure_117_TypeValidator_s.java    From coming with MIT License 5 votes vote down vote up
/**
 * Expect the type to be anything but the null or void type. If the
 * expectation is not met, issue a warning at the provided node's
 * source code position. Note that a union type that includes the
 * void type and at least one other type meets the expectation.
 * @return Whether the expectation was met.
 */
boolean expectNotNullOrUndefined(
    NodeTraversal t, Node n, JSType type, String msg, JSType expectedType) {
  if (!type.isNoType() && !type.isUnknownType() &&
      type.isSubtype(nullOrUndefined) &&
      !containsForwardDeclaredUnresolvedName(type)) {

    // There's one edge case right now that we don't handle well, and
    // that we don't want to warn about.
    // if (this.x == null) {
    //   this.initializeX();
    //   this.x.foo();
    // }
    // In this case, we incorrectly type x because of how we
    // infer properties locally. See issue 109.
    // http://code.google.com/p/closure-compiler/issues/detail?id=109
    //
    // We do not do this inference globally.
    if (n.isGetProp() &&
        !t.inGlobalScope() && type.isNullType()) {
      return true;
    }

    mismatch(t, n, msg, type, expectedType);
    return false;
  }
  return true;
}
 
Example 8
Source File: TypeValidator.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Expect that the property in an interface that this type implements is
 * implemented and correctly typed.
 */
private void expectInterfaceProperty(NodeTraversal t, Node n,
    ObjectType instance, ObjectType implementedInterface, String prop) {
  StaticSlot<JSType> propSlot = instance.getSlot(prop);
  if (propSlot == null) {
    // Not implemented
    String sourceName = n.getSourceFileName();
    sourceName = sourceName == null ? "" : sourceName;
    registerMismatch(instance, implementedInterface,
        report(JSError.make(sourceName, n,
        INTERFACE_METHOD_NOT_IMPLEMENTED,
        prop, implementedInterface.toString(), instance.toString())));
  } else {
    Node propNode = propSlot.getDeclaration() == null ?
        null : propSlot.getDeclaration().getNode();

    // Fall back on the constructor node if we can't find a node for the
    // property.
    propNode = propNode == null ? n : propNode;

    JSType found = propSlot.getType();
    JSType required
        = implementedInterface.getImplicitPrototype().getPropertyType(prop);
    found = found.restrictByNotNullOrUndefined();
    required = required.restrictByNotNullOrUndefined();
    if (!found.isSubtype(required)) {
      // Implemented, but not correctly typed
      FunctionType constructor =
          implementedInterface.toObjectType().getConstructor();
      registerMismatch(found, required, report(t.makeError(propNode,
          HIDDEN_INTERFACE_PROPERTY_MISMATCH, prop,
          constructor.getTopMostDefiningType(prop).toString(),
          required.toString(), found.toString())));
    }
  }
}
 
Example 9
Source File: TypeInference.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
private boolean isAddedAsNumber(JSType type) {
  return type.isSubtype(registry.createUnionType(VOID_TYPE, NULL_TYPE,
      NUMBER_VALUE_OR_OBJECT_TYPE, BOOLEAN_TYPE, BOOLEAN_OBJECT_TYPE));
}
 
Example 10
Source File: Closure_117_TypeValidator_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Expect that the property in an interface that this type implements is
 * implemented and correctly typed.
 */
private void expectInterfaceProperty(NodeTraversal t, Node n,
    ObjectType instance, ObjectType implementedInterface, String prop) {
  StaticSlot<JSType> propSlot = instance.getSlot(prop);
  if (propSlot == null) {
    // Not implemented
    String sourceName = n.getSourceFileName();
    sourceName = sourceName == null ? "" : sourceName;
    registerMismatch(instance, implementedInterface,
        report(JSError.make(sourceName, n,
        INTERFACE_METHOD_NOT_IMPLEMENTED,
        prop, implementedInterface.toString(), instance.toString())));
  } else {
    Node propNode = propSlot.getDeclaration() == null ?
        null : propSlot.getDeclaration().getNode();

    // Fall back on the constructor node if we can't find a node for the
    // property.
    propNode = propNode == null ? n : propNode;

    JSType found = propSlot.getType();
    found = found.restrictByNotNullOrUndefined();

    JSType required
        = implementedInterface.getImplicitPrototype().getPropertyType(prop);
    TemplateTypeMap typeMap = implementedInterface.getTemplateTypeMap();
    if (!typeMap.isEmpty()) {
      TemplateTypeMapReplacer replacer = new TemplateTypeMapReplacer(
          typeRegistry, typeMap);
      required = required.visit(replacer);
    }
    required = required.restrictByNotNullOrUndefined();

    if (!found.isSubtype(required)) {
      // Implemented, but not correctly typed
      FunctionType constructor =
          implementedInterface.toObjectType().getConstructor();
      registerMismatch(found, required, report(t.makeError(propNode,
          HIDDEN_INTERFACE_PROPERTY_MISMATCH, prop,
          constructor.getTopMostDefiningType(prop).toString(),
          required.toString(), found.toString())));
    }
  }
}
 
Example 11
Source File: Closure_43_TypedScopeCreator_s.java    From coming with MIT License 4 votes vote down vote up
private void defineObjectLiteral(Node objectLit) {
  // Handle the @lends annotation.
  JSType type = null;
  JSDocInfo info = objectLit.getJSDocInfo();
  if (info != null &&
      info.getLendsName() != null) {
    String lendsName = info.getLendsName();
    Var lendsVar = scope.getVar(lendsName);
    if (lendsVar == null) {
      compiler.report(
          JSError.make(sourceName, objectLit, UNKNOWN_LENDS, lendsName));
    } else {
      type = lendsVar.getType();
      if (type == null) {
        type = typeRegistry.getNativeType(UNKNOWN_TYPE);
      }
      if (!type.isSubtype(typeRegistry.getNativeType(OBJECT_TYPE))) {
        compiler.report(
            JSError.make(sourceName, objectLit, LENDS_ON_NON_OBJECT,
                lendsName, type.toString()));
        type = null;
      } else {
        objectLit.setJSType(type);
      }
    }
  }

  info = NodeUtil.getBestJSDocInfo(objectLit);
  Node lValue = NodeUtil.getBestLValue(objectLit);
  String lValueName = NodeUtil.getBestLValueName(lValue);
  boolean createdEnumType = false;
  if (info != null && info.hasEnumParameterType()) {
    type = createEnumTypeFromNodes(objectLit, lValueName, info, lValue);
    createdEnumType = true;
  }

  if (type == null) {
    type = typeRegistry.createAnonymousObjectType();
  }

  setDeferredType(objectLit, type);

  // If this is an enum, the properties were already taken care of above.
  processObjectLitProperties(
      objectLit, ObjectType.cast(objectLit.getJSType()), !createdEnumType);
}
 
Example 12
Source File: Closure_43_TypedScopeCreator_t.java    From coming with MIT License 4 votes vote down vote up
private void defineObjectLiteral(Node objectLit) {
  // Handle the @lends annotation.
  JSType type = null;
  JSDocInfo info = objectLit.getJSDocInfo();
  if (info != null &&
      info.getLendsName() != null) {
    String lendsName = info.getLendsName();
    Var lendsVar = scope.getVar(lendsName);
    if (lendsVar == null) {
      compiler.report(
          JSError.make(sourceName, objectLit, UNKNOWN_LENDS, lendsName));
    } else {
      type = lendsVar.getType();
      if (type == null) {
        type = typeRegistry.getNativeType(UNKNOWN_TYPE);
      }
      if (!type.isSubtype(typeRegistry.getNativeType(OBJECT_TYPE))) {
        compiler.report(
            JSError.make(sourceName, objectLit, LENDS_ON_NON_OBJECT,
                lendsName, type.toString()));
        type = null;
      } else {
        objectLit.setJSType(type);
      }
    }
  }

  info = NodeUtil.getBestJSDocInfo(objectLit);
  Node lValue = NodeUtil.getBestLValue(objectLit);
  String lValueName = NodeUtil.getBestLValueName(lValue);
  boolean createdEnumType = false;
  if (info != null && info.hasEnumParameterType()) {
    type = createEnumTypeFromNodes(objectLit, lValueName, info, lValue);
    createdEnumType = true;
  }

  if (type == null) {
    type = typeRegistry.createAnonymousObjectType();
  }

  setDeferredType(objectLit, type);

  // If this is an enum, the properties were already taken care of above.
  processObjectLitProperties(
      objectLit, ObjectType.cast(objectLit.getJSType()), !createdEnumType);
}
 
Example 13
Source File: Closure_48_TypedScopeCreator_t.java    From coming with MIT License 4 votes vote down vote up
private void defineObjectLiteral(NodeTraversal t, Node objectLit) {
  // Handle the @lends annotation.
  JSType type = null;
  JSDocInfo info = objectLit.getJSDocInfo();
  if (info != null &&
      info.getLendsName() != null) {
    String lendsName = info.getLendsName();
    Var lendsVar = scope.getVar(lendsName);
    if (lendsVar == null) {
      compiler.report(
          JSError.make(sourceName, objectLit, UNKNOWN_LENDS, lendsName));
    } else {
      type = lendsVar.getType();
      if (type == null) {
        type = typeRegistry.getNativeType(UNKNOWN_TYPE);
      }
      if (!type.isSubtype(typeRegistry.getNativeType(OBJECT_TYPE))) {
        compiler.report(
            JSError.make(sourceName, objectLit, LENDS_ON_NON_OBJECT,
                lendsName, type.toString()));
        type = null;
      } else {
        objectLit.setJSType(type);
      }
    }
  }

  info = NodeUtil.getBestJSDocInfo(objectLit);
  Node lValue = NodeUtil.getBestLValue(objectLit);
  String lValueName = NodeUtil.getBestLValueName(lValue);
  boolean createdEnumType = false;
  if (info != null && info.hasEnumParameterType()) {
    type = createEnumTypeFromNodes(objectLit, lValueName, info, lValue);
    createdEnumType = true;
  }

  if (type == null) {
    type = typeRegistry.createAnonymousObjectType();
  }

  setDeferredType(objectLit, type);

  // If this is an enum, the properties were already taken care of above.
  processObjectLitProperties(
      t, objectLit, ObjectType.cast(objectLit.getJSType()), !createdEnumType);
}
 
Example 14
Source File: Closure_25_TypeInference_s.java    From coming with MIT License 4 votes vote down vote up
private boolean isAddedAsNumber(JSType type) {
  return type.isSubtype(registry.createUnionType(VOID_TYPE, NULL_TYPE,
      NUMBER_VALUE_OR_OBJECT_TYPE, BOOLEAN_TYPE, BOOLEAN_OBJECT_TYPE));
}
 
Example 15
Source File: Closure_6_TypeValidator_s.java    From coming with MIT License 4 votes vote down vote up
/**
 * Expect the type to be a valid operand to a bitwise operator. This includes
 * numbers, any type convertible to a number, or any other primitive type
 * (undefined|null|boolean|string).
 */
void expectBitwiseable(NodeTraversal t, Node n, JSType type, String msg) {
  if (!type.matchesNumberContext() && !type.isSubtype(allValueTypes)) {
    mismatch(t, n, msg, type, allValueTypes);
  }
}
 
Example 16
Source File: Nopol2017_0027_t.java    From coming with MIT License 4 votes vote down vote up
private void defineObjectLiteral(Node objectLit) {
  // Handle the @lends annotation.
  JSType type = null;
  JSDocInfo info = objectLit.getJSDocInfo();
  if (info != null &&
      info.getLendsName() != null) {
    String lendsName = info.getLendsName();
    Var lendsVar = scope.getVar(lendsName);
    if (lendsVar == null) {
      compiler.report(
          JSError.make(sourceName, objectLit, UNKNOWN_LENDS, lendsName));
    } else {
      type = lendsVar.getType();
      if (type == null) {
        type = typeRegistry.getNativeType(UNKNOWN_TYPE);
      }
      if (!type.isSubtype(typeRegistry.getNativeType(OBJECT_TYPE))) {
        compiler.report(
            JSError.make(sourceName, objectLit, LENDS_ON_NON_OBJECT,
                lendsName, type.toString()));
        type = null;
      } else {
        objectLit.setJSType(type);
      }
    }
  }

  info = NodeUtil.getBestJSDocInfo(objectLit);
  Node lValue = NodeUtil.getBestLValue(objectLit);
  String lValueName = NodeUtil.getBestLValueName(lValue);
  boolean createdEnumType = false;
  if (info != null && info.hasEnumParameterType()) {
    type = createEnumTypeFromNodes(objectLit, lValueName, info, lValue);
    createdEnumType = true;
  }

  if (type == null) {
    type = typeRegistry.createAnonymousObjectType();
  }

  setDeferredType(objectLit, type);

  // If this is an enum, the properties were already taken care of above.
  processObjectLitProperties(
      objectLit, ObjectType.cast(objectLit.getJSType()), !createdEnumType);
}
 
Example 17
Source File: CheckMissingReturn.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * @return {@code true} if returnType is void, unknown, or a union
 *     containing void or unknown
 */
private boolean isVoidOrUnknown(JSType returnType) {
  final JSType voidType =
      compiler.getTypeRegistry().getNativeType(JSTypeNative.VOID_TYPE);
  return voidType.isSubtype(returnType);
}
 
Example 18
Source File: Closure_112_TypeInference_s.java    From coming with MIT License 4 votes vote down vote up
private boolean isAddedAsNumber(JSType type) {
  return type.isSubtype(registry.createUnionType(VOID_TYPE, NULL_TYPE,
      NUMBER_VALUE_OR_OBJECT_TYPE, BOOLEAN_TYPE, BOOLEAN_OBJECT_TYPE));
}
 
Example 19
Source File: Closure_117_TypeValidator_s.java    From coming with MIT License 3 votes vote down vote up
/**
 * Expect that the first type can override a property of the second
 * type.
 *
 * @param t The node traversal.
 * @param n The node to issue warnings on.
 * @param overridingType The overriding type.
 * @param hiddenType The type of the property being overridden.
 * @param propertyName The name of the property, for use in the
 *     warning message.
 * @param ownerType The type of the owner of the property, for use
 *     in the warning message.
 */
void expectCanOverride(NodeTraversal t, Node n, JSType overridingType,
    JSType hiddenType, String propertyName, JSType ownerType) {
  if (!overridingType.isSubtype(hiddenType)) {
    registerMismatch(overridingType, hiddenType,
        report(t.makeError(n, HIDDEN_PROPERTY_MISMATCH, propertyName,
          ownerType.toString(), hiddenType.toString(),
          overridingType.toString())));
  }
}
 
Example 20
Source File: TypeValidator.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Expect that the first type can be assigned to a symbol of the second
 * type.
 *
 * @param t The node traversal.
 * @param n The node to issue warnings on.
 * @param rightType The type on the RHS of the assign.
 * @param leftType The type of the symbol on the LHS of the assign.
 * @param msg An extra message for the mismatch warning, if necessary.
 * @return True if the types matched, false otherwise.
 */
boolean expectCanAssignTo(NodeTraversal t, Node n, JSType rightType,
    JSType leftType, String msg) {
  if (!rightType.isSubtype(leftType)) {
    mismatch(t, n, msg, rightType, leftType);
    return false;
  }
  return true;
}