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