forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassDelegateProtocolRule.swift
101 lines (85 loc) · 3.52 KB
/
ClassDelegateProtocolRule.swift
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//
// ClassDelegateProtocolRule.swift
// SwiftLint
//
// Created by Marcelo Fabri on 12/23/16.
// Copyright © 2016 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
public struct ClassDelegateProtocolRule: ASTRule, ConfigurationProviderRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "class_delegate_protocol",
name: "Class Delegate Protocol",
description: "Delegate protocols should be class-only so they can be weakly referenced.",
kind: .lint,
nonTriggeringExamples: [
"protocol FooDelegate: class {}\n",
"protocol FooDelegate: class, BarDelegate {}\n",
"protocol Foo {}\n",
"class FooDelegate {}\n",
"@objc protocol FooDelegate {}\n",
"@objc(MyFooDelegate)\n protocol FooDelegate {}\n",
"protocol FooDelegate: BarDelegate {}\n",
"protocol FooDelegate: AnyObject {}\n",
"protocol FooDelegate: NSObjectProtocol {}\n"
],
triggeringExamples: [
"↓protocol FooDelegate {}\n",
"↓protocol FooDelegate: Bar {}\n"
]
)
private let referenceTypeProtocols: Set = ["AnyObject", "NSObjectProtocol"]
public func validate(file: File, kind: SwiftDeclarationKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
guard kind == .protocol else {
return []
}
// Check if name contains "Delegate"
guard let name = dictionary.name, isDelegateProtocol(name) else {
return []
}
// Check if @objc
let objcAttributes: Set<String> = ["source.decl.attribute.objc",
"source.decl.attribute.objc.name"]
let isObjc = !objcAttributes.intersection(dictionary.enclosedSwiftAttributes).isEmpty
guard !isObjc else {
return []
}
// Check if inherits from another Delegate protocol
guard dictionary.inheritedTypes.filter(isDelegateProtocol).isEmpty else {
return []
}
// Check if inherits from a known reference type protocol
guard dictionary.inheritedTypes.filter(isReferenceTypeProtocol).isEmpty else {
return []
}
// Check if : class
guard let offset = dictionary.offset,
let nameOffset = dictionary.nameOffset,
let nameLength = dictionary.nameLength,
let bodyOffset = dictionary.bodyOffset,
case let contents = file.contents.bridge(),
case let start = nameOffset + nameLength,
let range = contents.byteRangeToNSRange(start: start, length: bodyOffset - start),
!isClassProtocol(file: file, range: range) else {
return []
}
return [
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, byteOffset: offset))
]
}
private func isClassProtocol(file: File, range: NSRange) -> Bool {
return !file.match(pattern: "\\bclass\\b", with: [.keyword], range: range).isEmpty
}
private func isDelegateProtocol(_ name: String) -> Bool {
return name.hasSuffix("Delegate")
}
private func isReferenceTypeProtocol(_ name: String) -> Bool {
return referenceTypeProtocols.contains(name)
}
}