@@ -35,7 +35,7 @@ struct FileNameRule: OptInRule, SourceKitFreeRule {
35
35
}
36
36
37
37
// Process nested type separator
38
- let allDeclaredTypeNames = TypeNameCollectingVisitor ( viewMode : . sourceAccurate )
38
+ let allDeclaredTypeNames = TypeNameCollectingVisitor ( requireFullyQualifiedNames : configuration . fullyQualified )
39
39
. walk ( tree: file. syntaxTree, handler: \. names)
40
40
. map {
41
41
$0. replacingOccurrences ( of: " . " , with: configuration. nestedTypeSeparator)
@@ -56,33 +56,98 @@ struct FileNameRule: OptInRule, SourceKitFreeRule {
56
56
}
57
57
58
58
private class TypeNameCollectingVisitor : SyntaxVisitor {
59
+ // All of a visited node's ancestor type names if that node is nested, starting with the furthest
60
+ // ancestor and ending with the direct parent
61
+ private var ancestorNames : [ String ] = [ ]
62
+
63
+ // All of the type names found in the file
59
64
private( set) var names : Set < String > = [ ]
60
65
66
+ // If true, nested types are only allowed in the file name when used by their fully-qualified name
67
+ // (e.g. `My.Nested.Type` and not just `Type`)
68
+ private let requireFullyQualifiedNames : Bool
69
+
70
+ init ( requireFullyQualifiedNames: Bool ) {
71
+ self . requireFullyQualifiedNames = requireFullyQualifiedNames
72
+ super. init ( viewMode: . sourceAccurate)
73
+ }
74
+
75
+ private func addVisitedNodeName( _ name: String ) {
76
+ let fullyQualifiedName = ( ancestorNames + [ name] ) . joined ( separator: " . " )
77
+ names. insert ( fullyQualifiedName)
78
+
79
+ if !requireFullyQualifiedNames {
80
+ names. insert ( name)
81
+ }
82
+ }
83
+
84
+ override func visit( _ node: ClassDeclSyntax ) -> SyntaxVisitorContinueKind {
85
+ ancestorNames. append ( node. name. text)
86
+ return . visitChildren
87
+ }
88
+
61
89
override func visitPost( _ node: ClassDeclSyntax ) {
62
- names. insert ( node. name. text)
90
+ ancestorNames. removeLast ( )
91
+ addVisitedNodeName ( node. name. text)
92
+ }
93
+
94
+ override func visit( _ node: ActorDeclSyntax ) -> SyntaxVisitorContinueKind {
95
+ ancestorNames. append ( node. name. text)
96
+ return . visitChildren
63
97
}
64
98
65
99
override func visitPost( _ node: ActorDeclSyntax ) {
66
- names. insert ( node. name. text)
100
+ ancestorNames. removeLast ( )
101
+ addVisitedNodeName ( node. name. text)
102
+ }
103
+
104
+ override func visit( _ node: StructDeclSyntax ) -> SyntaxVisitorContinueKind {
105
+ ancestorNames. append ( node. name. text)
106
+ return . visitChildren
67
107
}
68
108
69
109
override func visitPost( _ node: StructDeclSyntax ) {
70
- names. insert ( node. name. text)
110
+ ancestorNames. removeLast ( )
111
+ addVisitedNodeName ( node. name. text)
112
+ }
113
+
114
+ override func visit( _ node: TypeAliasDeclSyntax ) -> SyntaxVisitorContinueKind {
115
+ ancestorNames. append ( node. name. text)
116
+ return . visitChildren
71
117
}
72
118
73
119
override func visitPost( _ node: TypeAliasDeclSyntax ) {
74
- names. insert ( node. name. text)
120
+ ancestorNames. removeLast ( )
121
+ addVisitedNodeName ( node. name. text)
122
+ }
123
+
124
+ override func visit( _ node: EnumDeclSyntax ) -> SyntaxVisitorContinueKind {
125
+ ancestorNames. append ( node. name. text)
126
+ return . visitChildren
75
127
}
76
128
77
129
override func visitPost( _ node: EnumDeclSyntax ) {
78
- names. insert ( node. name. text)
130
+ ancestorNames. removeLast ( )
131
+ addVisitedNodeName ( node. name. text)
132
+ }
133
+
134
+ override func visit( _ node: ProtocolDeclSyntax ) -> SyntaxVisitorContinueKind {
135
+ ancestorNames. append ( node. name. text)
136
+ return . visitChildren
79
137
}
80
138
81
139
override func visitPost( _ node: ProtocolDeclSyntax ) {
82
- names. insert ( node. name. text)
140
+ ancestorNames. removeLast ( )
141
+ addVisitedNodeName ( node. name. text)
142
+ }
143
+
144
+ override func visit( _ node: ExtensionDeclSyntax ) -> SyntaxVisitorContinueKind {
145
+ ancestorNames. append ( node. extendedType. trimmedDescription)
146
+ return . visitChildren
83
147
}
84
148
85
149
override func visitPost( _ node: ExtensionDeclSyntax ) {
86
- names. insert ( node. extendedType. trimmedDescription)
150
+ ancestorNames. removeLast ( )
151
+ addVisitedNodeName ( node. extendedType. trimmedDescription)
87
152
}
88
153
}
0 commit comments