diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingBetweenDeclarationsWithAnnotationsRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingBetweenDeclarationsWithAnnotationsRule.kt index 31e58179b7..703a0b131e 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingBetweenDeclarationsWithAnnotationsRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingBetweenDeclarationsWithAnnotationsRule.kt @@ -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 @@ -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.", diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingBetweenDeclarationsWithAnnotationsRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingBetweenDeclarationsWithAnnotationsRuleTest.kt index f4e0e9f2fd..72e3a4edaa 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingBetweenDeclarationsWithAnnotationsRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingBetweenDeclarationsWithAnnotationsRuleTest.kt @@ -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) + } }