Skip to content

Commit 6dc8fc2

Browse files
authored
Merge pull request #125 from dylansturg/who_is_type_identifier_i_am_expr
No trailing commas in array/dict initializers.
2 parents 9ed28c7 + d3789e5 commit 6dc8fc2

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,12 @@ private final class TokenStreamCreator: SyntaxVisitor {
707707

708708
func visit(_ node: ArrayElementListSyntax) -> SyntaxVisitorContinueKind {
709709
insertTokens(.break(.same), betweenElementsOf: node)
710-
if let firstElement = node.first, let lastElement = node.last {
710+
711+
// The syntax library can't distinguish an array initializer (where the elements are types) from
712+
// an array literal (where the elements are the contents of the array). We never want to add a
713+
// trailing comma in the initializer case and there's always exactly 1 element, so we never add
714+
// a trailing comma to 1 element arrays.
715+
if let firstElement = node.first, let lastElement = node.last, firstElement != lastElement {
711716
before(firstElement.firstToken, tokens: .commaDelimitedRegionStart)
712717
let endToken = Token.commaDelimitedRegionEnd(
713718
hasTrailingComma: lastElement.trailingComma != nil, position: lastElement.endPosition)
@@ -733,7 +738,12 @@ private final class TokenStreamCreator: SyntaxVisitor {
733738

734739
func visit(_ node: DictionaryElementListSyntax) -> SyntaxVisitorContinueKind {
735740
insertTokens(.break(.same), betweenElementsOf: node)
736-
if let firstElement = node.first, let lastElement = node.last {
741+
742+
// The syntax library can't distinguish a dictionary initializer (where the elements are types)
743+
// from a dictionary literal (where the elements are the contents of the dictionary). We never
744+
// want to add a trailing comma in the initializer case and there's always exactly 1 element,
745+
// so we never add a trailing comma to 1 element dictionaries.
746+
if let firstElement = node.first, let lastElement = node.last, firstElement != lastElement {
737747
before(firstElement.firstToken, tokens: .commaDelimitedRegionStart)
738748
let endToken = Token.commaDelimitedRegionEnd(
739749
hasTrailingComma: lastElement.trailingComma != nil, position: lastElement.endPosition)

Tests/SwiftFormatPrettyPrintTests/ArrayDeclTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@ public class ArrayDeclTests: PrettyPrintTestCase {
6565
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
6666
}
6767

68+
public func testNoTrailingCommasInTypes() {
69+
let input =
70+
"""
71+
let a = [SomeSuperMegaLongTypeName]()
72+
"""
73+
74+
let expected =
75+
"""
76+
let a = [
77+
SomeSuperMegaLongTypeName
78+
]()
79+
80+
"""
81+
82+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 30)
83+
}
84+
6885
public func testWhitespaceOnlyDoesNotChangeTrailingComma() {
6986
let input =
7087
"""

Tests/SwiftFormatPrettyPrintTests/DictionaryDeclTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,23 @@ public class DictionaryDeclTests: PrettyPrintTestCase {
4848
assertPrettyPrintEqual(input: input, expected: expected, linelength: 50)
4949
}
5050

51+
public func testNoTrailingCommasInTypes() {
52+
let input =
53+
"""
54+
let a = [SomeVeryLongKeyType: SomePrettyLongValueType]()
55+
"""
56+
57+
let expected =
58+
"""
59+
let a = [
60+
SomeVeryLongKeyType: SomePrettyLongValueType
61+
]()
62+
63+
"""
64+
65+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 50)
66+
}
67+
5168
public func testWhitespaceOnlyDoesNotChangeTrailingComma() {
5269
let input =
5370
"""

Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ extension ArrayDeclTests {
2424
static let __allTests__ArrayDeclTests = [
2525
("testArrayOfFunctions", testArrayOfFunctions),
2626
("testBasicArrays", testBasicArrays),
27+
("testNoTrailingCommasInTypes", testNoTrailingCommasInTypes),
2728
("testTrailingCommaDiagnostics", testTrailingCommaDiagnostics),
2829
("testWhitespaceOnlyDoesNotChangeTrailingComma", testWhitespaceOnlyDoesNotChangeTrailingComma),
2930
]
@@ -178,6 +179,7 @@ extension DictionaryDeclTests {
178179
// to regenerate.
179180
static let __allTests__DictionaryDeclTests = [
180181
("testBasicDictionaries", testBasicDictionaries),
182+
("testNoTrailingCommasInTypes", testNoTrailingCommasInTypes),
181183
("testTrailingCommaDiagnostics", testTrailingCommaDiagnostics),
182184
("testWhitespaceOnlyDoesNotChangeTrailingComma", testWhitespaceOnlyDoesNotChangeTrailingComma),
183185
]

0 commit comments

Comments
 (0)