mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
b83e0991b9
The MIT license doesn't require that all files be prepended with this licensing or copyright information. Realm confirmed that they're ok with this change. This will enable some companies to contribute to SwiftLint and the date & authorship information will remain accessible via git source control.
45 lines
1.9 KiB
Swift
45 lines
1.9 KiB
Swift
import SourceKittenFramework
|
|
|
|
public struct SortedFirstLastRule: CallPairRule, OptInRule, ConfigurationProviderRule {
|
|
public var configuration = SeverityConfiguration(.warning)
|
|
|
|
public init() {}
|
|
|
|
public static let description = RuleDescription(
|
|
identifier: "sorted_first_last",
|
|
name: "Min or Max over Sorted First or Last",
|
|
description: "Prefer using `min()` or `max()` over `sorted().first` or `sorted().last`",
|
|
kind: .performance,
|
|
nonTriggeringExamples: [
|
|
"let min = myList.min()\n",
|
|
"let min = myList.min(by: { $0 < $1 })\n",
|
|
"let min = myList.min(by: >)\n",
|
|
"let min = myList.max()\n",
|
|
"let min = myList.max(by: { $0 < $1 })\n"
|
|
],
|
|
triggeringExamples: [
|
|
"↓myList.sorted().first\n",
|
|
"↓myList.sorted(by: { $0.description < $1.description }).first\n",
|
|
"↓myList.sorted(by: >).first\n",
|
|
"↓myList.map { $0 + 1 }.sorted().first\n",
|
|
"↓myList.sorted(by: someFunction).first\n",
|
|
"↓myList.map { $0 + 1 }.sorted { $0.description < $1.description }.first\n",
|
|
"↓myList.sorted().last\n",
|
|
"↓myList.sorted().last?.something()\n",
|
|
"↓myList.sorted(by: { $0.description < $1.description }).last\n",
|
|
"↓myList.map { $0 + 1 }.sorted().last\n",
|
|
"↓myList.sorted(by: someFunction).last\n",
|
|
"↓myList.map { $0 + 1 }.sorted { $0.description < $1.description }.last\n",
|
|
"↓myList.map { $0 + 1 }.sorted { $0.first < $1.first }.last\n"
|
|
]
|
|
)
|
|
|
|
public func validate(file: File) -> [StyleViolation] {
|
|
return validate(file: file,
|
|
pattern: "[\\}\\)]\\s*\\.(first|last)",
|
|
patternSyntaxKinds: [.identifier],
|
|
callNameSuffix: ".sorted",
|
|
severity: configuration.severity)
|
|
}
|
|
}
|