-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathExtensionAccessModifierRule.swift
More file actions
253 lines (238 loc) · 7.6 KB
/
ExtensionAccessModifierRule.swift
File metadata and controls
253 lines (238 loc) · 7.6 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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import SwiftLintCore
import SwiftSyntax
@SwiftSyntaxRule(optIn: true)
struct ExtensionAccessModifierRule: Rule {
var configuration = SeverityConfiguration<Self>(.warning)
static let description = RuleDescription(
identifier: "extension_access_modifier",
name: "Extension Access Modifier",
description: "Prefer to use extension access modifiers",
kind: .idiomatic,
nonTriggeringExamples: [
Example("""
extension Foo: SomeProtocol {
public var bar: Int { return 1 }
}
"""),
Example("""
extension Foo {
private var bar: Int { return 1 }
public var baz: Int { return 1 }
}
"""),
Example("""
extension Foo {
private var bar: Int { return 1 }
public func baz() {}
}
"""),
Example("""
extension Foo {
var bar: Int { return 1 }
var baz: Int { return 1 }
}
"""),
Example("""
extension Foo {
var bar: Int { return 1 }
internal var baz: Int { return 1 }
}
"""),
Example("""
internal extension Foo {
var bar: Int { return 1 }
var baz: Int { return 1 }
}
"""),
Example("""
public extension Foo {
var bar: Int { return 1 }
var baz: Int { return 1 }
}
"""),
Example("""
public extension Foo {
var bar: Int { return 1 }
internal var baz: Int { return 1 }
}
"""),
Example("""
extension Foo {
private var bar: Int { return 1 }
private var baz: Int { return 1 }
}
"""),
Example("""
extension Foo {
open var bar: Int { return 1 }
open var baz: Int { return 1 }
}
"""),
Example("""
extension Foo {
func setup() {}
public func update() {}
}
"""),
Example("""
private extension Foo {
private var bar: Int { return 1 }
var baz: Int { return 1 }
}
"""),
Example("""
extension Foo {
internal private(set) var bar: Int {
get { Foo.shared.bar }
set { Foo.shared.bar = newValue }
}
}
"""),
Example("""
extension Foo {
private(set) internal var bar: Int {
get { Foo.shared.bar }
set { Foo.shared.bar = newValue }
}
}
"""),
Example("""
public extension Foo {
private(set) var value: Int { 1 }
}
"""),
],
triggeringExamples: [
Example("""
↓extension Foo {
public var bar: Int { return 1 }
public var baz: Int { return 1 }
}
"""),
Example("""
↓extension Foo {
public var bar: Int { return 1 }
public func baz() {}
}
"""),
Example("""
public extension Foo {
↓public func bar() {}
↓public func baz() {}
}
"""),
Example("""
↓extension Foo {
public var bar: Int {
let value = 1
return value
}
public var baz: Int { return 1 }
}
"""),
Example("""
↓extension Array where Element: Equatable {
public var unique: [Element] {
var uniqueValues = [Element]()
for item in self where !uniqueValues.contains(item) {
uniqueValues.append(item)
}
return uniqueValues
}
}
"""),
Example("""
↓extension Foo {
#if DEBUG
public var bar: Int {
let value = 1
return value
}
#endif
public var baz: Int { return 1 }
}
"""),
Example("""
public extension Foo {
↓private func bar() {}
↓private func baz() {}
}
"""),
Example("""
↓extension Foo {
private(set) public var value: Int { 1 }
}
"""),
]
)
}
private extension ExtensionAccessModifierRule {
private enum ACL: Hashable {
case implicit
case explicit(TokenKind)
static func from(tokenKind: TokenKind?) -> Self {
switch tokenKind {
case nil:
.implicit
case let value?:
.explicit(value)
}
}
static func isAllowed(_ acl: Self) -> Bool {
[
.explicit(.keyword(.internal)),
.explicit(.keyword(.private)),
.explicit(.keyword(.open)),
.implicit,
].contains(acl)
}
}
final class Visitor: ViolationsSyntaxVisitor<ConfigurationType> {
override var skippableDeclarations: [any DeclSyntaxProtocol.Type] { .all }
override func visitPost(_ node: ExtensionDeclSyntax) {
guard node.inheritanceClause == nil else {
return
}
var areAllACLsEqual = true
var aclTokens = [(position: AbsolutePosition, acl: ACL)]()
for decl in node.memberBlock.expandingIfConfigs() {
let modifiers = decl.asProtocol((any WithModifiersSyntax).self)?.modifiers
let aclToken = modifiers?.accessLevelModifier()?.name
let acl = ACL.from(tokenKind: aclToken?.tokenKind)
if areAllACLsEqual, acl != aclTokens.last?.acl, aclTokens.isNotEmpty {
areAllACLsEqual = false
}
aclTokens.append((decl.positionAfterSkippingLeadingTrivia, acl))
}
guard areAllACLsEqual, let lastACL = aclTokens.last else {
return
}
let isAllowedACL = ACL.isAllowed(lastACL.acl)
let extensionACL = ACL.from(tokenKind: node.modifiers.accessLevelModifier?.name.tokenKind)
if extensionACL != .implicit {
if !isAllowedACL || lastACL.acl != extensionACL, lastACL.acl != .implicit {
violations.append(contentsOf: aclTokens.map(\.position))
}
} else if !isAllowedACL {
violations.append(node.extensionKeyword.positionAfterSkippingLeadingTrivia)
}
}
}
}
private extension MemberBlockSyntax {
func expandingIfConfigs() -> [DeclSyntax] {
members.flatMap { member in
if let ifConfig = member.decl.as(IfConfigDeclSyntax.self) {
return ifConfig.clauses.flatMap { clause -> [DeclSyntax] in
switch clause.elements {
case .decls(let decls):
decls.map(\.decl)
default:
[]
}
}
}
return [member.decl]
}
}
}