Skip to content

Commit

Permalink
Do not force blank line before function in right hand side of assignm…
Browse files Browse the repository at this point in the history
…ent `blank-line-before-declaration`

Closes #2260
  • Loading branch information
paul-dingemans committed Sep 16, 2023
1 parent 71da821 commit 0065813
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).

* Fix ktlint cli parameter `--code-style` [#2238](https://github.com/pinterest/ktlint/pull/2238)
* Ignore anonymous function in rule `function-naming` [#2260](https://github.com/pinterest/ktlint/issue/2260)
* Do not force blank line before function in right hand side of assignment `blank-line-before-declaration` [#2260](https://github.com/pinterest/ktlint/issue/2260)

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.BLOCK
import com.pinterest.ktlint.rule.engine.core.api.ElementType.CLASS
import com.pinterest.ktlint.rule.engine.core.api.ElementType.CLASS_BODY
import com.pinterest.ktlint.rule.engine.core.api.ElementType.CLASS_INITIALIZER
import com.pinterest.ktlint.rule.engine.core.api.ElementType.EQ
import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUN
import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUNCTION_LITERAL
import com.pinterest.ktlint.rule.engine.core.api.ElementType.LBRACE
Expand Down Expand Up @@ -116,10 +117,17 @@ public class BlankLineBeforeDeclarationRule :
return
}

if (node.elementType == FUN && node.prevCodeSibling()?.elementType == EQ) {
// Allow:
// val foo =
// fun(): String {
// return "foo"
// }
return
}

node
.takeIf { it.psi is KtDeclaration }
// (node.psi as KtDeclaration)
// .node
?.takeIf {
val prevLeaf = it.prevLeaf()
prevLeaf != null && (!prevLeaf.isWhiteSpace() || !prevLeaf.text.startsWith("\n\n"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,4 +447,16 @@ class BlankLineBeforeDeclarationRuleTest {
.hasLintViolation(3, 5, "Expected a blank line for this declaration")
.isFormattedAs(formattedCode)
}

@Test
fun `Issue 2260 - Given an anonymous function as right hand side in an assignment`() {
val code =
"""
val foo =
fun(): String {
return "foo"
}
""".trimIndent()
blankLineBeforeDeclarationRuleAssertThat(code).hasNoLintViolations()
}
}

0 comments on commit 0065813

Please sign in to comment.