Search in sources :

Example 91 with JSType

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

the class TypedScopeCreatorTest method testObjectPatternInParametersWithDefaultGetsCorrectType.

@Test
public void testObjectPatternInParametersWithDefaultGetsCorrectType() {
    testSame("/** @param {{a: string}=} obj */ function f({a = 'foo'} = {}) {}");
    JSType patternType = findTokenType(Token.OBJECT_PATTERN, globalScope);
    // the pattern has the type after the default value is evaluated, not ({a:string}|undefined)
    assertType(patternType).toStringIsEqualTo("({a: string}|{})");
    JSType aNameType = findNameType("a", globalScope);
    assertType(aNameType).isString();
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Test(org.junit.Test)

Example 92 with JSType

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

the class TypedScopeCreatorTest method testDeclaredConstType5a.

@Test
public void testDeclaredConstType5a() {
    testSame(externs(""), srcs("/** @const */ var goog = goog || {};" + "function f() { var y = goog; }"));
    JSType yType = lastLocalScope.getVar("y").getType();
    assertThat(yType.toString()).isEqualTo("{}");
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Test(org.junit.Test)

Example 93 with JSType

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

the class TypedScopeCreatorTest method testInferredObjectLitProperty1.

@Test
public void testInferredObjectLitProperty1() {
    testSame("var x = {prop: 3};");
    TypedVar prop = globalScope.getVar("x.prop");
    JSType propType = prop.getType();
    assertThat(propType.toString()).isEqualTo("number");
    assertThat(prop.isTypeInferred()).isTrue();
    assertThat(ObjectType.cast(globalScope.getVar("x").getType()).isPropertyTypeInferred("prop")).isTrue();
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Test(org.junit.Test)

Example 94 with JSType

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

the class TypedScopeCreatorTest method testPropertyInExterns2.

@Test
public void testPropertyInExterns2() {
    // Native extern types (such as Object) do not get stray properties declared, since this would
    // cause problems with bad externs (such as `/** @type {!Object} */ var api = {}; api.foo;`,
    // where we don't want to declare that all Objects have a "foo" property.  Nevertheless, the
    // specific qualified name (i.e. extern.one, in the example below) is still declared on the
    // global scope, so referring to the "one" property specifically on "extern" is still checked
    // as one would expect.
    testSame(externs(lines(// 
    "/** @type {Object} */ var extern;", "/** @return {number} */ extern.one;")), srcs(lines(// 
    "/** @type {Object} */ var normal;", "/** @return {number} */ normal.one;", "var result = extern.one();")));
    JSType e = globalScope.getVar("extern").getType();
    assertThat(e.dereference().hasOwnProperty("one")).isFalse();
    assertThat(globalScope.getVar("result").getType().toString()).isEqualTo("number");
    JSType normal = globalScope.getVar("normal").getType();
    assertThat(normal.dereference().hasOwnProperty("one")).isFalse();
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Test(org.junit.Test)

Example 95 with JSType

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

the class TypedScopeCreatorTest method testArrayDestructuringPatternParameterWithDefaultArray.

@Test
public void testArrayDestructuringPatternParameterWithDefaultArray() {
    testSame(lines(// 
    "/** @param {!Iterable<number>=} p */", "function f([x] = [0]) {}"));
    JSType xType = findNameType("x", lastFunctionScope);
    // This is unknown because we infer `[0]` to have type `!Array<?>`
    assertType(xType).isUnknown();
}
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