Search in sources :

Example 81 with JSType

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

the class TypeInferenceTest method testObjectSpread_isInferredToBeObject.

@Test
public void testObjectSpread_isInferredToBeObject() {
    // Given
    JSType recordType = registry.createRecordType(ImmutableMap.of("x", getNativeType(STRING_TYPE), "y", getNativeType(NUMBER_TYPE)));
    assuming("obj", recordType);
    assuming("before", BOOLEAN_TYPE);
    assuming("after", NULL_TYPE);
    // When
    inFunction(lines("let spread = {before, ...obj, after};"));
    // Then
    // TODO(b/128355893): Do smarter inferrence. There are a lot of potential issues with
    // inference on object-rest, so for now we just give up and say `Object`. In theory we could
    // infer something like `{after: null, before: boolean, x: string, y: number}`.
    verify("spread", OBJECT_TYPE);
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Test(org.junit.Test)

Example 82 with JSType

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

the class TypeInferenceTest method testEnumRAI2.

@Test
public void testEnumRAI2() {
    JSType enumType = createEnumType("MyEnum", NUMBER_TYPE).getElementsType();
    assuming("x", enumType);
    inFunction("var y = null; if (typeof x == 'number') y = x;");
    verify("y", createNullableType(enumType));
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Test(org.junit.Test)

Example 83 with JSType

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

the class TypeInferenceTest method testAssertFunction_narrowsAllTypeToFunction.

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

Example 84 with JSType

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

the class TypeInferenceTest method testAssertInstanceof_stringCtor_narrowsObjOrVoidToString.

@Test
public void testAssertInstanceof_stringCtor_narrowsObjOrVoidToString() {
    JSType startType = createUnionType(OBJECT_TYPE, VOID_TYPE);
    includeAssertInstanceof();
    assuming("x", startType);
    inFunction("out1 = x; goog.asserts.assertInstanceof(x, String); var r = x;");
    verify("out1", startType);
    verify("x", STRING_OBJECT_TYPE);
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Test(org.junit.Test)

Example 85 with JSType

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

the class TypeCheckTest method testBug911118a.

/**
 * Tests that assigning two untyped functions to a variable whose type is inferred and calling
 * this variable is legal.
 */
@Test
public void testBug911118a() {
    // verifying the type assigned to function expressions assigned variables
    TypedScope s = parseAndTypeCheckWithScope("var a = function(){};").scope;
    JSType type = s.getVar("a").getType();
    assertThat(type.toString()).isEqualTo("function(): undefined");
}
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