forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ColonRule.swift
50 lines (45 loc) · 1.59 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
//
// ColonRule.swift
// SwiftLint
//
// Created by JP Simard on 2015-05-16.
// Copyright (c) 2015 Realm. All rights reserved.
//
import SourceKittenFramework
public struct ColonRule: Rule {
public init() {}
public let identifier = "colon"
public func validateFile(file: File) -> [StyleViolation] {
let pattern1 = file.matchPattern("\\w+\\s+:\\s*\\S+",
withSyntaxKinds: [.Identifier, .Typeidentifier])
let pattern2 = file.matchPattern("\\w+:(?:\\s{0}|\\s{2,})\\S+",
withSyntaxKinds: [.Identifier, .Typeidentifier])
return (pattern1 + pattern2).map { range in
return StyleViolation(type: .Colon,
location: Location(file: file, offset: range.location),
severity: .Low,
reason: "When specifying a type, always associate the colon with the identifier")
}
}
public let example = RuleExample(
ruleName: "Colon Rule",
ruleDescription: "This rule checks whether you associate the colon with the identifier.",
nonTriggeringExamples: [
"let abc: Void\n",
"let abc: [Void: Void]\n",
"let abc: (Void, Void)\n",
"func abc(def: Void) {}\n"
],
triggeringExamples: [
"let abc:Void\n",
"let abc: Void\n",
"let abc :Void\n",
"let abc : Void\n",
"let abc : [Void: Void]\n",
"func abc(def:Void) {}\n",
"func abc(def: Void) {}\n",
"func abc(def :Void) {}\n",
"func abc(def : Void) {}\n"
]
)
}