Search in sources :

Example 1 with EnumElementType

use of com.google.javascript.rhino.jstype.EnumElementType in project closure-compiler by google.

the class TypeInferenceTest method testBigIntEnumWithUnaryPlus.

@Test
public void testBigIntEnumWithUnaryPlus() {
    EnumElementType enumElementBigIntType = createEnumType("MyEnum", BIGINT_TYPE).getElementsType();
    EnumElementType enumElementUnionType = createEnumType("MyEnum", BIGINT_NUMBER).getElementsType();
    assuming("x", enumElementBigIntType);
    assuming("y", registry.createUnionType(enumElementBigIntType, getNativeType(NUMBER_TYPE)));
    assuming("z", enumElementUnionType);
    inFunction("enumElementBigIntType = +x; unionEnumType = +y; enumElementUnionType = +z;");
    // Unary plus throws an exception when applied to a BigInt, so there is no valid type for its
    // result.
    verify("enumElementBigIntType", NO_TYPE);
    verify("unionEnumType", NO_TYPE);
    verify("enumElementUnionType", NO_TYPE);
}
Also used : EnumElementType(com.google.javascript.rhino.jstype.EnumElementType) Test(org.junit.Test)

Example 2 with EnumElementType

use of com.google.javascript.rhino.jstype.EnumElementType in project closure-compiler by google.

the class TypeInference method getBigIntPresence.

/**
 * Utility function for determining the BigIntPresence for any type.
 */
static BigIntPresence getBigIntPresence(JSType type) {
    // Base case
    if (type.isOnlyBigInt()) {
        return BigIntPresence.ALL_BIGINT;
    }
    // Checking enum case
    EnumElementType typeAsEnumElement = type.toMaybeEnumElementType();
    if (typeAsEnumElement != null) {
        // No matter what type the enum element is, this function can resolve it to a BigIntPresence
        return getBigIntPresence(typeAsEnumElement.getPrimitiveType());
    }
    // Union case
    UnionType typeAsUnion = type.toMaybeUnionType();
    if (typeAsUnion != null) {
        boolean containsBigInt = false;
        boolean containsNumber = false;
        boolean containsOther = false;
        for (JSType alternate : typeAsUnion.getAlternates()) {
            if (getBigIntPresence(alternate) != BigIntPresence.NO_BIGINT) {
                containsBigInt = true;
            } else if (alternate.isNumber() && !alternate.isUnknownType()) {
                containsNumber = true;
            } else {
                containsOther = true;
            }
        }
        if (containsBigInt) {
            if (containsOther) {
                return BigIntPresence.BIGINT_OR_OTHER;
            } else if (containsNumber) {
                return BigIntPresence.BIGINT_OR_NUMBER;
            } else {
                return BigIntPresence.ALL_BIGINT;
            }
        }
    }
    // safely assume that it’s not a bigint in anyway
    return BigIntPresence.NO_BIGINT;
}
Also used : UnionType(com.google.javascript.rhino.jstype.UnionType) JSType(com.google.javascript.rhino.jstype.JSType) EnumElementType(com.google.javascript.rhino.jstype.EnumElementType)

Aggregations

EnumElementType (com.google.javascript.rhino.jstype.EnumElementType)2 JSType (com.google.javascript.rhino.jstype.JSType)1 UnionType (com.google.javascript.rhino.jstype.UnionType)1 Test (org.junit.Test)1