From 9d3024cc0b24f890b9d43d3903fc13d98b7ea3a7 Mon Sep 17 00:00:00 2001 From: Callum Stott Date: Mon, 16 May 2022 14:08:44 +0100 Subject: [PATCH] Allow one whitespace line in chained expressions --- .../standard/NoConsecutiveBlankLinesRule.kt | 8 ++-- .../NoConsecutiveBlankLinesRuleTest.kt | 41 +++++++++++++------ 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/NoConsecutiveBlankLinesRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/NoConsecutiveBlankLinesRule.kt index f4fb1093a5..174a023dd9 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/NoConsecutiveBlankLinesRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/NoConsecutiveBlankLinesRule.kt @@ -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 @@ -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) diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/NoConsecutiveBlankLinesRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/NoConsecutiveBlankLinesRuleTest.kt index eb4e425a8c..8979fa173e 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/NoConsecutiveBlankLinesRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/NoConsecutiveBlankLinesRuleTest.kt @@ -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 @@ -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() + ) } }