Skip to content

Commit

Permalink
Fix false positive newline expected before comment in enum (#2527)
Browse files Browse the repository at this point in the history
Closes #2510
  • Loading branch information
paul-dingemans committed Feb 3, 2024
1 parent 4c5c305 commit 3b25af6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 '}'"),
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 3b25af6

Please sign in to comment.