Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force blank line before object declaration #2287

Merged
merged 2 commits into from
Oct 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
}
}