mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
fcf848608e
* Add Example wrapper in order to display test failures inline when running in Xcode. * Stop using Swift 5.1-only features so we can compile on Xcode 10.2. * Wrap strings in Example. * Add Changelog entry. * Wrap all examples in Example struct. * Better and more complete capturing of line numbers. * Fix broken test. * Better test traceability. * Address or disable linting warnings. * Add documentation comments. * Disable linter for a few cases. * Limit mutability and add copy-and-mutate utility functions. * Limit scope of mutability.
51 lines
2.1 KiB
Swift
51 lines
2.1 KiB
Swift
import SourceKittenFramework
|
|
|
|
public struct LastWhereRule: CallPairRule, OptInRule, ConfigurationProviderRule, AutomaticTestableRule {
|
|
public var configuration = SeverityConfiguration(.warning)
|
|
|
|
public init() {}
|
|
|
|
public static let description = RuleDescription(
|
|
identifier: "last_where",
|
|
name: "Last Where",
|
|
description: "Prefer using `.last(where:)` over `.filter { }.last` in collections.",
|
|
kind: .performance,
|
|
minSwiftVersion: .fourDotTwo,
|
|
nonTriggeringExamples: [
|
|
Example("kinds.filter(excludingKinds.contains).isEmpty && kinds.last == .identifier\n"),
|
|
Example("myList.last(where: { $0 % 2 == 0 })\n"),
|
|
Example("match(pattern: pattern).filter { $0.last == .identifier }\n"),
|
|
Example("(myList.filter { $0 == 1 }.suffix(2)).last\n"),
|
|
Example("collection.filter(\"stringCol = '3'\").last")
|
|
],
|
|
triggeringExamples: [
|
|
Example("↓myList.filter { $0 % 2 == 0 }.last\n"),
|
|
Example("↓myList.filter({ $0 % 2 == 0 }).last\n"),
|
|
Example("↓myList.map { $0 + 1 }.filter({ $0 % 2 == 0 }).last\n"),
|
|
Example("↓myList.map { $0 + 1 }.filter({ $0 % 2 == 0 }).last?.something()\n"),
|
|
Example("↓myList.filter(someFunction).last\n"),
|
|
Example("↓myList.filter({ $0 % 2 == 0 })\n.last\n"),
|
|
Example("(↓myList.filter { $0 == 1 }).last\n")
|
|
]
|
|
)
|
|
|
|
public func validate(file: SwiftLintFile) -> [StyleViolation] {
|
|
return validate(file: file,
|
|
pattern: "[\\}\\)]\\s*\\.last",
|
|
patternSyntaxKinds: [.identifier],
|
|
callNameSuffix: ".filter",
|
|
severity: configuration.severity) { dictionary in
|
|
if !dictionary.substructure.isEmpty {
|
|
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)
|
|
}
|
|
}
|
|
}
|