diff --git a/CHANGELOG.md b/CHANGELOG.md index 6337c22b61..882fb000c3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Added - SARIF output support ([#1102](https://github.com/pinterest/ktlint/issues/1102)) ### Fixed +- Remove needless blank lines in dot qualified expression ([#1077](https://github.com/pinterest/ktlint/issues/1077)) - Fix false positives for SpacingBetweenDeclarationsWithAnnotationsRule ([#1125](https://github.com/pinterest/ktlint/issues/1125)) - Fix false positive with eol comment (`annotation-spacing`) ([#1124](https://github.com/pinterest/ktlint/issues/1124)) - Fix KtLint dependency variant selection ([#1114](https://github.com/pinterest/ktlint/issues/1114)) @@ -44,7 +45,7 @@ Thank you to [t-kameyama](https://github.com/t-kameyama) and [paul-dingemans](ht - Fix experimental:annotation-spacing-rule autocorrection with comments - Migrate from klob dependency and fix negated globs passed to CLI are no longer worked ([#999](https://github.com/pinterest/ktlint/issues/999)) **Breaking**: absolute paths globs will no longer work, check updated README - + ### Changed - Update Gradle shadow plugin to `6.1.0` version - Align with Kotlin plugin on how alias pattern is represented for imports layout rule ([#753](https://github.com/pinterest/ktlint/issues/753)) 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 ce95cb49bd..f4fb1093a5 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,6 +2,7 @@ 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 @@ -29,14 +30,15 @@ public class NoConsecutiveBlankLinesRule : Rule("no-consecutive-blank-lines") { val betweenClassAndPrimaryConstructor = prevNode.elementType == IDENTIFIER && prevNode.treeParent.elementType == CLASS && node.treeNext.elementType == PRIMARY_CONSTRUCTOR - if (lfcount > 2 || eof || betweenClassAndPrimaryConstructor) { + val inDotQualifiedExpression = node.treeParent.elementType == DOT_QUALIFIED_EXPRESSION && lfcount > 1 + if (lfcount > 2 || eof || betweenClassAndPrimaryConstructor || inDotQualifiedExpression) { 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) append("\n") + if (!eof && !betweenClassAndPrimaryConstructor && !inDotQualifiedExpression) 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 7f105b852e..4815433287 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 @@ -223,4 +223,27 @@ class NoConsecutiveBlankLinesRuleTest { ) ).isEmpty() } + + @Test + fun `should remove line in dot qualified expression`() { + assertThat( + NoConsecutiveBlankLinesRule().format( + """ + fun foo(inputText: String) { + inputText + + + .toLowerCase() + } + """.trimIndent() + ) + ).isEqualTo( + """ + fun foo(inputText: String) { + inputText + .toLowerCase() + } + """.trimIndent() + ) + } }