Skip to content

Commit 4a3def9

Browse files
authored
Merge pull request #798 from TTOzzi/fix-multiElementCollectionTrailingCommas
Fix extraction of trailing comments only when necessary during afterToken addition
2 parents 7bca483 + 3f5d15a commit 4a3def9

File tree

2 files changed

+106
-2
lines changed

2 files changed

+106
-2
lines changed

Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift

+10-2
Original file line numberDiff line numberDiff line change
@@ -2929,8 +2929,16 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
29292929
var shouldExtractTrailingComment = false
29302930
if wasLineComment && !hasAppendedTrailingComment {
29312931
switch afterToken {
2932-
case .break, .printerControl: shouldExtractTrailingComment = true
2933-
default: break
2932+
case let .break(kind, _, _):
2933+
if case let .close(mustBreak) = kind {
2934+
shouldExtractTrailingComment = mustBreak
2935+
} else {
2936+
shouldExtractTrailingComment = true
2937+
}
2938+
case .printerControl:
2939+
shouldExtractTrailingComment = true
2940+
default:
2941+
break
29342942
}
29352943
}
29362944
if shouldExtractTrailingComment {

Tests/SwiftFormatTests/PrettyPrint/CommaTests.swift

+96
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,102 @@ final class CommaTests: PrettyPrintTestCase {
143143
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40, configuration: configuration)
144144
}
145145

146+
func testArrayWithCommentCommasPresentEnabled() {
147+
let input =
148+
"""
149+
let MyCollection = [
150+
1,
151+
2 // some comment
152+
]
153+
154+
"""
155+
156+
let expected =
157+
"""
158+
let MyCollection = [
159+
1,
160+
2, // some comment
161+
]
162+
163+
"""
164+
165+
var configuration = Configuration.forTesting
166+
configuration.multiElementCollectionTrailingCommas = true
167+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40, configuration: configuration)
168+
}
169+
170+
func testArrayWithCommentCommasPresentDisabled() {
171+
let input =
172+
"""
173+
let MyCollection = [
174+
1,
175+
2 // some comment
176+
]
177+
178+
"""
179+
180+
let expected =
181+
"""
182+
let MyCollection = [
183+
1,
184+
2 // some comment
185+
]
186+
187+
"""
188+
189+
var configuration = Configuration.forTesting
190+
configuration.multiElementCollectionTrailingCommas = false
191+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40, configuration: configuration)
192+
}
193+
194+
func testArrayWithTernaryOperatorAndCommentCommasPresentEnabled() {
195+
let input =
196+
"""
197+
let MyCollection = [
198+
1,
199+
true ? 1 : 2 // some comment
200+
]
201+
202+
"""
203+
204+
let expected =
205+
"""
206+
let MyCollection = [
207+
1,
208+
true ? 1 : 2, // some comment
209+
]
210+
211+
"""
212+
213+
var configuration = Configuration.forTesting
214+
configuration.multiElementCollectionTrailingCommas = true
215+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40, configuration: configuration)
216+
}
217+
218+
func testArrayWithTernaryOperatorAndCommentCommasPresentDisabled() {
219+
let input =
220+
"""
221+
let MyCollection = [
222+
1,
223+
true ? 1 : 2 // some comment
224+
]
225+
226+
"""
227+
228+
let expected =
229+
"""
230+
let MyCollection = [
231+
1,
232+
true ? 1 : 2 // some comment
233+
]
234+
235+
"""
236+
237+
var configuration = Configuration.forTesting
238+
configuration.multiElementCollectionTrailingCommas = false
239+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40, configuration: configuration)
240+
}
241+
146242
func testDictionaryCommasAbsentEnabled() {
147243
let input =
148244
"""

0 commit comments

Comments
 (0)