Files
SwiftLint/Source/SwiftLintFramework/Rules/Performance/FlatMapOverMapReduceRule.swift
T
2019-11-07 15:19:17 +02:00

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)
}
}