-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf017f6
commit b6bc4ae
Showing
6 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
magik-checks/src/main/java/nl/ramsolutions/sw/magik/checks/checks/TodoCommentCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package nl.ramsolutions.sw.magik.checks.checks; | ||
|
||
import com.sonar.sslr.api.Token; | ||
import com.sonar.sslr.api.Trivia; | ||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.stream.Collectors; | ||
import nl.ramsolutions.sw.magik.checks.MagikCheck; | ||
import org.sonar.check.Rule; | ||
import org.sonar.check.RuleProperty; | ||
|
||
/** | ||
* Todo/Fixme/... comment check. | ||
*/ | ||
@Rule(key = TodoCommentCheck.CHECK_KEY) | ||
public class TodoCommentCheck extends MagikCheck { | ||
|
||
@SuppressWarnings("checkstyle:JavadocVariable") | ||
public static final String CHECK_KEY = "CommentTodo"; | ||
private static final String MESSAGE = "Todo comment: %s."; | ||
|
||
private static final String DEFAULT_FORBIDDEN_WORDS = "TODO,FIXME,HACK,XXX"; | ||
|
||
/** | ||
* List of comment words, separated by ','. | ||
*/ | ||
@RuleProperty( | ||
key = "forbidden comment words", | ||
defaultValue = "" + DEFAULT_FORBIDDEN_WORDS, | ||
description = "List of forbidden words, separated by ',', case sensitive", | ||
type = "STRING") | ||
@SuppressWarnings("checkstyle:VisibilityModifier") | ||
public String forbiddenWords = DEFAULT_FORBIDDEN_WORDS; | ||
|
||
private Collection<String> getForbiddenWords() { | ||
return Arrays.stream(this.forbiddenWords.split(",")) | ||
.map(String::trim) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
@Override | ||
protected void walkTrivia(final Trivia trivia) { | ||
if (!trivia.isComment()) { | ||
return; | ||
} | ||
|
||
trivia.getTokens().forEach(this::checkComment); | ||
} | ||
|
||
private void checkComment(final Token token) { | ||
final String comment = token.getOriginalValue(); | ||
this.getForbiddenWords().stream() | ||
.forEach(word -> { | ||
int fromIndex = 0; | ||
while (fromIndex != -1) { | ||
fromIndex = comment.indexOf(word, fromIndex); | ||
if (fromIndex == -1) { | ||
break; | ||
} | ||
|
||
final String message = String.format(MESSAGE, word); | ||
this.addIssue(token.getLine(), fromIndex, token.getLine(), fromIndex + word.length(), message); | ||
|
||
fromIndex += word.length(); | ||
} | ||
}); | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
magik-checks/src/main/resources/nl/ramsolutions/sw/sonar/l10n/magik/rules/TodoComment.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<p>XXX/TODO/FIXME comments should be resolved.</p> |
15 changes: 15 additions & 0 deletions
15
magik-checks/src/main/resources/nl/ramsolutions/sw/sonar/l10n/magik/rules/TodoComment.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"title": "TODO comment", | ||
"type": "CODE_SMELL", | ||
"status": "ready", | ||
"remediation": { | ||
"func": "Constant\/Issue", | ||
"constantCost": "15min" | ||
}, | ||
"tags": [ | ||
"convention" | ||
], | ||
"defaultSeverity": "Minor", | ||
"ruleSpecification": "CommentTodo", | ||
"sqKey": "comment-todo" | ||
} |
49 changes: 49 additions & 0 deletions
49
magik-checks/src/test/java/nl/ramsolutions/sw/magik/checks/checks/TodoCommentCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package nl.ramsolutions.sw.magik.checks.checks; | ||
|
||
import java.util.List; | ||
import nl.ramsolutions.sw.magik.checks.MagikCheck; | ||
import nl.ramsolutions.sw.magik.checks.MagikIssue; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.ValueSource; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Test {@link TodoCommentCheck}. | ||
*/ | ||
class TodoCommentCheckTest extends MagikCheckTestBase { | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { | ||
"# This should not trigger an issue", | ||
"# This should trigger an issue, although it is used as a hack." | ||
}) | ||
void testOk(final String code) { | ||
final MagikCheck check = new TodoCommentCheck(); | ||
final List<MagikIssue> issues = this.runCheck(code, check); | ||
assertThat(issues).isEmpty(); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { | ||
"# TODO: This should trigger an issue", | ||
"# XXX: This should trigger an issue" | ||
}) | ||
void testForbiddenWord(final String code) { | ||
final MagikCheck check = new TodoCommentCheck(); | ||
final List<MagikIssue> issues = this.runCheck(code, check); | ||
assertThat(issues).hasSize(1); | ||
} | ||
|
||
@ParameterizedTest | ||
@ValueSource(strings = { | ||
"# XXX TODO: This should trigger two issues", | ||
"# XXX FIXME: This should trigger two issues" | ||
}) | ||
void testForbiddenWordTwice(final String code) { | ||
final MagikCheck check = new TodoCommentCheck(); | ||
final List<MagikIssue> issues = this.runCheck(code, check); | ||
assertThat(issues).hasSize(2); | ||
} | ||
|
||
} |