Skip to content

Commit

Permalink
Allow empty first line in function that returns anonymous object
Browse files Browse the repository at this point in the history
  • Loading branch information
romtsn committed Nov 25, 2019
1 parent 7519e64 commit f3a4914
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
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)
}
}

0 comments on commit f3a4914

Please sign in to comment.