diff options
4 files changed, 47 insertions, 29 deletions
diff --git a/documentation/release-latest/docs/rules/standard.md b/documentation/release-latest/docs/rules/standard.md index 6a7c9853..2ce684bb 100644 --- a/documentation/release-latest/docs/rules/standard.md +++ b/documentation/release-latest/docs/rules/standard.md @@ -60,21 +60,21 @@ Requires a blank line before any class or function declaration. No blank line is ```kotlin const val FOO_1 = "foo1" - + class FooBar { val foo2 = "foo2" val foo3 = "foo3" - + fun bar1() { val foo4 = "foo4" val foo5 = "foo5" } - + fun bar2() = "bar" - + val foo6 = "foo3" val foo7 = "foo4" - + enum class Foo } ``` @@ -82,23 +82,19 @@ Requires a blank line before any class or function declaration. No blank line is === "[:material-heart-off-outline:](#) Disallowed" ```kotlin - const val foo1 = "foo1" + const val FOO_1 = "foo1" class FooBar { val foo2 = "foo2" val foo3 = "foo3" - fun bar1() { - val foo4 = "foo4" - val foo5 = "foo5" + val foo4 = "foo4" + val foo5 = "foo5" } - fun bar2() = "bar" - val foo6 = "foo3" val foo7 = "foo4" - - enum class Foo {} + enum class Foo } ``` @@ -473,7 +469,7 @@ Enforce naming of function. fun fooBar() {} - fun `fun` {} // Any keyword is allowed when wrapped between backticks + fun `fun`() {} // Any keyword is allowed when wrapped between backticks ``` === "[:material-heart:](#) Ktlint Test" diff --git a/documentation/snapshot/docs/rules/standard.md b/documentation/snapshot/docs/rules/standard.md index ad913e1a..cb3e3310 100644 --- a/documentation/snapshot/docs/rules/standard.md +++ b/documentation/snapshot/docs/rules/standard.md @@ -60,21 +60,21 @@ Requires a blank line before any class or function declaration. No blank line is ```kotlin const val FOO_1 = "foo1" - + class FooBar { val foo2 = "foo2" val foo3 = "foo3" - + fun bar1() { val foo4 = "foo4" val foo5 = "foo5" } - + fun bar2() = "bar" - + val foo6 = "foo3" val foo7 = "foo4" - + enum class Foo } ``` @@ -82,23 +82,19 @@ Requires a blank line before any class or function declaration. No blank line is === "[:material-heart-off-outline:](#) Disallowed" ```kotlin - const val foo1 = "foo1" + const val FOO_1 = "foo1" class FooBar { val foo2 = "foo2" val foo3 = "foo3" - fun bar1() { - val foo4 = "foo4" - val foo5 = "foo5" + val foo4 = "foo4" + val foo5 = "foo5" } - fun bar2() = "bar" - val foo6 = "foo3" val foo7 = "foo4" - - enum class Foo {} + enum class Foo } ``` @@ -473,7 +469,7 @@ Enforce naming of function. fun fooBar() {} - fun `fun` {} // Any keyword is allowed when wrapped between backticks + fun `fun`() {} // Any keyword is allowed when wrapped between backticks ``` === "[:material-heart:](#) Ktlint Test" diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundOperatorsRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundOperatorsRule.kt index cb895963..a50826b9 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundOperatorsRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundOperatorsRule.kt @@ -31,6 +31,7 @@ import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.STABLE import com.pinterest.ktlint.rule.engine.core.api.isPartOf import com.pinterest.ktlint.rule.engine.core.api.nextLeaf +import com.pinterest.ktlint.rule.engine.core.api.parent import com.pinterest.ktlint.rule.engine.core.api.prevLeaf import com.pinterest.ktlint.rule.engine.core.api.upsertWhitespaceAfterMe import com.pinterest.ktlint.rule.engine.core.api.upsertWhitespaceBeforeMe @@ -39,6 +40,7 @@ import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet import org.jetbrains.kotlin.psi.KtImportDirective +import org.jetbrains.kotlin.psi.KtOperationExpression import org.jetbrains.kotlin.psi.KtPrefixExpression @SinceKtlint("0.1", STABLE) @@ -107,7 +109,7 @@ public class SpacingAroundOperatorsRule : StandardRule("op-spacing") { } } - private fun ASTNode.isUnaryOperator() = isPartOf(KtPrefixExpression::class) + private fun ASTNode.isUnaryOperator() = parent { it.psi is KtOperationExpression }?.psi is KtPrefixExpression private fun ASTNode.isSpreadOperator() = // fn(*array) diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundOperatorsRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundOperatorsRuleTest.kt index 86da5807..96b5560e 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundOperatorsRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundOperatorsRuleTest.kt @@ -314,4 +314,28 @@ class SpacingAroundOperatorsRuleTest { LintViolation(4, 41, "Missing spacing after \"andThen\""), ).isFormattedAs(formattedCode) } + + @Test + fun `Given a binary operator inside a unary expression`() { + val code = + """ + val foo1 = !(null?:true) + val foo2 = !(null?: true) + val foo3 = !(null ?:true) + val foo4 = !(null ?: true) + """.trimIndent() + val formattedCode = + """ + val foo1 = !(null ?: true) + val foo2 = !(null ?: true) + val foo3 = !(null ?: true) + val foo4 = !(null ?: true) + """.trimIndent() + spacingAroundOperatorsRuleAssertThat(code) + .hasLintViolations( + LintViolation(1, 18, "Missing spacing around \"?:\""), + LintViolation(2, 18, "Missing spacing before \"?:\""), + LintViolation(3, 21, "Missing spacing after \"?:\""), + ).isFormattedAs(formattedCode) + } } |