mirror of
https://github.com/jlengrand/detekt.git
synced 2026-03-10 08:11:23 +00:00
Add customMessage to ForbiddenComment Rule (#4126)
* Add config customMessage to rule ForbiddenComment * Add customMessage tests in FordbiddenCommentSpec * Update contributors list * Generate documentation
This commit is contained in:
committed by
GitHub
parent
7b9ee707b7
commit
aab94a2a91
@@ -229,6 +229,7 @@ If you contributed to detekt but your name is not in the list, please feel free
|
||||
- [Mohamed Elmahdi](https://github.com/mohamed-elmahdi) - Rule Improvement: Add descriptive alias
|
||||
- [Michael McCormick](https://github.com/MichaelM97) - Documentation improvement
|
||||
|
||||
- [Hans-Martin Schuller](https://github.com/hmSchuller) - Rule Improvement: ForbiddenComment
|
||||
|
||||
### Mentions
|
||||
|
||||
|
||||
@@ -619,6 +619,7 @@ style:
|
||||
- 'STOPSHIP:'
|
||||
- 'TODO:'
|
||||
allowedPatterns: ''
|
||||
customMessage: ''
|
||||
ForbiddenImport:
|
||||
active: false
|
||||
imports: []
|
||||
|
||||
@@ -43,6 +43,9 @@ class ForbiddenComment(config: Config = Config.empty) : Rule(config) {
|
||||
@Configuration("ignores comments which match the specified regular expression. For example `Ticket|Task`.")
|
||||
private val allowedPatterns: Regex by config("", String::toRegex)
|
||||
|
||||
@Configuration("error message which overrides the default one")
|
||||
private val customMessage: String by config("")
|
||||
|
||||
override fun visitComment(comment: PsiComment) {
|
||||
super.visitComment(comment)
|
||||
val text = comment.text
|
||||
@@ -66,11 +69,18 @@ class ForbiddenComment(config: Config = Config.empty) : Rule(config) {
|
||||
CodeSmell(
|
||||
issue,
|
||||
Entity.from(comment),
|
||||
"This comment contains '$it' that has been " +
|
||||
"defined as forbidden in detekt."
|
||||
getErrorMessage(it)
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getErrorMessage(value: String): String =
|
||||
customMessage.takeUnless { it.isEmpty() } ?: String.format(DEFAULT_ERROR_MESSAGE, value)
|
||||
|
||||
companion object {
|
||||
const val DEFAULT_ERROR_MESSAGE = "This comment contains '%s' " +
|
||||
"that has been defined as forbidden in detekt."
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import org.spekframework.spek2.style.specification.describe
|
||||
|
||||
private const val VALUES = "values"
|
||||
private const val ALLOWED_PATTERNS = "allowedPatterns"
|
||||
private const val MESSAGE = "customMessage"
|
||||
|
||||
class ForbiddenCommentSpec : Spek({
|
||||
|
||||
@@ -146,5 +147,41 @@ class ForbiddenCommentSpec : Spek({
|
||||
assertThat(findings).isEmpty()
|
||||
}
|
||||
}
|
||||
|
||||
context("custom message is configured") {
|
||||
val messageConfig by memoized {
|
||||
TestConfig(
|
||||
mapOf(
|
||||
VALUES to "Comment",
|
||||
MESSAGE to "Custom Message"
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
it("should report a Finding with message 'Custom Message'") {
|
||||
val comment = "// Comment"
|
||||
val findings = ForbiddenComment(messageConfig).compileAndLint(comment)
|
||||
assertThat(findings).hasSize(1)
|
||||
assertThat(findings.first().message).isEqualTo("Custom Message")
|
||||
}
|
||||
}
|
||||
|
||||
context("custom message is not configured") {
|
||||
val messageConfig by memoized {
|
||||
TestConfig(
|
||||
mapOf(
|
||||
VALUES to "Comment"
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
it("should report a Finding with default Message") {
|
||||
val comment = "// Comment"
|
||||
val findings = ForbiddenComment(messageConfig).compileAndLint(comment)
|
||||
val expectedMessage = String.format(ForbiddenComment.DEFAULT_ERROR_MESSAGE, "Comment")
|
||||
assertThat(findings).hasSize(1)
|
||||
assertThat(findings.first().message).isEqualTo(expectedMessage)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user