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);
}
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));
}
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);
}
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);
}
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");
}
Aggregations