Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added support for 
//cflint ignore:line

and 
//cflint ignore:MISSING_VAR
  • Loading branch information
ryaneberly committed Oct 15, 2016
1 parent a2a13b6 commit 8d256ff
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/main/java/com/cflint/CFLint.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,12 @@
import com.cflint.plugins.exceptions.DefaultCFLintExceptionListener;
import com.cflint.tools.CFLintFilter;

import cfml.CFSCRIPTLexer;
import cfml.CFSCRIPTParser;
import cfml.parsing.CFMLParser;
import cfml.parsing.CFMLSource;
import cfml.parsing.ParserTag;
import cfml.parsing.cfml.antlr.CFLexer;
import cfml.parsing.cfscript.CFAssignmentExpression;
import cfml.parsing.cfscript.CFBinaryExpression;
import cfml.parsing.cfscript.CFExpression;
Expand Down Expand Up @@ -805,7 +807,10 @@ protected void reportRule(final Element elem, final Object expression, final Con
bldr.setRuleParameters(ruleInfo.getParameters());
if (expression instanceof CFExpression) {
BugInfo bugInfo = bldr.build((CFExpression) expression, elem);
bugs.add(bugInfo);
final Token token = ((CFExpression) expression).getToken();
if(!suppressed(bugInfo,token,context)){
bugs.add(bugInfo);
}
} else {
final BugInfo bug = bldr.build((CFParsedStatement) expression, elem);
if (msg.getLine() != null) {
Expand All @@ -816,6 +821,40 @@ protected void reportRule(final Element elem, final Object expression, final Con
}
}

/*
* Look for a suppress comment on the same line.
* cflint:line - suppresses any messages on the same line
* cflint:MESSAGE_CODE - suppresses any message matching that code
*/
protected boolean suppressed(BugInfo bugInfo, Token token, Context context) {
Iterable<Token> tokens = context.afterTokens(token);
for (Token currentTok : tokens) {
if (currentTok.getLine() != token.getLine()) {
break;
}
if (currentTok.getChannel() == Token.HIDDEN_CHANNEL
&& currentTok.getType() == CFSCRIPTLexer.LINE_COMMENT) {
final String commentText = currentTok.getText().replaceFirst("^//\\s*", "").trim();
if(commentText.startsWith("cflint ")){
Pattern pattern = Pattern.compile("cflint\\s+ignore:([\\w,]+).*");
Matcher matcher = pattern.matcher(commentText);
if(matcher.matches() && matcher.groupCount() > 0){
final String ignoreCodes = matcher.group(1);
if(ignoreCodes.equalsIgnoreCase("line")){
return true;
}
for(final String ignoreCode : ignoreCodes.split(",\\s*")){
if(ignoreCode.equals(bugInfo.getMessageCode())){
return true;
}
}
}
}
}
}
return false;
}

public BugList getBugs() {
return bugs;
}
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/com/cflint/tests/Ignores/.cflintrc.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<config><includes code="MISSING_VAR"/></config>
7 changes: 7 additions & 0 deletions src/test/resources/com/cflint/tests/Ignores/ignoreLine1.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
component {

public void function function1() {
someVar = ''; // cflint ignore:line
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
component {

public void function function1() {
someVar = ''; // cflint ignore:OTHERCODE,MISSING_VAR,OTHERCODE2
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ ]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
component {

public void function function1() {
someVar = ''; // cflint ignore:OTHERCODE,OTHERCODE2
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[ {
"severity" : "ERROR",
"id" : "MISSING_VAR",
"message" : "MISSING_VAR",
"category" : "CFLINT",
"abbrev" : "MV",
"locations" : [ {
"file" : "src\\test\\resources\\com\\cflint\\tests\\Ignores\\ignoreLineMessageUnmatched.cfc",
"fileName" : "ignoreLineMessageUnmatched.cfc",
"function" : "function1",
"column" : "6",
"line" : "4",
"message" : "Variable someVar is not declared with a var statement.",
"variable" : "someVar",
"expression" : "someVar"
} ]
} ]

0 comments on commit 8d256ff

Please sign in to comment.