Skip to content

Commit

Permalink
Fix adding blank line between declaration and an annotated declaratio…
Browse files Browse the repository at this point in the history
…n which is preceded by comment (#2429)

Closes #2416
  • Loading branch information
paul-dingemans authored Dec 11, 2023
1 parent ad879b8 commit ac870c5
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import com.pinterest.ktlint.rule.engine.core.api.children
import com.pinterest.ktlint.rule.engine.core.api.indent
import com.pinterest.ktlint.rule.engine.core.api.isPartOfComment
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpace
import com.pinterest.ktlint.rule.engine.core.api.prevLeaf
import com.pinterest.ktlint.rule.engine.core.api.nextLeaf
import com.pinterest.ktlint.rule.engine.core.api.prevCodeLeaf
import com.pinterest.ktlint.rule.engine.core.api.upsertWhitespaceBeforeMe
import com.pinterest.ktlint.ruleset.standard.StandardRule
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
Expand Down Expand Up @@ -46,7 +47,7 @@ public class SpacingBetweenDeclarationsWithAnnotationsRule : StandardRule("spaci
?.takeIf { it is KtDeclaration }
?.takeIf { prevDeclaration -> hasNoBlankLineBetweenDeclarations(node, prevDeclaration) }
?.let {
val prevLeaf = node.prevLeaf { it.isWhiteSpace() || it.isPartOfComment() }!!
val prevLeaf = node.prevCodeLeaf()?.nextLeaf { it.isWhiteSpace() }!!
emit(
prevLeaf.startOffset + 1,
"Declarations and declarations with annotations should have an empty space between.",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,90 @@ class SpacingBetweenDeclarationsWithAnnotationsRuleTest {
""".trimIndent()
spacingBetweenDeclarationsWithAnnotationsRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Issue 2416 - Given a declaration followed by an EOL comment and other annotated declaration`() {
val code =
"""
fun foobar() {
val bar = "bar"
// Some comment
@Suppress("unused")
val foo = "foo"
}
""".trimIndent()
val formattedCode =
"""
fun foobar() {
val bar = "bar"
// Some comment
@Suppress("unused")
val foo = "foo"
}
""".trimIndent()
spacingBetweenDeclarationsWithAnnotationsRuleAssertThat(code)
.hasLintViolation(3, 1, "Declarations and declarations with annotations should have an empty space between.")
.isFormattedAs(formattedCode)
}

@Test
fun `Issue 2416 - Given a declaration followed by a block comment and other annotated declaration`() {
val code =
"""
fun foobar() {
val bar = "bar"
/*
* Some comment
*/
@Suppress("unused")
val foo = "foo"
}
""".trimIndent()
val formattedCode =
"""
fun foobar() {
val bar = "bar"
/*
* Some comment
*/
@Suppress("unused")
val foo = "foo"
}
""".trimIndent()
spacingBetweenDeclarationsWithAnnotationsRuleAssertThat(code)
.hasLintViolation(3, 1, "Declarations and declarations with annotations should have an empty space between.")
.isFormattedAs(formattedCode)
}

@Test
fun `Issue 2416 - Given a declaration followed by a KDoc and other annotated declaration`() {
val code =
"""
fun foobar() {
val bar = "bar"
/**
* Some comment
*/
@Suppress("unused")
val foo = "foo"
}
""".trimIndent()
val formattedCode =
"""
fun foobar() {
val bar = "bar"
/**
* Some comment
*/
@Suppress("unused")
val foo = "foo"
}
""".trimIndent()
spacingBetweenDeclarationsWithAnnotationsRuleAssertThat(code)
.hasLintViolation(3, 1, "Declarations and declarations with annotations should have an empty space between.")
.isFormattedAs(formattedCode)
}
}

0 comments on commit ac870c5

Please sign in to comment.