-
Notifications
You must be signed in to change notification settings - Fork 512
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new experimental rule fun-keyword-spacing (#1362)
Lints and formats the spacing after the fun keyword. This rule is required to create a rule which can rewrite the function signature automatically as is described in #1341
- Loading branch information
1 parent
f7f7900
commit 14c0adf
Showing
5 changed files
with
79 additions
and
0 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
34 changes: 34 additions & 0 deletions
34
...mental/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/FunKeywordSpacingRule.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,34 @@ | ||
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.FUN_KEYWORD | ||
import com.pinterest.ktlint.core.ast.nextLeaf | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement | ||
|
||
/** | ||
* Lints and formats the spacing after the fun keyword | ||
*/ | ||
public class FunKeywordSpacingRule : Rule("fun-keyword-spacing") { | ||
override fun visit( | ||
node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit | ||
) { | ||
node | ||
.takeIf { it.elementType == FUN_KEYWORD } | ||
?.nextLeaf(includeEmpty = true) | ||
?.takeIf { it.elementType == ElementType.WHITE_SPACE && it.text != " " } | ||
?.let { whiteSpaceAfterFunKeyword -> | ||
emit( | ||
whiteSpaceAfterFunKeyword.startOffset, | ||
"Single space expected after the fun keyword", | ||
true | ||
) | ||
if (autoCorrect) { | ||
(whiteSpaceAfterFunKeyword as LeafPsiElement).rawReplaceWithText(" ") | ||
} | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...al/src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/FunKeywordSpacingRuleTest.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,42 @@ | ||
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 FunKeywordSpacingRuleTest { | ||
@Test | ||
fun `Given a function signature with multiple spaces between the fun keyword and the function name then remove the redundant spaces`() { | ||
val code = | ||
""" | ||
fun foo() = "some-result" | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
fun foo() = "some-result" | ||
""".trimIndent() | ||
assertThat(FunKeywordSpacingRule().lint(code)).containsExactly( | ||
LintError(1, 4, "fun-keyword-spacing", "Single space expected after the fun keyword") | ||
) | ||
assertThat(FunKeywordSpacingRule().format(code)).isEqualTo(formattedCode) | ||
} | ||
|
||
@Test | ||
fun `Given a function signature with a newline between the fun keyword and the function name then remove the redundant newline`() { | ||
val code = | ||
""" | ||
fun | ||
foo() = "some-result" | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
fun foo() = "some-result" | ||
""".trimIndent() | ||
assertThat(FunKeywordSpacingRule().lint(code)).containsExactly( | ||
LintError(1, 4, "fun-keyword-spacing", "Single space expected after the fun keyword") | ||
) | ||
assertThat(FunKeywordSpacingRule().format(code)).isEqualTo(formattedCode) | ||
} | ||
} |