Search in sources :

Example 21 with FlowScope

use of com.google.javascript.jscomp.type.FlowScope in project closure-compiler by google.

the class SemanticReverseAbstractInterpreterTest method testBinop.

private void testBinop(FlowScope[] blind, Token binop, Node left, Node right, Collection<TypedName> trueOutcome, Collection<TypedName> falseOutcome) {
    Node condition = new Node(binop);
    condition.addChildToBack(left);
    condition.addChildToBack(right);
    // true outcome.
    FlowScope informedTrue = interpreter.getPreciserScopeKnowingConditionOutcome(condition, blind[0], Outcome.TRUE);
    for (TypedName p : trueOutcome) {
        assertTypeEquals(p.name, p.type, getVarType(informedTrue, p.name));
    }
    // false outcome.
    FlowScope informedFalse = interpreter.getPreciserScopeKnowingConditionOutcome(condition, blind[0], Outcome.FALSE);
    for (TypedName p : falseOutcome) {
        assertTypeEquals(p.type, getVarType(informedFalse, p.name));
    }
}
Also used : Node(com.google.javascript.rhino.Node) FlowScope(com.google.javascript.jscomp.type.FlowScope)

Example 22 with FlowScope

use of com.google.javascript.jscomp.type.FlowScope in project closure-compiler by google.

the class SemanticReverseAbstractInterpreterTest method testInequalitiesCondition1.

/**
 * Tests reverse interpretation of a COMPARE(NAME, NUMBER) expression, where COMPARE can be LE,
 * LT, GE or GT.
 */
@Test
public void testInequalitiesCondition1() {
    for (Token op : Arrays.asList(Token.LT, Token.GT, Token.LE, Token.GE)) {
        FlowScope[] blind = newScope();
        testBinop(blind, op, createVar(blind, "a", createUnionType(getNativeStringType(), getNativeVoidType())), createNumber(8), ImmutableSet.of(new TypedName("a", getNativeStringType())), ImmutableSet.of(new TypedName("a", createUnionType(getNativeStringType(), getNativeVoidType()))));
    }
}
Also used : Token(com.google.javascript.rhino.Token) FlowScope(com.google.javascript.jscomp.type.FlowScope) Test(org.junit.Test)

Example 23 with FlowScope

use of com.google.javascript.jscomp.type.FlowScope in project closure-compiler by google.

the class SemanticReverseAbstractInterpreterTest method testTypeof1.

@Test
public void testTypeof1() {
    FlowScope[] blind = newScope();
    testBinop(blind, Token.EQ, new Node(Token.TYPEOF, createVar(blind, "a", getNativeObjectType())), Node.newString("function"), ImmutableSet.of(new TypedName("a", getNativeFunctionType())), ImmutableSet.of(new TypedName("a", getNativeObjectType())));
}
Also used : Node(com.google.javascript.rhino.Node) FlowScope(com.google.javascript.jscomp.type.FlowScope) Test(org.junit.Test)

Example 24 with FlowScope

use of com.google.javascript.jscomp.type.FlowScope in project closure-compiler by google.

the class SemanticReverseAbstractInterpreterTest method testNameCondition.

/**
 * Tests reverse interpretation of a NAME expression.
 */
@Test
public void testNameCondition() {
    FlowScope[] blind = newScope();
    Node condition = createVar(blind, "a", createNullableType(getNativeStringType()));
    // true outcome.
    FlowScope informedTrue = interpreter.getPreciserScopeKnowingConditionOutcome(condition, blind[0], Outcome.TRUE);
    assertTypeEquals(getNativeStringType(), getVarType(informedTrue, "a"));
    // false outcome.
    FlowScope informedFalse = interpreter.getPreciserScopeKnowingConditionOutcome(condition, blind[0], Outcome.FALSE);
    assertTypeEquals(createNullableType(getNativeStringType()), getVarType(informedFalse, "a"));
}
Also used : Node(com.google.javascript.rhino.Node) FlowScope(com.google.javascript.jscomp.type.FlowScope) Test(org.junit.Test)

Example 25 with FlowScope

use of com.google.javascript.jscomp.type.FlowScope in project closure-compiler by google.

the class SemanticReverseAbstractInterpreterTest method testEqCondition3.

/**
 * Tests reverse interpretation of a EQ(NAME, NULL) expression.
 */
@Test
public void testEqCondition3() {
    FlowScope[] blind = newScope();
    // (number,undefined,null)
    JSType nullableOptionalNumber = createUnionType(getNativeNullType(), getNativeVoidType(), getNativeNumberType());
    // (null,undefined)
    JSType nullUndefined = createUnionType(getNativeVoidType(), getNativeNullType());
    testBinop(blind, Token.EQ, createVar(blind, "a", nullableOptionalNumber), createNull(), ImmutableSet.of(new TypedName("a", nullUndefined)), ImmutableSet.of(new TypedName("a", getNativeNumberType())));
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) FlowScope(com.google.javascript.jscomp.type.FlowScope) Test(org.junit.Test)

Aggregations

FlowScope (com.google.javascript.jscomp.type.FlowScope)60 Node (com.google.javascript.rhino.Node)20 Test (org.junit.Test)16 JSType (com.google.javascript.rhino.jstype.JSType)12 Token (com.google.javascript.rhino.Token)3 ImmutableMap (com.google.common.collect.ImmutableMap)1 CheckReturnValue (com.google.errorprone.annotations.CheckReturnValue)1 Branch (com.google.javascript.jscomp.ControlFlowGraph.Branch)1 AbstractScopedCallback (com.google.javascript.jscomp.NodeTraversal.AbstractScopedCallback)1 DiGraphEdge (com.google.javascript.jscomp.graph.DiGraph.DiGraphEdge)1 Module (com.google.javascript.jscomp.modules.Module)1 ClosureReverseAbstractInterpreter (com.google.javascript.jscomp.type.ClosureReverseAbstractInterpreter)1 ReverseAbstractInterpreter (com.google.javascript.jscomp.type.ReverseAbstractInterpreter)1 JSTypeResolver (com.google.javascript.rhino.jstype.JSTypeResolver)1 ObjectType (com.google.javascript.rhino.jstype.ObjectType)1 StaticTypedScope (com.google.javascript.rhino.jstype.StaticTypedScope)1 TemplateType (com.google.javascript.rhino.jstype.TemplateType)1 NodeSubject.assertNode (com.google.javascript.rhino.testing.NodeSubject.assertNode)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1