Make the comment rules use rulechain
This commit is contained in:
1 parent
fc9c3abbb8
commit
0c19203b62
2 files changed
+53
-50
No files matched your search
+25
-25
@@ -14,7 +14,7 @@ import java.util.regex.Pattern;
|
||||
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
|
||||
import net.sourceforge.pmd.lang.java.ast.Comment;
|
||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
|
||||
import net.sourceforge.pmd.properties.PropertyDescriptor;
|
||||
import net.sourceforge.pmd.util.document.Chars;
|
||||
|
||||
@@ -25,12 +25,12 @@ import net.sourceforge.pmd.util.document.Chars;
|
||||
*
|
||||
* @author Brian Remedios
|
||||
*/
|
||||
public class CommentContentRule extends AbstractJavaRule {
|
||||
public class CommentContentRule extends AbstractJavaRulechainRule {
|
||||
|
||||
private static final Pattern WHITESPACE = Pattern.compile("\\s+");
|
||||
|
||||
// ignored when property above == True
|
||||
public static final PropertyDescriptor<Boolean> CASE_SENSITIVE_DESCRIPTOR = booleanProperty("caseSensitive").defaultValue(false).desc("Case sensitive").build();
|
||||
public static final PropertyDescriptor<Boolean> CASE_SENSITIVE_DESCRIPTOR =
|
||||
booleanProperty("caseSensitive").defaultValue(false).desc("Whether the words are case sensitive").build();
|
||||
|
||||
public static final PropertyDescriptor<List<String>> DISSALLOWED_TERMS_DESCRIPTOR =
|
||||
stringListProperty("disallowedTerms")
|
||||
@@ -38,10 +38,30 @@ public class CommentContentRule extends AbstractJavaRule {
|
||||
.defaultValues("idiot", "jerk").build(); // TODO make blank property? or add more defaults?
|
||||
|
||||
public CommentContentRule() {
|
||||
super(ASTCompilationUnit.class);
|
||||
definePropertyDescriptor(CASE_SENSITIVE_DESCRIPTOR);
|
||||
definePropertyDescriptor(DISSALLOWED_TERMS_DESCRIPTOR);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object visit(ASTCompilationUnit cUnit, Object data) {
|
||||
|
||||
List<String> currentBadWords = getProperty(DISSALLOWED_TERMS_DESCRIPTOR);
|
||||
boolean caseSensitive = getProperty(CASE_SENSITIVE_DESCRIPTOR);
|
||||
|
||||
for (Comment comment : cUnit.getComments()) {
|
||||
List<String> badWords = illegalTermsIn(comment, currentBadWords, caseSensitive);
|
||||
if (badWords.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
addViolationWithMessage(data, cUnit, errorMsgFor(badWords), comment.getBeginLine(), comment.getEndLine());
|
||||
}
|
||||
|
||||
return super.visit(cUnit, data);
|
||||
}
|
||||
|
||||
private List<String> illegalTermsIn(Comment comment, List<String> badWords, boolean caseSensitive) {
|
||||
|
||||
if (badWords.isEmpty()) {
|
||||
@@ -79,28 +99,8 @@ public class CommentContentRule extends AbstractJavaRule {
|
||||
return msg.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ASTCompilationUnit cUnit, Object data) {
|
||||
|
||||
// NPE patch: Eclipse plugin doesn't call start() at onset?
|
||||
List<String> currentBadWords = getProperty(DISSALLOWED_TERMS_DESCRIPTOR);
|
||||
boolean caseSensitive = getProperty(CASE_SENSITIVE_DESCRIPTOR);
|
||||
|
||||
for (Comment comment : cUnit.getComments()) {
|
||||
List<String> badWords = illegalTermsIn(comment, currentBadWords, caseSensitive);
|
||||
if (badWords.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
addViolationWithMessage(data, cUnit, errorMsgFor(badWords), comment.getBeginLine(), comment.getEndLine());
|
||||
}
|
||||
|
||||
return super.visit(cUnit, data);
|
||||
}
|
||||
|
||||
private boolean hasDisallowedTerms() {
|
||||
List<String> terms = getProperty(DISSALLOWED_TERMS_DESCRIPTOR);
|
||||
return !terms.isEmpty();
|
||||
return !getProperty(DISSALLOWED_TERMS_DESCRIPTOR).isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+28
-25
@@ -15,7 +15,7 @@ import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit;
|
||||
import net.sourceforge.pmd.lang.java.ast.Comment;
|
||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRulechainRule;
|
||||
import net.sourceforge.pmd.properties.PropertyDescriptor;
|
||||
import net.sourceforge.pmd.properties.PropertyFactory;
|
||||
import net.sourceforge.pmd.util.document.Chars;
|
||||
@@ -25,7 +25,7 @@ import net.sourceforge.pmd.util.document.Chars;
|
||||
*
|
||||
* @author Brian Remedios
|
||||
*/
|
||||
public class CommentSizeRule extends AbstractJavaRule {
|
||||
public class CommentSizeRule extends AbstractJavaRulechainRule {
|
||||
|
||||
public static final PropertyDescriptor<Integer> MAX_LINES
|
||||
= PropertyFactory.intProperty("maxLines")
|
||||
@@ -44,10 +44,36 @@ public class CommentSizeRule extends AbstractJavaRule {
|
||||
Chars.wrap("*/"));
|
||||
|
||||
public CommentSizeRule() {
|
||||
super(ASTCompilationUnit.class);
|
||||
definePropertyDescriptor(MAX_LINES);
|
||||
definePropertyDescriptor(MAX_LINE_LENGTH);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Object visit(ASTCompilationUnit cUnit, Object data) {
|
||||
|
||||
for (Comment comment : cUnit.getComments()) {
|
||||
if (hasTooManyLines(comment)) {
|
||||
addViolationWithMessage(data, cUnit, this.getMessage() + ": Too many lines", comment.getBeginLine(),
|
||||
comment.getEndLine());
|
||||
}
|
||||
|
||||
List<Integer> lineNumbers = overLengthLineIndicesIn(comment);
|
||||
if (lineNumbers.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int offset = comment.getBeginLine();
|
||||
for (int lineNum : lineNumbers) {
|
||||
lineNum += offset;
|
||||
addViolationWithMessage(data, cUnit, this.getMessage() + ": Line too long", lineNum, lineNum);
|
||||
}
|
||||
}
|
||||
|
||||
return super.visit(cUnit, data);
|
||||
}
|
||||
|
||||
private static boolean hasRealText(Chars line) {
|
||||
|
||||
if (StringUtils.isBlank(line)) {
|
||||
@@ -92,27 +118,4 @@ public class CommentSizeRule extends AbstractJavaRule {
|
||||
return indices;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object visit(ASTCompilationUnit cUnit, Object data) {
|
||||
|
||||
for (Comment comment : cUnit.getComments()) {
|
||||
if (hasTooManyLines(comment)) {
|
||||
addViolationWithMessage(data, cUnit, this.getMessage() + ": Too many lines", comment.getBeginLine(),
|
||||
comment.getEndLine());
|
||||
}
|
||||
|
||||
List<Integer> lineNumbers = overLengthLineIndicesIn(comment);
|
||||
if (lineNumbers.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
int offset = comment.getBeginLine();
|
||||
for (int lineNum : lineNumbers) {
|
||||
lineNum += offset;
|
||||
addViolationWithMessage(data, cUnit, this.getMessage() + ": Line too long", lineNum, lineNum);
|
||||
}
|
||||
}
|
||||
|
||||
return super.visit(cUnit, data);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user