Skip to content

Commit

Permalink
add test with empty rule set and test for ignore:line fix
Browse files Browse the repository at this point in the history
this add the current source and a check for an ignore line where a bug is found
eventually we should maybe add the option of outputting the whole line in question so this is a start toward that
closes cflint#741
  • Loading branch information
denuno committed Aug 12, 2024
1 parent ddd5f2d commit 2b4775e
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main/java/com/cflint/CFLint.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public class CFLint implements IErrorReporter {
private static final String MISSING_SEMI = "MISSING_SEMI";
private static final String AVOID_EMPTY_FILES = "AVOID_EMPTY_FILES";
private static final String RESOURCE_BUNDLE_NAME = "com.cflint.cflint";
private static final Pattern IGNORE_LINE_PATTERN = Pattern.compile("(?i).*cflint\\s+ignore:\\s*line.*");

private CFMLTagInfo tagInfo;
private CFMLParser cfmlParser = new CFMLParser();
Expand All @@ -130,11 +131,14 @@ public class CFLint implements IErrorReporter {
private Element currentElement = null;
private boolean strictInclude;
private Set<List<Object>> processed = new HashSet<>();
private String currentSource = "";


// Stack to store include file depth to ensure no recursion
private final Stack<File> includeFileStack = new Stack<>();
private int[] lineOffsets;


public CFLint(final CFLintConfiguration configFile) throws IOException {
final CFLintFilter filter = CFLintFilter.createFilter(verbose);
bugs = new BugList(filter);
Expand Down Expand Up @@ -325,6 +329,7 @@ public void process(final String src, final String filename) throws CFLintScanEx
final Context context = new Context(filename, null, null, false, handler,configuration);
reportRule(null, null, context, null, new ContextMessage(AVOID_EMPTY_FILES, null));
} else {
currentSource = src;
lineOffsets = getLineOffsets(src.split("\n"));
final CFMLSource cfmlSource = new CFMLSource(src.contains("<!---") ? CommentReformatting.wrap(src) : src);
final ParserTag firstTag = getFirstTagQuietly(cfmlSource);
Expand Down Expand Up @@ -1430,6 +1435,14 @@ protected boolean suppressed(final BugInfo bugInfo, final Token token, final Con
if (context == null || context.isSuppressed(bugInfo)) {
return true;
}

if(hasIgnoreLineComment(bugInfo.getLine())) {
if (verbose) {
System.out.println("ignoring " + bugInfo.getMessage() + " at line " + bugInfo.getLine());
}
return true;
}

if(token == null){
return false;
}
Expand Down Expand Up @@ -1578,6 +1591,11 @@ public void setProgressUsesThread(final boolean progressUsesThread) {
this.progressUsesThread = progressUsesThread;
}

public boolean hasIgnoreLineComment(final int lineNr) {
String line = currentSource.split("\n")[lineNr-1];
return IGNORE_LINE_PATTERN.matcher(line).matches();
}

@Override
public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, int line,
int charPositionInLine, final String msg, final org.antlr.v4.runtime.RecognitionException re) {
Expand Down
37 changes: 37 additions & 0 deletions src/test/java/com/cflint/TestIgnoreLine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.cflint;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

import com.cflint.api.CFLintAPI;
import com.cflint.api.CFLintResult;
import com.cflint.config.ConfigBuilder;
import com.cflint.exception.CFLintScanException;

public class TestIgnoreLine {

private CFLintAPI cfBugs;

@Before
public void setUp() throws Exception {
final ConfigBuilder configBuilder = new ConfigBuilder();
cfBugs = new CFLintAPI(configBuilder.build());
}

@Test
public void testIgnoreLines() throws CFLintScanException {
final String scriptSrc = "component { // cflint ignore:line\n" +
" function functionFour() { // cflint ignore:line\n" +
" if (a == 1) { // cflint ignore:line\n" +
" b = // cflint ignore:line\n" +
" }\n" +
" }\n" +
"}";
cfBugs.setLogError(true);
CFLintResult lintresult = cfBugs.scan(scriptSrc, "test");
assertEquals(0, lintresult.getIssues().size());
}

}
6 changes: 6 additions & 0 deletions src/test/resources/com/cflint/tests/IgnoresAll/.cflintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"rule" : [ ],
"excludes" : [ ],
"includes" : [{}],
"inheritParent" : false
}
7 changes: 7 additions & 0 deletions src/test/resources/com/cflint/tests/IgnoresAll/ignoreAll.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
component {
function functionFour() {
if (a == 1) {
b = 1;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version" : "",
"timestamp" : 0,
"issues" : [ ],
"counts" : {
"totalFiles" : 0,
"totalLines" : 0,
"countByCode" : [ ],
"countBySeverity" : [ ]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
component { // cflint ignore:line
function functionFour() { // cflint ignore:line
if (a == 1) { // cflint ignore:line
b = 1; // cflint ignore:line
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"version" : "",
"timestamp" : 0,
"issues" : [ ],
"counts" : {
"totalFiles" : 0,
"totalLines" : 0,
"countByCode" : [ ],
"countBySeverity" : [ ]
}
}

0 comments on commit 2b4775e

Please sign in to comment.