Skip to content

Commit

Permalink
1077 needless line dot qualified expression (#1105)
Browse files Browse the repository at this point in the history
* Lint/format needless blank lines in dot qualified expression (#1077)

* Lint/format needless blank lines in dot qualified expression (#1077)

Co-authored-by: Paul Dingemans <pdingemans@bol.com>
Co-authored-by: Roman Zavarnitsyn <rom4ek93@gmail.com>
Co-authored-by: Yahor Berdnikau <egorr.berd@gmail.com>
  • Loading branch information
4 people committed Jun 23, 2021
1 parent f5b63a3 commit 27ea0a1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
}
}

0 comments on commit 27ea0a1

Please sign in to comment.