diff --git a/src/main/java/com/cflint/CFLint.java b/src/main/java/com/cflint/CFLint.java index 44d71189b..a19fb61f5 100644 --- a/src/main/java/com/cflint/CFLint.java +++ b/src/main/java/com/cflint/CFLint.java @@ -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; @@ -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) { @@ -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 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; } diff --git a/src/test/resources/com/cflint/tests/Ignores/.cflintrc.xml b/src/test/resources/com/cflint/tests/Ignores/.cflintrc.xml new file mode 100644 index 000000000..ffa7c6122 --- /dev/null +++ b/src/test/resources/com/cflint/tests/Ignores/.cflintrc.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/src/test/resources/com/cflint/tests/Ignores/ignoreLine1.cfc b/src/test/resources/com/cflint/tests/Ignores/ignoreLine1.cfc new file mode 100644 index 000000000..0ed5f59b4 --- /dev/null +++ b/src/test/resources/com/cflint/tests/Ignores/ignoreLine1.cfc @@ -0,0 +1,7 @@ +component { + + public void function function1() { + someVar = ''; // cflint ignore:line + } + +} \ No newline at end of file diff --git a/src/test/resources/com/cflint/tests/Ignores/ignoreLine1.expected.txt b/src/test/resources/com/cflint/tests/Ignores/ignoreLine1.expected.txt new file mode 100644 index 000000000..8878e547a --- /dev/null +++ b/src/test/resources/com/cflint/tests/Ignores/ignoreLine1.expected.txt @@ -0,0 +1 @@ +[ ] \ No newline at end of file diff --git a/src/test/resources/com/cflint/tests/Ignores/ignoreLineMessage1.cfc b/src/test/resources/com/cflint/tests/Ignores/ignoreLineMessage1.cfc new file mode 100644 index 000000000..3c629db25 --- /dev/null +++ b/src/test/resources/com/cflint/tests/Ignores/ignoreLineMessage1.cfc @@ -0,0 +1,7 @@ +component { + + public void function function1() { + someVar = ''; // cflint ignore:OTHERCODE,MISSING_VAR,OTHERCODE2 + } + +} \ No newline at end of file diff --git a/src/test/resources/com/cflint/tests/Ignores/ignoreLineMessage1.expected.txt b/src/test/resources/com/cflint/tests/Ignores/ignoreLineMessage1.expected.txt new file mode 100644 index 000000000..8878e547a --- /dev/null +++ b/src/test/resources/com/cflint/tests/Ignores/ignoreLineMessage1.expected.txt @@ -0,0 +1 @@ +[ ] \ No newline at end of file diff --git a/src/test/resources/com/cflint/tests/Ignores/ignoreLineMessageUnmatched.cfc b/src/test/resources/com/cflint/tests/Ignores/ignoreLineMessageUnmatched.cfc new file mode 100644 index 000000000..b9c9c6744 --- /dev/null +++ b/src/test/resources/com/cflint/tests/Ignores/ignoreLineMessageUnmatched.cfc @@ -0,0 +1,7 @@ +component { + + public void function function1() { + someVar = ''; // cflint ignore:OTHERCODE,OTHERCODE2 + } + +} \ No newline at end of file diff --git a/src/test/resources/com/cflint/tests/Ignores/ignoreLineMessageUnmatched.expected.txt b/src/test/resources/com/cflint/tests/Ignores/ignoreLineMessageUnmatched.expected.txt new file mode 100644 index 000000000..6bfc2bcea --- /dev/null +++ b/src/test/resources/com/cflint/tests/Ignores/ignoreLineMessageUnmatched.expected.txt @@ -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" + } ] +} ] \ No newline at end of file