Add documentation link for rules in html report (#4799)

* Add documentation link for rules in html report

* Extract detekt base URL from documentation URL into own constant

* Fix ImplicitDefaultLocale violations
This commit is contained in:
Marie Katrine Ekeberg
2022-05-03 06:16:51 +02:00
committed by GitHub
parent 16bbc16da0
commit 597f7cfef1
3 changed files with 31 additions and 2 deletions

View File

@@ -15,6 +15,7 @@ import kotlinx.html.FlowOrInteractiveContent
import kotlinx.html.HTMLTag
import kotlinx.html.HtmlTagMarker
import kotlinx.html.TagConsumer
import kotlinx.html.a
import kotlinx.html.attributesMapOf
import kotlinx.html.details
import kotlinx.html.div
@@ -37,6 +38,8 @@ private const val PLACEHOLDER_COMPLEXITY_REPORT = "@@@complexity@@@"
private const val PLACEHOLDER_VERSION = "@@@version@@@"
private const val PLACEHOLDER_DATE = "@@@date@@@"
private const val DETEKT_WEBSITE_BASE_URL = "https://detekt.dev"
/**
* Contains rule violations and metrics formatted in a human friendly way, so that it can be inspected in a web browser.
* See: https://detekt.dev/configurations.html#output-reports
@@ -106,11 +109,11 @@ class HtmlOutputReport : OutputReport() {
.toList()
.sortedBy { (rule, _) -> rule }
.forEach { (rule, ruleFindings) ->
renderRule(rule, ruleFindings)
renderRule(rule, group, ruleFindings)
}
}
private fun FlowContent.renderRule(rule: String, findings: List<Finding>) {
private fun FlowContent.renderRule(rule: String, group: String, findings: List<Finding>) {
details {
id = rule
open = true
@@ -120,6 +123,10 @@ class HtmlOutputReport : OutputReport() {
span("description") { text(findings.first().issue.description) }
}
a("$DETEKT_WEBSITE_BASE_URL/docs/rules/${group.toLowerCase(Locale.US)}#${rule.toLowerCase(Locale.US)}") {
+"Documentation"
}
ul {
findings
.sortedWith(compareBy({ it.file }, { it.location.source.line }, { it.location.source.column }))

View File

@@ -114,6 +114,26 @@ class HtmlOutputReportSpec {
assertThat(result).contains("<span class=\"description\">Description id_b</span>")
}
@Test
fun `renders the right documentation links for the rules`() {
val detektion = object : TestDetektion() {
override val findings: Map<String, List<Finding>> = mapOf(
"Style" to listOf(
createFinding(createIssue("ValCouldBeVar"), createEntity(""))
),
"empty" to listOf(
createFinding(createIssue("EmptyBody"), createEntity("")),
createFinding(createIssue("EmptyIf"), createEntity(""))
)
)
}
val result = htmlReport.render(detektion)
assertThat(result).contains("<a href=\"https://detekt.dev/docs/rules/style#valcouldbevar\">Documentation</a>")
assertThat(result).contains("<a href=\"https://detekt.dev/docs/rules/empty#emptybody\">Documentation</a>")
assertThat(result).contains("<a href=\"https://detekt.dev/docs/rules/empty#emptyif\">Documentation</a>")
}
@Test
fun `renders a metric report correctly`() {
val detektion = object : TestDetektion() {

View File

@@ -172,6 +172,7 @@
<h3>Section 1: 2</h3>
<details id="id_a" open="open">
<summary class="rule-container"><span class="rule">id_a: 2 </span><span class="description">Description id_a</span></summary>
<a href="https://detekt.dev/docs/rules/section 1#id_a">Documentation</a>
<ul>
<li><span class="location">src/main/com/sample/Sample1.kt:11:1</span><span class="message">Message finding 1</span>
<pre><code><span class="lineno"> 8 </span>
@@ -188,6 +189,7 @@
<h3>Section 2: 1</h3>
<details id="id_b" open="open">
<summary class="rule-container"><span class="rule">id_b: 1 </span><span class="description">Description id_b</span></summary>
<a href="https://detekt.dev/docs/rules/section 2#id_b">Documentation</a>
<ul>
<li><span class="location">src/main/com/sample/Sample3.kt:33:3</span></li>
</ul>