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
Copy file name to clipboardExpand all lines: README.md
+121Lines changed: 121 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1649,6 +1649,127 @@ _You can enable the following settings in Xcode by running [this script](resourc
1649
1649
1650
1650
</details>
1651
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.** [](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
+
classPlanet {
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
+
functerraform() {
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
+
classPlanet {
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
+
functerraform() {
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
+
functerraform() {
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
+
enumFeatureFlags {
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
+
structLegacyGeocentricUniverseModel {
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
+
enumPlanet {
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
+
enumPlanet {
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
+
1652
1773
*<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 `*/`) [](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#spaceAroundComments) [](https://github.com/nicklockwood/SwiftFormat/blob/master/Rules.md#spaceInsideComments)
0 commit comments