In my quest to develop a specialized rule for the Sonar javascript plugin, I am focusing on verifying whether an init() function is invoked within a set of JS source files. To kick things off, I subscribe to call expressions:
public void init() {
subscribeTo(EcmaScriptGrammar.CALL_EXPRESSION);
}
My next step involves confirming the invocation of the init() function by modifying the visitNode method:
public void visitNode(AstNode node){
String functionCall=new String();
List<Token> tokens = node.getTokens();
for(int i=0; i<tokens.size(); i++){
functionCall+=tokens.get(i).getValue();
}
if(functionCall.equals("init()"))
callMade=true;
}
To wrap things up, as I exit the file, I flag a violation if init() hasn't been called:
public void leaveFile(AstNode node){
if(!callMade)
getContext().createLineViolation(this,"No call to init()",node);
}
While this approach works effectively, it generates violations for every JS source file that lacks an init() call. My goal is to trigger a violation only if init() is not invoked in any of the JS source files. How can I make this happen?