-
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 experimental NullableTypeSpacingRule (#1426)
* Add new experimental rule which prohibits whitespaces inside a nullable type Required for implementing the signature rewrite rule (#1341)
- Loading branch information
1 parent
525dbc2
commit c82be37
Showing
5 changed files
with
194 additions
and
1 deletion.
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
...ntal/src/main/kotlin/com/pinterest/ktlint/ruleset/experimental/NullableTypeSpacingRule.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.QUEST | ||
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE | ||
import com.pinterest.ktlint.core.ast.prevLeaf | ||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement | ||
|
||
public class NullableTypeSpacingRule : Rule("nullable-type-spacing") { | ||
override fun visit( | ||
node: ASTNode, | ||
autoCorrect: Boolean, | ||
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit | ||
) { | ||
node | ||
.takeIf { node.elementType == QUEST } | ||
?.prevLeaf() | ||
?.takeIf { it.elementType == WHITE_SPACE } | ||
?.let { whiteSpaceBeforeQuest -> | ||
emit(whiteSpaceBeforeQuest.startOffset, "Unexpected whitespace", true) | ||
if (autoCorrect) { | ||
(whiteSpaceBeforeQuest as LeafPsiElement).rawRemove() | ||
} | ||
} | ||
} | ||
} |
163 changes: 163 additions & 0 deletions
163
.../src/test/kotlin/com/pinterest/ktlint/ruleset/experimental/NullableTypeSpacingRuleTest.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,163 @@ | ||
package com.pinterest.ktlint.ruleset.experimental | ||
|
||
import com.pinterest.ktlint.core.LintError | ||
import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThat | ||
import com.pinterest.ktlint.test.format | ||
import com.pinterest.ktlint.test.lint | ||
import org.assertj.core.api.Assertions | ||
import org.junit.jupiter.api.Test | ||
|
||
class NullableTypeSpacingRuleTest { | ||
private val nullableTypeSpacingRuleAssertThat = NullableTypeSpacingRule().assertThat() | ||
|
||
@Test | ||
fun `Given a simple nullable type with a space before the quest then remove this space`() { | ||
val code = | ||
""" | ||
val foo : String ? = null | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
val foo : String? = null | ||
""".trimIndent() | ||
Assertions.assertThat(NullableTypeSpacingRule().lint(code)).containsExactly( | ||
LintError(1, 17, "nullable-type-spacing", "Unexpected whitespace") | ||
) | ||
Assertions.assertThat(NullableTypeSpacingRule().format(code)).isEqualTo(formattedCode) | ||
} | ||
|
||
@Test | ||
fun `Given a non-nullable list of a nullable type with a space before the quest then remove this space`() { | ||
val code = | ||
""" | ||
val foo : List<String ?> = listOf(null) | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
val foo : List<String?> = listOf(null) | ||
""".trimIndent() | ||
nullableTypeSpacingRuleAssertThat(code) | ||
.hasLintViolation(1, 22, "Unexpected whitespace") | ||
.isFormattedAs(formattedCode) | ||
} | ||
|
||
@Test | ||
fun `Given a nullable list of a non-nullable type with a space before the quest then remove this space`() { | ||
val code = | ||
""" | ||
val foo : List<String> ? = null | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
val foo : List<String>? = null | ||
""".trimIndent() | ||
nullableTypeSpacingRuleAssertThat(code) | ||
.hasLintViolation(1, 23, "Unexpected whitespace") | ||
.isFormattedAs(formattedCode) | ||
} | ||
|
||
@Test | ||
fun `Given a type receiver of nullable simple type with a space before the quest then remove this space`() { | ||
val code = | ||
""" | ||
fun String ?.foo() = "some-result" | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
fun String?.foo() = "some-result" | ||
""".trimIndent() | ||
nullableTypeSpacingRuleAssertThat(code) | ||
.hasLintViolation(1, 11, "Unexpected whitespace") | ||
.isFormattedAs(formattedCode) | ||
} | ||
|
||
@Test | ||
fun `Given a type receiver of a non-nullable list of a nullable type with a space before the quest then remove this space`() { | ||
val code = | ||
""" | ||
fun List<String ?>.foo() = "some-result" | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
fun List<String?>.foo() = "some-result" | ||
""".trimIndent() | ||
nullableTypeSpacingRuleAssertThat(code) | ||
.hasLintViolation(1, 16, "Unexpected whitespace") | ||
.isFormattedAs(formattedCode) | ||
} | ||
|
||
@Test | ||
fun `Given a parameter of a nullable type with a space before the quest then remove this space`() { | ||
val code = | ||
""" | ||
fun foo(string: String ?) = "some-result" | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
fun foo(string: String?) = "some-result" | ||
""".trimIndent() | ||
nullableTypeSpacingRuleAssertThat(code) | ||
.hasLintViolation(1, 23, "Unexpected whitespace") | ||
.isFormattedAs(formattedCode) | ||
} | ||
|
||
@Test | ||
fun `Given a parameter of a list of a nullable type with a space before the quest then remove this space`() { | ||
val code = | ||
""" | ||
fun foo(string: List<String ?>) = "some-result" | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
fun foo(string: List<String?>) = "some-result" | ||
""".trimIndent() | ||
nullableTypeSpacingRuleAssertThat(code) | ||
.hasLintViolation(1, 28, "Unexpected whitespace") | ||
.isFormattedAs(formattedCode) | ||
} | ||
|
||
@Test | ||
fun `Given a nullable function return type with a space before the quest then remove this space`() { | ||
val code = | ||
""" | ||
fun foo(): String ? = "some-result" | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
fun foo(): String? = "some-result" | ||
""".trimIndent() | ||
nullableTypeSpacingRuleAssertThat(code) | ||
.hasLintViolation(1, 18, "Unexpected whitespace") | ||
.isFormattedAs(formattedCode) | ||
} | ||
|
||
@Test | ||
fun `Given a return type of a non-nullable list of a nullable type with a space before the quest then remove this space`() { | ||
val code = | ||
""" | ||
fun foo(): List<String ?> = listOf("some-result", null) | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
fun foo(): List<String?> = listOf("some-result", null) | ||
""".trimIndent() | ||
nullableTypeSpacingRuleAssertThat(code) | ||
.hasLintViolation(1, 23, "Unexpected whitespace") | ||
.isFormattedAs(formattedCode) | ||
} | ||
|
||
@Test | ||
fun `Given a return type of a nullable list with a space before the quest then remove this space`() { | ||
val code = | ||
""" | ||
fun foo(): List<String> ? = null | ||
""".trimIndent() | ||
val formattedCode = | ||
""" | ||
fun foo(): List<String>? = null | ||
""".trimIndent() | ||
nullableTypeSpacingRuleAssertThat(code) | ||
.hasLintViolation(1, 24, "Unexpected whitespace") | ||
.isFormattedAs(formattedCode) | ||
} | ||
} |