Skip to content

Commit

Permalink
Preserve trailing comments for opening_brace rule (realm#5780)
Browse files Browse the repository at this point in the history
  • Loading branch information
mildm8nnered authored Sep 8, 2024
1 parent 06e4e3c commit 9e78054
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
[SimplyDanny](https://github.com/SimplyDanny)
[#4754](https://github.com/realm/SwiftLint/issues/4754)

* Trailing comments are now preserved by the `opening_brace` rule when
rewriting.
[Martin Redington](https://github.com/mildm8nnered)
[#5751](https://github.com/realm/SwiftLint/issues/5751)

## 0.56.2: Heat Pump Dryer

#### Breaking
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,11 @@ private extension OpeningBraceRule {
let previousLocation = previousToken.endLocation(converter: locationConverter)
let leftBraceLocation = leftBrace.startLocation(converter: locationConverter)
if previousLocation.line != leftBraceLocation.line {
let trailingCommentText = previousToken.trailingTrivia.description.trimmingCharacters(in: .whitespaces)
return .init(
start: previousToken.endPositionBeforeTrailingTrivia,
end: openingPosition,
replacement: " "
end: openingPosition.advanced(by: trailingCommentText.isNotEmpty ? 1 : 0),
replacement: trailingCommentText.isNotEmpty ? " { \(trailingCommentText)" : " "
)
}
if previousLocation.column + 1 == leftBraceLocation.column {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,28 @@ struct OpeningBraceRuleExamples {
}
"""),
// https://github.com/realm/SwiftLint/issues/5598
Example("""
if c // A comment
{
return
}
"""): Example("""
if c { // A comment
return
}
"""),
// https://github.com/realm/SwiftLint/issues/5751
Example("""
if c // A comment
{ // Another comment
return
}
"""): Example("""
if c { // A comment // Another comment
return
}
"""),
// https://github.com/realm/SwiftLint/issues/5751
Example("""
func foo() {
if q1, q2
Expand Down

0 comments on commit 9e78054

Please sign in to comment.