forked from realm/SwiftLint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ExplicitTopLevelACLRule.swift
88 lines (75 loc) · 3.32 KB
/
ExplicitTopLevelACLRule.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
//
// ExplicitTopLevelACLRule.swift
// SwiftLint
//
// Created by Jose Cheyo Jimenez on 4/28/17.
// Copyright © 2017 Realm. All rights reserved.
//
import Foundation
import SourceKittenFramework
public struct ExplicitTopLevelACLRule: OptInRule, ConfigurationProviderRule {
public var configuration = SeverityConfiguration(.warning)
public init() {}
public static let description = RuleDescription(
identifier: "explicit_top_level_acl",
name: "Explicit Top Level ACL",
description: "Top-level declarations should specify Access Control Level keywords explicitly.",
kind: .idiomatic,
nonTriggeringExamples: [
"internal enum A {}\n",
"public final class B {}\n",
"private struct C {}\n",
"internal enum A {\n enum B {}\n}",
"internal final class Foo {}",
"internal\nclass Foo {}",
"internal func a() {}\n"
],
triggeringExamples: [
"enum A {}\n",
"final class B {}\n",
"struct C {}\n",
"func a() {}\n",
"internal let a = 0\nfunc b() {}\n"
]
)
public func validate(file: File) -> [StyleViolation] {
// find all top-level types marked as internal (either explictly or implictly)
let internalTypesOffsets = file.structure.dictionary.substructure.flatMap { element -> Int? in
if element.accessibility.flatMap(AccessControlLevel.init(identifier:)) == .internal {
return element.offset
}
return nil
}
guard !internalTypesOffsets.isEmpty else {
return []
}
// find all "internal" tokens
let contents = file.contents.bridge()
let allInternalRanges = file.match(pattern: "internal", with: [.attributeBuiltin]).flatMap {
contents.NSRangeToByteRange(start: $0.location, length: $0.length)
}
let violationOffsets = internalTypesOffsets.filter { typeOffset in
// find the last "internal" token before the type
guard let previousInternalByteRange = lastInternalByteRange(before: typeOffset,
in: allInternalRanges) else {
// didn't find a candidate token, so we are sure it's a violation
return true
}
// the "internal" 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 internalDoesntBelongToType = Set(file.syntaxMap.kinds(inByteRange: range)) != [.attributeBuiltin]
return internalDoesntBelongToType
}
return violationOffsets.map {
StyleViolation(ruleDescription: type(of: self).description,
severity: configuration.severity,
location: Location(file: file, byteOffset: $0))
}
}
private func lastInternalByteRange(before typeOffset: Int, in ranges: [NSRange]) -> NSRange? {
let firstPartition = ranges.prefix(while: { typeOffset > $0.location })
return firstPartition.last
}
}