-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #998 from marcelofabri/unused-enumerated
Add unused_enumerated rule
- Loading branch information
Showing
5 changed files
with
114 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
Source/SwiftLintFramework/Rules/UnusedEnumeratedRule.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// | ||
// 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 is not used, .enumerated() can be removed.", | ||
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() { }\n" | ||
], | ||
triggeringExamples: [ | ||
"for (↓_, foo) in bar.enumerated() { }\n", | ||
"for (↓_, foo) in abc.bar.enumerated() { }\n", | ||
"for (↓_, foo) in abc.something().enumerated() { }\n" | ||
] | ||
) | ||
|
||
public func validateFile(_ file: File, | ||
kind: StatementKind, | ||
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] { | ||
|
||
guard kind == .forEach, | ||
isEnumeratedCall(dictionary), | ||
let byteRange = byteRangeForVariables(dictionary), | ||
let firstToken = file.syntaxMap.tokensIn(byteRange).first, | ||
firstToken.length == 1, | ||
SyntaxKind(rawValue: firstToken.type) == .keyword, | ||
isUnderscore(file: file, token: firstToken) else { | ||
return [] | ||
} | ||
|
||
return [ | ||
StyleViolation(ruleDescription: type(of: self).description, | ||
severity: configuration.severity, | ||
location: Location(file: file, byteOffset: firstToken.offset)) | ||
] | ||
} | ||
|
||
private func isEnumeratedCall(_ dictionary: [String: SourceKitRepresentable]) -> Bool { | ||
let substructure = dictionary["key.substructure"] as? [SourceKitRepresentable] ?? [] | ||
for subItem in substructure { | ||
guard let subDict = subItem as? [String: SourceKitRepresentable], | ||
let kindString = subDict["key.kind"] as? String, | ||
SwiftExpressionKind(rawValue: kindString) == .call, | ||
let name = subDict["key.name"] as? String else { | ||
continue | ||
} | ||
|
||
if name.hasSuffix(".enumerated") { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
|
||
private func byteRangeForVariables(_ dictionary: [String: SourceKitRepresentable]) -> NSRange? { | ||
guard let elements = dictionary["key.elements"] as? [SourceKitRepresentable] else { | ||
return nil | ||
} | ||
|
||
let expectedKind = "source.lang.swift.structure.elem.id" | ||
for element in elements { | ||
guard let subDict = element as? [String: SourceKitRepresentable], | ||
subDict["key.kind"] as? String == expectedKind, | ||
let offset = (subDict["key.offset"] as? Int64).map({ Int($0) }), | ||
let length = (subDict["key.length"] as? Int64).map({ Int($0) }) 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) == "_" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters