/
EndOfSentenceFormat.kt
54 lines (47 loc) · 2.07 KB
/
EndOfSentenceFormat.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package io.gitlab.arturbosch.detekt.rules.documentation
import io.gitlab.arturbosch.detekt.api.CodeSmell
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.Debt
import io.gitlab.arturbosch.detekt.api.Entity
import io.gitlab.arturbosch.detekt.api.Issue
import io.gitlab.arturbosch.detekt.api.Rule
import io.gitlab.arturbosch.detekt.api.Severity
import io.gitlab.arturbosch.detekt.api.config
import io.gitlab.arturbosch.detekt.api.internal.Configuration
import io.gitlab.arturbosch.detekt.rules.lastArgumentMatchesUrl
import org.jetbrains.kotlin.psi.KtDeclaration
/**
* This rule validates the end of the first sentence of a KDoc comment.
* It should end with proper punctuation or with a correct URL.
*/
@Suppress("MemberNameEqualsClassName")
class EndOfSentenceFormat(config: Config = Config.empty) : Rule(config) {
override val issue = Issue(
javaClass.simpleName,
Severity.Maintainability,
"The first sentence in a KDoc comment should end with proper punctuation or with a correct URL.",
Debt.FIVE_MINS
)
@Configuration("regular expression which should match the end of the first sentence in the KDoc")
private val endOfSentenceFormat: Regex by config("""([.?!][ \t\n\r\f<])|([.?!:]$)""") { it.toRegex() }
private val htmlTag = Regex("<.+>")
override fun visitDeclaration(dcl: KtDeclaration) {
super.visitDeclaration(dcl)
dcl.docComment?.let {
val text = it.getDefaultSection().getContent()
if (text.isEmpty() || text.startsWithHtmlTag()) {
return
}
if (!endOfSentenceFormat.containsMatchIn(text) && !text.lastArgumentMatchesUrl()) {
report(
CodeSmell(
issue,
Entity.from(it.getDefaultSection()),
"The first sentence of this KDoc does not end with the correct punctuation."
)
)
}
}
}
private fun String.startsWithHtmlTag() = startsWith("<") && contains(htmlTag)
}