9
9
import Foundation
10
10
11
11
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.
14
13
/// This is useful for preventing noise in commits when items are added to end of array.
15
14
static let trailingCommas = FormatRule (
16
15
help: " Add or remove trailing commas where applicable. " ,
@@ -62,20 +61,22 @@ public extension FormatRule {
62
61
return
63
62
}
64
63
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 {
68
69
return
69
70
}
70
71
71
- guard let prevTokenIndex = formatter. index ( of: . nonSpaceOrComment, before: i) else {
72
+ guard let prevToEndTokenIndex = formatter. index ( of: . nonSpaceOrComment, before: i) else {
72
73
return
73
74
}
74
75
75
- switch formatter. tokens [ prevTokenIndex ] {
76
+ switch formatter. tokens [ prevToEndTokenIndex ] {
76
77
case . linebreak:
77
78
guard let lastArgIndex = formatter. index (
78
- of: . nonSpaceOrCommentOrLinebreak, before: prevTokenIndex + 1
79
+ of: . nonSpaceOrCommentOrLinebreak, before: prevToEndTokenIndex + 1
79
80
) else {
80
81
break
81
82
}
@@ -92,7 +93,7 @@ public extension FormatRule {
92
93
}
93
94
}
94
95
case . delimiter( " , " ) :
95
- formatter. removeToken ( at: prevTokenIndex )
96
+ formatter. removeToken ( at: prevToEndTokenIndex )
96
97
default :
97
98
break
98
99
}
@@ -122,6 +123,30 @@ public extension FormatRule {
122
123
+ bar _: Int,
123
124
) {}
124
125
```
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
+ ```
125
150
"""
126
151
}
127
152
}
0 commit comments