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

Spacing between function name and the opening parenthesis #1409

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
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,21 @@
All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](https://semver.org/).

## Unreleased
## Unreleased (0.46.0)

### API Changes & RuleSet providers

### Added
- Add experimental rule for unexpected spacing between function name and opening parenthesis (`spacing-between-function-name-and-opening-parenthesis`) ([#1341](https://github.com/pinterest/ktlint/issues/1341))

### Fixed

### Changed

### Removed


## Unreleased (0.45.0)

### API Changes & RuleSet providers

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ by passing the `--experimental` flag to `ktlint`.
- `experimental:annotation-spacing`: Annotations should be separated by the annotated declaration by a single line break
- `experimental:double-colon-spacing`: No spaces around `::`
- `experimental:fun-keyword-spacing`: Consistent spacing after the fun keyword
- `experimental:spacing-between-function-name-and-opening-parenthesis`: Consistent spacing between function name and opening parenthesis
- `experimental:function-type-reference-spacing`: Consistent spacing in the type reference before a function
- `experimental:modifier-list-spacing`: Consistent spacing between modifiers in and after the last modifier in a modifier list
- `experimental:spacing-around-angle-brackets`: No spaces around angle brackets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class ExperimentalRuleSetProvider : RuleSetProvider {
FunctionTypeReferenceSpacingRule(),
ModifierListSpacingRule(),
CommentWrappingRule(),
KdocWrappingRule()
KdocWrappingRule(),
SpacingBetweenFunctionNameAndOpeningParenthesisRule()
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.pinterest.ktlint.ruleset.experimental

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
import com.pinterest.ktlint.core.ast.nextSibling
import org.jetbrains.kotlin.com.intellij.lang.ASTNode

public class SpacingBetweenFunctionNameAndOpeningParenthesisRule : Rule("spacing-between-function-name-and-opening-parenthesis") {
override fun visit(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
node
.takeIf { node.elementType == ElementType.FUN }
?.findChildByType(ElementType.IDENTIFIER)
?.nextSibling { true }
?.takeIf { it.elementType == WHITE_SPACE }
?.let { whiteSpace ->
emit(whiteSpace.startOffset, "Unexpected whitespace", true)
if (autoCorrect) {
whiteSpace.treeParent.removeChild(whiteSpace)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.pinterest.ktlint.ruleset.experimental

import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThat
import org.junit.jupiter.api.Test

class SpacingBetweenFunctionNameAndOpeningParenthesisRuleTest {
private val spacingBetweenFunctionNameAndOpeningParenthesisRuleAssertThat = SpacingBetweenFunctionNameAndOpeningParenthesisRule().assertThat()

@Test
fun `Given a function signature without whitespace between function name and opening parenthesis then do not reformat`() {
val code =
"""
fun foo() = "foo"
""".trimIndent()
spacingBetweenFunctionNameAndOpeningParenthesisRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Given a function signature with one or more spaces between function name and opening parenthesis then do not reformat`() {
val code =
"""
fun foo () = "foo"
""".trimIndent()
val formattedCode =
"""
fun foo() = "foo"
""".trimIndent()
spacingBetweenFunctionNameAndOpeningParenthesisRuleAssertThat(code)
.hasLintViolation(1, 8, "Unexpected whitespace")
.isFormattedAs(formattedCode)
}

@Test
fun `Given a function signature with one or more new lines between function name and opening parenthesis then do not reformat`() {
val code =
"""
fun foo
() = "foo"
""".trimIndent()
val formattedCode =
"""
fun foo() = "foo"
""".trimIndent()
spacingBetweenFunctionNameAndOpeningParenthesisRuleAssertThat(code)
.hasLintViolation(1, 8, "Unexpected whitespace")
.isFormattedAs(formattedCode)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,10 @@ class TypeParameterListSpacingRuleTest {
"""
typealias Bar<T> = () -> T
""".trimIndent()
assertThat(TypeParameterListSpacingRule().lint(code)).containsExactly(
LintError(1, 14, "type-parameter-list-spacing", "No whitespace expected at this position"),
LintError(1, 19, "type-parameter-list-spacing", "Expected a single space")
)
assertThat(TypeParameterListSpacingRule().format(code)).isEqualTo(formattedCode)
typeParameterListSpacingRuleAssertThat(code)
.hasLintViolations(
LintViolation(1, 14, "No whitespace expected at this position"),
LintViolation(1, 19, "Expected a single space")
).isFormattedAs(formattedCode)
}
}