forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExtensionAccessModifierRule.swift
147 lines (129 loc) · 5.83 KB
/
ExtensionAccessModifierRule.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
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
//
// ExtensionAccessModifierRule.swift
// SwiftLint
//
// Created by Marcelo Fabri on 26/05/17.
// Copyright © 2017 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
public struct ExtensionAccessModifierRule: ASTRule, ConfigurationProviderRule, OptInRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "extension_access_modifier",
name: "Extension Access Modifier",
description: "Prefer to use extension access modifiers",
kind: .idiomatic,
nonTriggeringExamples: [
"extension Foo: SomeProtocol {\n" +
" public var bar: Int { return 1 }\n" +
"}",
"extension Foo {\n" +
" private var bar: Int { return 1 }\n" +
" public var baz: Int { return 1 }\n" +
"}",
"extension Foo {\n" +
" private var bar: Int { return 1 }\n" +
" public func baz() {}\n" +
"}",
"extension Foo {\n" +
" var bar: Int { return 1 }\n" +
" var baz: Int { return 1 }\n" +
"}",
"public extension Foo {\n" +
" var bar: Int { return 1 }\n" +
" var baz: Int { return 1 }\n" +
"}",
"extension Foo {\n" +
" private bar: Int { return 1 }\n" +
" private baz: Int { return 1 }\n" +
"}",
"extension Foo {\n" +
" open bar: Int { return 1 }\n" +
" open baz: Int { return 1 }\n" +
"}"
],
triggeringExamples: [
"↓extension Foo {\n" +
" public var bar: Int { return 1 }\n" +
" public var baz: Int { return 1 }\n" +
"}",
"↓extension Foo {\n" +
" public var bar: Int { return 1 }\n" +
" public func baz() {}\n" +
"}",
"public extension Foo {\n" +
" public ↓func bar() {}\n" +
" public ↓func baz() {}\n" +
"}"
]
)
public func validate(file: File, kind: SwiftDeclarationKind,
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
guard kind == .extension, let offset = dictionary.offset,
dictionary.inheritedTypes.isEmpty else {
return []
}
let declarations = dictionary.substructure.flatMap { entry -> (acl: AccessControlLevel, offset: Int)? in
guard entry.kind.flatMap(SwiftDeclarationKind.init) != nil,
let acl = entry.accessibility.flatMap(AccessControlLevel.init(identifier:)),
let offset = entry.offset else {
return nil
}
return (acl: acl, offset: offset)
}
let declarationsACLs = declarations.map { $0.acl }.unique
let allowedACLs: Set<AccessControlLevel> = [.internal, .private, .open]
guard declarationsACLs.count == 1, !allowedACLs.contains(declarationsACLs[0]) else {
return []
}
let syntaxTokens = file.syntaxMap.tokens
let parts = syntaxTokens.partitioned { offset <= $0.offset }
if let aclToken = parts.first.last, file.isACL(token: aclToken) {
return declarationsViolations(file: file, acl: declarationsACLs[0],
declarationOffsets: declarations.map { $0.offset },
dictionary: dictionary)
}
return [
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, byteOffset: offset))
]
}
private func declarationsViolations(file: File, acl: AccessControlLevel,
declarationOffsets: [Int],
dictionary: [String: SourceKitRepresentable]) -> [StyleViolation] {
guard let offset = dictionary.offset, let length = dictionary.length,
case let contents = file.contents.bridge(),
let range = contents.byteRangeToNSRange(start: offset, length: length) else {
return []
}
// find all ACL tokens
let allACLRanges = file.match(pattern: acl.description, with: [.attributeBuiltin], range: range).flatMap {
contents.NSRangeToByteRange(start: $0.location, length: $0.length)
}
let violationOffsets = declarationOffsets.filter { typeOffset in
// find the last ACL token before the type
guard let previousInternalByteRange = lastACLByteRange(before: typeOffset, in: allACLRanges) else {
// didn't find a candidate token, so the ACL is implict (not a violation)
return false
}
// the ACL token correspond to the type if there're only
// attributeBuiltin (`final` for example) tokens between them
let length = typeOffset - previousInternalByteRange.location
let range = NSRange(location: previousInternalByteRange.location, length: length)
let internalBelongsToType = Set(file.syntaxMap.kinds(inByteRange: range)) == [.attributeBuiltin]
return internalBelongsToType
}
return violationOffsets.map {
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, byteOffset: $0))
}
}
private func lastACLByteRange(before typeOffset: Int, in ranges: [NSRange]) -> NSRange? {
let firstPartition = ranges.partitioned(by: { $0.location > typeOffset }).first
return firstPartition.last
}
}