forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColonRule.swift
259 lines (239 loc) · 12.1 KB
/
ColonRule.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
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
256
257
258
259
//
// ColonRule.swift
// SwiftLint
//
// Created by JP Simard on 5/16/15.
// Copyright © 2015 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
private enum ColonKind {
case type
case dictionary
case functionCall
}
public struct ColonRule: CorrectableRule, ConfigurationProviderRule {
public var configuration = ColonConfiguration()
public init() {}
public static let description = RuleDescription(
identifier: "colon",
name: "Colon",
description: "Colons should be next to the identifier when specifying a type " +
"and next to the key in dictionary literals.",
kind: .style,
nonTriggeringExamples: [
"let abc: Void\n",
"let abc: [Void: Void]\n",
"let abc: (Void, Void)\n",
"let abc: ([Void], String, Int)\n",
"let abc: [([Void], String, Int)]\n",
"let abc: String=\"def\"\n",
"let abc: Int=0\n",
"let abc: Enum=Enum.Value\n",
"func abc(def: Void) {}\n",
"func abc(def: Void, ghi: Void) {}\n",
"// 周斌佳年周斌佳\nlet abc: String = \"abc:\"",
"let abc = [Void: Void]()\n",
"let abc = [1: [3: 2], 3: 4]\n",
"let abc = [\"string\": \"string\"]\n",
"let abc = [\"string:string\": \"string\"]\n",
"let abc: [String: Int]\n",
"func foo(bar: [String: Int]) {}\n",
"func foo() -> [String: Int] { return [:] }\n",
"let abc: Any\n",
"let abc: [Any: Int]\n",
"let abc: [String: Any]\n",
"class Foo: Bar {}\n",
"class Foo<T: Equatable> {}\n",
"switch foo {\n" +
"case .bar:\n" +
" _ = something()\n" +
"}\n",
"object.method(x: 5, y: \"string\")\n",
"object.method(x: 5, y:\n" +
" \"string\")",
"object.method(5, y: \"string\")\n",
"func abc() { def(ghi: jkl) }",
"func abc(def: Void) { ghi(jkl: mno) }",
"class ABC { let def = ghi(jkl: mno) } }",
"func foo() { let dict = [1: 1] }"
],
triggeringExamples: [
"let ↓abc:Void\n",
"let ↓abc: Void\n",
"let ↓abc :Void\n",
"let ↓abc : Void\n",
"let ↓abc : [Void: Void]\n",
"let ↓abc : (Void, String, Int)\n",
"let ↓abc : ([Void], String, Int)\n",
"let ↓abc : [([Void], String, Int)]\n",
"let ↓abc: (Void, String, Int)\n",
"let ↓abc: ([Void], String, Int)\n",
"let ↓abc: [([Void], String, Int)]\n",
"let ↓abc :String=\"def\"\n",
"let ↓abc :Int=0\n",
"let ↓abc :Int = 0\n",
"let ↓abc:Int=0\n",
"let ↓abc:Int = 0\n",
"let ↓abc:Enum=Enum.Value\n",
"func abc(↓def:Void) {}\n",
"func abc(↓def: Void) {}\n",
"func abc(↓def :Void) {}\n",
"func abc(↓def : Void) {}\n",
"func abc(def: Void, ↓ghi :Void) {}\n",
"let abc = [Void↓:Void]()\n",
"let abc = [Void↓ : Void]()\n",
"let abc = [Void↓: Void]()\n",
"let abc = [Void↓ : Void]()\n",
"let abc = [1: [3↓ : 2], 3: 4]\n",
"let abc = [1: [3↓ : 2], 3↓: 4]\n",
"let abc: [↓String : Int]\n",
"let abc: [↓String:Int]\n",
"func foo(bar: [↓String : Int]) {}\n",
"func foo(bar: [↓String:Int]) {}\n",
"func foo() -> [↓String : Int] { return [:] }\n",
"func foo() -> [↓String:Int] { return [:] }\n",
"let ↓abc : Any\n",
"let abc: [↓Any : Int]\n",
"let abc: [↓String : Any]\n",
"class ↓Foo : Bar {}\n",
"class ↓Foo:Bar {}\n",
"class Foo<↓T:Equatable> {}\n",
"class Foo<↓T : Equatable> {}\n",
"object.method(x: 5, y↓ : \"string\")\n",
"object.method(x↓:5, y: \"string\")\n",
"object.method(x↓: 5, y: \"string\")\n",
"func abc() { def(ghi↓:jkl) }",
"func abc(def: Void) { ghi(jkl↓:mno) }",
"class ABC { let def = ghi(jkl↓:mno) } }",
"func foo() { let dict = [1↓ : 1] }"
],
corrections: [
"let ↓abc:Void\n": "let abc: Void\n",
"let ↓abc: Void\n": "let abc: Void\n",
"let ↓abc :Void\n": "let abc: Void\n",
"let ↓abc : Void\n": "let abc: Void\n",
"let ↓abc : [Void: Void]\n": "let abc: [Void: Void]\n",
"let ↓abc : (Void, String, Int)\n": "let abc: (Void, String, Int)\n",
"let ↓abc : ([Void], String, Int)\n": "let abc: ([Void], String, Int)\n",
"let ↓abc : [([Void], String, Int)]\n": "let abc: [([Void], String, Int)]\n",
"let ↓abc: (Void, String, Int)\n": "let abc: (Void, String, Int)\n",
"let ↓abc: ([Void], String, Int)\n": "let abc: ([Void], String, Int)\n",
"let ↓abc: [([Void], String, Int)]\n": "let abc: [([Void], String, Int)]\n",
"let ↓abc :String=\"def\"\n": "let abc: String=\"def\"\n",
"let ↓abc :Int=0\n": "let abc: Int=0\n",
"let ↓abc :Int = 0\n": "let abc: Int = 0\n",
"let ↓abc:Int=0\n": "let abc: Int=0\n",
"let ↓abc:Int = 0\n": "let abc: Int = 0\n",
"let ↓abc:Enum=Enum.Value\n": "let abc: Enum=Enum.Value\n",
"func abc(↓def:Void) {}\n": "func abc(def: Void) {}\n",
"func abc(↓def: Void) {}\n": "func abc(def: Void) {}\n",
"func abc(↓def :Void) {}\n": "func abc(def: Void) {}\n",
"func abc(↓def : Void) {}\n": "func abc(def: Void) {}\n",
"func abc(def: Void, ↓ghi :Void) {}\n": "func abc(def: Void, ghi: Void) {}\n",
"let abc = [Void↓:Void]()\n": "let abc = [Void: Void]()\n",
"let abc = [Void↓ : Void]()\n": "let abc = [Void: Void]()\n",
"let abc = [Void↓: Void]()\n": "let abc = [Void: Void]()\n",
"let abc = [Void↓ : Void]()\n": "let abc = [Void: Void]()\n",
"let abc = [1: [3↓ : 2], 3: 4]\n": "let abc = [1: [3: 2], 3: 4]\n",
"let abc = [1: [3↓ : 2], 3↓: 4]\n": "let abc = [1: [3: 2], 3: 4]\n",
"let abc: [↓String : Int]\n": "let abc: [String: Int]\n",
"let abc: [↓String:Int]\n": "let abc: [String: Int]\n",
"func foo(bar: [↓String : Int]) {}\n": "func foo(bar: [String: Int]) {}\n",
"func foo(bar: [↓String:Int]) {}\n": "func foo(bar: [String: Int]) {}\n",
"func foo() -> [↓String : Int] { return [:] }\n": "func foo() -> [String: Int] { return [:] }\n",
"func foo() -> [↓String:Int] { return [:] }\n": "func foo() -> [String: Int] { return [:] }\n",
"let ↓abc : Any\n": "let abc: Any\n",
"let abc: [↓Any : Int]\n": "let abc: [Any: Int]\n",
"let abc: [↓String : Any]\n": "let abc: [String: Any]\n",
"class ↓Foo : Bar {}\n": "class Foo: Bar {}\n",
"class ↓Foo:Bar {}\n": "class Foo: Bar {}\n",
"class Foo<↓T:Equatable> {}\n": "class Foo<T: Equatable> {}\n",
"class Foo<↓T : Equatable> {}\n": "class Foo<T: Equatable> {}\n",
"object.method(x: 5, y↓ : \"string\")\n": "object.method(x: 5, y: \"string\")\n",
"object.method(x↓:5, y: \"string\")\n": "object.method(x: 5, y: \"string\")\n",
"object.method(x↓: 5, y: \"string\")\n": "object.method(x: 5, y: \"string\")\n",
"func abc() { def(ghi↓:jkl) }": "func abc() { def(ghi: jkl) }",
"func abc(def: Void) { ghi(jkl↓:mno) }": "func abc(def: Void) { ghi(jkl: mno) }",
"class ABC { let def = ghi(jkl↓:mno) } }": "class ABC { let def = ghi(jkl: mno) } }",
"func foo() { let dict = [1↓ : 1] }": "func foo() { let dict = [1: 1] }"
]
)
public func validate(file: File) -> [StyleViolation] {
let violations = typeColonViolationRanges(in: file, matching: pattern).flatMap { range in
return StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severityConfiguration.severity,
location: Location(file: file, characterOffset: range.location))
}
let dictionaryViolations: [StyleViolation]
if configuration.applyToDictionaries {
dictionaryViolations = validate(file: file, dictionary: file.structure.dictionary)
} else {
dictionaryViolations = []
}
return (violations + dictionaryViolations).sorted { $0.location < $1.location }
}
public func correct(file: File) -> [Correction] {
let violations = correctionRanges(in: file)
let matches = violations.filter {
!file.ruleEnabled(violatingRanges: [$0.range], for: self).isEmpty
}
guard !matches.isEmpty else { return [] }
let regularExpression = regex(pattern)
let description = type(of: self).description
var corrections = [Correction]()
var contents = file.contents
for (range, kind) in matches.reversed() {
switch kind {
case .type:
contents = regularExpression.stringByReplacingMatches(in: contents,
options: [],
range: range,
withTemplate: "$1: $2")
case .dictionary, .functionCall:
contents = contents.bridge().replacingCharacters(in: range, with: ": ")
}
let location = Location(file: file, characterOffset: range.location)
corrections.append(Correction(ruleDescription: description, location: location))
}
file.write(contents)
return corrections
}
private typealias RangeWithKind = (range: NSRange, kind: ColonKind)
private func correctionRanges(in file: File) -> [RangeWithKind] {
let violations: [RangeWithKind] = typeColonViolationRanges(in: file, matching: pattern).map {
(range: $0, kind: ColonKind.type)
}
let dictionary = file.structure.dictionary
let contents = file.contents.bridge()
let dictViolations: [RangeWithKind] = dictionaryColonViolationRanges(in: file, dictionary: dictionary).flatMap {
guard let range = contents.byteRangeToNSRange(start: $0.location, length: $0.length) else {
return nil
}
return (range: range, kind: .dictionary)
}
let functionViolations: [RangeWithKind] = functionCallColonViolationRanges(in: file,
dictionary: dictionary).flatMap {
guard let range = contents.byteRangeToNSRange(start: $0.location, length: $0.length) else {
return nil
}
return (range: range, kind: .functionCall)
}
return (violations + dictViolations + functionViolations).sorted {
$0.range.location < $1.range.location
}
}
}
extension ColonRule: ASTRule {
/// Only returns dictionary and function calls colon violations
public func validate(file: File, kind: SwiftExpressionKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
let ranges = dictionaryColonViolationRanges(in: file, kind: kind, dictionary: dictionary) +
functionCallColonViolationRanges(in: file, kind: kind, dictionary: dictionary)
return ranges.map {
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severityConfiguration.severity,
location: Location(file: file, byteOffset: $0.location))
}
}
}