Search in sources :

Example 21 with JSType

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

the class LinkedFlowScopeTest method verifyLongChains.

// Common chain verification for testLongChainN for all N.
private void verifyLongChains(FlowScope chainA, FlowScope chainB) {
    FlowScope joined = join(chainA, chainB);
    for (int i = 0; i < LONG_CHAIN_LENGTH; i++) {
        assertTypeEquals(i % 2 == 0 ? NUMBER_TYPE : BOOLEAN_TYPE, chainA.getSlot("local" + i).getType());
        assertTypeEquals(i % 3 == 0 ? STRING_TYPE : BOOLEAN_TYPE, chainB.getSlot("local" + i).getType());
        JSType joinedSlotType = joined.getSlot("local" + i).getType();
        if (i % 6 == 0) {
            assertTypeEquals(createUnionType(STRING_TYPE, NUMBER_TYPE), joinedSlotType);
        } else if (i % 2 == 0) {
            assertTypeEquals(createUnionType(NUMBER_TYPE, BOOLEAN_TYPE), joinedSlotType);
        } else if (i % 3 == 0) {
            assertTypeEquals(createUnionType(STRING_TYPE, BOOLEAN_TYPE), joinedSlotType);
        } else {
            assertTypeEquals(BOOLEAN_TYPE, 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)

Example 22 with JSType

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

the class TypeCheckTest method testAddingMethodsUsingPrototypeIdiomComplexNamespace.

private void testAddingMethodsUsingPrototypeIdiomComplexNamespace(TypeCheckResult p) {
    ObjectType goog = (ObjectType) p.scope.getVar("goog").getType();
    assertEquals(NATIVE_PROPERTIES_COUNT + 2, goog.getPropertiesCount());
    JSType googA = goog.getPropertyType("A");
    assertNotNull(googA);
    assertThat(googA).isInstanceOf(FunctionType.class);
    FunctionType googAFunction = (FunctionType) googA;
    ObjectType classA = googAFunction.getInstanceType();
    assertEquals(NATIVE_PROPERTIES_COUNT + 2, classA.getPropertiesCount());
    checkObjectType(classA, "m1", NUMBER_TYPE);
}
Also used : ObjectType(com.google.javascript.rhino.jstype.ObjectType) JSType(com.google.javascript.rhino.jstype.JSType) FunctionType(com.google.javascript.rhino.jstype.FunctionType)

Example 23 with JSType

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

the class TypeCheckTest method testTemplatizedTypeSubtypes2.

public void testTemplatizedTypeSubtypes2() {
    JSType arrayOfNumber = createTemplatizedType(ARRAY_TYPE, NUMBER_TYPE);
    JSType arrayOfString = createTemplatizedType(ARRAY_TYPE, STRING_TYPE);
    assertFalse(arrayOfString.isSubtypeOf(createUnionType(arrayOfNumber, NULL_VOID)));
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType)

Example 24 with JSType

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

the class TypeCheckTest method testExtendFunction1.

public void testExtendFunction1() {
    Node n = parseAndTypeCheck("/**@return {number}*/Function.prototype.f = " + "function() { return 1; };\n" + "(new Function()).f();");
    JSType type = n.getLastChild().getLastChild().getJSType();
    assertTypeEquals(NUMBER_TYPE, type);
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node)

Example 25 with JSType

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

the class TypeCheckTest method testDontAddMethodsIfNoConstructor.

public void testDontAddMethodsIfNoConstructor() {
    Node js1Node = parseAndTypeCheck("function A() {}" + "A.prototype = {m1: 5, m2: true}");
    JSType functionAType = js1Node.getFirstChild().getJSType();
    assertEquals("function(): undefined", functionAType.toString());
    assertTypeEquals(UNKNOWN_TYPE, U2U_FUNCTION_TYPE.getPropertyType("m1"));
    assertTypeEquals(UNKNOWN_TYPE, U2U_FUNCTION_TYPE.getPropertyType("m2"));
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node)

Aggregations

JSType (com.google.javascript.rhino.jstype.JSType)189 Node (com.google.javascript.rhino.Node)60 FunctionType (com.google.javascript.rhino.jstype.FunctionType)38 ObjectType (com.google.javascript.rhino.jstype.ObjectType)37 JSDocInfo (com.google.javascript.rhino.JSDocInfo)9 FlowScope (com.google.javascript.jscomp.type.FlowScope)7 TemplateType (com.google.javascript.rhino.jstype.TemplateType)6 TemplateTypeMap (com.google.javascript.rhino.jstype.TemplateTypeMap)5 UnionType (com.google.javascript.rhino.jstype.UnionType)4 ArrayList (java.util.ArrayList)4 EnumType (com.google.javascript.rhino.jstype.EnumType)3 JSTypeRegistry (com.google.javascript.rhino.jstype.JSTypeRegistry)3 TemplateTypeMapReplacer (com.google.javascript.rhino.jstype.TemplateTypeMapReplacer)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Branch (com.google.javascript.jscomp.ControlFlowGraph.Branch)2 Scope (com.google.javascript.jscomp.Scope)2 Token (com.google.javascript.rhino.Token)2 FunctionBuilder (com.google.javascript.rhino.jstype.FunctionBuilder)2 TemplatizedType (com.google.javascript.rhino.jstype.TemplatizedType)2 HashMap (java.util.HashMap)2