Skip to content

Commit fa3ae57

Browse files
authored
Add rule to use doc comments before declarations (airbnb#262)
1 parent c6b77ca commit fa3ae57

File tree

3 files changed

+125
-2
lines changed

3 files changed

+125
-2
lines changed

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ let package = Package(
4242

4343
.binaryTarget(
4444
name: "SwiftFormat",
45-
url: "https://github.com/calda/SwiftFormat/releases/download/0.54-beta-4/SwiftFormat.artifactbundle.zip",
46-
checksum: "65335d1e059714d570ee6dbe76d3738fbae3a404dafb109371a6a55670b5bcd7"),
45+
url: "https://github.com/calda/SwiftFormat/releases/download/0.54-beta-5/SwiftFormat.artifactbundle.zip",
46+
checksum: "7447986db45a51164d23672c07f971406a4c0589b0c423fcb85e95ed8f8e7e48"),
4747

4848
.binaryTarget(
4949
name: "SwiftLintBinary",

README.md

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,127 @@ _You can enable the following settings in Xcode by running [this script](resourc
16491649

16501650
</details>
16511651

1652+
* <a id='doc-comments-before-declarations'></a>(<a href='#doc-comments-before-declarations'>link</a>) **Use doc comments (`///`) instead of regular comments (`//`) before declarations within type bodies or at the top level.** [![SwiftFormat: docComments](https://img.shields.io/badge/SwiftFormat-docComments-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#docComments)
1653+
1654+
<details>
1655+
1656+
```swift
1657+
// WRONG
1658+
1659+
// A planet that exists somewhere in the universe.
1660+
class Planet {
1661+
// Data about the composition and density of the planet's atmosphere if present.
1662+
var atmosphere: Atmosphere?
1663+
1664+
// Data about the size, location, and composition of large bodies of water on the planet's surface.
1665+
var oceans: [Ocean]
1666+
1667+
// Terraforms the planet, by adding an atmosphere and ocean that is hospitable for life.
1668+
func terraform() {
1669+
// This gas composition has a pretty good track record so far!
1670+
let composition = AtmosphereComposition(nitrogen: 0.78, oxygen: 0.22)
1671+
1672+
// Generate the atmosphere first, then the oceans. Otherwise, the water will just boil off immediately.
1673+
generateAtmosphere(using: composition)
1674+
generateOceans()
1675+
}
1676+
}
1677+
1678+
// RIGHT
1679+
1680+
/// A planet that exists somewhere in the universe.
1681+
class Planet {
1682+
/// Data about the composition and density of the planet's atmosphere if present.
1683+
var atmosphere: Atmosphere?
1684+
1685+
/// Data about the size, location, and composition of large bodies of water on the planet's surface.
1686+
var oceans: [Ocean]
1687+
1688+
/// Terraforms the planet, by adding an atmosphere and ocean that is hospitable for life.
1689+
func terraform() {
1690+
// This gas composition has a pretty good track record so far!
1691+
let composition = AtmosphereComposition(nitrogen: 0.78, oxygen: 0.22)
1692+
1693+
// Generate the atmosphere first, then the oceans. Otherwise, the water will just boil off immediately.
1694+
generateAtmosphere(using: composition)
1695+
generateOceans()
1696+
}
1697+
}
1698+
1699+
// ALSO RIGHT:
1700+
1701+
func terraform() {
1702+
/// This gas composition has a pretty good track record so far!
1703+
/// - Doc comments are not required before local declarations in function scopes, but are permitted.
1704+
let composition = AtmosphereComposition(nitrogen: 0.78, oxygen: 0.22)
1705+
1706+
/// Generate the `atmosphere` first, **then** the `oceans`. Otherwise, the water will just boil off immediately.
1707+
/// - Comments not preceeding declarations can use doc comments, and will not be autocorrected into regular comments.
1708+
/// This can be useful because Xcode applies markdown styling to doc comments but not regular comments.
1709+
generateAtmosphere(using: composition)
1710+
generateOceans()
1711+
}
1712+
```
1713+
1714+
Regular comments are permitted before declarations in some cases.
1715+
1716+
For example, comment directives like `// swiftformat:`, `// swiftlint:`, `// sourcery:`, `// MARK:` and `// TODO:` are typically required to use regular comments and don't work correctly with doc comments:
1717+
1718+
```swift
1719+
// RIGHT
1720+
1721+
// swiftformat:sort
1722+
enum FeatureFlags {
1723+
case allowFasterThanLightTravel
1724+
case disableGravity
1725+
case enableDarkEnergy
1726+
case enableDarkMatter
1727+
}
1728+
1729+
// TODO: There are no more production consumers of this legacy model, so we
1730+
// should detangle the remaining code dependencies and clean it up.
1731+
struct LegacyGeocentricUniverseModel {
1732+
...
1733+
}
1734+
```
1735+
1736+
Regular comments are also allowed before a grouped block of delcarations, since it's possible that the comment refers to the block as a whole rather than just the following declaration:
1737+
1738+
```swift
1739+
// RIGHT
1740+
1741+
enum Planet {
1742+
// The inner planets
1743+
case mercury
1744+
case venus
1745+
case earth
1746+
case mars
1747+
1748+
// The outer planets
1749+
case jupiter
1750+
case saturn
1751+
case uranus
1752+
case neptune
1753+
}
1754+
1755+
// ALSO RIGHT
1756+
1757+
enum Planet {
1758+
/// The smallest planet
1759+
case mercury
1760+
case venus
1761+
case earth
1762+
case mars
1763+
/// The largest planet
1764+
case jupiter
1765+
case saturn
1766+
case uranus
1767+
case neptune
1768+
}
1769+
```
1770+
1771+
</details>
1772+
16521773
* <a id='whitespace-around-comment-delimiters'></a>(<a href='#whitespace-around-comment-delimiters'>link</a>) Include spaces or newlines before and after comment delimiters (`//`, `///`, `/*`, and `*/`) [![SwiftFormat: spaceAroundComments](https://img.shields.io/badge/SwiftFormat-spaceAroundComments-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#spaceAroundComments) [![SwiftFormat: spaceInsideComments](https://img.shields.io/badge/SwiftFormat-spaceInsideComments-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#spaceInsideComments)
16531774

16541775
<details>

Sources/AirbnbSwiftFormatTool/airbnb.swiftformat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
--onelineforeach convert #preferForLoop
3939
--shortoptionals always #typeSugar
4040
--semicolons never #semicolons
41+
--doccomments preserve #docComments
4142

4243
# We recommend a max width of 100 but _strictly enforce_ a max width of 130
4344
--maxwidth 130 # wrap
@@ -84,6 +85,7 @@
8485
--rules spaceAroundParens
8586
--rules enumNamespaces
8687
--rules blockComments
88+
--rules docComments
8789
--rules spaceAroundComments
8890
--rules spaceInsideComments
8991
--rules blankLinesAtStartOfScope

0 commit comments

Comments
 (0)