Skip to content

Commit 647e795

Browse files
Replace all function bodies with body expressions in a single run (#2395)
If a file contains multiple function bodies that have to be replaced with a body expression, then all should be replaced with a single run of ktlint. Closes #2394
1 parent 6617166 commit 647e795

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionExpressionBodyRule.kt

+6-4
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_STYLE_PROPE
2323
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.MAX_LINE_LENGTH_PROPERTY
2424
import com.pinterest.ktlint.rule.engine.core.api.firstChildLeafOrSelf
2525
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpace
26-
import com.pinterest.ktlint.rule.engine.core.api.leavesIncludingSelf
26+
import com.pinterest.ktlint.rule.engine.core.api.lastChildLeafOrSelf
27+
import com.pinterest.ktlint.rule.engine.core.api.leavesInClosedRange
2728
import com.pinterest.ktlint.rule.engine.core.api.nextSibling
2829
import com.pinterest.ktlint.rule.engine.core.api.prevSibling
2930
import com.pinterest.ktlint.ruleset.standard.StandardRule
@@ -111,7 +112,7 @@ public class FunctionExpressionBodyRule :
111112
require(block.elementType == BLOCK)
112113
block
113114
.takeIf { it.containingOnly(RETURN) }
114-
?.takeUnless { it.containsMultipleReturns() }
115+
?.takeUnless { it.countReturnKeywords() > 1 }
115116
?.findChildByType(RETURN)
116117
?.findChildByType(RETURN_KEYWORD)
117118
?.nextSibling { !it.isWhiteSpace() }
@@ -164,8 +165,9 @@ public class FunctionExpressionBodyRule :
164165
.singleOrNull()
165166
?.elementType
166167

167-
private fun ASTNode.containsMultipleReturns() =
168-
firstChildLeafOrSelf().leavesIncludingSelf().count { it.elementType == RETURN_KEYWORD } > 1
168+
private fun ASTNode.countReturnKeywords() =
169+
leavesInClosedRange(this.firstChildLeafOrSelf(), this.lastChildLeafOrSelf())
170+
.count { it.elementType == RETURN_KEYWORD }
169171

170172
private fun ASTNode.createUnitTypeReference() =
171173
PsiFileFactory

ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionExpressionBodyRuleTest.kt

+24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.pinterest.ktlint.ruleset.standard.rules
22

33
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule
4+
import com.pinterest.ktlint.test.LintViolation
45
import org.junit.jupiter.api.Test
56
import org.junit.jupiter.params.ParameterizedTest
67
import org.junit.jupiter.params.provider.ValueSource
@@ -168,4 +169,27 @@ class FunctionExpressionBodyRuleTest {
168169
""".trimIndent()
169170
functionExpressionBodyRule(code).hasNoLintViolations()
170171
}
172+
173+
@Test
174+
fun `Issue 2394 - Given multiple function bodies with a single return statement`() {
175+
val code =
176+
"""
177+
fun foo1(): String {
178+
return "foo1"
179+
}
180+
fun foo2(): String {
181+
return "foo2"
182+
}
183+
""".trimIndent()
184+
val formattedCode =
185+
"""
186+
fun foo1(): String = "foo1"
187+
fun foo2(): String = "foo2"
188+
""".trimIndent()
189+
functionExpressionBodyRule(code)
190+
.hasLintViolations(
191+
LintViolation(1, 20, "Function body should be replaced with body expression"),
192+
LintViolation(4, 20, "Function body should be replaced with body expression"),
193+
).isFormattedAs(formattedCode)
194+
}
171195
}

0 commit comments

Comments
 (0)