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

The following examples show how to use com.google.javascript.rhino.jstype.JSType#isNoType() . 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: Closure_117_TypeValidator_t.java    From coming with MIT License 6 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 owner The owner of the property being assigned to.
 * @param propName The name of the property being assigned to.
 * @return True if the types matched, false otherwise.
 */
boolean expectCanAssignToPropertyOf(NodeTraversal t, Node n, JSType rightType,
    JSType leftType, Node owner, String propName) {
  // The NoType check is a hack to make typedefs work OK.
  if (!leftType.isNoType() && !rightType.isSubtype(leftType)) {
    // Do not type-check interface methods, because we expect that
    // they will have dummy implementations that do not match the type
    // annotations.
    JSType ownerType = getJSType(owner);
    if (ownerType.isFunctionPrototypeType()) {
      FunctionType ownerFn = ownerType.toObjectType().getOwnerFunction();
      if (ownerFn.isInterface() &&
          rightType.isFunctionType() && leftType.isFunctionType()) {
        return true;
      }
    }

    mismatch(t, n,
        "assignment to property " + propName + " of " +
        getReadableJSTypeName(owner, true),
        rightType, leftType);
    return false;
  }
  return true;
}
 
Example 2
Source File: Closure_117_TypeValidator_s.java    From coming with MIT License 6 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 owner The owner of the property being assigned to.
 * @param propName The name of the property being assigned to.
 * @return True if the types matched, false otherwise.
 */
boolean expectCanAssignToPropertyOf(NodeTraversal t, Node n, JSType rightType,
    JSType leftType, Node owner, String propName) {
  // The NoType check is a hack to make typedefs work OK.
  if (!leftType.isNoType() && !rightType.isSubtype(leftType)) {
    // Do not type-check interface methods, because we expect that
    // they will have dummy implementations that do not match the type
    // annotations.
    JSType ownerType = getJSType(owner);
    if (ownerType.isFunctionPrototypeType()) {
      FunctionType ownerFn = ownerType.toObjectType().getOwnerFunction();
      if (ownerFn.isInterface() &&
          rightType.isFunctionType() && leftType.isFunctionType()) {
        return true;
      }
    }

    mismatch(t, n,
        "assignment to property " + propName + " of " +
        getReadableJSTypeName(owner, true),
        rightType, leftType);
    return false;
  }
  return true;
}
 
Example 3
Source File: Closure_6_TypeValidator_t.java    From coming with MIT License 6 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 owner The owner of the property being assigned to.
 * @param propName The name of the property being assigned to.
 * @return True if the types matched, false otherwise.
 */
boolean expectCanAssignToPropertyOf(NodeTraversal t, Node n, JSType rightType,
    JSType leftType, Node owner, String propName) {
  // The NoType check is a hack to make typedefs work OK.
  if (!leftType.isNoType() && !rightType.canAssignTo(leftType)) {
    // Do not type-check interface methods, because we expect that
    // they will have dummy implementations that do not match the type
    // annotations.
    JSType ownerType = getJSType(owner);
    if (ownerType.isFunctionPrototypeType()) {
      FunctionType ownerFn = ownerType.toObjectType().getOwnerFunction();
      if (ownerFn.isInterface() &&
          rightType.isFunctionType() && leftType.isFunctionType()) {
        return true;
      }
    }

    mismatch(t, n,
        "assignment to property " + propName + " of " +
        getReadableJSTypeName(owner, true),
        rightType, leftType);
    return false;
  }
  return true;
}
 
Example 4
Source File: TypeValidator.java    From astor with GNU General Public License v2.0 6 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 owner The owner of the property being assigned to.
 * @param propName The name of the property being assigned to.
 * @return True if the types matched, false otherwise.
 */
boolean expectCanAssignToPropertyOf(NodeTraversal t, Node n, JSType rightType,
    JSType leftType, Node owner, String propName) {
  // The NoType check is a hack to make typedefs work OK.
  if (!leftType.isNoType() && !rightType.isSubtype(leftType)) {
    // Do not type-check interface methods, because we expect that
    // they will have dummy implementations that do not match the type
    // annotations.
    JSType ownerType = getJSType(owner);
    if (ownerType.isFunctionPrototypeType()) {
      FunctionType ownerFn = ownerType.toObjectType().getOwnerFunction();
      if (ownerFn.isInterface() &&
          rightType.isFunctionType() && leftType.isFunctionType()) {
        return true;
      }
    }

    mismatch(t, n,
        "assignment to property " + propName + " of " +
        getReadableJSTypeName(owner, true),
        rightType, leftType);
    return false;
  }
  return true;
}
 
Example 5
Source File: DeclarationGenerator.java    From clutz with MIT License 5 votes vote down vote up
private boolean isAliasedClassOrInterface(TypedVar symbol, JSType type) {
  // Confusingly typedefs are constructors. However, they cannot be aliased AFAICT.
  if (type.isNoType()) return false;
  if (!type.isConstructor() && !type.isInterface()) return false;
  String symbolName = symbol.getName();
  String typeName = type.getDisplayName();
  // Turns out that for aliases the symbol and type name differ.
  return !symbolName.equals(typeName) || KNOWN_CLASS_ALIASES.containsKey(symbolName);
}
 
Example 6
Source File: Closure_117_TypeValidator_t.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 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: Closure_6_TypeValidator_t.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 9
Source File: Closure_6_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 10
Source File: Closure_6_TypeValidator_s.java    From coming with MIT License 5 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 owner The owner of the property being assigned to.
 * @param propName The name of the property being assigned to.
 * @return True if the types matched, false otherwise.
 */
boolean expectCanAssignToPropertyOf(NodeTraversal t, Node n, JSType rightType,
    JSType leftType, Node owner, String propName) {
  // The NoType check is a hack to make typedefs work OK.
  if (!leftType.isNoType() && !rightType.canAssignTo(leftType)) {
    if ((leftType.isConstructor() || leftType.isEnumType()) && (rightType.isConstructor() || rightType.isEnumType())) {
      registerMismatch(rightType, leftType, null);
    } else {
    // Do not type-check interface methods, because we expect that
    // they will have dummy implementations that do not match the type
    // annotations.
    JSType ownerType = getJSType(owner);
    if (ownerType.isFunctionPrototypeType()) {
      FunctionType ownerFn = ownerType.toObjectType().getOwnerFunction();
      if (ownerFn.isInterface() &&
          rightType.isFunctionType() && leftType.isFunctionType()) {
        return true;
      }
    }

    mismatch(t, n,
        "assignment to property " + propName + " of " +
        getReadableJSTypeName(owner, true),
        rightType, leftType);
    }
    return false;
  }
  return true;
}
 
Example 11
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 12
Source File: PureFunctionIdentifier.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @return Whether the jstype is something known to be a local value.
 */
private boolean isLocalValueType(JSType jstype, boolean recurse) {
  Preconditions.checkNotNull(jstype);
  JSType subtype =  jstype.getGreatestSubtype(
      compiler.getTypeRegistry().getNativeType(JSTypeNative.OBJECT_TYPE));
  // If the type includes anything related to a object type, don't assume
  // anything about the locality of the value.
  return subtype.isNoType();
}
 
Example 13
Source File: DeclarationGenerator.java    From clutz with MIT License 4 votes vote down vote up
private static boolean isTypedef(JSType type) {
  return type.isNoType();
}