Skip to content

Commit

Permalink
Fix false positive with elvis operator and comment (chain-wrapping)
Browse files Browse the repository at this point in the history
  • Loading branch information
t-kameyama authored and Tapchicoma committed Jul 4, 2021
1 parent 15b98b0 commit da19fe3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Fix false positive in lambda in dot qualified expression (`argument-list-wrapping`) ([#1112](https://github.com/pinterest/ktlint/issues/1112))
- Fix false positive with multiline expression with elvis operator in assignment (`indent`) ([#1165](https://github.com/pinterest/ktlint/issues/1165))
- Ignore backticks in imports for ordering purposes (`import-ordering`) ([#1106](https://github.com/pinterest/ktlint/issues/1106))
- Fix false positive with elvis operator and comment (`chain-wrapping`) ([#1055](https://github.com/pinterest/ktlint/issues/1055))
### Changed
- Updated to dokka 1.4.32 ([#1148](https://github.com/pinterest/ktlint/pull/1148))
- Updated Kotlin to 1.5.20 version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import com.pinterest.ktlint.core.ast.ElementType.SAFE_ACCESS
import com.pinterest.ktlint.core.ast.ElementType.WHEN_CONDITION_WITH_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
import com.pinterest.ktlint.core.ast.isPartOfComment
import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline
import com.pinterest.ktlint.core.ast.isWhiteSpaceWithoutNewline
import com.pinterest.ktlint.core.ast.nextCodeLeaf
import com.pinterest.ktlint.core.ast.nextLeaf
import com.pinterest.ktlint.core.ast.prevCodeLeaf
Expand All @@ -30,6 +32,7 @@ import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafElement
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.psiUtil.leaves

class ChainWrappingRule : Rule("chain-wrapping") {

Expand All @@ -54,9 +57,7 @@ class ChainWrappingRule : Rule("chain-wrapping") {
return
}
val nextLeaf = node.nextCodeLeaf()?.prevLeaf()
if (nextLeaf?.elementType == WHITE_SPACE &&
nextLeaf.textContains('\n')
) {
if (nextLeaf.isWhiteSpaceWithNewline() && !node.isElvisOperatorAndComment()) {
emit(node.startOffset, "Line must not end with \"${node.text}\"", true)
if (autoCorrect) {
// rewriting
Expand Down Expand Up @@ -129,4 +130,9 @@ class ChainWrappingRule : Rule("chain-wrapping") {

private fun ASTNode.isPartOfWhenCondition() =
treeParent?.treeParent?.treeParent?.elementType == WHEN_CONDITION_WITH_EXPRESSION

private fun ASTNode.isElvisOperatorAndComment(): Boolean {
return elementType == ELVIS &&
leaves().takeWhile { it.isWhiteSpaceWithoutNewline() || it.isPartOfComment() }.any()
}
}
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.test.diffFileFormat
import com.pinterest.ktlint.test.diffFileLint
import com.pinterest.ktlint.test.lint
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test

Expand All @@ -21,4 +22,26 @@ class ChainWrappingRuleTest {
)
).isEmpty()
}

// https://github.com/pinterest/ktlint/issues/1055
@Test
fun `lint elvis operator and comment`() {
assertThat(
ChainWrappingRule().lint(
"""
fun test(): Int {
val foo = foo()
?: // Comment
return bar()
return baz()
}
fun foo(): Int? = null
fun bar(): Int = 1
fun baz(): Int = 2
""".trimIndent()
)
).isEmpty()
}
}

0 comments on commit da19fe3

Please sign in to comment.