You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Update trailing commas rule to apply to all multi-element comma-separated lists (#323)
#### Summary
This PR updates the trailing commas rule to apply to all comma-separated
lists, including function calls and function declarations.
```swift
let terrestrialPlanets = [
mercury,
venus,
earth,
mars,
]
func buildSolarSystem(
innerPlanets: [Planet],
outerPlanets: [Planet],
) { ... }
buildSolarSystem(
innertPlanets: terrestrialPlanets,
outerPlanets: gasGiants,
)
```
This PR also refines the rule to omit the trailing comma if the list
only contains a single element. We feel this is especially the right
choice in function calls with only a single argument, where the trailing
commas adds unnecessary noise:
```swift
// WRONG
let planetsWithLife = [
earth,
]
func buildSolarSystem(
_ planets: [Planet],
)
buildSolarSystem(
terrestrialPlanets + gasGiants,
)
// RIGHT
let planetsWithLife = [
earth
]
func buildSolarSystem(
_ planets: [Planet]
) { ... }
buildSolarSystem(
terrestrialPlanets + gasGiants
)
```
<!--- required --->
Copy file name to clipboardExpand all lines: README.md
+61-9Lines changed: 61 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -526,26 +526,78 @@ _You can enable the following settings in Xcode by running [this script](resourc
526
526
527
527
</details>
528
528
529
-
* <a id='trailing-comma-array'></a>(<a href='#trailing-comma-array'>link</a>) **Add a trailing comma on the last element of a multi-line array.** [](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#trailingCommas)
529
+
* <a id='trailing-commas'></a>(<a href='#trailing-commas'>link</a>) **Add a trailing comma after the last element of multi-line, multi-element comma-separated lists.* This includes arrays, dictionaries, function declarations, function calls, etc. Don't include a trailing comma if the list spans only a single line, or contains only a single element. [](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#trailingCommas)
530
530
531
531
<details>
532
532
533
533
```swift
534
534
// WRONG
535
-
let rowContent = [
536
-
listingUrgencyDatesRowContent(),
537
-
listingUrgencyBookedRowContent(),
538
-
listingUrgencyBookedShortRowContent()
535
+
let terrestrialPlanets = [
536
+
mercury,
537
+
venus,
538
+
earth,
539
+
mars
539
540
]
540
541
542
+
func buildSolarSystem(
543
+
innerPlanets: [Planet],
544
+
outerPlanets: [Planet]
545
+
) { ... }
546
+
547
+
buildSolarSystem(
548
+
innertPlanets: terrestrialPlanets,
549
+
outerPlanets: gasGiants
550
+
)
551
+
541
552
// RIGHT
542
-
let rowContent = [
543
-
listingUrgencyDatesRowContent(),
544
-
listingUrgencyBookedRowContent(),
545
-
listingUrgencyBookedShortRowContent(),
553
+
let terrestrialPlanets = [
554
+
mercury,
555
+
venus,
556
+
earth,
557
+
mars,
558
+
]
559
+
560
+
func buildSolarSystem(
561
+
innerPlanets: [Planet],
562
+
outerPlanets: [Planet],
563
+
) { ... }
564
+
565
+
buildSolarSystem(
566
+
innertPlanets: terrestrialPlanets,
567
+
outerPlanets: gasGiants,
568
+
)
569
+
```
570
+
571
+
```swift
572
+
// WRONG: Omit the trailing comma in single-element lists.
573
+
let planetsWithLife = [
574
+
earth,
546
575
]
576
+
577
+
func buildSolarSystem(
578
+
_ planets: [Planet],
579
+
)
580
+
581
+
buildSolarSystem(
582
+
terrestrialPlanets + gasGiants,
583
+
)
584
+
585
+
// RIGHT
586
+
let planetsWithLife = [
587
+
earth
588
+
]
589
+
590
+
func buildSolarSystem(
591
+
_ planets: [Planet]
592
+
) { ... }
593
+
594
+
buildSolarSystem(
595
+
terrestrialPlanets + gasGiants
596
+
)
547
597
```
548
598
599
+
</details>
600
+
549
601
* <a id='no-space-inside-collection-brackets'></a>(<a href='#no-space-inside-brackets'>link</a>) **There should be no spaces inside the brackets of collection literals.** [](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#spaceInsideBrackets)
0 commit comments