Search in sources :

Example 86 with JSType

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

the class TypeCheckTest method testExtendFunction1.

@Test
public void testExtendFunction1() {
    Node n = parseAndTypeCheck("/**@return {number}*/Function.prototype.f = " + "function() { return 1; };\n" + "(new Function()).f();");
    JSType type = n.getLastChild().getLastChild().getJSType();
    assertTypeEquals(getNativeNumberType(), type);
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Test(org.junit.Test)

Example 87 with JSType

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

the class TypeCheckTest method testAddingMethodsUsingPrototypeIdiomComplexNamespace.

private void testAddingMethodsUsingPrototypeIdiomComplexNamespace(TypeCheckResult p) {
    ObjectType goog = (ObjectType) p.scope.getVar("goog").getType();
    assertHasXMorePropertiesThanNativeObject(goog, 1);
    JSType googA = goog.getPropertyType("A");
    assertThat(googA).isNotNull();
    assertThat(googA).isInstanceOf(FunctionType.class);
    FunctionType googAFunction = (FunctionType) googA;
    ObjectType classA = googAFunction.getInstanceType();
    assertHasXMorePropertiesThanNativeObject(classA, 1);
    checkObjectType(classA, "m1", getNativeNumberType());
}
Also used : ObjectType(com.google.javascript.rhino.jstype.ObjectType) JSType(com.google.javascript.rhino.jstype.JSType) FunctionType(com.google.javascript.rhino.jstype.FunctionType)

Example 88 with JSType

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

the class TypeCheckTest method testExtendFunction2.

@Test
public void testExtendFunction2() {
    Node n = parseAndTypeCheck("/**@return {number}*/Function.prototype.f = " + "function() { return 1; };\n" + "(function() {}).f();");
    JSType type = n.getLastChild().getLastChild().getJSType();
    assertTypeEquals(getNativeNumberType(), type);
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Test(org.junit.Test)

Example 89 with JSType

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

the class TypeCheckTest method testDontAddMethodsIfNoConstructor.

@Test
public void testDontAddMethodsIfNoConstructor() {
    Node js1Node = parseAndTypeCheck("function A() {}" + "A.prototype = {m1: 5, m2: true}");
    JSType functionAType = js1Node.getFirstChild().getJSType();
    assertThat(functionAType.toString()).isEqualTo("function(): undefined");
    assertTypeEquals(getNativeUnknownType(), getNativeFunctionType().getPropertyType("m1"));
    assertTypeEquals(getNativeUnknownType(), getNativeFunctionType().getPropertyType("m2"));
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node) NodeSubject.assertNode(com.google.javascript.rhino.testing.NodeSubject.assertNode) Test(org.junit.Test)

Example 90 with JSType

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

the class TypedScopeCreatorTest method testInferredObjectLitProperty2.

@Test
public void testInferredObjectLitProperty2() {
    testSame("var x = {prop: function(){}};");
    TypedVar prop = globalScope.getVar("x.prop");
    JSType propType = prop.getType();
    assertThat(propType.toString()).isEqualTo("function(): undefined");
    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)

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