forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControlStatementRule.swift
102 lines (92 loc) · 3.61 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
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
//
// ControlStatementRule.swift
// SwiftLint
//
// Created by Andrea Mazzini on 26/05/15.
// Copyright © 2015 Realm. All rights reserved.
//
import SourceKittenFramework
public struct ControlStatementRule: ConfigurationProviderRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "control_statement",
name: "Control Statement",
description: "if,for,while,do statements shouldn't wrap their conditionals in parentheses.",
kind: .style,
nonTriggeringExamples: [
"if condition {\n",
"if (a, b) == (0, 1) {\n",
"if (a || b) && (c || d) {\n",
"if (min...max).contains(value) {\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",
"guard condition else {\n",
"while condition {\n",
"} while condition {\n",
"do { ; } while condition {\n",
"switch foo {\n"
],
triggeringExamples: [
"↓if (condition) {\n",
"↓if(condition) {\n",
"↓if ((a || b) && (c || d)) {\n",
"↓if ((min...max).contains(value)) {\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",
"↓guard (condition) else {\n",
"↓while (condition) {\n",
"↓while(condition) {\n",
"} ↓while (condition) {\n",
"} ↓while(condition) {\n",
"do { ; } ↓while(condition) {\n",
"do { ; } ↓while (condition) {\n",
"↓switch (foo) {\n"
]
)
public func validate(file: File) -> [StyleViolation] {
let statements = ["if", "for", "guard", "switch", "while"]
return statements.flatMap { statementKind -> [StyleViolation] in
let pattern = statementKind == "guard"
? "\(statementKind)\\s*\\([^,{]*\\)\\s*else\\s*\\{"
: "\(statementKind)\\s*\\([^,{]*\\)\\s*\\{"
return file.match(pattern: pattern).flatMap { match, syntaxKinds in
let matchString = file.contents.substring(from: match.location, length: match.length)
if isFalsePositive(matchString, syntaxKind: syntaxKinds.first) {
return nil
}
return StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, characterOffset: match.location))
}
}
}
fileprivate func isFalsePositive(_ content: String, syntaxKind: SyntaxKind?) -> Bool {
if syntaxKind != .keyword {
return true
}
guard let lastClosingParenthesePosition = content.lastIndex(of: ")") else {
return false
}
var depth = 0
var index = 0
for char in content.characters {
if char == ")" {
if index != lastClosingParenthesePosition && depth == 1 {
return true
}
depth -= 1
} else if char == "(" {
depth += 1
}
index += 1
}
return false
}
}