Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test with empty rule set and test for ignore:line fix #742

Merged
merged 2 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/gradle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Set up JDK 8
- name: Set up JDK 11
uses: actions/setup-java@v4
with:
distribution: temurin
java-version: 8
java-version: 11

- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
Expand Down
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" : [ ]
}
}