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

Allow empty first line in function that returns anonymous object #655

Merged
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 @@ -2,6 +2,7 @@ package com.pinterest.ktlint.ruleset.experimental

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType
import com.pinterest.ktlint.core.ast.ElementType.CLASS_BODY
import com.pinterest.ktlint.core.ast.ElementType.FUN
import com.pinterest.ktlint.core.ast.isPartOf
import com.pinterest.ktlint.core.ast.prevLeaf
Expand All @@ -17,7 +18,8 @@ class NoEmptyFirstLineInMethodBlockRule : Rule("no-empty-first-line-in-method-bl
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
if (node is PsiWhiteSpace && node.textContains('\n') &&
node.prevLeaf()?.elementType == ElementType.LBRACE && node.isPartOf(FUN)
node.prevLeaf()?.elementType == ElementType.LBRACE && node.isPartOf(FUN) &&
node.treeParent.elementType != CLASS_BODY // fun fn() = object : Builder {\n\n fun stuff() = Unit }
) {
val split = node.getText().split("\n")
if (split.size > 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,32 @@ class NoEmptyFirstLineInMethodBlockRuleTest {
)
assertThat(NoEmptyFirstLineInMethodBlockRule().format(unformattedFunction)).isEqualTo(formattedFunction)
}

@Test
fun `lint empty first line may be placed in function inside anonymous object`() {
val code =
"""
fun fooBuilder() = object : Foo {

override fun foo() {
TODO()
}
}
""".trimIndent()
assertThat(NoEmptyFirstLineInMethodBlockRule().lint(code)).isEmpty()
}

@Test
fun `format empty first line may be placed in function inside anonymous object`() {
val code =
"""
fun fooBuilder() = object : Foo {

override fun foo() {
TODO()
}
}
""".trimIndent()
assertThat(NoEmptyFirstLineInMethodBlockRule().format(code)).isEqualTo(code)
}
}