void expectMatchingTypesStrict(Node n, JSType left, JSType right, String msg) { if (!left.isSubtypeOf(right) && !right.isSubtypeOf(left)) { registerMismatchAndReport(n, INVALID_OPERAND_TYPE, msg, right, left, null, null); } }
/** * Tests whether the type is a string (value or Object). * @return <code>this <: (String, string)</code> */ public final boolean isString() { return isSubtypeOf( getNativeType(JSTypeNative.STRING_VALUE_OR_OBJECT_TYPE)); }
@Override public JSType caseObjectType(ObjectType type) { JSType arrayType = getNativeType(ARRAY_TYPE); return arrayType.isSubtypeOf(type) ? arrayType : null; } };
/** * Tests whether the type is a number (value or Object). * @return <code>this <: (Number, number)</code> */ public final boolean isNumber() { return isSubtypeOf( getNativeType(JSTypeNative.NUMBER_VALUE_OR_OBJECT_TYPE)); }
@Override public TernaryValue testForEquality(JSType that) { if (UNKNOWN.equals(super.testForEquality(that))) { return UNKNOWN; } if (that.isSubtypeOf(this) || that.isSubtypeOf(getNativeType(JSTypeNative.NULL_TYPE))) { return TRUE; } return FALSE; }
/** Expect the type to be a AsyncGenerator or supertype of AsyncGenerator. */ void expectAsyncGeneratorSupertype(NodeTraversal t, Node n, JSType type, String msg) { if (!getNativeType(ASYNC_GENERATOR_TYPE).isSubtypeOf(type)) { mismatch(t, n, msg, type, ASYNC_GENERATOR_TYPE); } }
/** Expect the type to be a Generator or supertype of Generator. */ void expectGeneratorSupertype(NodeTraversal t, Node n, JSType type, String msg) { if (!getNativeType(GENERATOR_TYPE).isSubtypeOf(type)) { mismatch(t, n, msg, type, GENERATOR_TYPE); } }
/** Expect the type to be an ITemplateArray or supertype of ITemplateArray. */ void expectITemplateArraySupertype(NodeTraversal t, Node n, JSType type, String msg) { if (!getNativeType(I_TEMPLATE_ARRAY_TYPE).isSubtypeOf(type)) { mismatch(t, n, msg, type, I_TEMPLATE_ARRAY_TYPE); } }
/** * @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.isSubtypeOf(returnType); } }
boolean canCastToFunction(JSType thisType, FunctionType functionType) { if (thisType.isFunctionType()) { // TODO(johnlenz): visit function parts return true; } else { return thisType.isSubtypeOf(functionType) || functionType.isSubtype(thisType); } }
@Override public JSType getLeastSupertype(JSType that) { if (!that.isUnknownType() && !that.isUnionType()) { for (int i = 0; i < alternatesRetainingStructuralSubtypes.size(); i++) { JSType alternate = alternatesRetainingStructuralSubtypes.get(i); if (!alternate.isUnknownType() && that.isSubtypeOf(alternate)) { return this; } } } return JSType.getLeastSupertype(this, that); }
void expectStringOrNumberOrSymbolStrict(Node n, JSType type, String msg) { if (!type.isSubtypeOf(getNativeType(NUMBER_STRING_SYMBOL))) { registerMismatchAndReport( n, INVALID_OPERAND_TYPE, msg, type, getNativeType(NUMBER_STRING_SYMBOL), null, null); } }
void expectStringOrNumberStrict(Node n, JSType type, String msg) { if (!type.isSubtypeOf(getNativeType(NUMBER_STRING))) { registerMismatchAndReport( n, INVALID_OPERAND_TYPE, msg, type, getNativeType(NUMBER_STRING), null, null); } }
protected boolean isWhitelistedType(Node n) { if (whitelistedTypes != null && n.getJSType() != null) { JSType targetType = n.getJSType().restrictByNotNullOrUndefined(); if (targetType.isSubtypeOf(whitelistedTypes)) { return true; } } return false; }
/** * 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.isSubtypeOf(type) && !type.isEmptyType()) { mismatch(t, n, msg, type, anyObjectType); } } /**
@Override public TernaryValue testForEquality(JSType that) { TernaryValue result = super.testForEquality(that); if (result != null) { return result; } if (that.isUnknownType() || that.isSubtypeOf( getNativeType(JSTypeNative.OBJECT_NUMBER_STRING_BOOLEAN))) { return UNKNOWN; } return FALSE; }
/** * Expect the type to be a number or a subtype. */ void expectNumberStrict(Node n, JSType type, String msg) { if (!type.isSubtypeOf(getNativeType(NUMBER_TYPE))) { registerMismatchAndReport( n, INVALID_OPERAND_TYPE, msg, type, getNativeType(NUMBER_TYPE), null, null); } }
/** * 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.isSubtypeOf(allBitwisableValueTypes)) { mismatch(t, n, msg, type, allBitwisableValueTypes); } else { expectNumberStrict(n, type, msg); } }
@Override public TernaryValue testForEquality(JSType that) { TernaryValue result = super.testForEquality(that); if (result != null) { return result; } if (that.isUnknownType() || that.isSubtypeOf( getNativeType(JSTypeNative.OBJECT_NUMBER_STRING_BOOLEAN))) { return UNKNOWN; } return FALSE; }
/** * Only defines of literal number, string, or boolean are supported. */ private boolean isValidDefineType(JSTypeExpression expression) { JSTypeRegistry registry = compiler.getTypeRegistry(); JSType type = registry.evaluateTypeExpressionInGlobalScope(expression); return !type.isUnknownType() && type.isSubtypeOf(registry.getNativeType(NUMBER_STRING_BOOLEAN)); }