Search in sources :

Example 61 with JSType

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

the class InferJSDocInfoTest method testJSDocFromNamedFunctionPropagatesToDefinedType.

@Test
public void testJSDocFromNamedFunctionPropagatesToDefinedType() {
    // Given
    testSame(srcs(lines("/**", " * I'm a user type.", " * @constructor", " */", "function Foo() {};", "", // Just a hook to access type "Foo".
    "var x = new Foo();")));
    JSType xType = inferredTypeOfName("x");
    assertThat(xType.toString()).isEqualTo("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 62 with JSType

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

the class InferJSDocInfoTest method testJSDocFromVariableNameAssignmentPropagatesToDefinedType.

@Test
public void testJSDocFromVariableNameAssignmentPropagatesToDefinedType() {
    // Given
    testSame(srcs(lines("var /**", " * I'm a user type.", " * @constructor", " */", "Foo = function() {};", "", // Just a hook to access type "Foo".
    "var x = new Foo();")));
    JSType xType = inferredTypeOfName("x");
    assertThat(xType.toString()).isEqualTo("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 63 with JSType

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

the class InferJSDocInfoTest method testJSDocFromDuplicateDefinitionIsUsedIfThereWasNoOriginalJSDocFromOriginal.

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

Example 64 with JSType

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

the class InferJSDocInfoTest method testJSDocIsNotOverriddenByStructuralTypeAssignments_FunctionTypes.

@Test
public void testJSDocIsNotOverriddenByStructuralTypeAssignments_FunctionTypes() {
    // Given
    testSame(srcs(lines("/**", " * I'm test0.", " * @param {*} a", " * @return {string}", " */", "function test0(a) {};", "", // The type of these objects should be identical.
    "/**", " * I'm test1.", " * @param {string} a", " * @return {*}", // We need this so inferrence understands thes two objects/types are stuck together.
    " * @const", " */", "var test1 = test0;")));
    JSType test0Type = inferredTypeOfName("test0");
    JSType test1Type = inferredTypeOfName("test1");
    assertThat(test1Type).isSameInstanceAs(test0Type);
    // Then
    assertThat(test0Type.getJSDocInfo().getBlockDescription()).isEqualTo("I'm test0.");
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Test(org.junit.Test)

Example 65 with JSType

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

the class InferJSDocInfoTest method testJSDocFromExternTypesIsPreserved.

@Test
public void testJSDocFromExternTypesIsPreserved() {
    // Given
    testSame(externs(lines("/**", " * I'm an Object.", " * @param {*=} x", " * @return {!Object}", " * @constructor", " */", "function Object(x) {};")), srcs("var x = new Object();"));
    JSType xType = inferredTypeOfName("x");
    assertThat(xType.toString()).isEqualTo("Object");
    // Then
    assertThat(xType.getJSDocInfo().getBlockDescription()).isEqualTo("I'm an Object.");
}
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