javascriptsonarqubesonarqube5.6

How can I visit descendant nodes in a SonarQube custom rule?


I am attempting to write a SonarQube JavaScript custom rule. The rule should see that a for loop is not empty (i.e. has more than 0 statements in the for block body).

I've extended the DoubleDispatchVisitorCheck class, and overridden the visitForStatement method. Within that method I'm unsure of how to determine how many descendants the for statement has.

  @Override
  public void visitForStatement(ForStatementTree tree) {
      StatementTree statement = tree.statement();
      // How to see tree or statement descendants?
      super.visitForStatement(tree);
  }

The best documentation I've located does not go in depth on how to traverse more than a single node in the tree.


Solution

  • if (statement.is(Kind.BLOCK) && ((BlockTree) statement).statements().isEmpty()) {
      addIssue(tree, "message");
    }
    

    And btw there is similar rule https://sonarqube.com/coding_rules#rule_key=javascript%3AEmptyBlock