@Test public void test_children_when_one_child() { AstNodeType type1 = mock(AstNodeType.class); AstNodeType type2 = mock(AstNodeType.class); AstNode child = mock(AstNode.class); when(node.getNumberOfChildren()).thenReturn(1); when(node.getFirstChild()).thenReturn(child); AstSelect children = select.children(); assertThat((Object) children).isInstanceOf(SingleAstSelect.class); assertThat(children).containsOnly(child); when(node.getChildren()).thenReturn(ImmutableList.of(child)); children = select.children(type1); assertThat((Object) children).isSameAs(AstSelectFactory.empty()); when(child.getType()).thenReturn(type1); children = select.children(type1); assertThat((Object) children).isInstanceOf(SingleAstSelect.class); assertThat(children).containsOnly(child); children = select.children(type1, type2); assertThat((Object) children).isSameAs(AstSelectFactory.empty()); when(child.is(type1, type2)).thenReturn(true); children = select.children(type1, type2); assertThat((Object) children).isInstanceOf(SingleAstSelect.class); assertThat(children).containsOnly(child); }
/** This test demonstrates how to use {@link ParserAdapter} to parse and construct AST. */ @Test public void ast() { ParserAdapter<LexerlessGrammar> parser = new ParserAdapter<LexerlessGrammar>(Charsets.UTF_8, b.build()); AstNode rootNode = parser.parse("2 + var"); assertThat(rootNode.getType()).isSameAs(ExpressionGrammar.EXPRESSION); AstNode astNode = rootNode; assertThat(astNode.getNumberOfChildren()).isEqualTo(1); assertThat(astNode.getChild(0).getType()).isSameAs(ExpressionGrammar.ADDITIVE_EXPRESSION); astNode = rootNode.getChild(0); assertThat(astNode.getNumberOfChildren()).isEqualTo(3); assertThat(astNode.getChild(0).getType()).isSameAs(ExpressionGrammar.NUMBER); assertThat(astNode.getChild(1).getType()).isSameAs(ExpressionGrammar.PLUS); assertThat(astNode.getChild(2).getType()).isSameAs(ExpressionGrammar.VARIABLE); }
private static AstNode getSingleBlockStatement(AstNode node) { AstNode methodBody = node.getFirstChild(JavaGrammar.METHOD_BODY); if (methodBody == null) { return null; } AstNode blockStatements = methodBody.getFirstChild(JavaGrammar.BLOCK).getFirstChild(JavaGrammar.BLOCK_STATEMENTS); return blockStatements.getNumberOfChildren() == 1 ? blockStatements.getFirstChild() : null; }
private void checkAssignment(AstNode astNode) { AstNode memberExpr = astNode.getFirstChild(); AstNode variable = memberExpr.getFirstChild(); if (memberExpr.getNumberOfChildren() == 1 && variable.is(PHPGrammar.VARIABLE_WITHOUT_OBJECTS)) { String varName = variable.getTokenOriginalValue(); if (memberExpr.getTokens().size() == 1 && !CheckUtils.isSuperGlobal(varName) && !globalVariableNames.contains(varName)) { getContext().createLineViolation(this, "Move this variable into a class.", astNode); globalVariableNames.add(varName); } } }
private void checkFunction(AstNode astNode) { AstNode identifier = astNode.getFirstChild(getContext().getGrammar().identifier); if (identifier != null && isEvalOrArguments(identifier.getTokenValue())) { getContext() .createLineViolation( this, createMessageFor("function", identifier.getTokenValue()), identifier); } AstNode formalParameterList = astNode.getFirstChild(getContext().getGrammar().formalParameterList); if (formalParameterList != null) { for (int i = 0; i < formalParameterList.getNumberOfChildren(); i += 2) { identifier = formalParameterList.getChild(i); if (isEvalOrArguments(identifier.getTokenValue())) { getContext() .createLineViolation( this, createMessageFor("parameter", identifier.getTokenValue()), identifier); } } } }
@Override public boolean hasToBeSkippedFromAst(AstNode node) { return node.getNumberOfChildren() == 1; }