Skip to content

Commit

Permalink
Allow one whitespace line in chained expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
seadowg committed May 16, 2022
1 parent 504162c commit 9d3024c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ 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.DOT_QUALIFIED_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER
import com.pinterest.ktlint.core.ast.ElementType.PRIMARY_CONSTRUCTOR
import com.pinterest.ktlint.core.ast.nextLeaf
Expand All @@ -25,20 +24,21 @@ public class NoConsecutiveBlankLinesRule : Rule("no-consecutive-blank-lines") {
if (lfcount < 2) {
return
}

val eof = node.nextLeaf() == null
val prevNode = node.treePrev
val betweenClassAndPrimaryConstructor = prevNode.elementType == IDENTIFIER &&
prevNode.treeParent.elementType == CLASS &&
node.treeNext.elementType == PRIMARY_CONSTRUCTOR
val inDotQualifiedExpression = node.treeParent.elementType == DOT_QUALIFIED_EXPRESSION && lfcount > 1
if (lfcount > 2 || eof || betweenClassAndPrimaryConstructor || inDotQualifiedExpression) {

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) {
val newText = buildString {
append(split.first())
append("\n")
if (!eof && !betweenClassAndPrimaryConstructor && !inDotQualifiedExpression) 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 @@ -3,7 +3,6 @@ package com.pinterest.ktlint.ruleset.standard
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThat
import com.pinterest.ktlint.test.LintViolation
import com.pinterest.ktlint.test.MULTILINE_STRING_QUOTE
import com.pinterest.ktlint.test.format
import com.pinterest.ktlint.test.lint
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Nested
Expand Down Expand Up @@ -176,25 +175,41 @@ class NoConsecutiveBlankLinesRuleTest {
}

@Test
fun `should remove line in dot qualified expression`() {
assertThat(
NoConsecutiveBlankLinesRule().format(
"""
fun foo(inputText: String) {
inputText
fun `Given single blank line in dot qualified expression should not return lint errors`() {
val code =
"""
fun foo(inputText: String) {
inputText
.toLowerCase()
}
""".trimIndent()

.toLowerCase()
}
""".trimIndent()
)
).isEqualTo(
noConsecutiveBlankLinesRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Given multiple blank line in dot qualified expression should return lint error`() {
val code =
"""
fun foo(inputText: String) {
inputText
.toLowerCase()
}
""".trimIndent()
)

noConsecutiveBlankLinesRuleAssertThat(code)
.hasLintViolations(LintViolation(4, 1, "Needless blank line(s)"))
.isFormattedAs(
"""
fun foo(inputText: String) {
inputText
.toLowerCase()
}
""".trimIndent()
)
}
}

0 comments on commit 9d3024c

Please sign in to comment.