@@ -11,5 +11,105 @@ import SwiftSyntax
11
11
///
12
12
/// - SeeAlso: https://google.github.io/swift#naming-conventions-are-not-access-control
13
13
public final class NoLeadingUnderscores : SyntaxLintRule {
14
+
15
+ public override func visit( _ node: AssociatedtypeDeclSyntax ) {
16
+ diagnoseUnderscoreViolation ( name: node. identifier)
17
+ }
18
+
19
+ public override func visit( _ node: ClassDeclSyntax ) {
20
+ diagnoseUnderscoreViolation ( name: node. identifier)
21
+ super. visit ( node) // Visit children despite override
22
+ }
23
+
24
+ public override func visit( _ node: EnumDeclSyntax ) {
25
+ diagnoseUnderscoreViolation ( name: node. identifier)
26
+ super. visit ( node)
27
+ }
28
+
29
+ public override func visit( _ node: EnumCaseDeclSyntax ) {
30
+ for element in node. elements {
31
+ diagnoseUnderscoreViolation ( name: element. identifier)
32
+ }
33
+ }
34
+
35
+ public override func visit( _ node: FunctionDeclSyntax ) {
36
+ diagnoseUnderscoreViolation ( name: node. identifier)
37
+ // Check parameter names of function
38
+ let parameters = node. signature. input. parameterList
39
+ for parameter in parameters {
40
+ if let typeIdentifier = parameter. firstName {
41
+ diagnoseUnderscoreViolation ( name: typeIdentifier)
42
+ }
43
+ if let varIdentifier = parameter. secondName {
44
+ diagnoseUnderscoreViolation ( name: varIdentifier)
45
+ }
46
+ }
47
+ // Check generic parameter names
48
+ if let genParameters = node. genericParameterClause? . genericParameterList {
49
+ for genParameter in genParameters {
50
+ diagnoseUnderscoreViolation ( name: genParameter. name)
51
+ }
52
+ }
53
+ super. visit ( node)
54
+ }
55
+
56
+ public override func visit( _ node: PrecedenceGroupDeclSyntax ) {
57
+ diagnoseUnderscoreViolation ( name: node. identifier)
58
+ }
59
+
60
+ public override func visit( _ node: ProtocolDeclSyntax ) {
61
+ diagnoseUnderscoreViolation ( name: node. identifier)
62
+ super. visit ( node)
63
+ }
64
+
65
+ public override func visit( _ node: StructDeclSyntax ) {
66
+ diagnoseUnderscoreViolation ( name: node. identifier)
67
+ // Check generic parameter names
68
+ if let genParameters = node. genericParameterClause? . genericParameterList {
69
+ for genParameter in genParameters {
70
+ diagnoseUnderscoreViolation ( name: genParameter. name)
71
+ }
72
+ }
73
+ super. visit ( node)
74
+ }
75
+
76
+ public override func visit( _ node: TypealiasDeclSyntax ) {
77
+ diagnoseUnderscoreViolation ( name: node. identifier)
78
+ }
79
+
80
+ public override func visit( _ node: InitializerDeclSyntax ) {
81
+ // Check parameter names of initializer
82
+ let parameters = node. parameters. parameterList
83
+ for parameter in parameters {
84
+ if let typeIdentifier = parameter. firstName {
85
+ diagnoseUnderscoreViolation ( name: typeIdentifier)
86
+ }
87
+ if let varIdentifier = parameter. secondName {
88
+ diagnoseUnderscoreViolation ( name: varIdentifier)
89
+ }
90
+ }
91
+ super. visit ( node)
92
+ }
93
+
94
+ public override func visit( _ node: VariableDeclSyntax ) {
95
+ for binding in node. bindings {
96
+ if let pat = binding. pattern as? IdentifierPatternSyntax {
97
+ diagnoseUnderscoreViolation ( name: pat. identifier)
98
+ }
99
+ }
100
+ super. visit ( node)
101
+ }
102
+
103
+ func diagnoseUnderscoreViolation( name: TokenSyntax ) {
104
+ let leadingChar = name. text. first
105
+ if leadingChar == " _ " {
106
+ diagnose ( . doNotLeadWithUnderscore( identifier: name. text) , on: name)
107
+ }
108
+ }
109
+ }
14
110
111
+ extension Diagnostic . Message {
112
+ static func doNotLeadWithUnderscore( identifier: String ) -> Diagnostic . Message {
113
+ return . init( . warning, " Identifier \( identifier) should not lead with '_' " )
114
+ }
15
115
}
0 commit comments