0

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.

Koby Douek
  • 16,156
  • 19
  • 74
  • 103

1 Answers1

3
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

Elena Vilchik
  • 1,085
  • 6
  • 11
  • +1 this was very helpful. I'd like to also add a link to the sonar-javascript source as a source of more concrete code examples: https://github.com/SonarSource/sonar-javascript – gabrielfreiberg Mar 16 '17 at 15:45