Search in sources :

Example 66 with JSType

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

the class InferJSDocInfoTest method testJSDocFromNamespacedNameAssignmentPropagatesToDefinedType.

@Test
public void testJSDocFromNamespacedNameAssignmentPropagatesToDefinedType() {
    // Given
    testSame(srcs(lines("var namespace = {}", "", "/**", " * I'm a user type.", " * @constructor", " */", "namespace.Foo = function() {};", "", // Just a hook to access type "Foo".
    "var x = new namespace.Foo();")));
    JSType xType = inferredTypeOfName("x");
    assertThat(xType.toString()).isEqualTo("namespace.Foo");
    // Then
    assertThat(xType.getJSDocInfo().getBlockDescription()).isEqualTo("I'm a user type.");
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Test(org.junit.Test)

Example 67 with JSType

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

the class LinkedFlowScopeTest method testLongChain.

/**
 * Create a long chain of flow scopes.
 */
@Test
public void testLongChain() {
    FlowScope chainA = localEntry;
    FlowScope chainB = localEntry;
    for (int i = 0; i < LONG_CHAIN_LENGTH; i++) {
        localScope.declare("local" + i, null, null, null, true);
        chainA = chainA.inferSlotType("local" + i, i % 2 == 0 ? getNativeNumberType() : getNativeBooleanType());
        chainB = chainB.inferSlotType("local" + i, i % 3 == 0 ? getNativeStringType() : getNativeBooleanType());
    }
    FlowScope joined = join(chainA, chainB);
    for (int i = 0; i < LONG_CHAIN_LENGTH; i++) {
        assertTypeEquals(i % 2 == 0 ? getNativeNumberType() : getNativeBooleanType(), chainA.getSlot("local" + i).getType());
        assertTypeEquals(i % 3 == 0 ? getNativeStringType() : getNativeBooleanType(), chainB.getSlot("local" + i).getType());
        JSType joinedSlotType = joined.getSlot("local" + i).getType();
        if (i % 6 == 0) {
            assertTypeEquals(createUnionType(getNativeStringType(), getNativeNumberType()), joinedSlotType);
        } else if (i % 2 == 0) {
            assertTypeEquals(createUnionType(getNativeNumberType(), getNativeBooleanType()), joinedSlotType);
        } else if (i % 3 == 0) {
            assertTypeEquals(createUnionType(getNativeStringType(), getNativeBooleanType()), joinedSlotType);
        } else {
            assertTypeEquals(getNativeBooleanType(), joinedSlotType);
        }
    }
    assertScopesDiffer(chainA, chainB);
    assertScopesDiffer(chainA, joined);
    assertScopesDiffer(chainB, joined);
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) FlowScope(com.google.javascript.jscomp.type.FlowScope) Test(org.junit.Test)

Example 68 with JSType

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

the class TypeInferenceTest method testOptChainGetProp_multipleChains_nullableReceiver.

// a?.b(x?.y.z).c with nullable object
@Test
public void testOptChainGetProp_multipleChains_nullableReceiver() {
    JSType lhsType = createNullableType(createRecordType("b", registry.createFunctionType(registry.getNativeType(NUMBER_TYPE))));
    assuming("a", lhsType);
    inFunction("let res = a?.b(x?.y.z);");
    verify("res", registry.createUnionType(VOID_TYPE, NUMBER_TYPE));
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Test(org.junit.Test)

Example 69 with JSType

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

the class TypeInferenceTest method testAssertObject_narrowsNullableArrayToArray.

@Test
public void testAssertObject_narrowsNullableArrayToArray() {
    JSType startType = createNullableType(ARRAY_TYPE);
    includeGoogAssertionFn("assertObject", getNativeType(OBJECT_TYPE));
    assuming("x", startType);
    inFunction("out1 = x; goog.asserts.assertObject(x); out2 = x;");
    verify("out1", startType);
    verify("out2", ARRAY_TYPE);
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Test(org.junit.Test)

Example 70 with JSType

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

the class TypeInferenceTest method testOptChainGetProp_functionProp.

// Tests existing FUNCTION_TYPE property access on non-null object gives FUNCTION_TYPE.
// Note - Although this test looks similar to the test above, this test is required to pass to
// make sure the OptChain_CALL tests pass.
@Test
public void testOptChainGetProp_functionProp() {
    JSType funcType = registry.createFunctionType(registry.getNativeType(NUMBER_TYPE));
    JSType lhsType = createRecordType("y", funcType);
    assuming("x", lhsType);
    inFunction("let a = x?.y;");
    verify("x", lhsType);
    // not the function `y`'s return type
    verify("a", funcType);
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Test(org.junit.Test)

Aggregations

JSType (com.google.javascript.rhino.jstype.JSType)447 Test (org.junit.Test)182 Node (com.google.javascript.rhino.Node)158 FunctionType (com.google.javascript.rhino.jstype.FunctionType)71 ObjectType (com.google.javascript.rhino.jstype.ObjectType)71 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)30 JSDocInfo (com.google.javascript.rhino.JSDocInfo)19 TemplateType (com.google.javascript.rhino.jstype.TemplateType)14 FlowScope (com.google.javascript.jscomp.type.FlowScope)13 CheckReturnValue (com.google.errorprone.annotations.CheckReturnValue)11 JSTypeExpression (com.google.javascript.rhino.JSTypeExpression)9 UnionType (com.google.javascript.rhino.jstype.UnionType)9 JSTypeRegistry (com.google.javascript.rhino.jstype.JSTypeRegistry)8 TemplateTypeMap (com.google.javascript.rhino.jstype.TemplateTypeMap)8 LinkedHashMap (java.util.LinkedHashMap)8 ImmutableList (com.google.common.collect.ImmutableList)7 Color (com.google.javascript.jscomp.colors.Color)7 StaticTypedSlot (com.google.javascript.rhino.jstype.StaticTypedSlot)7 ArrayList (java.util.ArrayList)7 ImmutableMap (com.google.common.collect.ImmutableMap)6