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

Fix adding blank line between declaration and an annotated declaration which is preceded by comment #2429

Merged
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
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)
}
}