-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
function-type-modifier-spacing
rule (#2216)
Closes #2202 Co-authored-by: paul-dingemans <paul-dingemans@users.noreply.github.com>
- Loading branch information
1 parent
be54aa4
commit 231ad6f
Showing
6 changed files
with
211 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
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
44 changes: 44 additions & 0 deletions
44
...ain/kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionTypeModifierSpacingRule.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,44 @@ | ||
package com.pinterest.ktlint.ruleset.standard.rules | ||
|
||
import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUNCTION_TYPE | ||
import com.pinterest.ktlint.rule.engine.core.api.ElementType.MODIFIER_LIST | ||
import com.pinterest.ktlint.rule.engine.core.api.ElementType.WHITE_SPACE | ||
import com.pinterest.ktlint.rule.engine.core.api.Rule | ||
import com.pinterest.ktlint.rule.engine.core.api.RuleId | ||
import com.pinterest.ktlint.rule.engine.core.api.nextCodeSibling | ||
import com.pinterest.ktlint.rule.engine.core.api.prevSibling | ||
import com.pinterest.ktlint.rule.engine.core.api.upsertWhitespaceBeforeMe | ||
import com.pinterest.ktlint.ruleset.standard.StandardRule | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
|
||
/** | ||
* Lints and formats a single space between the modifier list and the function type | ||
*/ | ||
public class FunctionTypeModifierSpacingRule : | ||
StandardRule("function-type-modifier-spacing"), | ||
Rule.Experimental { | ||
override fun beforeVisitChildNodes( | ||
node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit, | ||
) { | ||
node | ||
.takeIf { it.elementType == MODIFIER_LIST } | ||
?.nextCodeSibling() | ||
?.takeIf { it.elementType == FUNCTION_TYPE } | ||
?.takeUnless { it.isPrecededBySingleSpace() } | ||
?.let { functionTypeNode -> | ||
emit(functionTypeNode.startOffset, "Expected a single space between the modifier list and the function type", true) | ||
if (autoCorrect) { | ||
functionTypeNode.upsertWhitespaceBeforeMe(" ") | ||
} | ||
} | ||
} | ||
|
||
private fun ASTNode.isPrecededBySingleSpace(): Boolean = | ||
prevSibling() | ||
?.let { it.elementType == WHITE_SPACE && it.text == " " } | ||
?: false | ||
} | ||
|
||
public val FUNCTION_TYPE_MODIFIER_SPACING_RULE: RuleId = FunctionTypeModifierSpacingRule().ruleId |
135 changes: 135 additions & 0 deletions
135
...kotlin/com/pinterest/ktlint/ruleset/standard/rules/FunctionTypeModifierSpacingRuleTest.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,135 @@ | ||
package com.pinterest.ktlint.ruleset.standard.rules | ||
|
||
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule | ||
import org.junit.jupiter.api.Nested | ||
import org.junit.jupiter.api.Test | ||
|
||
class FunctionTypeModifierSpacingRuleTest { | ||
private val assertThatRule = assertThatRule { FunctionTypeModifierSpacingRule() } | ||
|
||
@Nested | ||
inner class `Missing space before the function type` { | ||
@Test | ||
fun `Given no space between the modifier list and the function property type`() { | ||
val code = | ||
""" | ||
val foo: suspend() -> Unit = {} | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
val foo: suspend () -> Unit = {} | ||
""".trimIndent() | ||
assertThatRule(code) | ||
.hasLintViolation(1, 17, "Expected a single space between the modifier list and the function type") | ||
.isFormattedAs(formattedCode) | ||
} | ||
|
||
@Test | ||
fun `Given no space between the modifier list and the function parameter type`() { | ||
val code = | ||
""" | ||
suspend fun bar(baz: suspend() -> Unit) = baz() | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
suspend fun bar(baz: suspend () -> Unit) = baz() | ||
""".trimIndent() | ||
assertThatRule(code) | ||
.hasLintViolation(1, 29, "Expected a single space between the modifier list and the function type") | ||
.isFormattedAs(formattedCode) | ||
} | ||
} | ||
|
||
@Nested | ||
inner class `Exactly one space before the function type` { | ||
@Test | ||
fun `Given a single space between the modifier list and the function property type`() { | ||
val code = | ||
""" | ||
val foo: suspend () -> Unit = {} | ||
""".trimIndent() | ||
assertThatRule(code).hasNoLintViolations() | ||
} | ||
|
||
@Test | ||
fun `Given a single space between the modifier list and the function parameter type`() { | ||
val code = | ||
""" | ||
suspend fun bar(baz: suspend () -> Unit) = baz() | ||
""".trimIndent() | ||
assertThatRule(code).hasNoLintViolations() | ||
} | ||
} | ||
|
||
@Nested | ||
inner class `Too many spaces before the function type` { | ||
@Test | ||
fun `Given multiple spaces between the modifier list and the function property type`() { | ||
val code = | ||
""" | ||
val foo: suspend () -> Unit = {} | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
val foo: suspend () -> Unit = {} | ||
""".trimIndent() | ||
assertThatRule(code) | ||
.hasLintViolation(1, 19, "Expected a single space between the modifier list and the function type") | ||
.isFormattedAs(formattedCode) | ||
} | ||
|
||
@Test | ||
fun `Given multiple spaces between the modifier list and the function parameter type`() { | ||
val code = | ||
""" | ||
suspend fun bar(baz: suspend () -> Unit) = baz() | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
suspend fun bar(baz: suspend () -> Unit) = baz() | ||
""".trimIndent() | ||
assertThatRule(code) | ||
.hasLintViolation(1, 33, "Expected a single space between the modifier list and the function type") | ||
.isFormattedAs(formattedCode) | ||
} | ||
} | ||
|
||
@Nested | ||
inner class `Given unexpected newline before the function type` { | ||
@Test | ||
fun `Given unexpected newline between the modifier list and the function property type`() { | ||
val code = | ||
""" | ||
val foo: suspend | ||
() -> Unit = {} | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
val foo: suspend () -> Unit = {} | ||
""".trimIndent() | ||
assertThatRule(code) | ||
.hasLintViolation(2, 10, "Expected a single space between the modifier list and the function type") | ||
.isFormattedAs(formattedCode) | ||
} | ||
|
||
@Test | ||
fun `Given unexpected newline between the modifier list and the function parameter type`() { | ||
val code = | ||
""" | ||
suspend fun bar( | ||
baz: suspend | ||
() -> Unit | ||
) = baz() | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
suspend fun bar( | ||
baz: suspend () -> Unit | ||
) = baz() | ||
""".trimIndent() | ||
assertThatRule(code) | ||
.hasLintViolation(3, 10, "Expected a single space between the modifier list and the function type") | ||
.isFormattedAs(formattedCode) | ||
} | ||
} | ||
} |