@@ -29,7 +29,23 @@ public struct ValidateDocumentationComments: SyntaxLintRule {
29
29
self . context = context
30
30
}
31
31
32
+ public func visit( _ node: InitializerDeclSyntax ) -> SyntaxVisitorContinueKind {
33
+ return checkFunctionLikeDocumentation (
34
+ node, name: " init " , parameters: node. parameters. parameterList)
35
+ }
36
+
32
37
public func visit( _ node: FunctionDeclSyntax ) -> SyntaxVisitorContinueKind {
38
+ return checkFunctionLikeDocumentation (
39
+ node, name: node. identifier. text, parameters: node. signature. input. parameterList,
40
+ returnClause: node. signature. output)
41
+ }
42
+
43
+ private func checkFunctionLikeDocumentation(
44
+ _ node: DeclSyntax ,
45
+ name: String ,
46
+ parameters: FunctionParameterListSyntax ,
47
+ returnClause: ReturnClauseSyntax ? = nil
48
+ ) -> SyntaxVisitorContinueKind {
33
49
guard let declComment = node. docComment else { return . skipChildren }
34
50
guard let commentInfo = node. docCommentInfo else { return . skipChildren }
35
51
guard let params = commentInfo. parameters else { return . skipChildren }
@@ -48,8 +64,8 @@ public struct ValidateDocumentationComments: SyntaxLintRule {
48
64
let hasPluralDesc = declComment. components ( separatedBy: . newlines)
49
65
. contains { $0. trimmingCharacters ( in: . whitespaces) . starts ( with: " - Parameters " ) }
50
66
51
- validateReturn ( node , returnDesc: commentInfo. returnsDescription)
52
- let funcParameters = funcParametersIdentifiers ( in: node . signature . input . parameterList )
67
+ validateReturn ( returnClause , name : name , returnDesc: commentInfo. returnsDescription)
68
+ let funcParameters = funcParametersIdentifiers ( in: parameters )
53
69
54
70
// If the documentation of the parameters is wrong 'docCommentInfo' won't
55
71
// parse the parameters correctly. First the documentation has to be fix
@@ -67,20 +83,20 @@ public struct ValidateDocumentationComments: SyntaxLintRule {
67
83
// are the same.
68
84
if ( params. count != funcParameters. count) ||
69
85
!parametersAreEqual( params: params, funcParam: funcParameters) {
70
- diagnose ( . parametersDontMatch( funcName: node . identifier . text ) , on: node)
86
+ diagnose ( . parametersDontMatch( funcName: name ) , on: node)
71
87
}
72
88
73
89
return . skipChildren
74
90
}
75
91
76
92
/// Ensures the function has a return documentation if it actually returns
77
93
/// a value.
78
- func validateReturn( _ node : FunctionDeclSyntax , returnDesc: String ? ) {
79
- if node . signature . output == nil && returnDesc != nil {
80
- diagnose ( . removeReturnComment( funcName: node . identifier . text ) , on: node )
94
+ func validateReturn( _ returnClause : ReturnClauseSyntax ? , name : String , returnDesc: String ? ) {
95
+ if returnClause == nil && returnDesc != nil {
96
+ diagnose ( . removeReturnComment( funcName: name ) , on: returnClause )
81
97
}
82
- else if node . signature . output != nil && returnDesc == nil {
83
- diagnose ( . documentReturnValue( funcName: node . identifier . text ) , on: node )
98
+ else if returnClause != nil && returnDesc == nil {
99
+ diagnose ( . documentReturnValue( funcName: name ) , on: returnClause )
84
100
}
85
101
}
86
102
}
@@ -89,9 +105,13 @@ public struct ValidateDocumentationComments: SyntaxLintRule {
89
105
/// paramters identifiers.
90
106
func funcParametersIdentifiers( in paramList: FunctionParameterListSyntax ) -> [ String ] {
91
107
var funcParameters = [ String] ( )
92
- for parameter in paramList
93
- {
94
- guard let parameterIdentifier = parameter. firstName else { continue }
108
+ for parameter in paramList {
109
+ // If there is a label and an identifier, then the identifier (`secondName`) is the name that
110
+ // should be documented. Otherwise, the label and identifier are the same, occupying
111
+ // `firstName`.
112
+ guard let parameterIdentifier = parameter. secondName ?? parameter. firstName else {
113
+ continue
114
+ }
95
115
funcParameters. append ( parameterIdentifier. text)
96
116
}
97
117
return funcParameters
0 commit comments