-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathSwitchCaseAlignmentRule.swift
199 lines (174 loc) · 6.76 KB
/
SwitchCaseAlignmentRule.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import Foundation
import SourceKittenFramework
public struct SwitchCaseAlignmentRule: ASTRule, ConfigurationProviderRule {
public var configuration = SwitchCaseAlignmentConfiguration()
public init() {}
public static let description = RuleDescription(
identifier: "switch_case_alignment",
name: "Switch and Case Statement Alignment",
description: "Case statements should vertically align with their enclosing switch statement, " +
"or indented if configured otherwise.",
kind: .style,
nonTriggeringExamples: Examples(indentedCases: false).nonTriggeringExamples,
triggeringExamples: Examples(indentedCases: false).triggeringExamples
)
public func validate(file: File, kind: StatementKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
let contents = file.contents.bridge()
guard kind == .switch,
let offset = dictionary.offset,
let (_, switchCharacter) = contents.lineAndCharacter(forByteOffset: offset) else {
return []
}
let caseStatements = dictionary.substructure.filter { subDict in
// includes both `case` and `default` statements
return subDict.kind.flatMap(StatementKind.init) == .case
}
if caseStatements.isEmpty {
return []
}
let caseLocations = caseStatements.compactMap { caseDict -> Location? in
guard let byteOffset = caseDict.offset,
let (line, char) = contents.lineAndCharacter(forByteOffset: byteOffset) else {
return nil
}
return Location(file: file.path, line: line, character: char)
}
guard let firstCaseCharacter = caseLocations.first?.character else {
return []
}
// If indented_cases is on, the first case should be indented from its containing switch.
if configuration.indentedCases, firstCaseCharacter <= switchCharacter {
return caseLocations.map(locationToViolation)
}
let indentation = configuration.indentedCases ? firstCaseCharacter - switchCharacter : 0
return caseLocations
.filter { $0.character != switchCharacter + indentation }
.map(locationToViolation)
}
private func locationToViolation(_ location: Location) -> StyleViolation {
let reason = """
Case statements should \
\(configuration.indentedCases ? "be indented within" : "vertically align with") \
their enclosing switch statement.
"""
return StyleViolation(ruleDescription: SwitchCaseAlignmentRule.description,
severity: configuration.severityConfiguration.severity,
location: location,
reason: reason)
}
}
extension SwitchCaseAlignmentRule {
struct Examples {
private let indentedCasesOption: Bool
private let violationMarker = "↓"
init(indentedCases: Bool) {
self.indentedCasesOption = indentedCases
}
var triggeringExamples: [String] {
return (indentedCasesOption ? nonIndentedCases : indentedCases) + invalidCases
}
var nonTriggeringExamples: [String] {
return indentedCasesOption ? indentedCases : nonIndentedCases
}
private var indentedCases: [String] {
let violationMarker = indentedCasesOption ? "" : self.violationMarker
return [
"""
switch someBool {
\(violationMarker)case true:
print("red")
\(violationMarker)case false:
print("blue")
}
""",
"""
if aBool {
switch someBool {
\(violationMarker)case true:
print('red')
\(violationMarker)case false:
print('blue')
}
}
""",
"""
switch someInt {
\(violationMarker)case 0:
print('Zero')
\(violationMarker)case 1:
print('One')
\(violationMarker)default:
print('Some other number')
}
"""
]
}
private var nonIndentedCases: [String] {
let violationMarker = indentedCasesOption ? self.violationMarker : ""
return [
"""
switch someBool {
\(violationMarker)case true: // case 1
print('red')
\(violationMarker)case false:
/*
case 2
*/
if case let .someEnum(val) = someFunc() {
print('blue')
}
}
enum SomeEnum {
case innocent
}
""",
"""
if aBool {
switch someBool {
\(violationMarker)case true:
print('red')
\(violationMarker)case false:
print('blue')
}
}
""",
"""
switch someInt {
// comments ignored
\(violationMarker)case 0:
// zero case
print('Zero')
\(violationMarker)case 1:
print('One')
\(violationMarker)default:
print('Some other number')
}
"""
]
}
private var invalidCases: [String] {
let indentation = indentedCasesOption ? " " : ""
return [
"""
switch someBool {
\(indentation)case true:
\(indentation)print('red')
\(indentation)\(violationMarker)case false:
\(indentation)print('blue')
}
""",
"""
if aBool {
switch someBool {
\(indentation)\(indentedCasesOption ? "" : violationMarker)case true:
\(indentation)print('red')
\(indentation)\(indentedCasesOption ? violationMarker : "")case false:
\(indentation)print('blue')
}
}
"""
]
}
}
}