From 10a54049d4183af7922f88328478245b0cbb04e9 Mon Sep 17 00:00:00 2001 From: Jolan Rensen Date: Wed, 29 May 2024 12:08:25 +0200 Subject: [PATCH 1/2] Fixed [{ }] notation for issue #2675 in standard:curly-spacing rule. Added tests. --- .../standard/rules/SpacingAroundCurlyRule.kt | 2 +- .../rules/SpacingAroundCurlyRuleTest.kt | 23 +++++++++++++++++++ .../SpacingAroundSquareBracketsRuleTest.kt | 20 ++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundCurlyRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundCurlyRule.kt index 05131d4eb6..3f5df33ee1 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundCurlyRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundCurlyRule.kt @@ -87,7 +87,7 @@ public class SpacingAroundCurlyRule : prevLeaf is PsiWhiteSpace || prevLeaf?.elementType == AT || ( - prevLeaf?.elementType == LPAR && + (prevLeaf?.elementType == LPAR || prevLeaf?.elementType == LBRACKET) && ((node as LeafPsiElement).parent is KtLambdaExpression || node.parent.parent is KtLambdaExpression) ) spacingAfter = nextLeaf is PsiWhiteSpace || nextLeaf?.elementType == RBRACE diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundCurlyRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundCurlyRuleTest.kt index d668516bac..79fa69d2a8 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundCurlyRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundCurlyRuleTest.kt @@ -179,6 +179,29 @@ class SpacingAroundCurlyRuleTest { .isFormattedAs(formattedCode) } + @Test + fun `Issue 2675 - operator get with lambda`() { + val code = + """ + val foo1 = bar[{a -> a}] + val foo2 = bar[{ }] + val foo3 = bar[{}] + """.trimIndent() + + val formattedCode = + """ + val foo1 = bar[{ a -> a }] + val foo2 = bar[{ }] + val foo3 = bar[{}] + """.trimIndent() + + spacingAroundCurlyRuleAssertThat(code) + .hasLintViolations( + LintViolation(line = 1, col = 17, detail = "Missing spacing after \"{\"", canBeAutoCorrected = true), + LintViolation(line = 1, col = 23, detail = "Missing spacing before \"}\"", canBeAutoCorrected = true), + ).isFormattedAs(formattedCode) + } + @Test fun `Given curly braces inside a string template`() { val code = diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundSquareBracketsRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundSquareBracketsRuleTest.kt index 408354acbe..03cdd16daa 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundSquareBracketsRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundSquareBracketsRuleTest.kt @@ -1,6 +1,7 @@ package com.pinterest.ktlint.ruleset.standard.rules import com.pinterest.ktlint.test.KtLintAssertThat.Companion.assertThatRule +import com.pinterest.ktlint.test.LintViolation import org.junit.jupiter.api.Test class SpacingAroundSquareBracketsRuleTest { @@ -125,4 +126,23 @@ class SpacingAroundSquareBracketsRuleTest { """.trimIndent() spacingAroundSquareBracketsRuleAssertThat(code).hasNoLintViolations() } + + @Test + fun `Issue 2675 - operator get with lambda`() { + val code = + """ + val foo = bar[ { 123 } ] + """.trimIndent() + + val formattedCode = + """ + val foo = bar[{ 123 }] + """.trimIndent() + + spacingAroundSquareBracketsRuleAssertThat(code) + .hasLintViolations( + LintViolation(line = 1, col = 15, detail = "Unexpected spacing after '['", canBeAutoCorrected = true), + LintViolation(line = 1, col = 23, detail = "Unexpected spacing before ']'", canBeAutoCorrected = true), + ).isFormattedAs(formattedCode) + } } From 43e611d951fe3a51f5f8a46c8fe3b3c62526ec64 Mon Sep 17 00:00:00 2001 From: Paul Dingemans Date: Wed, 29 May 2024 20:27:47 +0200 Subject: [PATCH 2/2] Fix test names, remove call argument names for non-booleans Closes #2675 --- .../ruleset/standard/rules/SpacingAroundCurlyRuleTest.kt | 6 +++--- .../standard/rules/SpacingAroundSquareBracketsRuleTest.kt | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundCurlyRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundCurlyRuleTest.kt index 79fa69d2a8..13a0d5fc09 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundCurlyRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundCurlyRuleTest.kt @@ -180,7 +180,7 @@ class SpacingAroundCurlyRuleTest { } @Test - fun `Issue 2675 - operator get with lambda`() { + fun `Issue 2675 - array access expression containing a lambda expression`() { val code = """ val foo1 = bar[{a -> a}] @@ -197,8 +197,8 @@ class SpacingAroundCurlyRuleTest { spacingAroundCurlyRuleAssertThat(code) .hasLintViolations( - LintViolation(line = 1, col = 17, detail = "Missing spacing after \"{\"", canBeAutoCorrected = true), - LintViolation(line = 1, col = 23, detail = "Missing spacing before \"}\"", canBeAutoCorrected = true), + LintViolation(1, 17, "Missing spacing after \"{\"", canBeAutoCorrected = true), + LintViolation(1, 23, "Missing spacing before \"}\"", canBeAutoCorrected = true), ).isFormattedAs(formattedCode) } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundSquareBracketsRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundSquareBracketsRuleTest.kt index 03cdd16daa..699bea6a84 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundSquareBracketsRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/SpacingAroundSquareBracketsRuleTest.kt @@ -128,7 +128,7 @@ class SpacingAroundSquareBracketsRuleTest { } @Test - fun `Issue 2675 - operator get with lambda`() { + fun `Issue 2675 - array access expression containing a lambda expression`() { val code = """ val foo = bar[ { 123 } ] @@ -141,8 +141,8 @@ class SpacingAroundSquareBracketsRuleTest { spacingAroundSquareBracketsRuleAssertThat(code) .hasLintViolations( - LintViolation(line = 1, col = 15, detail = "Unexpected spacing after '['", canBeAutoCorrected = true), - LintViolation(line = 1, col = 23, detail = "Unexpected spacing before ']'", canBeAutoCorrected = true), + LintViolation(1, 15, "Unexpected spacing after '['", canBeAutoCorrected = true), + LintViolation(1, 23, "Unexpected spacing before ']'", canBeAutoCorrected = true), ).isFormattedAs(formattedCode) } }