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

Add missing space between fun keyword and identifier when latter is wrapped between backticks #2890

Merged
merged 1 commit into from
Dec 3, 2024
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 @@ -12,6 +12,7 @@ import com.pinterest.ktlint.rule.engine.core.api.nextLeaf
import com.pinterest.ktlint.ruleset.standard.StandardRule
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl

/**
* Lints and formats the spacing after the fun keyword
Expand All @@ -26,14 +27,30 @@ public class FunKeywordSpacingRule : StandardRule("fun-keyword-spacing") {
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,
).ifAutocorrectAllowed {
(whiteSpaceAfterFunKeyword as LeafPsiElement).rawReplaceWithText(" ")
?.let { leafAfterFunKeyword ->
when {
leafAfterFunKeyword.elementType == ElementType.WHITE_SPACE && leafAfterFunKeyword.text != " " -> {
emit(
leafAfterFunKeyword.startOffset,
"Single space expected after the fun keyword",
true,
).ifAutocorrectAllowed {
(leafAfterFunKeyword as LeafPsiElement).rawReplaceWithText(" ")
}
}

leafAfterFunKeyword.elementType == ElementType.IDENTIFIER -> {
// Identifier can only be adjacent to fun keyword in case the identifier is wrapped between backticks:
// fun`foo`() {}
emit(leafAfterFunKeyword.startOffset, "Space expected between the fun keyword and backtick", true)
.ifAutocorrectAllowed {
leafAfterFunKeyword.treeParent.addChild(PsiWhiteSpaceImpl(" "), leafAfterFunKeyword)
}
}

else -> {
Unit
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,19 @@ class FunKeywordSpacingRuleTest {
.hasLintViolation(1, 4, "Single space expected after the fun keyword")
.isFormattedAs(formattedCode)
}

@Test
fun `Issue 2879 - Given a function with name between backticks then the fun keyword and name should be separated by a space`() {
val code =
"""
fun`foo or bar`() = "foo"
""".trimIndent()
val formattedCode =
"""
fun `foo or bar`() = "foo"
""".trimIndent()
funKeywordSpacingRuleAssertThat(code)
.hasLintViolation(1, 4, "Space expected between the fun keyword and backtick")
.isFormattedAs(formattedCode)
}
}
Loading