Skip to content

Commit

Permalink
Force blank line before object declaration (#2287)
Browse files Browse the repository at this point in the history
* Force blank line before object declaration if preceded by another declaration

Closes #2284
  • Loading branch information
paul-dingemans authored Oct 2, 2023
1 parent 347eb9b commit 95d95ee
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
* 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)
* Ignore override of function in rule `function-naming` [#2271](https://github.com/pinterest/ktlint/issue/2271)
* Do not replace function body having a return statement only in case the return statement contains an intermediate exit point 'function-expression-body' [#2269](https://github.com/pinterest/ktlint/issue/2269)
* Force blank line before object declaration if preceded by another declaration `blank-line-before-declaration` [#2284](https://github.com/pinterest/ktlint/issues/2284)

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ 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
import com.pinterest.ktlint.rule.engine.core.api.ElementType.OBJECT_DECLARATION
import com.pinterest.ktlint.rule.engine.core.api.ElementType.OBJECT_LITERAL
import com.pinterest.ktlint.rule.engine.core.api.ElementType.PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.ElementType.PROPERTY_ACCESSOR
import com.pinterest.ktlint.rule.engine.core.api.ElementType.WHEN
Expand Down Expand Up @@ -48,6 +50,7 @@ public class BlankLineBeforeDeclarationRule :
CLASS,
CLASS_INITIALIZER,
FUN,
OBJECT_DECLARATION,
PROPERTY,
PROPERTY_ACCESSOR,
->
Expand Down Expand Up @@ -126,6 +129,15 @@ public class BlankLineBeforeDeclarationRule :
return
}

if (node.elementType == OBJECT_DECLARATION && node.treeParent.elementType == OBJECT_LITERAL) {
// Allow:
// fun foo() =
// object : Foo() {
// // some declarations
// }
return
}

node
.takeIf { it.psi is KtDeclaration }
?.takeIf {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.pinterest.ktlint.ruleset.standard.rules
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.CODE_STYLE_PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.CodeStyleValue
import com.pinterest.ktlint.test.KtLintAssertThat
import com.pinterest.ktlint.test.KtlintDocumentationTest
import com.pinterest.ktlint.test.LintViolation
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -459,4 +460,43 @@ class BlankLineBeforeDeclarationRuleTest {
""".trimIndent()
blankLineBeforeDeclarationRuleAssertThat(code).hasNoLintViolations()
}

@KtlintDocumentationTest
fun `Issue 2284 - Given an object declaration preceded by another declaration`() {
val code =
"""
class C
data class DC(val v: Any)
interface I
object O
""".trimIndent()
val formattedCode =
"""
class C
data class DC(val v: Any)
interface I
object O
""".trimIndent()
blankLineBeforeDeclarationRuleAssertThat(code)
.hasLintViolations(
LintViolation(2, 1, "Expected a blank line for this declaration"),
LintViolation(3, 1, "Expected a blank line for this declaration"),
LintViolation(4, 1, "Expected a blank line for this declaration"),
).isFormattedAs(formattedCode)
}

@Test
fun `Issue 2284 - Given an object declaration wrapped in object literal`() {
val code =
"""
fun foo() =
object : Foo() {
// some declarations
}
""".trimIndent()
blankLineBeforeDeclarationRuleAssertThat(code).hasNoLintViolations()
}
}

0 comments on commit 95d95ee

Please sign in to comment.