Skip to content

Commit

Permalink
Fix CommentContentRule
Browse files Browse the repository at this point in the history
oowekyala committed Mar 11, 2023
1 parent f2f8357 commit 2584356
Showing 1 changed file with 15 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -6,10 +6,10 @@

import static net.sourceforge.pmd.properties.PropertyFactory.regexProperty;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import net.sourceforge.pmd.RuleContext;
import net.sourceforge.pmd.lang.ast.Node;
import net.sourceforge.pmd.lang.document.Chars;
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
import net.sourceforge.pmd.lang.java.ast.JavaComment;
@@ -35,43 +35,32 @@ public CommentContentRule() {


@Override
public Object visit(ASTCompilationUnit cUnit, Object data) {
public Object visit(ASTCompilationUnit node, Object data) {

Pattern pattern = getProperty(DISSALLOWED_TERMS_DESCRIPTOR);

for (JavaComment comment : cUnit.getComments()) {
List<Integer> lineNumbers = illegalTermsIn(comment, pattern);
if (lineNumbers.isEmpty()) {
continue;
}

int offset = comment.getBeginLine();
for (int lineNum : lineNumbers) {
int lineNumWithOff = lineNum + offset;
addViolationWithMessage(
data,
cUnit,
"Line matches forbidden content regex (" + pattern.pattern() + ")",
lineNumWithOff,
lineNumWithOff
);
}
for (JavaComment comment : node.getComments()) {
reportIllegalTerms(asCtx(data), comment, pattern, node);
}

return null;
}

private List<Integer> illegalTermsIn(JavaComment comment, Pattern violationRegex) {
private void reportIllegalTerms(RuleContext ctx, JavaComment comment, Pattern violationRegex, Node acu) {

List<Integer> lines = new ArrayList<>();
int i = 0;
int i = comment.getReportLocation().getStartLine();
for (Chars line : comment.getFilteredLines(true)) {
if (violationRegex.matcher(line).find()) {
lines.add(i);
ctx.addViolationWithPosition(
acu,
i,
i,
"Line matches forbidden content regex ({0})",
violationRegex.pattern()
);
}
i++;
}

return lines;
}

}

0 comments on commit 2584356

Please sign in to comment.