diff --git a/build.gradle b/build.gradle index 971b6463d..db90082b8 100644 --- a/build.gradle +++ b/build.gradle @@ -35,9 +35,9 @@ repositories { maven { url "http://cfmlprojects.org/artifacts" } } dependencies { - compile group: 'com.github.cfparser', name: 'cfparser', version:'2.2.8' - compile group: 'com.github.cfparser', name: 'cfml.parsing', version:'2.2.8' - compile group: 'com.github.cfparser', name: 'cfml.dictionary', version:'2.2.8' + compile group: 'com.github.cfparser', name: 'cfparser', version:'2.2.9' + compile group: 'com.github.cfparser', name: 'cfml.parsing', version:'2.2.9' + compile group: 'com.github.cfparser', name: 'cfml.dictionary', version:'2.2.9' compile group: 'junit', name: 'junit', version:'4.12' compile group: 'org.jdom', name: 'jdom', version:'1.1.3' compile group: 'org.antlr', name: 'antlr4-runtime', version:'4.5.2-1' diff --git a/pom.xml b/pom.xml index d7a631852..52588ebeb 100644 --- a/pom.xml +++ b/pom.xml @@ -65,18 +65,18 @@ com.github.cfparser cfparser - 2.2.8 + 2.2.9 pom com.github.cfparser cfml.parsing - 2.2.8 + 2.2.9 com.github.cfparser cfml.dictionary - 2.2.8 + 2.2.9 junit diff --git a/src/main/java/com/cflint/CFLint.java b/src/main/java/com/cflint/CFLint.java index 5c9f3ae3a..a14e03444 100644 --- a/src/main/java/com/cflint/CFLint.java +++ b/src/main/java/com/cflint/CFLint.java @@ -8,6 +8,7 @@ import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; +import java.util.Iterator; import java.util.List; import java.util.ResourceBundle; import java.util.Stack; @@ -501,6 +502,22 @@ private void process(final CFScriptStatement expression, Context context) { final CFFuncDeclStatement function = (CFFuncDeclStatement) expression; functionName = function.getName().getName(); context.setFunctionName(functionName); + Iterable tokens = context.beforeTokens(function.getToken()); + for(Token currentTok: tokens){ + if(currentTok.getChannel() == Token.HIDDEN_CHANNEL + && currentTok.getType() == CFSCRIPTLexer.ML_COMMENT){ + String mlText = currentTok.getText(); + Pattern pattern = Pattern.compile(".*\\s*@CFLintIgnore\\s+([\\w,_]+)\\s*.*", Pattern.DOTALL); + Matcher matcher = pattern.matcher(mlText); + if(matcher.matches() ){ + String ignoreCodes = matcher.group(1); + context.ignore(Arrays.asList(ignoreCodes.split(",\\s*"))); + }else + System.out.println("no match"); + }else if(currentTok.getLine() < function.getToken().getLine()){ + break; + } + } inFunction = true; handler.push("function"); for (final CFFunctionParameter param : function.getFormals()) { @@ -629,11 +646,8 @@ private String exceptionText(final Exception e) { private void process(final CFExpression expression, final Element elem, Context oldcontext) { - final String filename = oldcontext.getFilename(); - final String functionName = oldcontext.getFunctionName(); - final Context context = new Context(filename, elem, functionName, inAssignment, handler, oldcontext.getTokens()); - context.setInComponent(inComponent); - + final Context context = oldcontext.subContext(elem); + for (final CFLintScanner plugin : extensions) { try { plugin.expression(expression, context, bugs); @@ -839,6 +853,8 @@ protected void reportRule(final Element elem, final Object expression, final Con * cflint:MESSAGE_CODE - suppresses any message matching that code */ protected boolean suppressed(BugInfo bugInfo, Token token, Context context) { + if(context != null && context.isSuppressed(bugInfo)) + return true; Iterable tokens = context.afterTokens(token); for (Token currentTok : tokens) { if (currentTok.getLine() != token.getLine()) { diff --git a/src/main/java/com/cflint/plugins/Context.java b/src/main/java/com/cflint/plugins/Context.java index 1c436b7ce..d1e4633f2 100644 --- a/src/main/java/com/cflint/plugins/Context.java +++ b/src/main/java/com/cflint/plugins/Context.java @@ -7,6 +7,7 @@ import org.antlr.v4.runtime.CommonTokenStream; import org.antlr.v4.runtime.Token; +import com.cflint.BugInfo; import com.cflint.StackHandler; import cfml.parsing.cfscript.CFIdentifier; @@ -24,6 +25,7 @@ public class Context { final CommonTokenStream tokens; final List messages = new ArrayList(); Context parent=null; + List ignores = new ArrayList(); public Context(final String filename, final Element element, final CFIdentifier functionName, final boolean inAssignmentExpression, final StackHandler handler) { @@ -250,4 +252,13 @@ public void remove(){ public Context getParent() { return parent; } + + public void ignore(List ignores) { + this.ignores.addAll(ignores); + } + + public boolean isSuppressed(BugInfo bugInfo){ + return ignores.contains(bugInfo.getMessageCode()) + || (parent != null && parent.isSuppressed(bugInfo)); + } } diff --git a/src/test/resources/com/cflint/tests/Ignores/ignoreMultiLine.cfc b/src/test/resources/com/cflint/tests/Ignores/ignoreMultiLine.cfc new file mode 100644 index 000000000..67393ab58 --- /dev/null +++ b/src/test/resources/com/cflint/tests/Ignores/ignoreMultiLine.cfc @@ -0,0 +1,10 @@ +component { + + /* + Test! + */ + public void function function1() { + someVar = ''; + } + +} \ No newline at end of file diff --git a/src/test/resources/com/cflint/tests/Ignores/ignoreMultiLine.expected.txt b/src/test/resources/com/cflint/tests/Ignores/ignoreMultiLine.expected.txt new file mode 100644 index 000000000..8878e547a --- /dev/null +++ b/src/test/resources/com/cflint/tests/Ignores/ignoreMultiLine.expected.txt @@ -0,0 +1 @@ +[ ] \ No newline at end of file diff --git a/src/test/resources/com/cflint/tests/Ignores/ignoreMultiLine2.cfc b/src/test/resources/com/cflint/tests/Ignores/ignoreMultiLine2.cfc new file mode 100644 index 000000000..e3cf11658 --- /dev/null +++ b/src/test/resources/com/cflint/tests/Ignores/ignoreMultiLine2.cfc @@ -0,0 +1,11 @@ +component { + + /* + Test! + @CFLintIgnore SOMETHINGELSE,MISSING_VAR,ANOTHERTHINGTOIGNORE + */ + public void function function1() { + someVar = ''; + } + +} \ No newline at end of file diff --git a/src/test/resources/com/cflint/tests/Ignores/ignoreMultiLine2.expected.txt b/src/test/resources/com/cflint/tests/Ignores/ignoreMultiLine2.expected.txt new file mode 100644 index 000000000..8878e547a --- /dev/null +++ b/src/test/resources/com/cflint/tests/Ignores/ignoreMultiLine2.expected.txt @@ -0,0 +1 @@ +[ ] \ No newline at end of file