Skip to content

Commit

Permalink
Do not enforce multiline function signature when function has no para…
Browse files Browse the repository at this point in the history
…meters

Closes: #1773

Co-authored-by: Jeroen Tietema <jeroen.tietema@siriusxm.com>
Co-authored-by: paul-dingemans <paul-dingemans@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 15, 2023
1 parent 480dd87 commit f339e19
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
### Removed

### Fixed
* A function signature not having any parameters which exceeds the `max-line-length` should be ignored by rule `function-signature` ([#1773](https://github.com/pinterest/ktlint/issues/1773))

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,17 @@ public class FunctionSignatureRule :
node.containsParameterPrecededByAnnotationOnSeparateLine()
if (isMaxLineLengthSet()) {
val singleLineFunctionSignatureLength = calculateFunctionSignatureLengthAsSingleLineSignature(node, emit, autoCorrect)
if (forceMultilineSignature ||
singleLineFunctionSignatureLength > maxLineLength ||
node.hasMinimumNumberOfParameters()
) {
// Function signatures not having parameters, should not be reformatted automatically. It would result in function signatures
// like below, which are not acceptable:
// fun aVeryLongFunctionName(
// ) = "some-value"
//
// fun aVeryLongFunctionName(
// ): SomeVeryLongTypeName =
// SomeVeryLongTypeName(...)
// Leave it up to the max-line-length rule to detect those violations so that the developer can handle it manually.
val rewriteFunctionSignatureWithParameters = node.countParameters() > 0 && singleLineFunctionSignatureLength > maxLineLength
if (forceMultilineSignature || rewriteFunctionSignatureWithParameters) {
fixWhiteSpacesInValueParameterList(node, emit, autoCorrect, multiline = true, dryRun = false)
// Due to rewriting the function signature, the remaining length on the last line of the multiline
// signature needs to be recalculated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,49 @@ class FunctionSignatureRuleTest {
.hasNoLintViolations()
}

@Nested
inner class `Issue 1773 - Given a unction signature without parameters exceeding the max line length` {
@Test
fun `Given a function name between backticks and expression body`() {
val code =
"""
// $MAX_LINE_LENGTH_MARKER $EOL_CHAR
fun `a very long function name as found in a test case`() =
"some-result"
""".trimIndent()
functionSignatureWrappingRuleAssertThat(code)
.setMaxLineLength()
.hasNoLintViolations()
}

@Test
fun `Given a function name not between backticks and expression body`() {
val code =
"""
// $MAX_LINE_LENGTH_MARKER $EOL_CHAR
fun aVeryLongFunctionNameAsFoundInATestCase() =
"some-result"
""".trimIndent()
functionSignatureWrappingRuleAssertThat(code)
.setMaxLineLength()
.hasNoLintViolations()
}

@Test
fun `Given a function name not between backticks and a return type and body block`() {
val code =
"""
// $MAX_LINE_LENGTH_MARKER $EOL_CHAR
fun aVeryLongFunctionNameAsFoundInATestCase(): String {
return "some-result"
}
""".trimIndent()
functionSignatureWrappingRuleAssertThat(code)
.setMaxLineLength()
.hasNoLintViolations()
}
}

private companion object {
const val UNEXPECTED_SPACES = " "
const val NO_SPACE = ""
Expand Down

0 comments on commit f339e19

Please sign in to comment.