Skip to content

Commit a0f1a10

Browse files
kfamyncalda
authored andcommitted
Add support for trailing commas in all cases containing parentheses in Swift 6.1 (nicklockwood#2008)
1 parent 3b361c5 commit a0f1a10

File tree

3 files changed

+592
-9
lines changed

3 files changed

+592
-9
lines changed

Rules.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3106,6 +3106,30 @@ func foo(
31063106
) {}
31073107
```
31083108

3109+
```diff
3110+
let foo = (
3111+
bar: 0,
3112+
- baz: 1
3113+
)
3114+
3115+
let foo = (
3116+
bar: 0,
3117+
+ baz: 1,
3118+
)
3119+
```
3120+
3121+
```diff
3122+
@Foo(
3123+
"bar",
3124+
- "baz"
3125+
)
3126+
3127+
@Foo(
3128+
"bar",
3129+
+ "baz",
3130+
)
3131+
```
3132+
31093133
</details>
31103134
<br/>
31113135

Sources/Rules/TrailingCommas.swift

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
import Foundation
1010

1111
public extension FormatRule {
12-
/// Ensure that the last item in a multi-line collection literal, parameter or argument list,
13-
/// or enum case with associated values is followed by a comma.
12+
/// Ensure that the last item in a multi-line list is followed by a comma, where applicable.
1413
/// This is useful for preventing noise in commits when items are added to end of array.
1514
static let trailingCommas = FormatRule(
1615
help: "Add or remove trailing commas where applicable.",
@@ -62,20 +61,22 @@ public extension FormatRule {
6261
return
6362
}
6463

65-
guard let functionStartIndex = formatter.index(of: .nonSpaceOrComment, before: startIndex),
66-
case .identifier = formatter.token(at: functionStartIndex)
67-
else {
64+
guard let prevToStartTokenIndex = formatter.index(of: .nonSpaceOrComment, before: startIndex) else {
65+
return
66+
}
67+
68+
guard formatter.tokens[prevToStartTokenIndex] != .delimiter(":") else {
6869
return
6970
}
7071

71-
guard let prevTokenIndex = formatter.index(of: .nonSpaceOrComment, before: i) else {
72+
guard let prevToEndTokenIndex = formatter.index(of: .nonSpaceOrComment, before: i) else {
7273
return
7374
}
7475

75-
switch formatter.tokens[prevTokenIndex] {
76+
switch formatter.tokens[prevToEndTokenIndex] {
7677
case .linebreak:
7778
guard let lastArgIndex = formatter.index(
78-
of: .nonSpaceOrCommentOrLinebreak, before: prevTokenIndex + 1
79+
of: .nonSpaceOrCommentOrLinebreak, before: prevToEndTokenIndex + 1
7980
) else {
8081
break
8182
}
@@ -92,7 +93,7 @@ public extension FormatRule {
9293
}
9394
}
9495
case .delimiter(","):
95-
formatter.removeToken(at: prevTokenIndex)
96+
formatter.removeToken(at: prevToEndTokenIndex)
9697
default:
9798
break
9899
}
@@ -122,6 +123,30 @@ public extension FormatRule {
122123
+ bar _: Int,
123124
) {}
124125
```
126+
127+
```diff
128+
let foo = (
129+
bar: 0,
130+
- baz: 1
131+
)
132+
133+
let foo = (
134+
bar: 0,
135+
+ baz: 1,
136+
)
137+
```
138+
139+
```diff
140+
@Foo(
141+
"bar",
142+
- "baz"
143+
)
144+
145+
@Foo(
146+
"bar",
147+
+ "baz",
148+
)
149+
```
125150
"""
126151
}
127152
}

0 commit comments

Comments
 (0)