From b7a8465ac02ebd8591baa192bb462ca4fe6bd2f7 Mon Sep 17 00:00:00 2001 From: sanggggg Date: Sun, 9 Oct 2022 22:25:14 +0900 Subject: [PATCH 1/2] Add testcase for annotation contains a parameter on sperate line --- .../detekt/formatting/RulesWhichCantBeCorrectedSpec.kt | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/detekt-formatting/src/test/kotlin/io/gitlab/arturbosch/detekt/formatting/RulesWhichCantBeCorrectedSpec.kt b/detekt-formatting/src/test/kotlin/io/gitlab/arturbosch/detekt/formatting/RulesWhichCantBeCorrectedSpec.kt index 8dfb465d473..c595c5176d1 100644 --- a/detekt-formatting/src/test/kotlin/io/gitlab/arturbosch/detekt/formatting/RulesWhichCantBeCorrectedSpec.kt +++ b/detekt-formatting/src/test/kotlin/io/gitlab/arturbosch/detekt/formatting/RulesWhichCantBeCorrectedSpec.kt @@ -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 @@ -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) + } } From a8e122c11e19c321174927c45b7e577ac82fa199 Mon Sep 17 00:00:00 2001 From: sanggggg Date: Sun, 9 Oct 2022 22:28:31 +0900 Subject: [PATCH 2/2] Check Auto-correctable by ktlint rule's response --- .../gitlab/arturbosch/detekt/formatting/FormattingRule.kt | 7 ++----- .../detekt/formatting/wrappers/EnumEntryNameCase.kt | 2 -- .../arturbosch/detekt/formatting/wrappers/Filename.kt | 2 -- .../detekt/formatting/wrappers/ImportOrdering.kt | 2 -- .../arturbosch/detekt/formatting/wrappers/Indentation.kt | 2 -- .../detekt/formatting/wrappers/MaximumLineLength.kt | 2 -- .../detekt/formatting/wrappers/NoWildcardImports.kt | 2 -- .../arturbosch/detekt/formatting/wrappers/PackageName.kt | 2 -- 8 files changed, 2 insertions(+), 19 deletions(-) diff --git a/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/FormattingRule.kt b/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/FormattingRule.kt index 521496b7624..7016ea15ca2 100644 --- a/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/FormattingRule.kt +++ b/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/FormattingRule.kt @@ -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), @@ -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)) @@ -71,9 +71,6 @@ abstract class FormattingRule(config: Config) : Rule(config) { private var positionByOffset: (offset: Int) -> Pair 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) diff --git a/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/EnumEntryNameCase.kt b/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/EnumEntryNameCase.kt index 4b9da825332..5a17c111741 100644 --- a/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/EnumEntryNameCase.kt +++ b/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/EnumEntryNameCase.kt @@ -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 } diff --git a/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/Filename.kt b/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/Filename.kt index 116ca427e2f..aa8dede5439 100644 --- a/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/Filename.kt +++ b/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/Filename.kt @@ -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 } diff --git a/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/ImportOrdering.kt b/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/ImportOrdering.kt index 5ad39af2f83..a44fa4ddc86 100644 --- a/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/ImportOrdering.kt +++ b/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/ImportOrdering.kt @@ -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, String> = mapOf(ImportOrderingRule.ideaImportsLayoutProperty to layout) diff --git a/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/Indentation.kt b/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/Indentation.kt index 5d3bec78765..00450be08ac 100644 --- a/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/Indentation.kt +++ b/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/Indentation.kt @@ -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, String> = mapOf( DefaultEditorConfigProperties.indentSizeProperty to indentSize.toString(), diff --git a/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/MaximumLineLength.kt b/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/MaximumLineLength.kt index db7e3d8ecf9..e0c1d67772e 100644 --- a/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/MaximumLineLength.kt +++ b/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/MaximumLineLength.kt @@ -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, String> = mapOf( MaxLineLengthRule.ignoreBackTickedIdentifierProperty to ignoreBackTickedIdentifier.toString(), diff --git a/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/NoWildcardImports.kt b/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/NoWildcardImports.kt index 4c4b08ac4aa..3dc3a50deef 100644 --- a/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/NoWildcardImports.kt +++ b/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/NoWildcardImports.kt @@ -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, String> = mapOf( NoWildcardImportsRule.packagesToUseImportOnDemandProperty to packagesToUseImportOnDemandProperty diff --git a/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/PackageName.kt b/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/PackageName.kt index 436e0c84b22..a15d6d59418 100644 --- a/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/PackageName.kt +++ b/detekt-formatting/src/main/kotlin/io/gitlab/arturbosch/detekt/formatting/wrappers/PackageName.kt @@ -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 }