Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed [{ }] notation for issue #2675 #2677

Merged
merged 2 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}