Skip to content

Commit

Permalink
Differentiate between correctable and non-correctable KtLint rules
Browse files Browse the repository at this point in the history
  • Loading branch information
arturbosch committed Sep 20, 2022
1 parent 93a2306 commit 3fbbfcb
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
Expand Up @@ -7,6 +7,7 @@ import com.pinterest.ktlint.core.api.DefaultEditorConfigProperties.codeStyleSetP
import com.pinterest.ktlint.core.api.UsesEditorConfigProperties
import io.github.detekt.psi.fileName
import io.github.detekt.psi.toFilePath
import io.gitlab.arturbosch.detekt.api.CodeSmell
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.api.CorrectableCodeSmell
import io.gitlab.arturbosch.detekt.api.Debt
Expand All @@ -31,6 +32,9 @@ abstract class FormattingRule(config: Config) : Rule(config) {

abstract val wrapping: com.pinterest.ktlint.core.Rule

// KtLint has rules which prompts the user to manually correct issues e.g. Filename and PackageName.
open val canBeCorrectedByKtLint: Boolean = true

/**
* Should the android style guide be enforced?
* This property is read from the ruleSet config.
Expand Down Expand Up @@ -102,7 +106,12 @@ abstract class FormattingRule(config: Config) : Rule(config) {
?.plus(".")
.orEmpty()
val entity = Entity("", "$packageName${root.fileName}:$line", location, root)
report(CorrectableCodeSmell(issue, entity, message, autoCorrectEnabled = autoCorrect))

if (canBeCorrectedByKtLint) {
report(CorrectableCodeSmell(issue, entity, message, autoCorrectEnabled = autoCorrect))
} else {
report(CodeSmell(issue, entity, message))
}
}
}

Expand Down
Expand Up @@ -16,4 +16,5 @@ class Filename(config: Config) : FormattingRule(config) {

override val wrapping = FilenameRule()
override val issue = issueFor("Checks if top level class matches the filename")
override val canBeCorrectedByKtLint: Boolean = false
}
Expand Up @@ -16,4 +16,5 @@ class PackageName(config: Config) : FormattingRule(config) {

override val wrapping = PackageNameRule()
override val issue = issueFor("Checks package name is formatted correctly")
override val canBeCorrectedByKtLint: Boolean = false
}
@@ -0,0 +1,26 @@
package io.gitlab.arturbosch.detekt.formatting

import io.gitlab.arturbosch.detekt.api.CodeSmell
import io.gitlab.arturbosch.detekt.api.Config
import io.gitlab.arturbosch.detekt.formatting.wrappers.Filename
import io.gitlab.arturbosch.detekt.formatting.wrappers.PackageName
import io.gitlab.arturbosch.detekt.test.assertThat
import org.junit.jupiter.api.Test

class RulesWhichCantBeCorrectedSpec {

@Test
fun `verify findings of these rules are not correctable`() {
val code = """
package under_score
class NotTheFilename
""".trimIndent()

assertThat(Filename(Config.empty).lint(code))
.isNotEmpty
.hasExactlyElementsOfTypes(CodeSmell::class.java)
assertThat(PackageName(Config.empty).lint(code))
.isNotEmpty
.hasExactlyElementsOfTypes(CodeSmell::class.java)
}
}

0 comments on commit 3fbbfcb

Please sign in to comment.