Skip to content
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

Replace all function bodies with body expressions in a single run #2395

Merged
merged 1 commit into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_STYLE_PROPE
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.MAX_LINE_LENGTH_PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.firstChildLeafOrSelf
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpace
import com.pinterest.ktlint.rule.engine.core.api.leavesIncludingSelf
import com.pinterest.ktlint.rule.engine.core.api.lastChildLeafOrSelf
import com.pinterest.ktlint.rule.engine.core.api.leavesInClosedRange
import com.pinterest.ktlint.rule.engine.core.api.nextSibling
import com.pinterest.ktlint.rule.engine.core.api.prevSibling
import com.pinterest.ktlint.ruleset.standard.StandardRule
Expand Down Expand Up @@ -111,7 +112,7 @@ public class FunctionExpressionBodyRule :
require(block.elementType == BLOCK)
block
.takeIf { it.containingOnly(RETURN) }
?.takeUnless { it.containsMultipleReturns() }
?.takeUnless { it.countReturnKeywords() > 1 }
?.findChildByType(RETURN)
?.findChildByType(RETURN_KEYWORD)
?.nextSibling { !it.isWhiteSpace() }
Expand Down Expand Up @@ -164,8 +165,9 @@ public class FunctionExpressionBodyRule :
.singleOrNull()
?.elementType

private fun ASTNode.containsMultipleReturns() =
firstChildLeafOrSelf().leavesIncludingSelf().count { it.elementType == RETURN_KEYWORD } > 1
private fun ASTNode.countReturnKeywords() =
leavesInClosedRange(this.firstChildLeafOrSelf(), this.lastChildLeafOrSelf())
.count { it.elementType == RETURN_KEYWORD }

private fun ASTNode.createUnitTypeReference() =
PsiFileFactory
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule
import com.pinterest.ktlint.test.LintViolation
import org.junit.jupiter.api.Test
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.ValueSource
Expand Down Expand Up @@ -168,4 +169,27 @@ class FunctionExpressionBodyRuleTest {
""".trimIndent()
functionExpressionBodyRule(code).hasNoLintViolations()
}

@Test
fun `Issue 2394 - Given multiple function bodies with a single return statement`() {
val code =
"""
fun foo1(): String {
return "foo1"
}
fun foo2(): String {
return "foo2"
}
""".trimIndent()
val formattedCode =
"""
fun foo1(): String = "foo1"
fun foo2(): String = "foo2"
""".trimIndent()
functionExpressionBodyRule(code)
.hasLintViolations(
LintViolation(1, 20, "Function body should be replaced with body expression"),
LintViolation(4, 20, "Function body should be replaced with body expression"),
).isFormattedAs(formattedCode)
}
}