-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Expand file tree
/
Copy pathMissingDocsRule.swift
More file actions
179 lines (158 loc) · 6.63 KB
/
MissingDocsRule.swift
File metadata and controls
179 lines (158 loc) · 6.63 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
import SwiftLintCore
import SwiftSyntax
@SwiftSyntaxRule(optIn: true)
struct MissingDocsRule: Rule {
var configuration = MissingDocsConfiguration()
static let description = RuleDescription(
identifier: "missing_docs",
name: "Missing Docs",
description: "Declarations should be documented.",
kind: .lint,
nonTriggeringExamples: MissingDocsRuleExamples.nonTriggeringExamples,
triggeringExamples: MissingDocsRuleExamples.triggeringExamples
)
}
private extension MissingDocsRule {
final class Visitor: EffectiveAccessControlSyntaxVisitor<ConfigurationType> {
init(configuration: ConfigurationType, file: SwiftLintFile) {
super.init(
configuration: configuration,
file: file,
evaluateEffectiveAcl: configuration.evaluateEffectiveAccessControlLevel
)
}
override func visit(_ node: ActorDeclSyntax) -> SyntaxVisitorContinueKind {
if configuration.excludesInheritedTypes {
_ = super.visit(node)
return .skipChildren
}
collectViolation(from: node, on: node.actorKeyword)
return super.visit(node)
}
override func visitPost(_ node: AssociatedTypeDeclSyntax) {
collectViolation(from: node, on: node.associatedtypeKeyword)
}
override func visit(_ node: ClassDeclSyntax) -> SyntaxVisitorContinueKind {
if node.inherits, configuration.excludesInheritedTypes {
_ = super.visit(node)
return .skipChildren
}
collectViolation(from: node, on: node.classKeyword)
return super.visit(node)
}
override func visit(_: ClosureExprSyntax) -> SyntaxVisitorContinueKind {
.skipChildren
}
override func visit(_: CodeBlockSyntax) -> SyntaxVisitorContinueKind {
.skipChildren
}
override func visitPost(_ node: EnumCaseDeclSyntax) {
guard !node.hasDocComment, let enumAccessControlLevel else {
return
}
if let parameter = configuration.parameters.first(where: { $0.value == enumAccessControlLevel }) {
violations.append(
.init(
position: node.caseKeyword.positionAfterSkippingLeadingTrivia,
reason: "\(enumAccessControlLevel) declarations should be documented",
severity: parameter.severity
)
)
}
}
override func visit(_ node: EnumDeclSyntax) -> SyntaxVisitorContinueKind {
if !(node.inherits && configuration.excludesInheritedTypes) {
collectViolation(from: node, on: node.enumKeyword)
}
return super.visit(node)
}
override func visit(_ node: ExtensionDeclSyntax) -> SyntaxVisitorContinueKind {
if node.inherits, configuration.excludesInheritedTypes {
_ = super.visit(node)
return .skipChildren
}
if !configuration.excludesExtensions {
collectViolation(from: node, on: node.extensionKeyword)
}
return super.visit(node)
}
override func visit(_ node: FunctionDeclSyntax) -> SyntaxVisitorContinueKind {
collectViolation(from: node, on: node.funcKeyword)
return .skipChildren
}
override func visit(_ node: InitializerDeclSyntax) -> SyntaxVisitorContinueKind {
if node.signature.parameterClause.parameters.isNotEmpty || !configuration.excludesTrivialInit {
collectViolation(from: node, on: node.initKeyword)
}
return .skipChildren
}
override func visit(_ node: ProtocolDeclSyntax) -> SyntaxVisitorContinueKind {
if !(node.inherits && configuration.excludesInheritedTypes) {
collectViolation(from: node, on: node.protocolKeyword)
}
return super.visit(node)
}
override func visit(_ node: StructDeclSyntax) -> SyntaxVisitorContinueKind {
if !(node.inherits && configuration.excludesInheritedTypes) {
collectViolation(from: node, on: node.structKeyword)
}
return super.visit(node)
}
override func visit(_ node: SubscriptDeclSyntax) -> SyntaxVisitorContinueKind {
collectViolation(from: node, on: node.subscriptKeyword)
return .skipChildren
}
override func visitPost(_ node: TypeAliasDeclSyntax) {
collectViolation(from: node, on: node.typealiasKeyword)
}
override func visit(_ node: VariableDeclSyntax) -> SyntaxVisitorContinueKind {
collectViolation(from: node, on: node.bindingSpecifier)
return .skipChildren
}
private func collectViolation(from node: some WithModifiersSyntax, on token: TokenSyntax) {
if node.modifiers.contains(keyword: .override) || node.hasDocComment {
return
}
let acl = effectiveAccessControlLevel(for: node.modifiers)
if let parameter = configuration.parameters.first(where: { $0.value == acl }) {
violations.append(
ReasonedRuleViolation(
position: token.positionAfterSkippingLeadingTrivia,
reason: "\(acl) declarations should be documented",
severity: parameter.severity
)
)
}
}
}
}
private extension DeclGroupSyntax {
var inherits: Bool {
if let types = inheritanceClause?.inheritedTypes, types.isNotEmpty {
return types.contains { !$0.type.is(SuppressedTypeSyntax.self) }
}
return false
}
}
private extension SyntaxProtocol {
var hasDocComment: Bool {
switch leadingTrivia.pieces.last(where: { !$0.isWhitespace }) {
case .docBlockComment, .docLineComment:
return true
default:
guard let item = parent?.as(CodeBlockItemSyntax.self),
let itemList = item.parent?.as(CodeBlockItemListSyntax.self),
itemList.first == item else {
return false
}
let ifConfigDecl = itemList
.parent?.as(IfConfigClauseSyntax.self)?
.parent?.as(IfConfigClauseListSyntax.self)?
.parent?.as(IfConfigDeclSyntax.self)
if let ifConfigDecl {
return ifConfigDecl.hasDocComment
}
return false
}
}
}