-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathColonRule.swift
142 lines (131 loc) · 6.33 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
//
// ColonRule.swift
// SwiftLint
//
// Created by JP Simard on 2015-05-16.
// Copyright (c) 2015 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
public struct ColonRule: CorrectableRule, ConfigurationProviderRule {
public var configuration = SeverityConfiguration(.Warning)
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] {
return violationRangesInFile(file, withPattern: pattern).flatMap { range in
return StyleViolation(ruleDescription: self.dynamicType.description,
severity: configuration.severity,
location: Location(file: file, characterOffset: range.location))
}
}
public func correctFile(file: File) -> [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
}
// MARK: - Private
private let pattern =
"(\\w)" + // Capture an identifier
"(?:" + // start group
"\\s+" + // followed by whitespace
":" + // to the left of a colon
"\\s*" + // followed by any amount of whitespace.
"|" + // or
":" + // immediately followed by a colon
"(?:\\s{0}|\\s{2,})" + // followed by 0 or 2+ whitespace characters.
")" + // end group
"(" + // Capture a type identifier
"[\\[|\\(]*" + // which may begin with a series of nested parenthesis or brackets
"\\S)" // lazily to the first non-whitespace character.
private func violationRangesInFile(file: File, withPattern pattern: String) -> [NSRange] {
let nsstring = file.contents as NSString
let commentAndStringKindsSet = Set(SyntaxKind.commentAndStringKinds())
return file.rangesAndTokensMatching(pattern).filter { range, syntaxTokens in
let syntaxKinds = syntaxTokens.map({ $0.type }).flatMap(SyntaxKind.init)
if !syntaxKinds.startsWith([.Identifier, .Typeidentifier]) {
return false
}
return Set(syntaxKinds).intersect(commentAndStringKindsSet).isEmpty
}.flatMap { range, syntaxTokens in
let identifierRange = nsstring // swiftlint:disable:next force_unwrapping
.byteRangeToNSRange(start: syntaxTokens.first!.offset, length: 0)
return identifierRange.map { NSUnionRange($0, range) }
}
}
}