Skip to content

Commit

Permalink
Check FormattingRule is auto-correctable by information provided by k…
Browse files Browse the repository at this point in the history
…tlint (#5398)
  • Loading branch information
sanggggg committed Oct 14, 2022
1 parent 9f4b0ca commit 6c54364
Show file tree
Hide file tree
Showing 9 changed files with 10 additions and 19 deletions.
Expand Up @@ -42,7 +42,7 @@ abstract class FormattingRule(config: Config) : Rule(config) {
val runAsLateAsPossible
get() = RunAsLateAsPossible in wrapping.visitorModifiers

private val emit = { offset: Int, message: String, _: Boolean ->
private val emit = { offset: Int, message: String, canBeAutoCorrected: Boolean ->
val (line, column) = positionByOffset(offset)
val location = Location(
SourceLocation(line, column),
Expand All @@ -61,7 +61,7 @@ abstract class FormattingRule(config: Config) : Rule(config) {
.orEmpty()
val entity = Entity("", "$packageName${root.fileName}:$line", location, root)

if (canBeCorrectedByKtLint(message)) {
if (canBeAutoCorrected) {
report(CorrectableCodeSmell(issue, entity, message, autoCorrectEnabled = autoCorrect))
} else {
report(CodeSmell(issue, entity, message))
Expand All @@ -71,9 +71,6 @@ 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 @@ -15,6 +15,4 @@ 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,6 +16,4 @@ 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,8 +24,6 @@ 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 @@ -28,8 +28,6 @@ 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,8 +32,6 @@ 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,8 +20,6 @@ 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,6 +16,4 @@ 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
}
Expand Up @@ -2,6 +2,7 @@ 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.AnnotationOnSeparateLine
import io.gitlab.arturbosch.detekt.formatting.wrappers.EnumEntryNameCase
import io.gitlab.arturbosch.detekt.formatting.wrappers.Filename
import io.gitlab.arturbosch.detekt.formatting.wrappers.ImportOrdering
Expand Down Expand Up @@ -82,4 +83,11 @@ class RulesWhichCantBeCorrectedSpec {
.isNotEmpty
.hasExactlyElementsOfTypes(CodeSmell::class.java)
}

@Test
fun `Annotation contains a parameter on single line can't be corrected`() {
assertThat(AnnotationOnSeparateLine(Config.empty).lint("fun foo1() = @Suppress(\"DEPRECATION\") bar()"))
.isNotEmpty
.hasExactlyElementsOfTypes(CodeSmell::class.java)
}
}

0 comments on commit 6c54364

Please sign in to comment.