コード例 #1
0
  @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);
  }
コード例 #2
0
  /** 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);
  }
コード例 #3
0
  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;
  }
コード例 #4
0
 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);
     }
   }
 }
コード例 #5
0
 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);
       }
     }
   }
 }
コード例 #6
0
 @Override
 public boolean hasToBeSkippedFromAst(AstNode node) {
   return node.getNumberOfChildren() == 1;
 }