Skip to content

Commit a702f58

Browse files
authored
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 --->
1 parent ad689bf commit a702f58

File tree

4 files changed

+68
-16
lines changed

4 files changed

+68
-16
lines changed

Package.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ let package = Package(
55
name: "AirbnbSwift",
66
platforms: [.macOS(.v10_13)],
77
products: [
8-
.plugin(name: "FormatSwift", targets: ["FormatSwift"]),
8+
.plugin(name: "FormatSwift", targets: ["FormatSwift"])
99
],
1010
dependencies: [
11-
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.3"),
11+
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.0.3")
1212
],
1313
targets: [
1414
.plugin(
@@ -19,7 +19,7 @@ let package = Package(
1919
description: "Formats Swift source files according to the Airbnb Swift Style Guide"
2020
),
2121
permissions: [
22-
.writeToPackageDirectory(reason: "Format Swift source files"),
22+
.writeToPackageDirectory(reason: "Format Swift source files")
2323
]
2424
),
2525
dependencies: [
@@ -32,7 +32,7 @@ let package = Package(
3232
.executableTarget(
3333
name: "AirbnbSwiftFormatTool",
3434
dependencies: [
35-
.product(name: "ArgumentParser", package: "swift-argument-parser"),
35+
.product(name: "ArgumentParser", package: "swift-argument-parser")
3636
],
3737
resources: [
3838
.process("airbnb.swiftformat"),

Plugins/FormatSwift/Plugin.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ extension Package {
209209
guard let projectDirectory = URL(string: directory.string) else { return [] }
210210

211211
var supportedSwiftVersions = [
212-
SwiftVersion(major: toolsVersion.major, minor: toolsVersion.minor),
212+
SwiftVersion(major: toolsVersion.major, minor: toolsVersion.minor)
213213
]
214214

215215
// Look for all of the package manifest files in the directory root

README.md

Lines changed: 61 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -526,26 +526,78 @@ _You can enable the following settings in Xcode by running [this script](resourc
526526

527527
</details>
528528

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.** [![SwiftFormat: trailingCommas](https://img.shields.io/badge/SwiftFormat-trailingCommas-7B0051.svg)](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. [![SwiftFormat: trailingCommas](https://img.shields.io/badge/SwiftFormat-trailingCommas-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#trailingCommas)
530530

531531
<details>
532532

533533
```swift
534534
// WRONG
535-
let rowContent = [
536-
listingUrgencyDatesRowContent(),
537-
listingUrgencyBookedRowContent(),
538-
listingUrgencyBookedShortRowContent()
535+
let terrestrialPlanets = [
536+
mercury,
537+
venus,
538+
earth,
539+
mars
539540
]
540541

542+
func buildSolarSystem(
543+
innerPlanets: [Planet],
544+
outerPlanets: [Planet]
545+
) { ... }
546+
547+
buildSolarSystem(
548+
innertPlanets: terrestrialPlanets,
549+
outerPlanets: gasGiants
550+
)
551+
541552
// 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,
546575
]
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+
)
547597
```
548598

599+
</details>
600+
549601
* <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.** [![SwiftFormat: spaceInsideBrackets](https://img.shields.io/badge/SwiftFormat-spaceInsideBrackets-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/main/Rules.md#spaceInsideBrackets)
550602

551603
<details>

Sources/AirbnbSwiftFormatTool/airbnb.swiftformat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
--exclude Carthage,Pods,.build
33

44
# options
5-
--swift-version 6.0
5+
--swift-version 6.1
66
--language-mode 5
77
--self remove # redundantSelf
88
--import-grouping testable-bottom # sortedImports
9-
--trailing-commas always # trailingCommas
9+
--trailing-commas multi-element-lists # trailingCommas
1010
--trim-whitespace always # trailingSpace
1111
--indent 2 #indent
1212
--ifdef no-indent #indent

0 commit comments

Comments
 (0)