Skip to content

Commit

Permalink
Add option to switch_case_alignment rule to ignore switch statement…
Browse files Browse the repository at this point in the history
…s written on a single line (#5390)
  • Loading branch information
tonell-m authored Jan 3, 2024
1 parent a8a676f commit 902ac30
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

#### Enhancements

* Add new `ignore_one_liners` option to `switch_case_alignment`
rule to ignore switch statements written in a single line.
[tonell-m](https://github.com/tonell-m)
[#5373](https://github.com/realm/SwiftLint/issues/5373)

* Add new `one_declaration_per_file` rule that allows only a
single class/struct/enum/protocol declaration per file.
Extensions are an exception; more than one is allowed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ struct SwitchCaseAlignmentConfiguration: SeverityBasedRuleConfiguration {
private(set) var severityConfiguration = SeverityConfiguration<Parent>(.warning)
@ConfigurationElement(key: "indented_cases")
private(set) var indentedCases = false
@ConfigurationElement(key: "ignore_one_liners")
private(set) var ignoreOneLiners = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,22 @@ struct SwitchCaseAlignmentRule: Rule {
extension SwitchCaseAlignmentRule {
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
override func visitPost(_ node: SwitchExprSyntax) {
let closingBracePosition = node.rightBrace.positionAfterSkippingLeadingTrivia
let closingBraceColumn = locationConverter.location(for: closingBracePosition).column
guard node.cases.isNotEmpty,
let firstCasePosition = node.cases.first?.positionAfterSkippingLeadingTrivia
let firstCasePosition = node.cases.first?.positionAfterSkippingLeadingTrivia
else {
return
}

let closingBracePosition = node.rightBrace.positionAfterSkippingLeadingTrivia
let closingBraceLocation = locationConverter.location(for: closingBracePosition)
let switchKeywordPosition = node.switchKeyword.positionAfterSkippingLeadingTrivia
let switchKeywordLocation = locationConverter.location(for: switchKeywordPosition)

if configuration.ignoreOneLiners && switchKeywordLocation.line == closingBraceLocation.line {
return
}

let closingBraceColumn = closingBraceLocation.column
let firstCaseColumn = locationConverter.location(for: firstCasePosition).column

for `case` in node.cases where `case`.is(SwitchCaseSyntax.self) {
Expand Down Expand Up @@ -85,11 +93,14 @@ extension SwitchCaseAlignmentRule {
}

var triggeringExamples: [Example] {
return (indentedCasesOption ? nonIndentedCases : indentedCases) + invalidCases
return (indentedCasesOption ? nonIndentedCases : indentedCases)
+ invalidCases
+ invalidOneLiners
}

var nonTriggeringExamples: [Example] {
return indentedCasesOption ? indentedCases : nonIndentedCases
+ validOneLiners
}

private var indentedCases: [Example] {
Expand Down Expand Up @@ -216,5 +227,46 @@ extension SwitchCaseAlignmentRule {
""")
]
}

private var validOneLiners: [Example] = [
Example(
"switch i { case .x: 1 default: 0 }",
configuration: ["ignore_one_liners": true]
),
Example(
"let a = switch i { case .x: 1 default: 0 }",
configuration: ["ignore_one_liners": true]
)
]

private var invalidOneLiners: [Example] {
[
// Default configuration should not ignore one liners
Example(
"switch i { \(violationMarker)case .x: 1 \(violationMarker)default: 0 }"
),
Example("""
switch i {
\(violationMarker)case .x: 1 \(violationMarker)default: 0 }
""", configuration: ["ignore_one_liners": true]),
Example("""
switch i { \(violationMarker)case .x: 1 \(violationMarker)default: 0
}
""", configuration: ["ignore_one_liners": true]),
Example("""
switch i
{ \(violationMarker)case .x: 1 \(violationMarker)default: 0 }
""", configuration: ["ignore_one_liners": true]),
Example("""
let a = switch i {
case .x: 1 \(violationMarker)default: 0
}
""", configuration: ["ignore_one_liners": true]),
Example("""
let a = switch i {
\(violationMarker)case .x: 1 \(violationMarker)default: 0 }
""", configuration: ["ignore_one_liners": true])
]
}
}
}

0 comments on commit 902ac30

Please sign in to comment.