forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStatementPositionRule.swift
More file actions
255 lines (228 loc) · 10.3 KB
/
StatementPositionRule.swift
File metadata and controls
255 lines (228 loc) · 10.3 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import Foundation
import SourceKittenFramework
@DisabledWithoutSourceKit
struct StatementPositionRule: CorrectableRule {
var configuration = StatementPositionConfiguration()
static let description = RuleDescription(
identifier: "statement_position",
name: "Statement Position",
description: "Else and catch should be on the same line, one space after the previous declaration",
kind: .style,
nonTriggeringExamples: [
Example("} else if {"),
Example("} else {"),
Example("} catch {"),
Example("guard foo() else { return }"),
Example("\"}else{\""),
Example("struct A { let catchphrase: Int }\nlet a = A(\n catchphrase: 0\n)"),
Example("struct A { let `catch`: Int }\nlet a = A(\n `catch`: 0\n)"),
],
triggeringExamples: [
Example("↓}else if {"),
Example("↓} else {"),
Example("↓}\ncatch {"),
Example("↓}\n\t catch {"),
Example("guard foo()↓else { return }"),
],
corrections: [
Example("↓}\n else {"): Example("} else {"),
Example("↓}\n else if {"): Example("} else if {"),
Example("↓}\n catch {"): Example("} catch {"),
Example("guard foo()↓else { return }"): Example("guard foo() else { return }"),
]
)
static let uncuddledDescription = RuleDescription(
identifier: "statement_position",
name: "Statement Position",
description: "Else and catch should be on the next line, with equal indentation to the " +
"previous declaration",
kind: .style,
nonTriggeringExamples: [
Example(" }\n else if {"),
Example(" }\n else {"),
Example(" }\n catch {"),
Example(" }\n\n catch {"),
Example("\n\n }\n catch {"),
Example("\"}\nelse{\""),
Example("struct A { let catchphrase: Int }\nlet a = A(\n catchphrase: 0\n)"),
Example("struct A { let `catch`: Int }\nlet a = A(\n `catch`: 0\n)"),
],
triggeringExamples: [
Example("↓ }else if {"),
Example("↓}\n else {"),
Example("↓ }\ncatch {"),
Example("↓}\n\t catch {"),
],
corrections: [
Example(" }else if {"): Example(" }\n else if {"),
Example("}\n else {"): Example("}\nelse {"),
Example(" }\ncatch {"): Example(" }\n catch {"),
Example("}\n\t catch {"): Example("}\ncatch {"),
]
)
func validate(file: SwiftLintFile) -> [StyleViolation] {
switch configuration.statementMode {
case .default:
return defaultValidate(file: file)
case .uncuddledElse:
return uncuddledValidate(file: file)
}
}
func correct(file: SwiftLintFile) -> Int {
switch configuration.statementMode {
case .default:
defaultCorrect(file: file)
case .uncuddledElse:
uncuddledCorrect(file: file)
}
}
}
// Default Behaviors
private extension StatementPositionRule {
// match literal '}'
// followed by 1) nothing, 2) two+ whitespace/newlines or 3) newlines or tabs
// followed by 'else' or 'catch' literals
static let defaultPattern = "\\}(?:[\\s\\n\\r]{2,}|[\\n\\t\\r]+)?\\b(else|catch)\\b"
// match a guard statement where `else` is glued to the condition without whitespace
static let defaultGuardPattern = "(\\bguard\\b[^\\n]*\\S)(else\\b)"
static let defaultGuardRegex = regex(defaultGuardPattern)
func defaultValidate(file: SwiftLintFile) -> [StyleViolation] {
defaultViolationRanges(in: file).compactMap { range in
StyleViolation(ruleDescription: Self.description,
severity: configuration.severity,
location: Location(file: file, characterOffset: range.location))
}
}
func defaultViolationRanges(in file: SwiftLintFile) -> [NSRange] {
defaultBraceViolationRanges(in: file) + defaultGuardViolationRanges(in: file)
}
func defaultBraceViolationRanges(in file: SwiftLintFile) -> [NSRange] {
file.match(pattern: Self.defaultPattern).filter { _, syntaxKinds in
syntaxKinds.starts(with: [.keyword])
}.compactMap(\.0)
}
func defaultGuardViolationRanges(in file: SwiftLintFile) -> [NSRange] {
defaultGuardMatches(in: file).map { $0.range(at: 2) }
}
func defaultGuardCorrectionRanges(in file: SwiftLintFile) -> [NSRange] {
defaultGuardMatches(in: file).map(\.range)
}
func defaultGuardMatches(in file: SwiftLintFile) -> [NSTextCheckingResult] {
let contents = file.stringView
let syntaxMap = file.syntaxMap
return Self.defaultGuardRegex.matches(in: file).filter { match in
guard let elseRange = contents.NSRangeToByteRange(
start: match.range(at: 2).location,
length: match.range(at: 2).length
) else {
return false
}
return syntaxMap.kinds(inByteRange: elseRange) == [.keyword]
}
}
func defaultCorrect(file: SwiftLintFile) -> Int {
let braceViolations = defaultBraceViolationRanges(in: file)
let guardViolations = defaultGuardCorrectionRanges(in: file)
let enabledBraceViolations = file.ruleEnabled(violatingRanges: braceViolations, for: self)
let enabledGuardViolations = file.ruleEnabled(violatingRanges: guardViolations, for: self)
if enabledBraceViolations.isEmpty, enabledGuardViolations.isEmpty {
return 0
}
var contents = file.contents
let braceRegex = regex(Self.defaultPattern)
for range in enabledBraceViolations.reversed() {
contents = braceRegex.stringByReplacingMatches(in: contents, options: [], range: range,
withTemplate: "} $1")
}
for range in enabledGuardViolations.reversed() {
contents = Self.defaultGuardRegex.stringByReplacingMatches(in: contents, options: [], range: range,
withTemplate: "$1 $2")
}
file.write(contents)
return enabledBraceViolations.count + enabledGuardViolations.count
}
}
// Uncuddled Behaviors
private extension StatementPositionRule {
func uncuddledValidate(file: SwiftLintFile) -> [StyleViolation] {
uncuddledViolationRanges(in: file).compactMap { range in
StyleViolation(ruleDescription: Self.uncuddledDescription,
severity: configuration.severity,
location: Location(file: file, characterOffset: range.location))
}
}
// match literal '}'
// preceded by whitespace (or nothing)
// followed by 1) nothing, 2) two+ whitespace/newlines or 3) newlines or tabs
// followed by newline and the same amount of whitespace then 'else' or 'catch' literals
static let uncuddledPattern = "([ \t]*)\\}(\\n+)?([ \t]*)\\b(else|catch)\\b"
static let uncuddledRegex = regex(uncuddledPattern, options: [])
static func uncuddledMatchValidator(contents: StringView) -> ((NSTextCheckingResult)
-> NSTextCheckingResult?) {
{ match in
if match.numberOfRanges != 5 {
return match
}
if match.range(at: 2).length == 0 {
return match
}
let range1 = match.range(at: 1)
let range2 = match.range(at: 3)
let whitespace1 = contents.string.substring(from: range1.location, length: range1.length)
let whitespace2 = contents.string.substring(from: range2.location, length: range2.length)
if whitespace1 == whitespace2 {
return nil
}
return match
}
}
static func uncuddledMatchFilter(contents: StringView,
syntaxMap: SwiftLintSyntaxMap) -> ((NSTextCheckingResult) -> Bool) {
{ match in
let range = match.range
guard let matchRange = contents.NSRangeToByteRange(start: range.location,
length: range.length) else {
return false
}
return syntaxMap.kinds(inByteRange: matchRange) == [.keyword]
}
}
func uncuddledViolationRanges(in file: SwiftLintFile) -> [NSRange] {
let contents = file.stringView
let syntaxMap = file.syntaxMap
let matches = Self.uncuddledRegex.matches(in: file)
let validator = Self.uncuddledMatchValidator(contents: contents)
let filterMatches = Self.uncuddledMatchFilter(contents: contents, syntaxMap: syntaxMap)
return matches.compactMap(validator).filter(filterMatches).map(\.range)
}
func uncuddledCorrect(file: SwiftLintFile) -> Int {
var contents = file.contents
let syntaxMap = file.syntaxMap
let matches = Self.uncuddledRegex.matches(in: file)
let validator = Self.uncuddledMatchValidator(contents: file.stringView)
let filterRanges = Self.uncuddledMatchFilter(contents: file.stringView, syntaxMap: syntaxMap)
let validMatches = matches.compactMap(validator).filter(filterRanges)
.filter { file.ruleEnabled(violatingRanges: [$0.range], for: self).isNotEmpty }
if validMatches.isEmpty {
return 0
}
for match in validMatches.reversed() {
let range1 = match.range(at: 1)
let range2 = match.range(at: 3)
let newlineRange = match.range(at: 2)
var whitespace = contents.bridge().substring(with: range1)
let newLines: String
if newlineRange.location != NSNotFound {
newLines = contents.bridge().substring(with: newlineRange)
} else {
newLines = ""
}
if !whitespace.hasPrefix("\n"), newLines != "\n" {
whitespace.insert("\n", at: whitespace.startIndex)
}
contents = contents.bridge().replacingCharacters(in: range2, with: whitespace)
}
file.write(contents)
return validMatches.count
}
}