From f3a49146836726a9e6663ebf9848743576e0e956 Mon Sep 17 00:00:00 2001 From: rom4ek Date: Mon, 25 Nov 2019 09:08:53 +0100 Subject: [PATCH] Allow empty first line in function that returns anonymous object --- .../NoEmptyFirstLineInMethodBlockRule.kt | 4 ++- .../NoEmptyFirstLineInMethodBlockRuleTest.kt | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/NoEmptyFirstLineInMethodBlockRule.kt b/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/NoEmptyFirstLineInMethodBlockRule.kt index 83274b81f8..b0d5d416d8 100644 --- a/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/NoEmptyFirstLineInMethodBlockRule.kt +++ b/ktlint-ruleset-experimental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/NoEmptyFirstLineInMethodBlockRule.kt @@ -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 @@ -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) { diff --git a/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/NoEmptyFirstLineInMethodBlockRuleTest.kt b/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/NoEmptyFirstLineInMethodBlockRuleTest.kt index 5e814a2724..9c46f40d39 100644 --- a/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/NoEmptyFirstLineInMethodBlockRuleTest.kt +++ b/ktlint-ruleset-experimental/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/NoEmptyFirstLineInMethodBlockRuleTest.kt @@ -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) + } }