forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControlStatementRule.swift
64 lines (59 loc) · 2.22 KB
/
ControlStatementRule.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
//
// ControlStatementRule.swift
// SwiftLint
//
// Created by Andrea Mazzini on 26/05/15.
// Copyright (c) 2015 Realm. All rights reserved.
//
import SourceKittenFramework
public struct ControlStatementRule: Rule {
public init() {}
public let identifier = "control_statement"
public func validateFile(file: File) -> [StyleViolation] {
return ["if", "for", "while"].flatMap { statementKind in
let pattern = "\(statementKind)\\s*\\([^,]*\\)\\s*\\{"
return compact(file.matchPattern(pattern).map { match, syntaxKinds in
if syntaxKinds.first != .Keyword {
return nil
}
return StyleViolation(type: .ControlStatement,
location: Location(file: file, offset: match.location),
severity: .Low,
reason: "\(statementKind) statements shouldn't wrap their conditionals in " +
"parentheses.")
})
}
}
public let example = RuleExample(
ruleName: "Control Statement",
ruleDescription: "if,for,while,do statements shouldn't wrap their conditionals in " +
"parentheses.",
nonTriggeringExamples: [
"if condition {\n",
"if (a, b) == (0, 1) {\n",
"if renderGif(data) {\n",
"renderGif(data)\n",
"for item in collection {\n",
"for (key, value) in dictionary {\n",
"for (index, value) in enumerate(array) {\n",
"for var index = 0; index < 42; index++ {\n",
"while condition {\n",
"} while condition {\n",
"do { ; } while condition {\n"
],
triggeringExamples: [
"if (condition) {\n",
"if(condition) {\n",
"for (item in collection) {\n",
"for (var index = 0; index < 42; index++) {\n",
"for(item in collection) {\n",
"for(var index = 0; index < 42; index++) {\n",
"while (condition) {\n",
"while(condition) {\n",
"} while (condition) {\n",
"} while(condition) {\n",
"do { ; } while(condition) {\n",
"do { ; } while (condition) {\n"
]
)
}