From 3b25af66921a24d12df2c65ae57dabd211bd37a5 Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Sat, 3 Feb 2024 16:13:42 +0100 Subject: [PATCH] Fix false positive newline expected before comment in enum (#2527) Closes #2510 --- .../standard/rules/EnumWrappingRule.kt | 11 +++++--- .../standard/rules/EnumWrappingRuleTest.kt | 28 ++++++++++++++++++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/EnumWrappingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/EnumWrappingRule.kt index 6bbf36e2c9..8b197d48ec 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/EnumWrappingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/EnumWrappingRule.kt @@ -15,9 +15,11 @@ import com.pinterest.ktlint.rule.engine.core.api.children import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EditorConfig import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_SIZE_PROPERTY import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_STYLE_PROPERTY +import com.pinterest.ktlint.rule.engine.core.api.firstChildLeafOrSelf import com.pinterest.ktlint.rule.engine.core.api.isPartOfComment import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithNewline import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithoutNewline +import com.pinterest.ktlint.rule.engine.core.api.leavesIncludingSelf import com.pinterest.ktlint.rule.engine.core.api.nextCodeSibling import com.pinterest.ktlint.rule.engine.core.api.nextSibling import com.pinterest.ktlint.rule.engine.core.api.prevLeaf @@ -85,16 +87,17 @@ public class EnumWrappingRule : emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit, autoCorrect: Boolean, ): Boolean { - val firstEnumEntry = node.findChildByType(ENUM_ENTRY) + val firstEnumEntry = node.findChildByType(ENUM_ENTRY)?.firstChildLeafOrSelf() if (firstEnumEntry != null) { node - .children() + .firstChildLeafOrSelf() + .leavesIncludingSelf() .takeWhile { it != firstEnumEntry } .firstOrNull { it.isPartOfComment() } ?.let { commentBeforeFirstEnumEntry -> - val expectedIndent = indentConfig.parentIndentOf(node) + val expectedIndent = indentConfig.childIndentOf(node) if (commentBeforeFirstEnumEntry.prevLeaf()?.text != expectedIndent) { - emit(node.startOffset, "Expected a newline before comment", true) + emit(node.startOffset, "Expected a (single) newline before comment", true) if (autoCorrect) { commentBeforeFirstEnumEntry.upsertWhitespaceBeforeMe(indentConfig.siblingIndentOf(node)) } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/EnumWrappingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/EnumWrappingRuleTest.kt index 314ff057a4..771d39a903 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/EnumWrappingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/EnumWrappingRuleTest.kt @@ -153,7 +153,7 @@ class EnumWrappingRuleTest { """.trimIndent() enumWrappingRuleAssertThat(code) .hasLintViolations( - LintViolation(1, 16, "Expected a newline before comment"), + LintViolation(1, 16, "Expected a (single) newline before comment"), LintViolation(1, 32, "Enum entry should start on a separate line"), LintViolation(1, 35, "Enum entry should start on a separate line"), LintViolation(1, 37, "Expected newline before '}'"), @@ -242,4 +242,30 @@ class EnumWrappingRuleTest { """.trimIndent() enumWrappingRuleAssertThat(code).hasNoLintViolations() } + + @Test + fun `Issue 2510 - Given an enum class with comment before the first entry`() { + val code = + """ + enum class FooBar { + + // Some comment about FooBar + + // Foo + Foo, + } + """.trimIndent() + val formattedCode = + """ + enum class FooBar { + // Some comment about FooBar + + // Foo + Foo, + } + """.trimIndent() + enumWrappingRuleAssertThat(code) + .hasLintViolation(1, 19, "Expected a (single) newline before comment") + .isFormattedAs(formattedCode) + } }