Skip to content

Commit

Permalink
Fix ForbiddenComment rule not checking for KDoc (#3275)
Browse files Browse the repository at this point in the history
* Fix ForbiddenComment rule not checking for KDoc

ForbiddenComment now analyzes KDoc elements for forbidden comments.
KDoc sections don't inherit from the PsiComment type.
Hence, this rule needs to also gather and analyze KDocSection types.

Fixes #3273

* Remove wildcard import

* Add ForbiddenComment suppression to rule itself

This allows to document rule violations in the noncompliant section.

* Format report statement accordingly

revert back to old style

* Update report message to include the violated value
  • Loading branch information
schalkms committed Dec 14, 2020
1 parent 481dab9 commit 84e42ec
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
1 change: 1 addition & 0 deletions config/detekt/detekt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ style:
ForbiddenComment:
active: true
values: ['TODO:', 'FIXME:', 'STOPSHIP:', '@author']
excludes: ['**/detekt-rules-style/**/ForbiddenComment.kt']
LibraryCodeMustSpecifyReturnType:
active: true
excludes: ['**/*.kt']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.api.internal.valueOrDefaultCommaSeparated
import org.jetbrains.kotlin.com.intellij.psi.PsiComment
import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.kdoc.psi.impl.KDocSection
import org.jetbrains.kotlin.psi.KtFile
import org.jetbrains.kotlin.psi.psiUtil.collectDescendantsOfType

/**
* This rule allows to set a list of comments which are forbidden in the codebase and should only be used during
Expand All @@ -31,7 +35,7 @@ class ForbiddenComment(config: Config = Config.empty) : Rule(config) {

override val issue = Issue(javaClass.simpleName,
Severity.Style,
"Flags a forbidden comment. Defaults values are TODO:, FIXME: or STOPSHIP:",
"Flags a forbidden comment.",
Debt.TEN_MINS)

private val values: List<String> = valueOrDefaultCommaSeparated(VALUES, listOf("TODO:", "FIXME:", "STOPSHIP:"))
Expand All @@ -40,15 +44,25 @@ class ForbiddenComment(config: Config = Config.empty) : Rule(config) {

override fun visitComment(comment: PsiComment) {
super.visitComment(comment)

val text = comment.text
checkForbiddenComment(text, comment)
}

override fun visitKtFile(file: KtFile) {
super.visitKtFile(file)
file.collectDescendantsOfType<KDocSection>().forEach { comment ->
val text = comment.getContent()
checkForbiddenComment(text, comment)
}
}

private fun checkForbiddenComment(text: String, comment: PsiElement) {
if (allowedPatterns.pattern.isNotEmpty() && allowedPatterns.containsMatchIn(text)) return

values.forEach {
if (text.contains(it, ignoreCase = true)) {
report(CodeSmell(issue, Entity.from(comment), "This comment contains text that has been " +
"defined as forbidden in detekt."))
report(CodeSmell(issue, Entity.from(comment), "This comment contains '$it' that has been " +
"defined as forbidden in detekt."))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,17 @@ class ForbiddenCommentSpec : Spek({

it("should report violation in KDoc") {
val code = """
/*
/**
* TODO: I need to fix this.
*/
class A
class A {
/**
* TODO: I need to fix this.
*/
}
"""
val findings = ForbiddenComment().compileAndLint(code)
assertThat(findings).hasSize(1)
assertThat(findings).hasSize(2)
}
}

Expand Down

0 comments on commit 84e42ec

Please sign in to comment.