-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathCombineFlatMapRule.swift
More file actions
58 lines (49 loc) · 1.74 KB
/
CombineFlatMapRule.swift
File metadata and controls
58 lines (49 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import SwiftSyntax
@SwiftSyntaxRule
struct CombineFlatMapRule: Rule {
var configuration = SeverityConfiguration<Self>(.warning)
static let description = RuleDescription(
identifier: "combine_flatmap",
name: "Combine FlatMap",
description: "Avoid using Combine's flatMap operator.",
kind: .lint,
nonTriggeringExamples: [
Example("[1, 2, 3].flatMap { $0 }"),
Example("array.flatMap { $0 }"),
Example("Combine.map { $0 }")
],
triggeringExamples: [
Example("Combine.flatMap { $0 }"),
Example("let transform = Combine.flatMap")
]
)
}
private extension CombineFlatMapRule {
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
override func visitPost(_ node: FunctionCallExprSyntax) {
guard let memberAccess = node.calledExpression.as(MemberAccessExprSyntax.self),
memberAccess.isCombineFlatMap else {
return
}
violations.append(node.positionAfterSkippingLeadingTrivia)
}
override func visitPost(_ node: MemberAccessExprSyntax) {
guard node.isCombineFlatMap else {
return
}
if let parent = node.parent?.as(FunctionCallExprSyntax.self),
parent.calledExpression.id == node.id {
return
}
violations.append(node.positionAfterSkippingLeadingTrivia)
}
}
}
private extension MemberAccessExprSyntax {
var isCombineFlatMap: Bool {
guard declName.baseName.text == "flatMap" else {
return false
}
return base?.as(DeclReferenceExprSyntax.self)?.baseName.text == "Combine"
}
}