Skip to content

Commit cd9f84f

Browse files
Fix nicklockwood#1991: Disable trailingComma for capture lists
1 parent 22a472c commit cd9f84f

2 files changed

Lines changed: 10 additions & 19 deletions

File tree

Sources/Rules/TrailingCommas.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,16 @@ public extension FormatRule {
2121
formatter.addOrRemoveTrailingComma(beforeEndOfScope: i, trailingCommaSupported: true, isCollection: true)
2222
return
2323

24-
case .subscript, .captureList, .functionCall, .parameterList:
24+
case .subscript, .functionCall, .parameterList:
2525
let trailingCommaSupported = formatter.options.swiftVersion >= "6.1"
2626
formatter.addOrRemoveTrailingComma(beforeEndOfScope: i, trailingCommaSupported: trailingCommaSupported)
2727
return
2828

29+
case .captureList:
30+
// Trailing commas in closure capture lists are not supported by the compiler yet.
31+
formatter.addOrRemoveTrailingComma(beforeEndOfScope: i, trailingCommaSupported: false)
32+
return
33+
2934
case .arrayType, .dictionaryType, .throwsType:
3035
return
3136

Tests/Rules/TrailingCommasTests.swift

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,23 +1438,16 @@ final class TrailingCommasTests: XCTestCase {
14381438
testFormatting(for: input, output, rule: .trailingCommas, options: options)
14391439
}
14401440

1441-
func testTrailingCommasAddedToCaptureList() {
1441+
func testTrailingCommasNotAddedToCaptureListSwift6_1() {
14421442
let input = """
14431443
{ [
14441444
capturedValue1,
14451445
capturedValue2
14461446
] in
14471447
}
14481448
"""
1449-
let output = """
1450-
{ [
1451-
capturedValue1,
1452-
capturedValue2,
1453-
] in
1454-
}
1455-
"""
14561449
let options = FormatOptions(trailingCommas: .always, swiftVersion: "6.1")
1457-
testFormatting(for: input, output, rule: .trailingCommas, options: options)
1450+
testFormatting(for: input, rule: .trailingCommas, options: options)
14581451
}
14591452

14601453
func testTrailingCommasRemovedFromSingleElementCaptureList() {
@@ -3038,23 +3031,16 @@ final class TrailingCommasTests: XCTestCase {
30383031
testFormatting(for: input, output, rule: .trailingCommas, options: options)
30393032
}
30403033

3041-
func testTrailingCommasAddedToCaptureListSwift6_2() {
3034+
func testTrailingCommasNotAddedToCaptureListSwift6_2() {
30423035
let input = """
30433036
{ [
30443037
capturedValue1,
30453038
capturedValue2
30463039
] in
30473040
}
30483041
"""
3049-
let output = """
3050-
{ [
3051-
capturedValue1,
3052-
capturedValue2,
3053-
] in
3054-
}
3055-
"""
30563042
let options = FormatOptions(trailingCommas: .always, swiftVersion: "6.2")
3057-
testFormatting(for: input, output, rule: .trailingCommas, options: options)
3043+
testFormatting(for: input, rule: .trailingCommas, options: options)
30583044
}
30593045

30603046
func testTrailingCommasRemovedFromSingleElementCaptureListSwift6_2() {

0 commit comments

Comments
 (0)