forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColonRule.swift
158 lines (146 loc) · 6.46 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
//
// ColonRule.swift
// SwiftLint
//
// Created by JP Simard on 2015-05-16.
// Copyright (c) 2015 Realm. All rights reserved.
//
import SourceKittenFramework
public struct ColonRule: CorrectableRule {
public init() {}
public static let description = RuleDescription(
identifier: "colon",
name: "Colon",
description: "Colons should be next to the identifier when specifying a type.",
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:\""
],
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"
],
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"
]
)
public func validateFile(file: File) -> [StyleViolation] {
let pattern = patterns().joinWithSeparator("|")
return violationRangesInFile(file, withPattern: pattern).flatMap { range in
return StyleViolation(ruleDescription: self.dynamicType.description,
location: Location(file: file, characterOffset: range.location))
}
}
public func correctFile(file: File) -> [Correction] {
// Do not join the patterns when correcting, we need 2 explicit capture groups.
return patterns().reduce([Correction]()) { corrections, pattern in
return corrections + correctFile(file, withPattern: pattern)
}
}
// MARK: - Private Methods
private func patterns() -> [String] {
let spacingLeftOfColonPattern = "" +
// Capture an identifier
"(\\w+)" +
// followed by whitespace
"\\s+" +
// to the left of a colon
":" +
// followed by any amount of whitespace.
"\\s*" +
// Capture a type identifier
"(" +
// which may begin with a series of nested parenthesis or brackets
"(?:\\[|\\()*" +
// lazily to the first non-whitespace character.
"\\S+?)"
let spacingRightOfColonPattern = "" +
// Capture an identifier
"(\\w+)" +
// immediately followed by a colon
":" +
// followed by 0 or 2+ whitespace characters.
"(?:\\s{0}|\\s{2,})" +
// Capture a type identifier
"(" +
// which may begin with a series of nested parenthesis or brackets
"(?:\\[|\\()*" +
// lazily to the first non-whitespace character.
"\\S+?)"
return [spacingLeftOfColonPattern, spacingRightOfColonPattern]
}
private func violationRangesInFile(file: File, withPattern pattern: String) -> [NSRange] {
return file.matchPattern(pattern).filter { range, syntaxKinds in
if !syntaxKinds.startsWith([.Identifier, .Typeidentifier]) {
return false
}
return Set(syntaxKinds).intersect(Set(SyntaxKind.commentAndStringKinds())).isEmpty
}.flatMap { $0.0 }
}
private func correctFile(file: File, withPattern pattern: String) -> [Correction] {
let matches = violationRangesInFile(file, withPattern: pattern)
guard !matches.isEmpty else { return [] }
let regularExpression = regex(pattern)
let description = self.dynamicType.description
var corrections = [Correction]()
var contents = file.contents
for range in matches.reverse() {
contents = regularExpression.stringByReplacingMatchesInString(contents,
options: [], range: range, withTemplate: "$1: $2")
let location = Location(file: file, characterOffset: range.location)
corrections.append(Correction(ruleDescription: description, location: location))
}
file.write(contents)
return corrections
}
}