forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUnusedEnumeratedRule.swift
102 lines (88 loc) · 3.96 KB
/
UnusedEnumeratedRule.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
import SourceKittenFramework
public struct UnusedEnumeratedRule: ASTRule, ConfigurationProviderRule, AutomaticTestableRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "unused_enumerated",
name: "Unused Enumerated",
description: "When the index or the item is not used, `.enumerated()` can be removed.",
kind: .idiomatic,
nonTriggeringExamples: [
Example("for (idx, foo) in bar.enumerated() { }\n"),
Example("for (_, foo) in bar.enumerated().something() { }\n"),
Example("for (_, foo) in bar.something() { }\n"),
Example("for foo in bar.enumerated() { }\n"),
Example("for foo in bar { }\n"),
Example("for (idx, _) in bar.enumerated().something() { }\n"),
Example("for (idx, _) in bar.something() { }\n"),
Example("for idx in bar.indices { }\n"),
Example("for (section, (event, _)) in data.enumerated() {}\n")
],
triggeringExamples: [
Example("for (↓_, foo) in bar.enumerated() { }\n"),
Example("for (↓_, foo) in abc.bar.enumerated() { }\n"),
Example("for (↓_, foo) in abc.something().enumerated() { }\n"),
Example("for (idx, ↓_) in bar.enumerated() { }\n")
]
)
public func validate(file: SwiftLintFile, kind: StatementKind,
dictionary: SourceKittenDictionary) -> [StyleViolation] {
guard kind == .forEach,
isEnumeratedCall(dictionary: dictionary),
let byteRange = byteRangeForVariables(dictionary: dictionary),
case let tokens = file.syntaxMap.tokens(inByteRange: byteRange),
tokens.count == 2,
let lastToken = tokens.last,
case let firstTokenIsUnderscore = isTokenUnderscore(tokens[0], file: file),
case let lastTokenIsUnderscore = isTokenUnderscore(lastToken, file: file),
firstTokenIsUnderscore || lastTokenIsUnderscore else {
return []
}
let offset: ByteCount
let reason: String
if firstTokenIsUnderscore {
offset = tokens[0].offset
reason = "When the index is not used, `.enumerated()` can be removed."
} else {
offset = lastToken.offset
reason = "When the item is not used, `.indices` should be used instead of `.enumerated()`."
}
return [
StyleViolation(ruleDescription: Self.description,
severity: configuration.severity,
location: Location(file: file, byteOffset: offset),
reason: reason)
]
}
private func isTokenUnderscore(_ token: SwiftLintSyntaxToken, file: SwiftLintFile) -> Bool {
return token.length == 1 &&
token.kind == .keyword &&
isUnderscore(file: file, token: token)
}
private func isEnumeratedCall(dictionary: SourceKittenDictionary) -> Bool {
for subDict in dictionary.substructure {
guard subDict.expressionKind == .call,
let name = subDict.name else {
continue
}
if name.hasSuffix(".enumerated") {
return true
}
}
return false
}
private func byteRangeForVariables(dictionary: SourceKittenDictionary) -> ByteRange? {
let expectedKind = "source.lang.swift.structure.elem.id"
for subDict in dictionary.elements where subDict.kind == expectedKind {
guard let offset = subDict.offset,
let length = subDict.length else {
continue
}
return ByteRange(location: offset, length: length)
}
return nil
}
private func isUnderscore(file: SwiftLintFile, token: SwiftLintSyntaxToken) -> Bool {
return file.contents(for: token) == "_"
}
}