Skip to content

Commit 200fa5a

Browse files
authored
Merge pull request swiftlang#151 from dylansturg/no_lonely_commas
Absorb trailing comma of function call args in grouped exprs.
2 parents 9cbfbcb + 096078e commit 200fa5a

File tree

9 files changed

+175
-0
lines changed

9 files changed

+175
-0
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,6 +699,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
699699
before(node.firstToken, tokens: .open)
700700
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
701701
after(node.lastToken, tokens: .close)
702+
if let trailingComma = node.trailingComma {
703+
closingDelimiterTokens.insert(trailingComma)
704+
}
702705
}
703706

704707
override func visit(_ node: ArrayExprSyntax) -> SyntaxVisitorContinueKind {
@@ -729,6 +732,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
729732
override func visit(_ node: ArrayElementSyntax) -> SyntaxVisitorContinueKind {
730733
before(node.firstToken, tokens: .open)
731734
after(node.lastToken, tokens: .close)
735+
if let trailingComma = node.trailingComma {
736+
closingDelimiterTokens.insert(trailingComma)
737+
}
732738
return .visitChildren
733739
}
734740

@@ -766,6 +772,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
766772
before(node.firstToken, tokens: .open)
767773
after(node.colon, tokens: .break(.continue, newlines: .elective(ignoresDiscretionary: true)))
768774
after(node.lastToken, tokens: .close)
775+
if let trailingComma = node.trailingComma {
776+
closingDelimiterTokens.insert(trailingComma)
777+
}
769778
return .visitChildren
770779
}
771780

@@ -837,6 +846,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
837846

838847
let shouldGroupAroundArgument = !isCompactSingleFunctionCallArgument(arguments)
839848
for argument in arguments {
849+
if let trailingComma = argument.trailingComma {
850+
closingDelimiterTokens.insert(trailingComma)
851+
}
840852
arrangeAsFunctionCallArgument(argument, shouldGroup: shouldGroupAroundArgument)
841853
}
842854
}
@@ -1626,6 +1638,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
16261638
// If this is one of multiple comma-delimited bindings, move any pending close breaks to
16271639
// follow the comma so that it doesn't get separated from the tokens before it.
16281640
closeAfterToken = trailingComma
1641+
closingDelimiterTokens.insert(trailingComma)
16291642
}
16301643

16311644
if closeAfterToken != nil && closesNeeded > 0 {

Tests/SwiftFormatPrettyPrintTests/ArrayDeclTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,27 @@ final class ArrayDeclTests: PrettyPrintTestCase {
111111
XCTAssertDiagnosed(.removeTrailingComma, line: 1, column: 17)
112112
XCTAssertDiagnosed(.addTrailingComma, line: 4, column: 26)
113113
}
114+
115+
func testGroupsTrailingComma() {
116+
let input =
117+
"""
118+
let a = [
119+
condition ? firstOption : secondOption,
120+
bar(),
121+
]
122+
"""
123+
124+
let expected =
125+
"""
126+
let a = [
127+
condition
128+
? firstOption
129+
: secondOption,
130+
bar(),
131+
]
132+
133+
"""
134+
135+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 32)
136+
}
114137
}

Tests/SwiftFormatPrettyPrintTests/DictionaryDeclTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,27 @@ final class DictionaryDeclTests: PrettyPrintTestCase {
133133

134134
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20)
135135
}
136+
137+
func testGroupsTrailingComma() {
138+
let input =
139+
"""
140+
let d = [
141+
key: cond ? firstOption : secondOption,
142+
key2: bar(),
143+
]
144+
"""
145+
146+
let expected =
147+
"""
148+
let d = [
149+
key: cond
150+
? firstOption
151+
: secondOption,
152+
key2: bar(),
153+
]
154+
155+
"""
156+
157+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 32)
158+
}
136159
}

Tests/SwiftFormatPrettyPrintTests/FunctionCallTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,25 @@ final class FunctionCallTests: PrettyPrintTestCase {
249249

250250
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20)
251251
}
252+
253+
func testGroupsTrailingComma() {
254+
let input =
255+
"""
256+
foo(
257+
image: useLongName ? image(named: .longNameImage) : image(named: .veryLongNameImageZ),
258+
bar: bar)
259+
"""
260+
261+
let expected =
262+
"""
263+
foo(
264+
image: useLongName
265+
? image(named: .longNameImage)
266+
: image(named: .veryLongNameImageZ),
267+
bar: bar)
268+
269+
"""
270+
271+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 70)
272+
}
252273
}

Tests/SwiftFormatPrettyPrintTests/ObjectLiteralExprTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,25 @@ final class ObjectLiteralExprTests: PrettyPrintTestCase {
110110
config.lineBreakBeforeEachArgument = false
111111
assertPrettyPrintEqual(input: input, expected: expected, linelength: 38, configuration: config)
112112
}
113+
114+
func testGroupsTrailingComma() {
115+
let input =
116+
"""
117+
#imageLiteral(
118+
image: useLongName ? image(named: .longNameImage) : image(named: .veryLongNameImageZ),
119+
bar: bar)
120+
"""
121+
122+
let expected =
123+
"""
124+
#imageLiteral(
125+
image: useLongName
126+
? image(named: .longNameImage)
127+
: image(named: .veryLongNameImageZ),
128+
bar: bar)
129+
130+
"""
131+
132+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 70)
133+
}
113134
}

Tests/SwiftFormatPrettyPrintTests/PatternBindingTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,27 @@ final class PatternBindingTests: PrettyPrintTestCase {
4343

4444
assertPrettyPrintEqual(input: input, expected: expected, linelength: 28)
4545
}
46+
47+
func testGroupingIncludesTrailingComma() {
48+
let input =
49+
"""
50+
let foo = veryLongCondition
51+
? firstOption
52+
: secondOption,
53+
bar = bar()
54+
"""
55+
56+
let expected =
57+
"""
58+
let
59+
foo =
60+
veryLongCondition
61+
? firstOption
62+
: secondOption,
63+
bar = bar()
64+
65+
"""
66+
67+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 18)
68+
}
4669
}

