Skip to content

Commit 9b8324b

Browse files
authored
Handle Swift 6.2 bug where trailing commas are not allowed in closure literal tuple return types (#2213)
1 parent 6afe143 commit 9b8324b

File tree

2 files changed

+86
-2
lines changed

2 files changed

+86
-2
lines changed

Sources/Rules/TrailingCommas.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public extension FormatRule {
7777
{
7878
trailingCommaSupported = true
7979

80-
// However, this is one bug in Swift 6.2 where trailing commas are unexpectedly
81-
// not allowed in tuple types within generic type argument lists.
80+
// However, this is a bug in Swift 6.2 where trailing commas are unexpectedly
81+
// not allowed in tuple types within generic type argument lists.
8282
// https://github.com/swiftlang/swift-syntax/pull/3153
8383
if formatter.options.swiftVersion == "6.2" {
8484
var startOfScope = startOfScope
@@ -91,6 +91,15 @@ public extension FormatRule {
9191
startOfScope = outerScope
9292
}
9393
}
94+
95+
// There is also a bug in Swift 6.2 where closure tuple return types don't support trailing commas.
96+
if formatter.options.swiftVersion == "6.2",
97+
let tokenBeforeStartOfScope = formatter.index(of: .nonSpaceOrCommentOrLinebreak, before: startOfScope),
98+
formatter.tokens[tokenBeforeStartOfScope] == .operator("->", .infix),
99+
formatter.isInClosureArguments(at: tokenBeforeStartOfScope)
100+
{
101+
trailingCommaSupported = false
102+
}
94103
}
95104

96105
// In Swift 6.1, trailing commas are only supported in tuple values,

Tests/Rules/TrailingCommasTests.swift

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2451,6 +2451,81 @@ class TrailingCommasTests: XCTestCase {
24512451
testFormatting(for: input, output, rule: .trailingCommas, options: options, exclude: [.typeSugar, .propertyTypes])
24522452
}
24532453

2454+
func testTrailingCommasNotAddedToClosureTupleReturnType() {
2455+
// Trailing commas are unexpectedly not allowed here in Swift 6.2
2456+
let input = """
2457+
let closure = { () -> (
2458+
foo: String,
2459+
bar: String
2460+
) in
2461+
(foo: "foo", bar: "bar")
2462+
}
2463+
2464+
func foo() -> (
2465+
foo: String,
2466+
bar: String
2467+
) {
2468+
(foo: "foo", bar: "bar")
2469+
}
2470+
"""
2471+
2472+
let output = """
2473+
let closure = { () -> (
2474+
foo: String,
2475+
bar: String
2476+
) in
2477+
(foo: "foo", bar: "bar")
2478+
}
2479+
2480+
func foo() -> (
2481+
foo: String,
2482+
bar: String,
2483+
) {
2484+
(foo: "foo", bar: "bar")
2485+
}
2486+
"""
2487+
2488+
let options = FormatOptions(trailingCommas: .always, swiftVersion: "6.2")
2489+
testFormatting(for: input, output, rule: .trailingCommas, options: options, exclude: [.typeSugar, .propertyTypes])
2490+
}
2491+
2492+
func testTrailingCommasAddedToClosureTupleReturnTypeSwift6_3() {
2493+
let input = """
2494+
let closure = { () -> (
2495+
foo: String,
2496+
bar: String
2497+
) in
2498+
(foo: "foo", bar: "bar")
2499+
}
2500+
2501+
func foo() -> (
2502+
foo: String,
2503+
bar: String
2504+
) {
2505+
(foo: "foo", bar: "bar")
2506+
}
2507+
"""
2508+
2509+
let output = """
2510+
let closure = { () -> (
2511+
foo: String,
2512+
bar: String,
2513+
) in
2514+
(foo: "foo", bar: "bar")
2515+
}
2516+
2517+
func foo() -> (
2518+
foo: String,
2519+
bar: String,
2520+
) {
2521+
(foo: "foo", bar: "bar")
2522+
}
2523+
"""
2524+
2525+
let options = FormatOptions(trailingCommas: .always, swiftVersion: "6.3")
2526+
testFormatting(for: input, output, rule: .trailingCommas, options: options, exclude: [.typeSugar, .propertyTypes])
2527+
}
2528+
24542529
func testTrailingCommasAddedToTupleFunctionArgumentInSwift6_2() {
24552530
let input = """
24562531
func updateBackgroundMusic(

0 commit comments

Comments
 (0)