Update trailingCommas rule to handle closure capture lists using a macro as the capture value (#2383)

Co-authored-by: calda <1811727+calda@users.noreply.github.com>
This commit is contained in:
Copilot
2026-02-18 10:58:57 -08:00
committed by Cal Stephens
parent 0c55f9e3aa
commit 0fee45048a
2 changed files with 60 additions and 1 deletions
+1 -1
View File
@@ -790,7 +790,7 @@ extension Formatter {
return true
case .startOfScope("("), .startOfScope("["), .startOfScope("<"),
.endOfScope(")"), .endOfScope("]"), .endOfScope(">"),
.keyword where token.isAttribute, _ where token.isComment:
.keyword where token.isAttribute || token.isMacro, _ where token.isComment:
break
case .keyword, .startOfScope, .endOfScope:
return false
+59
View File
@@ -330,6 +330,44 @@ final class TrailingCommasTests: XCTestCase {
testFormatting(for: input, rule: .trailingCommas)
}
func testTrailingCommaNotAddedToCaptureListWithMacro() {
let input = """
{ [
a = a(),
b = #b
] in
a + b
}
"""
testFormatting(for: input, rule: .trailingCommas)
}
func testTrailingCommaNotAddedToCaptureListWithMacroCollectionsOnly() {
let input = """
{ [
a = a(),
b = #b
] in
a + b
}
"""
let options = FormatOptions(trailingCommas: .collectionsOnly)
testFormatting(for: input, rule: .trailingCommas, options: options)
}
func testTrailingCommaNotAddedToCaptureListWithMacroSwift6() {
let input = """
{ [
a = a(),
b = #b
] in
a + b
}
"""
let options = FormatOptions(swiftVersion: "6.0")
testFormatting(for: input, rule: .trailingCommas, options: options)
}
func testTrailingCommaNotAddedToArrayExtension() {
let input = """
extension [
@@ -1457,6 +1495,27 @@ final class TrailingCommasTests: XCTestCase {
testFormatting(for: input, output, rule: .trailingCommas, options: options)
}
func testTrailingCommasAddedToCaptureListWithMacro() {
let input = """
{ [
a = a(),
b = #b
] in
a + b
}
"""
let output = """
{ [
a = a(),
b = #b,
] in
a + b
}
"""
let options = FormatOptions(trailingCommas: .always, swiftVersion: "6.1")
testFormatting(for: input, output, rule: .trailingCommas, options: options)
}
func testTrailingCommasRemovedFromSingleElementCaptureList() {
let input = """
{ [