Skip to content

Commit da79b03

Browse files
committed
- adopt PR recommendations
1 parent ff93f49 commit da79b03

File tree

2 files changed

+163
-19
lines changed

2 files changed

+163
-19
lines changed

Sources/SwiftFormat/API/Configuration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public struct Configuration: Codable, Equatable {
163163
/// Contains exceptions for the `NoAssignmentInExpressions` rule.
164164
public var noAssignmentInExpressions: NoAssignmentInExpressionsConfiguration
165165

166-
/// Determines whether multi-line list initializers should have trailing commas
166+
/// Determines whether multi-line list initializers should have trailing commas.
167167
public var multilineCollectionTrailingCommas: Bool
168168

169169
/// Constructs a Configuration by loading it from a configuration file.

Tests/SwiftFormatTests/PrettyPrint/CommaTests.swift

Lines changed: 162 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import SwiftFormat
22

33
final class CommaTests: PrettyPrintTestCase {
4-
func testCommasAbsentEnabled() {
4+
func testArrayCommasAbsentEnabled() {
55
let input =
66
"""
7-
let MyList = [
7+
let MyCollection = [
88
1,
99
2,
1010
3
@@ -14,7 +14,7 @@ final class CommaTests: PrettyPrintTestCase {
1414

1515
let expected =
1616
"""
17-
let MyList = [
17+
let MyCollection = [
1818
1,
1919
2,
2020
3,
@@ -27,10 +27,10 @@ final class CommaTests: PrettyPrintTestCase {
2727
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20, configuration: configuration)
2828
}
2929

30-
func testCommasAbsentDisabled() {
30+
func testArrayCommasAbsentDisabled() {
3131
let input =
3232
"""
33-
let MyList = [
33+
let MyCollection = [
3434
1,
3535
2,
3636
3
@@ -40,7 +40,7 @@ final class CommaTests: PrettyPrintTestCase {
4040

4141
let expected =
4242
"""
43-
let MyList = [
43+
let MyCollection = [
4444
1,
4545
2,
4646
3
@@ -53,10 +53,10 @@ final class CommaTests: PrettyPrintTestCase {
5353
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20, configuration: configuration)
5454
}
5555

56-
func testCommasPresentEnabled() {
56+
func testArrayCommasPresentEnabled() {
5757
let input =
5858
"""
59-
let MyList = [
59+
let MyCollection = [
6060
1,
6161
2,
6262
3,
@@ -66,7 +66,7 @@ final class CommaTests: PrettyPrintTestCase {
6666

6767
let expected =
6868
"""
69-
let MyList = [
69+
let MyCollection = [
7070
1,
7171
2,
7272
3,
@@ -79,10 +79,10 @@ final class CommaTests: PrettyPrintTestCase {
7979
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20, configuration: configuration)
8080
}
8181

82-
func testCommasPresentDisabled() {
82+
func testArrayCommasPresentDisabled() {
8383
let input =
8484
"""
85-
let MyList = [
85+
let MyCollection = [
8686
1,
8787
2,
8888
3,
@@ -92,7 +92,7 @@ final class CommaTests: PrettyPrintTestCase {
9292

9393
let expected =
9494
"""
95-
let MyList = [
95+
let MyCollection = [
9696
1,
9797
2,
9898
3
@@ -105,17 +105,17 @@ final class CommaTests: PrettyPrintTestCase {
105105
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20, configuration: configuration)
106106
}
107107

108-
func testCommasPresentSingleLineDisabled() {
108+
func testArraySingleLineCommasPresentDisabled() {
109109
let input =
110110
"""
111-
let MyList = [1, 2, 3,]
111+
let MyCollection = [1, 2, 3,]
112112
113113
"""
114114

115115
// no effect expected
116116
let expected =
117117
"""
118-
let MyList = [1, 2, 3]
118+
let MyCollection = [1, 2, 3]
119119
120120
"""
121121

@@ -124,17 +124,161 @@ final class CommaTests: PrettyPrintTestCase {
124124
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40, configuration: configuration)
125125
}
126126

127-
func testCommasPresentSingleLineEnabled() {
127+
func testArraySingleLineCommasPresentEnabled() {
128128
let input =
129129
"""
130-
let MyList = [1, 2, 3,]
130+
let MyCollection = [1, 2, 3,]
131131
132132
"""
133133

134134
// no effect expected
135135
let expected =
136136
"""
137-
let MyList = [1, 2, 3]
137+
let MyCollection = [1, 2, 3]
138+
139+
"""
140+
141+
var configuration = Configuration.forTesting
142+
configuration.multilineCollectionTrailingCommas = false
143+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40, configuration: configuration)
144+
}
145+
146+
func testDictionaryCommasAbsentEnabled() {
147+
let input =
148+
"""
149+
let MyCollection = [
150+
"a": 1,
151+
"b": 2,
152+
"c": 3
153+
]
154+
155+
"""
156+
157+
let expected =
158+
"""
159+
let MyCollection = [
160+
"a": 1,
161+
"b": 2,
162+
"c": 3,
163+
]
164+
165+
"""
166+
167+
var configuration = Configuration.forTesting
168+
configuration.multilineCollectionTrailingCommas = true
169+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20, configuration: configuration)
170+
}
171+
172+
func testDictionaryCommasAbsentDisabled() {
173+
let input =
174+
"""
175+
let MyCollection = [
176+
"a": 1,
177+
"b": 2,
178+
"c": 3
179+
]
180+
181+
"""
182+
183+
let expected =
184+
"""
185+
let MyCollection = [
186+
"a": 1,
187+
"b": 2,
188+
"c": 3
189+
]
190+
191+
"""
192+
193+
var configuration = Configuration.forTesting
194+
configuration.multilineCollectionTrailingCommas = false
195+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20, configuration: configuration)
196+
}
197+
198+
func testDictionaryCommasPresentEnabled() {
199+
let input =
200+
"""
201+
let MyCollection = [
202+
"a": 1,
203+
"b": 2,
204+
"c": 3,
205+
]
206+
207+
"""
208+
209+
let expected =
210+
"""
211+
let MyCollection = [
212+
"a": 1,
213+
"b": 2,
214+
"c": 3,
215+
]
216+
217+
"""
218+
219+
var configuration = Configuration.forTesting
220+
configuration.multilineCollectionTrailingCommas = true
221+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20, configuration: configuration)
222+
}
223+
224+
func testDictionaryCommasPresentDisabled() {
225+
let input =
226+
"""
227+
let MyCollection = [
228+
"a": 1,
229+
"b": 2,
230+
"c": 3,
231+
]
232+
233+
"""
234+
235+
let expected =
236+
"""
237+
let MyCollection = [
238+
"a": 1,
239+
"b": 2,
240+
"c": 3
241+
]
242+
243+
"""
244+
245+
var configuration = Configuration.forTesting
246+
configuration.multilineCollectionTrailingCommas = false
247+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20, configuration: configuration)
248+
}
249+
250+
func testDictionarySingleLineCommasPresentDisabled() {
251+
let input =
252+
"""
253+
let MyCollection = ["a": 1, "b": 2, "c": 3,]
254+
255+
"""
256+
257+
let expected =
258+
"""
259+
let MyCollection = [
260+
"a": 1, "b": 2, "c": 3,
261+
]
262+
263+
"""
264+
265+
var configuration = Configuration.forTesting
266+
configuration.multilineCollectionTrailingCommas = true
267+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40, configuration: configuration)
268+
}
269+
270+
func testDictionarySingleLineCommasPresentEnabled() {
271+
let input =
272+
"""
273+
let MyCollection = ["a": 1, "b": 2, "c": 3,]
274+
275+
"""
276+
277+
let expected =
278+
"""
279+
let MyCollection = [
280+
"a": 1, "b": 2, "c": 3
281+
]
138282
139283
"""
140284

0 commit comments

Comments
 (0)