Search in sources :

Example 41 with JSType

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

the class PartialCompilationTest method testUnresolvedBaseClassDoesNotHideFields.

public void testUnresolvedBaseClassDoesNotHideFields() throws Exception {
    assertPartialCompilationSucceeds("/** @constructor @extends {MissingBase} */", "var Klass = function () {", "  /** @type {string} */", "  this.foo;", "};");
    TypedVar x = compiler.getTopScope().getSlot("Klass");
    JSType type = x.getType();
    assertThat(type.isFunctionType()).isTrue();
    FunctionType fType = (FunctionType) type;
    assertThat(fType.getTypeOfThis().hasProperty("foo")).isTrue();
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) FunctionType(com.google.javascript.rhino.jstype.FunctionType)

Example 42 with JSType

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

the class TypeInferenceTest method testAssertObject5.

public void testAssertObject5() {
    JSType startType = createNullableType(ALL_TYPE);
    assuming("x", startType);
    inFunction("out1 = x;" + "out2 = /** @type {!Array} */ (goog.asserts.assertObject(x));");
    verify("out1", startType);
    verify("out2", ARRAY_TYPE);
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType)

Example 43 with JSType

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

the class TypeMatchingStrategyTest method assertMatch.

private static void assertMatch(TypeMatchingStrategy typeMatchingStrategy, String templateType, String type, boolean isMatch, boolean isLooseMatch) {
    // It's important that the test uses the same compiler to compile the template type and the
    // type to be matched. Otherwise, equal types won't be considered equal.
    Compiler compiler = new Compiler();
    compiler.disableThreads();
    CompilerOptions options = new CompilerOptions();
    options.setLanguageIn(LanguageMode.ECMASCRIPT3);
    options.setCheckTypes(true);
    compiler.compile(ImmutableList.of(SourceFile.fromCode("externs", EXTERNS)), ImmutableList.of(SourceFile.fromCode("test", String.format("/** @type {%s} */ var x; /** @type {%s} */ var y;", templateType, type))), options);
    Node script = compiler.getRoot().getLastChild().getFirstChild();
    Node xNode = script.getFirstChild();
    Node yNode = script.getLastChild();
    JSType templateJsType = xNode.getFirstChild().getJSType();
    JSType jsType = yNode.getFirstChild().getJSType();
    MatchResult matchResult = typeMatchingStrategy.match(templateJsType, jsType);
    assertEquals(isMatch ? "'" + templateJsType + "' should match '" + jsType + "'" : "'" + templateType + "' should not match '" + type + "'", isMatch, matchResult.isMatch());
    assertEquals(isLooseMatch ? "'" + templateType + "' should loosely match '" + type + "'" : "'" + templateType + "' should not loosely match '" + type + "'", isLooseMatch, matchResult.isLooseMatch());
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node) MatchResult(com.google.javascript.jscomp.TypeMatchingStrategy.MatchResult)

Example 44 with JSType

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

the class TypeInferenceTest method testAssertNumber3.

public void testAssertNumber3() {
    // Make sure it ignores expressions.
    JSType startType = createNullableType(ALL_TYPE);
    assuming("x", startType);
    inFunction("out1 = x; out2 = goog.asserts.assertNumber(x + x);");
    verify("out1", startType);
    verify("out2", NUMBER_TYPE);
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType)

Example 45 with JSType

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

the class TypeInferenceTest method testAssert5.

public void testAssert5() {
    JSType startType = createNullableType(OBJECT_TYPE);
    assuming("x", startType);
    assuming("y", startType);
    inFunction("goog.asserts.assert(x || y); out1 = x; out2 = y;");
    verify("out1", startType);
    verify("out2", startType);
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType)

Aggregations

JSType (com.google.javascript.rhino.jstype.JSType)189 Node (com.google.javascript.rhino.Node)60 FunctionType (com.google.javascript.rhino.jstype.FunctionType)38 ObjectType (com.google.javascript.rhino.jstype.ObjectType)37 JSDocInfo (com.google.javascript.rhino.JSDocInfo)9 FlowScope (com.google.javascript.jscomp.type.FlowScope)7 TemplateType (com.google.javascript.rhino.jstype.TemplateType)6 TemplateTypeMap (com.google.javascript.rhino.jstype.TemplateTypeMap)5 UnionType (com.google.javascript.rhino.jstype.UnionType)4 ArrayList (java.util.ArrayList)4 EnumType (com.google.javascript.rhino.jstype.EnumType)3 JSTypeRegistry (com.google.javascript.rhino.jstype.JSTypeRegistry)3 TemplateTypeMapReplacer (com.google.javascript.rhino.jstype.TemplateTypeMapReplacer)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 Branch (com.google.javascript.jscomp.ControlFlowGraph.Branch)2 Scope (com.google.javascript.jscomp.Scope)2 Token (com.google.javascript.rhino.Token)2 FunctionBuilder (com.google.javascript.rhino.jstype.FunctionBuilder)2 TemplatizedType (com.google.javascript.rhino.jstype.TemplatizedType)2 HashMap (java.util.HashMap)2