@@ -37,16 +37,15 @@ private extension PreferSelfInStaticReferencesRule {
3737
3838 final class Visitor : ViolationsSyntaxVisitor < ConfigurationType > {
3939 private var parentDeclScopes = Stack < ParentDeclBehavior > ( )
40- private var shadowingNestedTypeScopes = Stack < Bool > ( )
4140 private var variableDeclScopes = Stack < VariableDeclBehavior > ( )
4241
4342 override func visit( _ node: ActorDeclSyntax ) -> SyntaxVisitorContinueKind {
44- pushParentDeclScope ( . likeClass( name: node. name. text) , for : node . name . text , memberBlock: node. memberBlock)
43+ pushParentDeclScope ( . likeClass( name: node. name. text) , memberBlock: node. memberBlock)
4544 return . skipChildren
4645 }
4746
4847 override func visitPost( _: ActorDeclSyntax ) {
49- popParentDeclScope ( )
48+ parentDeclScopes . pop ( )
5049 }
5150
5251 override func visit( _: AttributeSyntax ) -> SyntaxVisitorContinueKind {
@@ -57,12 +56,12 @@ private extension PreferSelfInStaticReferencesRule {
5756 }
5857
5958 override func visit( _ node: ClassDeclSyntax ) -> SyntaxVisitorContinueKind {
60- pushParentDeclScope ( . likeClass( name: node. name. text) , for : node . name . text , memberBlock: node. memberBlock)
59+ pushParentDeclScope ( . likeClass( name: node. name. text) , memberBlock: node. memberBlock)
6160 return . visitChildren
6261 }
6362
6463 override func visitPost( _: ClassDeclSyntax ) {
65- popParentDeclScope ( )
64+ parentDeclScopes . pop ( )
6665 }
6766
6867 override func visit( _: CodeBlockItemListSyntax ) -> SyntaxVisitorContinueKind {
@@ -75,21 +74,21 @@ private extension PreferSelfInStaticReferencesRule {
7574 }
7675
7776 override func visit( _ node: EnumDeclSyntax ) -> SyntaxVisitorContinueKind {
78- pushParentDeclScope ( . likeStruct( node. name. text) , for : node . name . text , memberBlock: node. memberBlock)
77+ pushParentDeclScope ( . likeStruct( node. name. text) , memberBlock: node. memberBlock)
7978 return . visitChildren
8079 }
8180
8281 override func visitPost( _: EnumDeclSyntax ) {
83- popParentDeclScope ( )
82+ parentDeclScopes . pop ( )
8483 }
8584
8685 override func visit( _: ExtensionDeclSyntax ) -> SyntaxVisitorContinueKind {
87- pushParentDeclScope ( . skipReferences)
86+ parentDeclScopes . push ( . skipReferences)
8887 return . visitChildren
8988 }
9089
9190 override func visitPost( _: ExtensionDeclSyntax ) {
92- popParentDeclScope ( )
91+ parentDeclScopes . pop ( )
9392 }
9493
9594 override func visit( _ node: MemberAccessExprSyntax ) -> SyntaxVisitorContinueKind {
@@ -147,12 +146,12 @@ private extension PreferSelfInStaticReferencesRule {
147146 }
148147
149148 override func visit( _: ProtocolDeclSyntax ) -> SyntaxVisitorContinueKind {
150- pushParentDeclScope ( . skipReferences)
149+ parentDeclScopes . push ( . skipReferences)
151150 return . skipChildren
152151 }
153152
154153 override func visitPost( _: ProtocolDeclSyntax ) {
155- popParentDeclScope ( )
154+ parentDeclScopes . pop ( )
156155 }
157156
158157 override func visit( _: ReturnClauseSyntax ) -> SyntaxVisitorContinueKind {
@@ -163,12 +162,12 @@ private extension PreferSelfInStaticReferencesRule {
163162 }
164163
165164 override func visit( _ node: StructDeclSyntax ) -> SyntaxVisitorContinueKind {
166- pushParentDeclScope ( . likeStruct( node. name. text) , for : node . name . text , memberBlock: node. memberBlock)
165+ pushParentDeclScope ( . likeStruct( node. name. text) , memberBlock: node. memberBlock)
167166 return . visitChildren
168167 }
169168
170169 override func visitPost( _: StructDeclSyntax ) {
171- popParentDeclScope ( )
170+ parentDeclScopes . pop ( )
172171 }
173172
174173 override func visit( _: GenericArgumentListSyntax ) -> SyntaxVisitorContinueKind {
@@ -214,8 +213,7 @@ private extension PreferSelfInStaticReferencesRule {
214213 }
215214
216215 private func addViolation( on node: TokenSyntax ) {
217- if shadowingNestedTypeScopes. peek ( ) != true ,
218- let parentName = parentDeclScopes. peek ( ) ? . parentName,
216+ if let parentName = parentDeclScopes. peek ( ) ? . parentName,
219217 node. tokenKind == . identifier( parentName) {
220218 violations. append (
221219 at: node. positionAfterSkippingLeadingTrivia,
@@ -228,39 +226,20 @@ private extension PreferSelfInStaticReferencesRule {
228226 }
229227 }
230228
231- private func pushParentDeclScope(
232- _ behavior: ParentDeclBehavior ,
233- for name: String ? = nil ,
234- memberBlock: MemberBlockSyntax ? = nil
235- ) {
236- parentDeclScopes. push ( behavior)
237- let hasShadowingNestedType : Bool
238- if let name, let memberBlock {
239- hasShadowingNestedType = containsSameNamedNestedType ( named: name, in: memberBlock)
240- } else {
241- hasShadowingNestedType = false
242- }
243- shadowingNestedTypeScopes. push ( hasShadowingNestedType)
244- }
245-
246- private func popParentDeclScope( ) {
247- parentDeclScopes. pop ( )
248- shadowingNestedTypeScopes. pop ( )
229+ private func pushParentDeclScope( _ behavior: ParentDeclBehavior , memberBlock: MemberBlockSyntax ) {
230+ let hasShadowingNestedType =
231+ if let name = behavior. parentName {
232+ containsSameNamedNestedType ( named: name, in: memberBlock)
233+ } else {
234+ false
235+ }
236+ parentDeclScopes. push ( hasShadowingNestedType ? . skipReferences : behavior)
249237 }
250238
251239 private func containsSameNamedNestedType( named name: String , in memberBlock: MemberBlockSyntax ) -> Bool {
252240 memberBlock. members. contains { member in
253- if let actor = member. decl. as ( ActorDeclSyntax . self) {
254- return actor . name. text == name
255- }
256- if let classDecl = member. decl. as ( ClassDeclSyntax . self) {
257- return classDecl. name. text == name
258- }
259- if let enumDecl = member. decl. as ( EnumDeclSyntax . self) {
260- return enumDecl. name. text == name
261- }
262- if let structDecl = member. decl. as ( StructDeclSyntax . self) {
263- return structDecl. name. text == name
241+ if member. decl. isProtocol ( ( any DeclGroupSyntax ) . self) || member. decl. is ( TypeAliasDeclSyntax . self) {
242+ return member. decl. asProtocol ( ( any NamedDeclSyntax ) . self) ? . name. text == name
264243 }
265244
266245 return false
0 commit comments