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