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

NoConsecutiveBlankLinesRule: report blank line between class name and primary constructor #1005

Merged
merged 1 commit into from
Dec 12, 2020
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
@@ -1,6 +1,9 @@
package com.pinterest.ktlint.ruleset.standard

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType.CLASS
import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER
import com.pinterest.ktlint.core.ast.ElementType.PRIMARY_CONSTRUCTOR
import com.pinterest.ktlint.core.ast.nextLeaf
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace
Expand All @@ -20,12 +23,21 @@ class NoConsecutiveBlankLinesRule : Rule("no-consecutive-blank-lines") {
return
}
val eof = node.nextLeaf() == null
if (lfcount > 2 || eof) {
val prevNode = node.treePrev
val betweenClassAndPrimaryConstructor = prevNode.elementType == IDENTIFIER &&
prevNode.treeParent.elementType == CLASS &&
node.treeNext.elementType == PRIMARY_CONSTRUCTOR
if (lfcount > 2 || eof || betweenClassAndPrimaryConstructor) {
val split = text.split("\n")
emit(node.startOffset + split[0].length + split[1].length + 2, "Needless blank line(s)", true)
if (autoCorrect) {
(node as LeafPsiElement)
.rawReplaceWithText("${split.first()}\n${if (eof) "" else "\n"}${split.last()}")
val newText = buildString {
append(split.first())
append("\n")
if (!eof && !betweenClassAndPrimaryConstructor) append("\n")
append(split.last())
}
(node as LeafPsiElement).rawReplaceWithText(newText)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class NoConsecutiveBlankLinesRuleTest {
NoConsecutiveBlankLinesRule().lint(
"""
package com.test

fun main() {
}
""".trimIndent()
Expand Down Expand Up @@ -150,4 +150,61 @@ class NoConsecutiveBlankLinesRuleTest {
assertThat(NoConsecutiveBlankLinesRule().format("class A\n\n")).isEqualTo("class A\n")
assertThat(NoConsecutiveBlankLinesRule().format("class A\n")).isEqualTo("class A\n")
}

@Test
fun `test two line breaks between class name and primary constructor`() {
assertThat(
NoConsecutiveBlankLinesRule().format(
"""
class A

constructor(a: Int)

class B

private constructor(b: Int)
""".trimIndent()
)
).isEqualTo(
"""
class A
constructor(a: Int)

class B
private constructor(b: Int)
""".trimIndent()
)
}

@Test
fun `test three line breaks between class name and primary constructor`() {
assertThat(
NoConsecutiveBlankLinesRule().format(
"""
class A


constructor(a: Int)
""".trimIndent()
)
).isEqualTo(
"""
class A
constructor(a: Int)
""".trimIndent()
)
}

@Test
fun `test two line breaks between comment and primary constructor`() {
assertThat(
NoConsecutiveBlankLinesRule().lint(
"""
class A // comment

constructor(a: Int)
""".trimIndent()
)
).isEmpty()
}
}