Add rule to use doc comments before declarations #262
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Please react with 👍/👎 below if you agree or disagree with this proposal.
Summary
This PR proposes a new rule to use doc comments (
///
) instead of regular comments (//
) before declarations within type bodies or at the top level.Reasoning
This rule was originally proposed in #198 (Sept 2022). We closed that PR following @swiftal64's thoughtful feedback here: #198 (comment).
There are a few changes to this new proposal that resolves the issues @swiftal64 identified with the previous approach.
We never autocorrect doc comments into regular comments. We continue to permit using doc comments elsewhere in the code, even if the comment doesn't precede a declaration in a type body or at the top level. This is supported by a new
--doccomments preserve
option added to SwiftFormat in nicklockwood/SwiftFormat#1638.The autocorrect implementation intelligently handles comment directives like
// swiftformat:
,// swiftlint:
,// sourcery:
,// MARK:
and// TODO:
. These examples are left as regular comments:We continue allowing regular comments before a grouped block of declarations, since it's possible that the comment refers to the block as a whole rather than just the following declaration. This was implemented in nicklockwood/SwiftFormat#1638.
These changes resolve all of the feedback that we received during the previous proposal review.