我正在尝试编写SonarQube JavaScript自定义规则。该规则应该看到for循环不为空(即for for block body中有超过0个语句)。
我扩展了 DoubleDispatchVisitorCheck 类,并重写了 visitForStatement 方法。在该方法中,我不确定如何确定for语句有多少后代。
@Override
public void visitForStatement(ForStatementTree tree) {
StatementTree statement = tree.statement();
// How to see tree or statement descendants?
super.visitForStatement(tree);
}
best documentation I've located没有详细介绍如何遍历树中的多个节点。
答案 0 :(得分:3)
if (statement.is(Kind.BLOCK) && ((BlockTree) statement).statements().isEmpty()) {
addIssue(tree, "message");
}
顺便说一下,有类似的规则https://sonarqube.com/coding_rules#rule_key=javascript%3AEmptyBlock