Search in sources :

Example 1 with ClosureReverseAbstractInterpreter

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

the class ClosureReverseAbstractInterpreterTest method testClosureFunction.

private void testClosureFunction(String function, JSType type, JSType trueType, JSType falseType) {
    // function(a) where a : type
    Node n = compiler.parseTestCode("var a; " + function + "(a)");
    Node call = n.getLastChild().getLastChild();
    Node name = call.getLastChild();
    TypedScope scope = (TypedScope) SyntacticScopeCreator.makeTyped(compiler).createScope(n, null);
    FlowScope flowScope = LinkedFlowScope.createEntryLattice(scope);
    assertEquals(Token.CALL, call.getToken());
    assertEquals(Token.NAME, name.getToken());
    flowScope.inferSlotType("a", type);
    ClosureReverseAbstractInterpreter rai = new ClosureReverseAbstractInterpreter(registry);
    // trueScope
    Asserts.assertTypeEquals(trueType, rai.getPreciserScopeKnowingConditionOutcome(call, flowScope, true).getSlot("a").getType());
    // falseScope
    JSType aType = rai.getPreciserScopeKnowingConditionOutcome(call, flowScope, false).getSlot("a").getType();
    if (falseType == null) {
        assertThat(aType).isNull();
    } else {
        Asserts.assertTypeEquals(falseType, aType);
    }
}
Also used : JSType(com.google.javascript.rhino.jstype.JSType) Node(com.google.javascript.rhino.Node) ClosureReverseAbstractInterpreter(com.google.javascript.jscomp.type.ClosureReverseAbstractInterpreter) FlowScope(com.google.javascript.jscomp.type.FlowScope)

Example 2 with ClosureReverseAbstractInterpreter

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

the class TypeCheckTest method testClosureTypesMultipleWarnings.

private void testClosureTypesMultipleWarnings(String js, List<String> descriptions) {
    compiler.initOptions(compiler.getOptions());
    Node n = compiler.parseTestCode(js);
    Node externs = IR.root();
    IR.root(externs, n);
    assertEquals("parsing error: " + Joiner.on(", ").join(compiler.getErrors()), 0, compiler.getErrorCount());
    // For processing goog.addDependency for forward typedefs.
    new ProcessClosurePrimitives(compiler, null, CheckLevel.ERROR, false).process(externs, n);
    new TypeCheck(compiler, new ClosureReverseAbstractInterpreter(registry).append(new SemanticReverseAbstractInterpreter(registry)).getFirst(), registry).processForTesting(null, n);
    assertEquals("unexpected error(s) : " + Joiner.on(", ").join(compiler.getErrors()), 0, compiler.getErrorCount());
    if (descriptions == null) {
        assertEquals("unexpected warning(s) : " + Joiner.on(", ").join(compiler.getWarnings()), 0, compiler.getWarningCount());
    } else {
        assertEquals("unexpected warning(s) : " + Joiner.on(", ").join(compiler.getWarnings()), descriptions.size(), compiler.getWarningCount());
        Set<String> actualWarningDescriptions = new HashSet<>();
        for (int i = 0; i < descriptions.size(); i++) {
            actualWarningDescriptions.add(compiler.getWarnings()[i].description);
        }
        assertEquals(new HashSet<>(descriptions), actualWarningDescriptions);
    }
}
Also used : SemanticReverseAbstractInterpreter(com.google.javascript.jscomp.type.SemanticReverseAbstractInterpreter) Node(com.google.javascript.rhino.Node) ClosureReverseAbstractInterpreter(com.google.javascript.jscomp.type.ClosureReverseAbstractInterpreter) HashSet(java.util.HashSet)

Example 3 with ClosureReverseAbstractInterpreter

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

the class TypeCheckNoTranspileTest method testClosureTypesMultipleWarnings.

private void testClosureTypesMultipleWarnings(String js, List<String> descriptions) {
    compiler.initOptions(compiler.getOptions());
    Node n = compiler.parseTestCode(js);
    Node externs = IR.root();
    IR.root(externs, n);
    assertEquals("parsing error: " + Joiner.on(", ").join(compiler.getErrors()), 0, compiler.getErrorCount());
    // For processing goog.addDependency for forward typedefs.
    new ProcessClosurePrimitives(compiler, null, CheckLevel.ERROR, false).process(externs, n);
    new TypeCheck(compiler, new ClosureReverseAbstractInterpreter(registry).append(new SemanticReverseAbstractInterpreter(registry)).getFirst(), registry).processForTesting(null, n);
    assertEquals("unexpected error(s) : " + Joiner.on(", ").join(compiler.getErrors()), 0, compiler.getErrorCount());
    if (descriptions == null) {
        assertEquals("unexpected warning(s) : " + Joiner.on(", ").join(compiler.getWarnings()), 0, compiler.getWarningCount());
    } else {
        assertEquals("unexpected warning(s) : " + Joiner.on(", ").join(compiler.getWarnings()), descriptions.size(), compiler.getWarningCount());
        Set<String> actualWarningDescriptions = new HashSet<>();
        for (int i = 0; i < descriptions.size(); i++) {
            actualWarningDescriptions.add(compiler.getWarnings()[i].description);
        }
        assertEquals(new HashSet<>(descriptions), actualWarningDescriptions);
    }
}
Also used : SemanticReverseAbstractInterpreter(com.google.javascript.jscomp.type.SemanticReverseAbstractInterpreter) Node(com.google.javascript.rhino.Node) ClosureReverseAbstractInterpreter(com.google.javascript.jscomp.type.ClosureReverseAbstractInterpreter) HashSet(java.util.HashSet)

Example 4 with ClosureReverseAbstractInterpreter

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

the class Compiler method getReverseAbstractInterpreter.

@Override
public ReverseAbstractInterpreter getReverseAbstractInterpreter() {
    if (abstractInterpreter == null) {
        ChainableReverseAbstractInterpreter interpreter = new SemanticReverseAbstractInterpreter(getTypeRegistry());
        if (options.closurePass) {
            interpreter = new ClosureReverseAbstractInterpreter(getTypeRegistry()).append(interpreter).getFirst();
        }
        abstractInterpreter = interpreter;
    }
    return abstractInterpreter;
}
Also used : ChainableReverseAbstractInterpreter(com.google.javascript.jscomp.type.ChainableReverseAbstractInterpreter) SemanticReverseAbstractInterpreter(com.google.javascript.jscomp.type.SemanticReverseAbstractInterpreter) ClosureReverseAbstractInterpreter(com.google.javascript.jscomp.type.ClosureReverseAbstractInterpreter)

Aggregations

ClosureReverseAbstractInterpreter (com.google.javascript.jscomp.type.ClosureReverseAbstractInterpreter)4 SemanticReverseAbstractInterpreter (com.google.javascript.jscomp.type.SemanticReverseAbstractInterpreter)3 Node (com.google.javascript.rhino.Node)3 HashSet (java.util.HashSet)2 ChainableReverseAbstractInterpreter (com.google.javascript.jscomp.type.ChainableReverseAbstractInterpreter)1 FlowScope (com.google.javascript.jscomp.type.FlowScope)1 JSType (com.google.javascript.rhino.jstype.JSType)1