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);
}
Aggregations