Skip to content

Commit

Permalink
Force blank line before object declaration if preceded by another dec…
Browse files Browse the repository at this point in the history
…laration

Closes #2284
  • Loading branch information
paul-dingemans committed Sep 30, 2023
1 parent a679dd4 commit 715b8c6
Show file tree
Hide file tree
Showing 3 changed files with 31 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,7 @@ 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.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 +49,7 @@ public class BlankLineBeforeDeclarationRule :
CLASS,
CLASS_INITIALIZER,
FUN,
OBJECT_DECLARATION,
PROPERTY,
PROPERTY_ACCESSOR,
->
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,31 @@ 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)
}
}

0 comments on commit 715b8c6

Please sign in to comment.