Skip to content

Commit

Permalink
Respect comments before opening braces in opening_brace rule (#5582)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimplyDanny committed May 15, 2024
1 parent 3a3ec07 commit dfe19ac
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
[SimplyDanny](https://github.com/SimplyDanny)
[#5571](https://github.com/realm/SwiftLint/issues/5571)

* Respect comments before opening brace in `opening_brace` rule when there is
one space before the brace after the comment. Everything else is still a
violation, yet the rewriter will not remove the comment anymore.
[SimplyDanny](https://github.com/SimplyDanny)
[#5578](https://github.com/realm/SwiftLint/issues/5578)

## 0.55.0: Universal Washing Powder

#### Breaking
Expand Down
22 changes: 19 additions & 3 deletions Source/SwiftLintBuiltInRules/Rules/Style/OpeningBraceRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,32 @@ private extension BracedSyntax {

func violationCorrection(_ locationConverter: SourceLocationConverter) -> ViolationCorrection? {
if let previousToken = leftBrace.previousToken(viewMode: .sourceAccurate) {
let triviaBetween = previousToken.trailingTrivia + leftBrace.leadingTrivia
let previousLocation = previousToken.endLocation(converter: locationConverter)
let leftBraceLocation = leftBrace.startLocation(converter: locationConverter)
if previousLocation.line != leftBraceLocation.line
|| previousLocation.column + 1 != leftBraceLocation.column {
let violation = ViolationCorrection(
start: previousToken.endPositionBeforeTrailingTrivia,
end: leftBrace.positionAfterSkippingLeadingTrivia,
replacement: " "
)
if previousLocation.line != leftBraceLocation.line {
return violation
}
if previousLocation.column + 1 == leftBraceLocation.column {
return nil
}
if triviaBetween.containsComments {
if triviaBetween.pieces.last == .spaces(1) {
return nil
}
let comment = triviaBetween.description.trimmingTrailingCharacters(in: .whitespaces)
return ViolationCorrection(
start: previousToken.endPositionBeforeTrailingTrivia,
start: previousToken.endPositionBeforeTrailingTrivia + SourceLength(of: comment),
end: leftBrace.positionAfterSkippingLeadingTrivia,
replacement: " "
)
}
return violation
}
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ struct OpeningBraceRuleExamples {
Example("""
if c {}
else {}
""")
"""),
Example("""
if c /* comment */ {
return
}
""")
]

static let triggeringExamples = [
Expand Down Expand Up @@ -210,7 +215,7 @@ struct OpeningBraceRuleExamples {
"""),
Example("""
if c ↓{}
else ↓{}
else /* comment */ ↓{}
""")
]

Expand Down Expand Up @@ -531,6 +536,15 @@ struct OpeningBraceRuleExamples {
precedencegroup Group {
assignment: true
}
""")
"""),
Example("""
if c /* comment */ {
return
}
"""): Example("""
if c /* comment */ {
return
}
""")
]
}
6 changes: 6 additions & 0 deletions Source/SwiftLintCore/Extensions/SwiftSyntax+SwiftLint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,12 @@ public extension Trivia {
}
}

var containsComments: Bool {
isNotEmpty && contains { piece in
!piece.isWhitespace && !piece.isNewline
}
}

var isSingleSpace: Bool {
self == .spaces(1)
}
Expand Down

0 comments on commit dfe19ac

Please sign in to comment.