mirror of
https://github.com/nicklockwood/SwiftFormat.git
synced 2026-05-17 10:30:35 +00:00
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:
@@ -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
|
||||
|
||||
@@ -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 = """
|
||||
{ [
|
||||
|
||||
Reference in New Issue
Block a user