mirror of
https://github.com/realm/SwiftLint.git
synced 2026-05-07 20:12:49 +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.
28 lines
1.1 KiB
Swift
28 lines
1.1 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: [
|
|
Example("let foo = bar.map { $0.count }.reduce(0, +)"),
|
|
Example("let foo = bar.flatMap { $0.array }")
|
|
],
|
|
triggeringExamples: [
|
|
Example("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)
|
|
}
|
|
}
|