Tests/SwiftFormatPrettyPrintTests/SubscriptExprTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,25 @@ final class SubscriptExprTests: PrettyPrintTestCase {
8585

8686
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
8787
}
88+
89+
func testGroupsTrailingComma() {
90+
let input =
91+
"""
92+
myCollection[
93+
image: useLongName ? image(named: .longNameImage) : image(named: .veryLongNameImageZ),
94+
bar: bar]
95+
"""
96+
97+
let expected =
98+
"""
99+
myCollection[
100+
image: useLongName
101+
? image(named: .longNameImage)
102+
: image(named: .veryLongNameImageZ),
103+
bar: bar]
104+
105+
"""
106+
107+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 70)
108+
}
88109
}

Tests/SwiftFormatPrettyPrintTests/TupleDeclTests.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,27 @@ final class TupleDeclTests: PrettyPrintTestCase {
7878

7979
assertPrettyPrintEqual(input: input, expected: expected, linelength: 25)
8080
}
81+
82+
func testGroupsTrailingComma() {
83+
let input =
84+
"""
85+
let t = (
86+
condition ? firstOption : secondOption,
87+
bar()
88+
)
89+
"""
90+
91+
let expected =
92+
"""
93+
let t = (
94+
condition
95+
? firstOption
96+
: secondOption,
97+
bar()
98+
)
99+
100+
"""
101+
102+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 32)
103+
}
81104
}

Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift

Lines changed: 7 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+
("testGroupsTrailingComma", testGroupsTrailingComma),
2728
("testNoTrailingCommasInTypes", testNoTrailingCommasInTypes),
2829
("testTrailingCommaDiagnostics", testTrailingCommaDiagnostics),
2930
("testWhitespaceOnlyDoesNotChangeTrailingComma", testWhitespaceOnlyDoesNotChangeTrailingComma),
@@ -181,6 +182,7 @@ extension DictionaryDeclTests {
181182
// to regenerate.
182183
static let __allTests__DictionaryDeclTests = [
183184
("testBasicDictionaries", testBasicDictionaries),
185+
("testGroupsTrailingComma", testGroupsTrailingComma),
184186
("testIgnoresDiscretionaryNewlineAfterColon", testIgnoresDiscretionaryNewlineAfterColon),
185187
("testNoTrailingCommasInTypes", testNoTrailingCommasInTypes),
186188
("testTrailingCommaDiagnostics", testTrailingCommaDiagnostics),
@@ -270,6 +272,7 @@ extension FunctionCallTests {
270272
("testBasicFunctionCalls_packArguments", testBasicFunctionCalls_packArguments),
271273
("testDiscretionaryLineBreakBeforeClosingParenthesis", testDiscretionaryLineBreakBeforeClosingParenthesis),
272274
("testDiscretionaryLineBreaksAreSelfCorrecting", testDiscretionaryLineBreaksAreSelfCorrecting),
275+
("testGroupsTrailingComma", testGroupsTrailingComma),
273276
("testIgnoresDiscretionaryLineBreakAfterColon", testIgnoresDiscretionaryLineBreakAfterColon),
274277
("testNestedFunctionCallExprSequences", testNestedFunctionCallExprSequences),
275278
("testSingleUnlabeledArgumentWithDelimiters", testSingleUnlabeledArgumentWithDelimiters),
@@ -473,6 +476,7 @@ extension ObjectLiteralExprTests {
473476
static let __allTests__ObjectLiteralExprTests = [
474477
("testColorLiteral_noPackArguments", testColorLiteral_noPackArguments),
475478
("testColorLiteral_packArguments", testColorLiteral_packArguments),
479+
("testGroupsTrailingComma", testGroupsTrailingComma),
476480
("testImageLiteral_noPackArguments", testImageLiteral_noPackArguments),
477481
("testImageLiteral_packArguments", testImageLiteral_packArguments),
478482
]
@@ -508,6 +512,7 @@ extension PatternBindingTests {
508512
// to regenerate.
509513
static let __allTests__PatternBindingTests = [
510514
("testBindingIncludingTypeAnnotation", testBindingIncludingTypeAnnotation),
515+
("testGroupingIncludesTrailingComma", testGroupingIncludesTrailingComma),
511516
("testIgnoresDiscretionaryNewlineAfterColon", testIgnoresDiscretionaryNewlineAfterColon),
512517
]
513518
}
@@ -660,6 +665,7 @@ extension SubscriptExprTests {
660665
static let __allTests__SubscriptExprTests = [
661666
("testBasicSubscriptGetters", testBasicSubscriptGetters),
662667
("testBasicSubscriptSetters", testBasicSubscriptSetters),
668+
("testGroupsTrailingComma", testGroupsTrailingComma),
663669
("testSubscriptGettersWithTrailingClosures", testSubscriptGettersWithTrailingClosures),
664670
("testSubscriptSettersWithTrailingClosures", testSubscriptSettersWithTrailingClosures),
665671
]
@@ -715,6 +721,7 @@ extension TupleDeclTests {
715721
// to regenerate.
716722
static let __allTests__TupleDeclTests = [
717723
("testBasicTuples", testBasicTuples),
724+
("testGroupsTrailingComma", testGroupsTrailingComma),
718725
("testIgnoresDiscretionaryNewlineAfterColon", testIgnoresDiscretionaryNewlineAfterColon),
719726
("testLabeledTuples", testLabeledTuples),
720727
]

0 commit comments

Comments
 (0)