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 cb8959633c..a50826b942 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 86da5807bc..96b5560e70 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) + } }