Skip to content

Commit

Permalink
support suppress messages in multi-line comments before functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ryaneberly committed Oct 21, 2016
1 parent e2f2036 commit 2f0fd4b
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 11 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@
<dependency>
<groupId>com.github.cfparser</groupId>
<artifactId>cfparser</artifactId>
<version>2.2.8</version>
<version>2.2.9</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.github.cfparser</groupId>
<artifactId>cfml.parsing</artifactId>
<version>2.2.8</version>
<version>2.2.9</version>
</dependency>
<dependency>
<groupId>com.github.cfparser</groupId>
<artifactId>cfml.dictionary</artifactId>
<version>2.2.8</version>
<version>2.2.9</version>
</dependency>
<dependency>
<groupId>junit</groupId>
Expand Down
26 changes: 21 additions & 5 deletions src/main/java/com/cflint/CFLint.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Token> 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()) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<Token> tokens = context.afterTokens(token);
for (Token currentTok : tokens) {
if (currentTok.getLine() != token.getLine()) {
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/cflint/plugins/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -24,6 +25,7 @@ public class Context {
final CommonTokenStream tokens;
final List<ContextMessage> messages = new ArrayList<ContextMessage>();
Context parent=null;
List<String> ignores = new ArrayList<String>();

public Context(final String filename, final Element element, final CFIdentifier functionName,
final boolean inAssignmentExpression, final StackHandler handler) {
Expand Down Expand Up @@ -250,4 +252,13 @@ public void remove(){
public Context getParent() {
return parent;
}

public void ignore(List<String> ignores) {
this.ignores.addAll(ignores);
}

public boolean isSuppressed(BugInfo bugInfo){
return ignores.contains(bugInfo.getMessageCode())
|| (parent != null && parent.isSuppressed(bugInfo));
}
}
10 changes: 10 additions & 0 deletions src/test/resources/com/cflint/tests/Ignores/ignoreMultiLine.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
component {

/*
Test!
*/
public void function function1() {
someVar = '';
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[ ]
11 changes: 11 additions & 0 deletions src/test/resources/com/cflint/tests/Ignores/ignoreMultiLine2.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
component {

/*
Test!
@CFLintIgnore SOMETHINGELSE,MISSING_VAR,ANOTHERTHINGTOIGNORE
*/
public void function function1() {
someVar = '';
}

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

0 comments on commit 2f0fd4b

Please sign in to comment.