Skip to content

Commit 32f42fd

Browse files
committed
Update rule to only apply to comments before declarations and continue permitting doc comments elsewhere
1 parent f6dbf7e commit 32f42fd

File tree

2 files changed

+92
-14
lines changed

2 files changed

+92
-14
lines changed

README.md

+91-14
Original file line numberDiff line numberDiff line change
@@ -1647,7 +1647,9 @@ _You can enable the following settings in Xcode by running [this script](resourc
16471647
}
16481648
```
16491649

1650-
* <a id='doc-comments'></a>(<a href='#doc-comments'>link</a>) Use doc comments (`///`) before declarations, and regular comments (`//`) elsewhere. [![SwiftFormat: docComments](https://img.shields.io/badge/SwiftFormat-docComments-7B0051.svg)](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#docComments)
1650+
</details>
1651+
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)
16511653

16521654
<details>
16531655

@@ -1656,16 +1658,18 @@ _You can enable the following settings in Xcode by running [this script](resourc
16561658

16571659
// A planet that exists somewhere in the universe.
16581660
class Planet {
1659-
// Properties of a planet that are important for life
1660-
let atmosphere: Atmosphere
1661-
let oceans: [Ocean]
1661+
// Data about the composition and density of the planet's atmosphere if present.
1662+
var atmosphere: Atmosphere?
16621663

1663-
// Terraforms the planet, by adding an atmosphere and ocean that is hospitable for life.
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.
16641668
func terraform() {
1665-
/// This gas concentration has a pretty good track record so far
1669+
// This gas concentration has a pretty good track record so far
16661670
let configuration = AtmosphereConfiguration(nitrogen: 0.78, oxygen: 0.22)
1667-
1668-
/// Generate the atmosphere first, then the oceans
1671+
1672+
// Generate the atmosphere first, then the oceans. Otherwise, the water will just boil off immediately.
16691673
generateAtmosphere(using: configuration)
16701674
generateOceans()
16711675
}
@@ -1675,20 +1679,93 @@ _You can enable the following settings in Xcode by running [this script](resourc
16751679

16761680
/// A planet that exists somewhere in the universe.
16771681
class Planet {
1678-
/// Properties of a planet that are important for life
1679-
let atmosphere: Atmosphere
1680-
let oceans: [Ocean]
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]
16811687

16821688
/// Terraforms the planet, by adding an atmosphere and ocean that is hospitable for life.
16831689
func terraform() {
1684-
// This gas concentration has a pretty good track record so far
1690+
// This gas concentration has a pretty good track record so far.
16851691
let configuration = AtmosphereConfiguration(nitrogen: 0.78, oxygen: 0.22)
1686-
1687-
// Generate the atmosphere first, then the oceans
1692+
1693+
// Generate the atmosphere first, then the oceans. Otherwise, the water will just boil off immediately.
16881694
generateAtmosphere(using: configuration)
16891695
generateOceans()
16901696
}
16911697
}
1698+
1699+
// ALSO RIGHT:
1700+
1701+
func terraform() {
1702+
/// This gas concentration 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 configuration = AtmosphereConfiguration(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: configuration)
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+
}
16921769
```
16931770

16941771
</details>

Sources/AirbnbSwiftFormatTool/airbnb.swiftformat

+1
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

0 commit comments

Comments
 (0)