forked from pinterest/ktlint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforce consistent spacing (e.g. no whitespace element at all) betwee…
…n function name and the opening parenthesis Includes cleanup of AnnotationSpacingRuleTest as it broke due to existence of test which was not related to annotation spacing. Also removed the unneeded blank final line in code examples which are linted and formatted. This rule is required for implementing the function signature rewrite rule pinterest#1341
- Loading branch information
1 parent
4646371
commit 5b9587d
Showing
6 changed files
with
97 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...terest/ktlint/ruleset/experimental/SpacingBetweenFunctionNameAndOpeningParenthesisRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
...st/ktlint/ruleset/experimental/SpacingBetweenFunctionNameAndOpeningParenthesisRuleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.pinterest.ktlint.ruleset.experimental | ||
|
||
import com.pinterest.ktlint.core.LintError | ||
import com.pinterest.ktlint.test.format | ||
import com.pinterest.ktlint.test.lint | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.Test | ||
|
||
class SpacingBetweenFunctionNameAndOpeningParenthesisRuleTest { | ||
@Test | ||
fun `Given a function signature without whitespace between function name and opening parenthesis then do not reformat`() { | ||
val code = | ||
""" | ||
fun foo() = "foo" | ||
""".trimIndent() | ||
assertThat(SpacingBetweenFunctionNameAndOpeningParenthesisRule().lint(code)).isEmpty() | ||
assertThat(SpacingBetweenFunctionNameAndOpeningParenthesisRule().format(code)).isEqualTo(code) | ||
} | ||
|
||
@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() | ||
assertThat(SpacingBetweenFunctionNameAndOpeningParenthesisRule().lint(code)).containsExactly( | ||
LintError(1, 8, "spacing-between-function-name-and-opening-parenthesis", "Unexpected whitespace") | ||
) | ||
assertThat(SpacingBetweenFunctionNameAndOpeningParenthesisRule().format(code)).isEqualTo(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() | ||
assertThat(SpacingBetweenFunctionNameAndOpeningParenthesisRule().lint(code)).containsExactly( | ||
LintError(1, 8, "spacing-between-function-name-and-opening-parenthesis", "Unexpected whitespace") | ||
) | ||
assertThat(SpacingBetweenFunctionNameAndOpeningParenthesisRule().format(code)).isEqualTo(formattedCode) | ||
} | ||
} |