Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix false positive newline expected before comment in enum #2527

Merged
merged 1 commit into from
Feb 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}