Tested for isBlockScopedFunctionDeclaration(Node n) in NodeUtil and added parseSecond(Token token, String js)
@@ -126,6 +126,10 @@ public final class NodeUtilTest {
checkState(rootNode.isScript(), rootNode);
return token.equals(SCRIPT) ? rootNode : getNode(rootNode, token);
}
+
+ private Node parseSecond(Token token, String js) {
+ return getNode(parseFirst(token, js), token);
+ }
}
private static Node parse(String js) {
@@ -142,6 +146,16 @@ public final class NodeUtilTest {
return new ParseHelper().parseFirst(token, js);
}
+ /**
+ * Parses {@code js} into an AST and then returns the second Node in pre-traversal order with the
+ * given token.
+ *
+ * <p>TODO(nickreid): Consider an overload that takes a `Predicate` rather than a `Token`.
+ */
+ private static Node parseSecond(Token token, String js) {
+ return new ParseHelper().parseSecond(token, js);
+ }
+
/** Returns the parsed expression (e.g. returns a NAME given 'a') */
private static Node parseExpr(String js) {
Node script = parse("(" + js + ");"); // Parens force interpretation as an expression.
@@ -712,6 +726,29 @@ public final class NodeUtilTest {
.isFalse();
}
+ @Test
+ public void testisBlockScopedFunctionDeclaration() {
+ assertThat(
+ NodeUtil.isBlockScopedFunctionDeclaration(parseFirst(FUNCTION, "function foo(){}")))
+ .isFalse();
+ assertThat(
+ NodeUtil.isBlockScopedFunctionDeclaration(
+ parseFirst(FUNCTION, "var x = function(){}")))
+ .isFalse();
+ assertThat(
+ NodeUtil.isBlockScopedFunctionDeclaration(
+ parseFirst(FUNCTION, "{ function foo(){} }")))
+ .isTrue();
+ assertThat(
+ NodeUtil.isBlockScopedFunctionDeclaration(
+ parseFirst(FUNCTION, "class C { static { function foo(){} } }")))
+ .isTrue();
+ assertThat(
+ NodeUtil.isBlockScopedFunctionDeclaration(
+ parseSecond(FUNCTION, "function foo(){ class C { static { function a(){} } } }")))
+ .isTrue();
+ }
+
@Test
public void testIsMethodDeclaration() {
assertThat(NodeUtil.isMethodDeclaration(parseFirst(FUNCTION, "class C { constructor() {} }")))
