2017-03-16 99 views
0

我正在嘗試編寫一個SonarQube JavaScript自定義規則。該規則應該看到一個for循環不是空的(即for塊體中有多於0個語句)。如何訪問SonarQube自定義規則中的後代節點?

我已經延長了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沒有深入下去如何遍歷比在樹上的一個節點多。

回答

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

而且順便說一句有類似的規則https://sonarqube.com/coding_rules#rule_key=javascript%3AEmptyBlock

+0

+1這是非常有益的。我還想添加一個鏈接到sonar-javascript源代碼作爲更具體的代碼示例的源代碼: https://github.com/SonarSource/sonar-javascript – gabrielfreiberg

相關問題