Skip to content

Commit

Permalink
Fixed [{ }] notation for issue #2675 (#2677)
Browse files Browse the repository at this point in the history
Closes #2675

---------

Co-authored-by: Paul Dingemans <paul-dingemans@users.noreply.github.com>
  • Loading branch information
Jolanrensen and paul-dingemans authored May 29, 2024
1 parent c3ed2d7 commit 58a5365
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,29 @@ class SpacingAroundCurlyRuleTest {
.isFormattedAs(formattedCode)
}

@Test
fun `Issue 2675 - array access expression containing a lambda expression`() {
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(1, 17, "Missing spacing after \"{\"", canBeAutoCorrected = true),
LintViolation(1, 23, "Missing spacing before \"}\"", canBeAutoCorrected = true),
).isFormattedAs(formattedCode)
}

@Test
fun `Given curly braces inside a string template`() {
val code =
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -125,4 +126,23 @@ class SpacingAroundSquareBracketsRuleTest {
""".trimIndent()
spacingAroundSquareBracketsRuleAssertThat(code).hasNoLintViolations()
}

@Test
fun `Issue 2675 - array access expression containing a lambda expression`() {
val code =
"""
val foo = bar[ { 123 } ]
""".trimIndent()

val formattedCode =
"""
val foo = bar[{ 123 }]
""".trimIndent()

spacingAroundSquareBracketsRuleAssertThat(code)
.hasLintViolations(
LintViolation(1, 15, "Unexpected spacing after '['", canBeAutoCorrected = true),
LintViolation(1, 23, "Unexpected spacing before ']'", canBeAutoCorrected = true),
).isFormattedAs(formattedCode)
}
}

0 comments on commit 58a5365

Please sign in to comment.