-
Notifications
You must be signed in to change notification settings - Fork 506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow a single whitespace line in chained expressions #1469
Merged
paul-dingemans
merged 10 commits into
pinterest:master
from
seadowg:fix-comments-in-chain
May 25, 2022
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9d3024c
Allow one whitespace line in chained expressions
seadowg cc21285
Update CHANGELOG
seadowg 4cd2566
Add new empty rule to standard set
seadowg 4828472
Don't allow blank lines in chained method calls
seadowg 7a00b36
Remove blank line from chained methods
seadowg afd6846
Only format when autoCorrect is true
seadowg ae7202f
Add new rule to README
seadowg 2eb7c87
Update changelog
seadowg aefb60c
Refactor test
5fb5428
Fix changelog and readme
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
47 changes: 47 additions & 0 deletions
47
.../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,47 @@ | ||
package com.pinterest.ktlint.ruleset.standard | ||
|
||
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThat | ||
import com.pinterest.ktlint.test.LintViolation | ||
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() | ||
|
||
noBlankLinesInChainedMethodCallsRuleAssertThat(code) | ||
.hasLintViolations(LintViolation(3, 1, "Needless blank line(s)")) | ||
.isFormattedAs( | ||
""" | ||
fun foo(inputText: String) { | ||
inputText | ||
.toLowerCase() | ||
} | ||
""".trimIndent() | ||
) | ||
} | ||
|
||
@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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume we need this check, but I couldn't find a good way to write a test that drives it out. Help would be appreciated!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you mean
if (autoCorrect) {
then you have already done so in test casesingle blank line in dot qualified expression returns lint error
.When calling
hasLintViolations
, the rule engine is executed withautocorrect
set tofalse
which results in emitting the error without fixing it. When calling 'isFormattedAs, the rule engine is invoked with
autocorrectset to
true` resulting in formattting the code.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah right. But, if I remove the
if
statement (and just always remove the blank line), I don't see a failure. I'm assuming we don't want to be messing around with the code whenautocorrect
isfalse
. Maybe we need to add a check tohasLintViolations
to check that the code is unchanged?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@paul-dingemans thanks for merging, but I'm still a little concerned there's no coverage for the
if (autocorrect)
check. I'd be happy to look at that in the future if you agree it's an issue?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you think that there is no coverage for the line? As I explained, the line is already covered. You can easily check it out by placing a breakpoint inside the if-statement (line 20) and then run test
single blank line in dot qualified expression returns lint error
in debug mode.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, being more specific, I mean that it doesn't have "branch" coverage - it does indeed have "line" coverage. If I change the
if
statement to just beif (true)
for example (or just remove theif
and keep therawReplaceWithText
), the tests will still all pass. There's no test that ensures that we only run reformat whenautoCorrect
istrue
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't have to worry about that. There is not a single rule for which this is being tested.