Skip to content

Commit

Permalink
Differentiate between correctable and non-correctable KtLint rules (#…
Browse files Browse the repository at this point in the history
…5324)

* Differentiate between correctable and non-correctable KtLint rules

* Split testcases into separate test functions
  • Loading branch information
arturbosch committed Sep 24, 2022
1 parent 9a35a3a commit d04d85e
Show file tree
Hide file tree
Showing 9 changed files with 109 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 Down Expand Up @@ -47,6 +48,9 @@ abstract class FormattingRule(config: Config) : Rule(config) {
private var positionByOffset: (offset: Int) -> Pair<Int, Int> by SingleAssign()
private var root: KtFile by SingleAssign()

// KtLint has rules which prompts the user to manually correct issues e.g. Filename and PackageName.
protected open fun canBeCorrectedByKtLint(message: String): Boolean = true

protected fun issueFor(description: String) =
Issue(javaClass.simpleName, Severity.Style, description, Debt.FIVE_MINS)

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(message)) {
report(CorrectableCodeSmell(issue, entity, message, autoCorrectEnabled = autoCorrect))
} else {
report(CodeSmell(issue, entity, message))
}
}
}

Expand Down
Expand Up @@ -15,4 +15,6 @@ class EnumEntryNameCase(config: Config) : FormattingRule(config) {

override val wrapping = EnumEntryNameCaseRule()
override val issue = issueFor("Reports enum entries with names that don't meet standard conventions.")

override fun canBeCorrectedByKtLint(message: String): Boolean = false
}
Expand Up @@ -16,4 +16,6 @@ class Filename(config: Config) : FormattingRule(config) {

override val wrapping = FilenameRule()
override val issue = issueFor("Checks if top level class matches the filename")

override fun canBeCorrectedByKtLint(message: String): Boolean = false
}
Expand Up @@ -24,6 +24,8 @@ class ImportOrdering(config: Config) : FormattingRule(config) {
@Configuration("the import ordering layout")
private val layout: String by configWithAndroidVariants(IDEA_PATTERN, ASCII_PATTERN)

override fun canBeCorrectedByKtLint(message: String): Boolean = "no autocorrection" !in message

override fun overrideEditorConfigProperties(): Map<UsesEditorConfigProperties.EditorConfigProperty<*>, String> =
mapOf(ImportOrderingRule.ideaImportsLayoutProperty to layout)

Expand Down
Expand Up @@ -30,6 +30,8 @@ class Indentation(config: Config) : FormattingRule(config) {
@Suppress("UnusedPrivateMember")
private val continuationIndentSize by config(4)

override fun canBeCorrectedByKtLint(message: String): Boolean = "not contain both tab(s) and space(s)" !in message

override fun overrideEditorConfigProperties(): Map<UsesEditorConfigProperties.EditorConfigProperty<*>, String> =
mapOf(
DefaultEditorConfigProperties.indentSizeProperty to indentSize.toString(),
Expand Down
Expand Up @@ -32,6 +32,8 @@ class MaximumLineLength(config: Config) : FormattingRule(config) {
@Configuration("ignore back ticked identifier")
private val ignoreBackTickedIdentifier by config(false)

override fun canBeCorrectedByKtLint(message: String): Boolean = false

override fun overrideEditorConfigProperties(): Map<UsesEditorConfigProperties.EditorConfigProperty<*>, String> =
mapOf(
MaxLineLengthRule.ignoreBackTickedIdentifierProperty to ignoreBackTickedIdentifier.toString(),
Expand Down
Expand Up @@ -20,6 +20,8 @@ class NoWildcardImports(config: Config) : FormattingRule(config) {
@Configuration("Defines allowed wildcard imports")
private val packagesToUseImportOnDemandProperty by config(ALLOWED_WILDCARD_IMPORTS)

override fun canBeCorrectedByKtLint(message: String): Boolean = false

override fun overrideEditorConfigProperties(): Map<UsesEditorConfigProperties.EditorConfigProperty<*>, String> =
mapOf(
NoWildcardImportsRule.packagesToUseImportOnDemandProperty to packagesToUseImportOnDemandProperty
Expand Down
Expand Up @@ -16,4 +16,6 @@ class PackageName(config: Config) : FormattingRule(config) {

override val wrapping = PackageNameRule()
override val issue = issueFor("Checks package name is formatted correctly")

override fun canBeCorrectedByKtLint(message: String): Boolean = false
}
@@ -0,0 +1,85 @@
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.EnumEntryNameCase
import io.gitlab.arturbosch.detekt.formatting.wrappers.Filename
import io.gitlab.arturbosch.detekt.formatting.wrappers.ImportOrdering
import io.gitlab.arturbosch.detekt.formatting.wrappers.Indentation
import io.gitlab.arturbosch.detekt.formatting.wrappers.MaximumLineLength
import io.gitlab.arturbosch.detekt.formatting.wrappers.NoWildcardImports
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 `Filename findings can't be corrected`() {
assertThat(Filename(Config.empty).lint("class NotTheFilename"))
.isNotEmpty
.hasExactlyElementsOfTypes(CodeSmell::class.java)
}

@Test
fun `PackageName findings can't be corrected`() {
assertThat(PackageName(Config.empty).lint("package under_score"))
.isNotEmpty
.hasExactlyElementsOfTypes(CodeSmell::class.java)
}

@Test
fun `ImportOrdering has a case with comments which is not correctable`() {
assertThat(
ImportOrdering(Config.empty).lint(
"""
import xyz.wrong_order
/*comment in between*/
import java.io.*
""".trimIndent()
)
).isNotEmpty
.hasExactlyElementsOfTypes(CodeSmell::class.java)
}

@Test
fun `NoWildcardImports can't be corrected`() {
assertThat(NoWildcardImports(Config.empty).lint("import java.io.*"))
.isNotEmpty
.hasExactlyElementsOfTypes(CodeSmell::class.java)
}

@Test
fun `MaximumLineLength can't be corrected`() {
assertThat(
MaximumLineLength(Config.empty).lint(
"""
class MaximumLeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeth
""".trimIndent()
)
).isNotEmpty
.hasExactlyElementsOfTypes(CodeSmell::class.java)
}

@Test
fun `EnumEntryNameCase can't be corrected`() {
assertThat(EnumEntryNameCase(Config.empty).lint("enum class Enum { violation_triggering_name }"))
.isNotEmpty
.hasExactlyElementsOfTypes(CodeSmell::class.java)
}

@Test
fun `Indentation finding inside string templates can't be corrected`() {
val multilineQuote = "${'"'}${'"'}${'"'}"
val code = """
val foo = $multilineQuote
line1
${'\t'}line2
$multilineQuote.trimIndent()
""".trimIndent()

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

0 comments on commit d04d85e

Please sign in to comment.