forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShorthandOperatorRule.swift
116 lines (96 loc) · 4.21 KB
/
ShorthandOperatorRule.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
//
// ShorthandOperatorRule.swift
// SwiftLint
//
// Created by Marcelo Fabri on 01/06/17.
// Copyright © 2017 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
public struct ShorthandOperatorRule: ConfigurationProviderRule {
public var configuration = SeverityConfiguration(.error)
public init() {}
public static let description = RuleDescription(
identifier: "shorthand_operator",
name: "Shorthand Operator",
description: "Prefer shorthand operators (+=, -=, *=, /=) over doing the operation and assigning.",
kind: .style,
nonTriggeringExamples: allOperators.flatMap { operation in
[
"foo \(operation)= 1",
"foo \(operation)= variable",
"foo \(operation)= bar.method()",
"self.foo = foo \(operation) 1",
"foo = self.foo \(operation) 1",
"page = ceilf(currentOffset \(operation) pageWidth)",
"foo = aMethod(foo \(operation) bar)",
"foo = aMethod(bar \(operation) foo)"
]
} + [
"var helloWorld = \"world!\"\n helloWorld = \"Hello, \" + helloWorld",
"angle = someCheck ? angle : -angle",
"seconds = seconds * 60 + value"
],
triggeringExamples: allOperators.flatMap { operation in
[
"↓foo = foo \(operation) 1\n",
"↓foo = foo \(operation) aVariable\n",
"↓foo = foo \(operation) bar.method()\n",
"↓foo.aProperty = foo.aProperty \(operation) 1\n",
"↓self.aProperty = self.aProperty \(operation) 1\n"
]
} + [
"n = n + i / outputLength",
"n = n - i / outputLength"
// "d = d * 60 * 60"
]
)
private static let allOperators = ["-", "/", "+", "*"]
private static let pattern: String = {
let escaped = { (operators: [String]) -> String in
return "[\(operators.map { "\\\($0)" }.joined())]"
}
let escapedAll = escaped(allOperators)
let operatorsWithoutPrecedence = escaped(["-", "+"])
let operatorsWithPrecedence = escaped(["/", "*"])
let operand = "[\\w\\d\\.]+?"
let spaces = "[^\\S\\r\\n]*?"
let pattern1 = "\(operatorsWithoutPrecedence)"
let pattern2 = "\(operatorsWithPrecedence)\(spaces)\\S+$"
return "^\(spaces)(\(operand))\(spaces)=\(spaces)(\\1)\(spaces)(\(pattern1)|\(pattern2))"
}()
private static let violationRegex: NSRegularExpression = {
return regex(pattern, options: [.anchorsMatchLines])
}()
public func validate(file: File) -> [StyleViolation] {
let contents = file.contents.bridge()
let range = NSRange(location: 0, length: contents.length)
let matches = ShorthandOperatorRule.violationRegex.matches(in: file.contents, options: [], range: range)
return matches.flatMap { match -> StyleViolation? in
// byteRanges will have the ranges of captured groups
let byteRanges: [NSRange?] = (1..<match.numberOfRanges).map { rangeIdx in
let range = match.range(at: rangeIdx)
guard range.location != NSNotFound else {
return nil
}
return contents.NSRangeToByteRange(start: range.location, length: range.length)
}
guard let byteRange = byteRanges[0] else {
return nil
}
let kindsInCaptureGroups = byteRanges.map { range -> [SyntaxKind] in
return range.flatMap(file.syntaxMap.kinds(inByteRange:)) ?? []
}
guard kindsAreValid(kindsInCaptureGroups[0]) &&
kindsAreValid(kindsInCaptureGroups[1]) else {
return nil
}
return StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, byteOffset: byteRange.location))
}
}
private func kindsAreValid(_ kinds: [SyntaxKind]) -> Bool {
return Set(kinds).isSubset(of: [.identifier, .keyword])
}
}