-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract new rule no-blank-lines-in-chained-method-calls from no-conse…
…cutive-blank-lines (#1469, #1248) The 'no-consecutive-blank-lines' rule no longer disallows a single blank line in a chained method call. Multiple consecutive blank lines are still prohibited. The new rule 'no-blank-lines-in-chained-method-calls' also prohibits single blank lines in chained method call. Closes #1248
- Loading branch information
Showing
7 changed files
with
102 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
...main/kotlin/com/pinterest/ktlint/ruleset/standard/NoBlankLinesInChainedMethodCallsRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.pinterest.ktlint.ruleset.standard | ||
|
||
import com.pinterest.ktlint.core.Rule | ||
import com.pinterest.ktlint.core.ast.ElementType.DOT_QUALIFIED_EXPRESSION | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace | ||
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement | ||
|
||
public class NoBlankLinesInChainedMethodCallsRule : Rule("no-blank-lines-in-chained-method-calls") { | ||
override fun visit( | ||
node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit | ||
) { | ||
val isBlankLine = node is PsiWhiteSpace && node.getText().contains("\n\n") | ||
if (isBlankLine && node.treeParent.elementType == DOT_QUALIFIED_EXPRESSION) { | ||
emit(node.startOffset + 1, "Needless blank line(s)", true) | ||
|
||
if (autoCorrect) { | ||
(node as LeafPsiElement).rawReplaceWithText("\n" + node.getText().split("\n\n")[1]) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
.../kotlin/com/pinterest/ktlint/ruleset/standard/NoBlankLinesInChainedMethodCallsRuleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.pinterest.ktlint.ruleset.standard | ||
|
||
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThat | ||
import org.junit.jupiter.api.Test | ||
|
||
class NoBlankLinesInChainedMethodCallsRuleTest { | ||
private val noBlankLinesInChainedMethodCallsRuleAssertThat = NoBlankLinesInChainedMethodCallsRule().assertThat() | ||
|
||
@Test | ||
fun `single blank line in dot qualified expression returns lint error`() { | ||
val code = | ||
""" | ||
fun foo(inputText: String) { | ||
inputText | ||
.toLowerCase() | ||
} | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
fun foo(inputText: String) { | ||
inputText | ||
.toLowerCase() | ||
} | ||
""".trimIndent() | ||
noBlankLinesInChainedMethodCallsRuleAssertThat(code) | ||
.hasLintViolation(3, 1, "Needless blank line(s)") | ||
.isFormattedAs(formattedCode) | ||
} | ||
|
||
@Test | ||
fun `single blank line between statements does not return lint error`() { | ||
val code = | ||
""" | ||
fun foo(inputText: String) { | ||
bar() | ||
bar() | ||
} | ||
""".trimIndent() | ||
noBlankLinesInChainedMethodCallsRuleAssertThat(code).hasNoLintViolations() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters