mirror of
https://github.com/realm/SwiftLint.git
synced 2026-05-07 20:12:49 +00:00
58 lines
2.5 KiB
Swift
58 lines
2.5 KiB
Swift
import SourceKittenFramework
|
|
|
|
public struct FirstWhereRule: CallPairRule, OptInRule, ConfigurationProviderRule, AutomaticTestableRule {
|
|
public var configuration = SeverityConfiguration(.warning)
|
|
|
|
public init() {}
|
|
|
|
public static let description = RuleDescription(
|
|
identifier: "first_where",
|
|
name: "First Where",
|
|
description: "Prefer using `.first(where:)` over `.filter { }.first` in collections.",
|
|
kind: .performance,
|
|
nonTriggeringExamples: [
|
|
Example("kinds.filter(excludingKinds.contains).isEmpty && kinds.first == .identifier\n"),
|
|
Example("myList.first(where: { $0 % 2 == 0 })\n"),
|
|
Example("match(pattern: pattern).filter { $0.first == .identifier }\n"),
|
|
Example("(myList.filter { $0 == 1 }.suffix(2)).first\n"),
|
|
Example("collection.filter(\"stringCol = '3'\").first"),
|
|
Example("realm?.objects(User.self).filter(NSPredicate(format: \"email ==[c] %@\", email)).first"),
|
|
Example("if let pause = timeTracker.pauses.filter(\"beginDate < %@\", beginDate).first { print(pause) }")
|
|
],
|
|
triggeringExamples: [
|
|
Example("↓myList.filter { $0 % 2 == 0 }.first\n"),
|
|
Example("↓myList.filter({ $0 % 2 == 0 }).first\n"),
|
|
Example("↓myList.map { $0 + 1 }.filter({ $0 % 2 == 0 }).first\n"),
|
|
Example("↓myList.map { $0 + 1 }.filter({ $0 % 2 == 0 }).first?.something()\n"),
|
|
Example("↓myList.filter(someFunction).first\n"),
|
|
Example("↓myList.filter({ $0 % 2 == 0 })\n.first\n"),
|
|
Example("(↓myList.filter { $0 == 1 }).first\n")
|
|
]
|
|
)
|
|
|
|
public func validate(file: SwiftLintFile) -> [StyleViolation] {
|
|
return validate(
|
|
file: file,
|
|
pattern: "[\\}\\)]\\s*\\.first",
|
|
patternSyntaxKinds: [.identifier],
|
|
callNameSuffix: ".filter",
|
|
severity: configuration.severity
|
|
) { dictionary in
|
|
if
|
|
dictionary.substructure.isNotEmpty &&
|
|
dictionary.substructure.last?.expressionKind != .argument &&
|
|
dictionary.substructure.last?.name != "NSPredicate"
|
|
{
|
|
return true // has a substructure, like a closure
|
|
}
|
|
|
|
guard let bodyRange = dictionary.bodyByteRange else {
|
|
return true
|
|
}
|
|
|
|
let syntaxKinds = file.syntaxMap.kinds(inByteRange: bodyRange)
|
|
return !syntaxKinds.contains(.string)
|
|
}
|
|
}
|
|
}
|