Skip to content

Commit b27e7c5

Browse files
committed
Handle bug where Swift 6.2 doesn't allow trailing commas in tuples within generic types
1 parent 02e4582 commit b27e7c5

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

Sources/Rules/TrailingCommas.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,21 @@ public extension FormatRule {
7676
formatter.commaSeparatedElementsInScope(startOfScope: startOfScope).count > 1
7777
{
7878
trailingCommaSupported = true
79+
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.
82+
// https://github.com/swiftlang/swift-syntax/pull/3153
83+
if formatter.options.swiftVersion == "6.2" {
84+
var startOfScope = startOfScope
85+
while let outerScope = formatter.startOfScope(at: startOfScope) {
86+
if formatter.tokens[outerScope] == .startOfScope("<") {
87+
trailingCommaSupported = false
88+
break
89+
}
90+
91+
startOfScope = outerScope
92+
}
93+
}
7994
}
8095

8196
// In Swift 6.1, trailing commas are only supported in tuple values,
@@ -89,8 +104,8 @@ public extension FormatRule {
89104
{
90105
// `{ (...) }`, `return (...)` etc are always tuple values
91106
// (except in the case of a typealias, where the rhs is a type)
92-
let tokensPreceedingValuesNotTypes: Set<Token> = [.startOfScope("{"), .keyword("return"), .keyword("throw"), .keyword("switch"), .endOfScope("case")]
93-
if tokensPreceedingValuesNotTypes.contains(formatter.tokens[tokenBeforeStartOfScope]) {
107+
let tokensPrecedingValuesNotTypes: Set<Token> = [.startOfScope("{"), .keyword("return"), .keyword("throw"), .keyword("switch"), .endOfScope("case")]
108+
if tokensPrecedingValuesNotTypes.contains(formatter.tokens[tokenBeforeStartOfScope]) {
94109
trailingCommaSupported = true
95110
}
96111

Tests/Rules/TrailingCommasTests.swift

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2418,32 +2418,36 @@ class TrailingCommasTests: XCTestCase {
24182418
testFormatting(for: input, output, rule: .trailingCommas, options: options, exclude: [.propertyTypes])
24192419
}
24202420

2421-
func testTrailingCommasAddedToTupleTypeInGenericBracketsInSwift6_2() {
2422-
// Trailing commas are now supported in tuple types in Swift 6.2
2421+
func testTrailingCommasNotAddedToTupleTypeInGenericBracketsInSwift6_2() {
2422+
// In Swift 6.2, trailing commas are unexpectedly not supported in tuple types
2423+
// within generic arguments: https://github.com/swiftlang/swift-syntax/pull/3153
24232424
let input = """
24242425
let foo: Array<(
24252426
bar: String,
24262427
quux: String
24272428
)>
2429+
"""
24282430

2429-
let foo = Array<(
2431+
let options = FormatOptions(trailingCommas: .always, swiftVersion: "6.2")
2432+
testFormatting(for: input, rule: .trailingCommas, options: options, exclude: [.typeSugar, .propertyTypes])
2433+
}
2434+
2435+
func testTrailingCommasAddedToTupleTypeInGenericBracketsInSwift6_3() {
2436+
let input = """
2437+
let foo: Array<(
24302438
bar: String,
24312439
quux: String
2432-
)>()
2440+
)>
24332441
"""
2442+
24342443
let output = """
24352444
let foo: Array<(
24362445
bar: String,
24372446
quux: String,
24382447
)>
2439-
2440-
let foo = Array<(
2441-
bar: String,
2442-
quux: String,
2443-
)>()
24442448
"""
24452449

2446-
let options = FormatOptions(trailingCommas: .always, swiftVersion: "6.2")
2450+
let options = FormatOptions(trailingCommas: .always, swiftVersion: "6.3")
24472451
testFormatting(for: input, output, rule: .trailingCommas, options: options, exclude: [.typeSugar, .propertyTypes])
24482452
}
24492453

0 commit comments

Comments
 (0)