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