Search in sources :

Example 1 with FunctionlessLocalScope

use of com.google.javascript.jscomp.NodeIterators.FunctionlessLocalScope in project closure-compiler by google.

the class NodeIteratorsTest method testVarMotionWithCode.

/**
 * @see #testVarMotionWithCode(String, Token ...)
 */
private void testVarMotionWithCode(String code, List<Token> expectedTokens) {
    List<Node> ancestors = new ArrayList<>();
    // Add an empty node to the beginning of the code and start there.
    Node root = (new Compiler()).parseTestCode(";" + code);
    for (Node n = root; n != null; n = n.getFirstChild()) {
        ancestors.add(0, n);
    }
    FunctionlessLocalScope searchIt = new FunctionlessLocalScope(ancestors.toArray(new Node[0]));
    boolean found = false;
    while (searchIt.hasNext()) {
        Node n = searchIt.next();
        if (n.isName() && NodeUtil.isNameDeclaration(searchIt.currentParent()) && n.getString().equals("X")) {
            found = true;
            break;
        }
    }
    assertTrue("Variable X not found! " + root.toStringTree(), found);
    List<Node> currentAncestors = searchIt.currentAncestors();
    assert (currentAncestors.size() >= 3);
    Iterator<Node> moveIt = LocalVarMotion.forVar(currentAncestors.get(0), currentAncestors.get(1), currentAncestors.get(2));
    List<Token> actualTokens = new ArrayList<>();
    while (moveIt.hasNext()) {
        actualTokens.add(moveIt.next().getToken());
    }
    assertEquals(expectedTokens, actualTokens);
}
Also used : Node(com.google.javascript.rhino.Node) ArrayList(java.util.ArrayList) Token(com.google.javascript.rhino.Token) FunctionlessLocalScope(com.google.javascript.jscomp.NodeIterators.FunctionlessLocalScope)

Aggregations

FunctionlessLocalScope (com.google.javascript.jscomp.NodeIterators.FunctionlessLocalScope)1 Node (com.google.javascript.rhino.Node)1 Token (com.google.javascript.rhino.Token)1 ArrayList (java.util.ArrayList)1