mirror of
https://github.com/realm/SwiftLint.git
synced 2026-06-06 20:18:40 +00:00
28 lines
1.0 KiB
Swift
28 lines
1.0 KiB
Swift
import SourceKittenFramework
|
|
|
|
public struct FlatMapOverMapReduceRule: CallPairRule, OptInRule, ConfigurationProviderRule, AutomaticTestableRule {
|
|
public var configuration = SeverityConfiguration(.warning)
|
|
|
|
public init() {}
|
|
|
|
public static let description = RuleDescription(
|
|
identifier: "flatmap_over_map_reduce",
|
|
name: "FlatMap over map and reduce",
|
|
description: "Prefer `flatMap` over `map` followed by `reduce([], +)`.",
|
|
kind: .performance,
|
|
nonTriggeringExamples: [
|
|
"let foo = bar.map { $0.count }.reduce(0, +)",
|
|
"let foo = bar.flatMap { $0.array }"
|
|
],
|
|
triggeringExamples: [
|
|
"let foo = ↓bar.map { $0.array }.reduce([], +)"
|
|
]
|
|
)
|
|
|
|
public func validate(file: SwiftLintFile) -> [StyleViolation] {
|
|
let pattern = "[\\}\\)]\\s*\\.reduce\\s*\\(\\[\\s*\\],\\s*\\+\\s*\\)"
|
|
return validate(file: file, pattern: pattern, patternSyntaxKinds: [.identifier],
|
|
callNameSuffix: ".map", severity: configuration.severity)
|
|
}
|
|
}
|