forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UnusedEnumeratedRule.swift
113 lines (97 loc) · 4.08 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
103
104
105
106
107
108
109
110
111
112
113
//
// UnusedEnumeratedRule.swift
// SwiftLint
//
// Created by Marcelo Fabri on 12/17/16.
// Copyright © 2016 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
public struct UnusedEnumeratedRule: ASTRule, ConfigurationProviderRule {
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: [
"for (idx, foo) in bar.enumerated() { }\n",
"for (_, foo) in bar.enumerated().something() { }\n",
"for (_, foo) in bar.something() { }\n",
"for foo in bar.enumerated() { }\n",
"for foo in bar { }\n",
"for (idx, _) in bar.enumerated().something() { }\n",
"for (idx, _) in bar.something() { }\n",
"for idx in bar.indices { }\n"
],
triggeringExamples: [
"for (↓_, foo) in bar.enumerated() { }\n",
"for (↓_, foo) in abc.bar.enumerated() { }\n",
"for (↓_, foo) in abc.something().enumerated() { }\n",
"for (idx, ↓_) in bar.enumerated() { }\n"
]
)
public func validate(file: File, kind: StatementKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
guard kind == .forEach,
isEnumeratedCall(dictionary: dictionary),
let byteRange = byteRangeForVariables(dictionary: dictionary),
case let tokens = file.syntaxMap.tokens(inByteRange: byteRange),
tokens.count > 1,
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: Int
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: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, byteOffset: offset),
reason: reason)
]
}
private func isTokenUnderscore(_ token: SyntaxToken, file: File) -> Bool {
return token.length == 1 &&
SyntaxKind(rawValue: token.type) == .keyword &&
isUnderscore(file: file, token: token)
}
private func isEnumeratedCall(dictionary: [String: SourceKitRepresentable]) -> Bool {
for subDict in dictionary.substructure {
guard let kindString = subDict.kind,
SwiftExpressionKind(rawValue: kindString) == .call,
let name = subDict.name else {
continue
}
if name.hasSuffix(".enumerated") {
return true
}
}
return false
}
private func byteRangeForVariables(dictionary: [String: SourceKitRepresentable]) -> NSRange? {
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 NSRange(location: offset, length: length)
}
return nil
}
private func isUnderscore(file: File, token: SyntaxToken) -> Bool {
let contents = file.contents.bridge()
return contents.substringWithByteRange(start: token.offset, length: token.length) == "_"
}
}