Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@

### Bug Fixes

* Fix `vertical_whitespace_between_cases` false positives when comments separate cases.
[leno23](https://github.com/leno23)
[#6412](https://github.com/realm/SwiftLint/issues/6412)

* Avoid false positives in `prefer_self_in_static_references` for generic
constraints and generic parameter bounds such as `where A: P` and `<A: P>`
in classes and extensions.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Foundation
import SwiftBasicFormat
import SwiftSyntax

Expand Down Expand Up @@ -126,9 +127,52 @@ private extension VerticalWhitespaceBetweenCasesRule {
let currentIsOneLiner = currentCaseStartLine == currentCaseEndLine
let nextIsOneLiner = nextCaseStartLine == nextCaseEndLine

// Skip if both are one-liners on consecutive lines.
return currentIsOneLiner && nextIsOneLiner && nextCaseStartLine == currentCaseStartLine + 1
guard currentIsOneLiner, nextIsOneLiner else {
return false
}

// Skip if both are one-liners with only empty or comment-only lines between them.
if nextCaseStartLine == currentCaseStartLine + 1 {
return true
}

return linesBetweenContainOnlyCommentsOrAreEmpty(
startingLine: currentCaseEndLine + 1,
endingLine: nextCaseStartLine - 1
)
}

private func linesBetweenContainOnlyCommentsOrAreEmpty(startingLine: Int, endingLine: Int) -> Bool {
guard startingLine <= endingLine else {
return true
}

for lineNumber in startingLine...endingLine {
if emptyLines.contains(lineNumber) {
continue
}

let line = locationConverter.sourceLines[lineNumber - 1]
if !line.isCommentOnly {
return false
}
}

return true
}
}
}

private extension String {
var isCommentOnly: Bool {
let trimmed = trimmingCharacters(in: .whitespaces)
if trimmed.isEmpty {
return true
}

return trimmed.hasPrefix("//")
|| trimmed.hasPrefix("/*")
|| trimmed.hasPrefix("*")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ internal struct VerticalWhitespaceBetweenCasesRuleExamples {
@unknown default: print("x is out of this world")
}
"""),
Example("""
switch myEnum {
case .a: print("a")
// Now we handle b.
case .b: print("b")
}
"""),
// Testing handling of trailing spaces
Example("""
switch x { \("")
Expand Down