Search in sources :

Example 96 with JSType

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

the class TypedScopeCreatorTest method testDeclaredConstType1.

@Test
public void testDeclaredConstType1() {
    testSame("/** @const */ var x = 3;" + "function f() { var y = x; }");
    JSType yType = lastLocalScope.getVar("y").getType();
    assertThat(yType.toString()).isEqualTo("number");
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Test(org.junit.Test)

Example 97 with JSType

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

the class TypedScopeCreatorTest method testConstDeclarationObjectPatternInfersType_forAliasedTypedef.

@Test
public void testConstDeclarationObjectPatternInfersType_forAliasedTypedef() {
    testSame(lines("const ns = {};", "/** @typedef {string} */ ns.Foo;", "const {Foo} = ns;", "let /** !Foo */ f = 'bar';"));
    TypedVar fooVar = checkNotNull(globalScope.getVar("Foo"));
    assertType(fooVar.getType()).toStringIsEqualTo("None");
    JSType fooType = registry.getGlobalType("Foo");
    assertType(fooType).isEqualTo(getNativeType(JSTypeNative.STRING_TYPE));
    TypedVar fooInstanceVar = checkNotNull(globalScope.getVar("f"));
    assertType(fooInstanceVar.getType()).toStringIsEqualTo("string");
    assertThat(fooInstanceVar.isTypeInferred()).isFalse();
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Test(org.junit.Test)

Example 98 with JSType

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

the class TypedScopeCreatorTest method testDefaultParameterInferredNotUndefinedInCallback.

@Test
public void testDefaultParameterInferredNotUndefinedInCallback() {
    testSame(lines("function takesCallback(/** function(string=): ? */ cb) {}", "", "takesCallback((str = '') => {})", ""));
    TypedVar strVar = checkNotNull(lastFunctionScope.getVar("str"));
    assertType(strVar.getType()).isString();
    assertThat(strVar.isTypeInferred()).isTrue();
    JSType strType = findNameType("str", globalScope);
    // the actual parameter is also typed as not-undefined
    assertType(strType).isString();
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Test(org.junit.Test)

Example 99 with JSType

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

the class TypedScopeCreatorTest method testStubsInExterns3.

@Test
public void testStubsInExterns3() {
    testSame(externs("/** @type {number} */ myExtern.foo;" + "/** @type {Extern} */ var myExtern;" + "/** @constructor */ function Extern() {}"), srcs(""));
    JSType e = globalScope.getVar("myExtern").getType();
    assertThat(e.toString()).isEqualTo("(Extern|null)");
    ObjectType externType = (ObjectType) e.restrictByNotNullOrUndefined();
    assertWithMessage(globalScope.getRootNode().toStringTree()).that(externType.hasOwnProperty("foo")).isTrue();
    assertThat(externType.isPropertyTypeDeclared("foo")).isTrue();
    assertThat(externType.getPropertyType("foo").toString()).isEqualTo("number");
    assertThat(externType.isPropertyInExterns("foo")).isTrue();
}
Also used : ObjectType(com.google.javascript.rhino.jstype.ObjectType) JSType(com.google.javascript.rhino.jstype.JSType) Test(org.junit.Test)

Example 100 with JSType

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

the class TypedScopeCreatorTest method testTemplateType4a.

@Test
public void testTemplateType4a() {
    testSame("/**\n" + " * @param {function():T} x\n" + " * @return {T}\n" + " * @template T\n" + " */\n" + "function f(x) {\n" + "  return x;\n" + "}" + "/** @return {string} */\n" + "var g = function(){return 'hi'};\n" + "(function() {var result = f(g);})();");
    JSType resultType = findNameType("result", lastLocalScope);
    assertThat(resultType.toString()).isEqualTo("string");
}
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