forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathToggleBoolRule.swift
55 lines (49 loc) · 2.36 KB
/
ToggleBoolRule.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
import Foundation
import SourceKittenFramework
public struct ToggleBoolRule: SubstitutionCorrectableRule, ConfigurationProviderRule, OptInRule, AutomaticTestableRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static var description = RuleDescription(
identifier: "toggle_bool",
name: "Toggle Bool",
description: "Prefer `someBool.toggle()` over `someBool = !someBool`.",
kind: .idiomatic,
minSwiftVersion: .fourDotTwo,
nonTriggeringExamples: [
Example("isHidden.toggle()\n"),
Example("view.clipsToBounds.toggle()\n"),
Example("func foo() { abc.toggle() }"),
Example("view.clipsToBounds = !clipsToBounds\n"),
Example("disconnected = !connected\n"),
Example("result = !result.toggle()")
],
triggeringExamples: [
Example("↓isHidden = !isHidden\n"),
Example("↓view.clipsToBounds = !view.clipsToBounds\n"),
Example("func foo() { ↓abc = !abc }")
],
corrections: [
Example("↓isHidden = !isHidden\n"): Example("isHidden.toggle()\n"),
Example("↓view.clipsToBounds = !view.clipsToBounds\n"): Example("view.clipsToBounds.toggle()\n"),
Example("func foo() { ↓abc = !abc }"): Example("func foo() { abc.toggle() }")
]
)
public func validate(file: SwiftLintFile) -> [StyleViolation] {
return violationRanges(in: file).map {
StyleViolation(ruleDescription: ToggleBoolRule.description,
severity: configuration.severity,
location: Location(file: file, characterOffset: $0.location)
)
}
}
public func violationRanges(in file: SwiftLintFile) -> [NSRange] {
let pattern = #"(?<![\w.])([\w.]+) = !\1\b(?!\.)"#
let excludingKinds = SyntaxKind.commentAndStringKinds
return file.match(pattern: pattern, excludingSyntaxKinds: excludingKinds)
}
public func substitution(for violationRange: NSRange, in file: SwiftLintFile) -> (NSRange, String)? {
let violationString = file.stringView.substring(with: violationRange)
let identifier = violationString.components(separatedBy: .whitespaces).first { $0.isNotEmpty }
return (violationRange, identifier! + ".toggle()")
}
}