@@ -92,7 +92,7 @@ class TypeDeclResolver {
9292 }
9393
9494 /// Builds the type name scope for a given type usage
95- private func buildScope ( type : IdentifierTypeSyntax ) -> QualifiedName {
95+ private func buildScope ( type : TypeSyntax ) -> QualifiedName {
9696 var innerToOuter : [ String ] = [ ]
9797 var context : SyntaxProtocol = type
9898 while let parent = context . parent {
@@ -108,25 +108,29 @@ class TypeDeclResolver {
108108 return innerToOuter . reversed ( )
109109 }
110110
111- /// Looks up a qualified name of a type declaration by its unqualified type usage
111+ /// Looks up a qualified name of a type declaration relative to its lexical scope
112112 /// Returns the qualified name hierarchy of the type declaration
113- /// If the type declaration is not found, returns the unqualified name
114- private func tryQualify ( type : IdentifierTypeSyntax ) -> QualifiedName {
115- let name = type . name . text
113+ /// If the type declaration is not found, returns the original qualified name
114+ private func tryQualify ( type : TypeSyntax ) -> QualifiedName ? {
115+ guard let components = type . qualifiedComponents else {
116+ return nil
117+ }
116118 let scope = buildScope ( type : type )
117119 /// Search for the type declaration from the innermost scope to the outermost scope
118120 for i in ( 0 ... scope . count ) . reversed ( ) {
119- let qualifiedName = Array ( scope [ 0 ..< i ] + [ name ] )
121+ let qualifiedName = Array ( scope . prefix ( i ) ) + components
120122 if typeDeclByQualifiedName [ qualifiedName ] != nil || typeAliasByQualifiedName [ qualifiedName ] != nil {
121123 return qualifiedName
122124 }
123125 }
124- return [ name ]
126+ return components
125127 }
126128
127129 /// Looks up a type declaration by its unqualified type usage
128130 func lookupType ( for type : IdentifierTypeSyntax ) -> TypeDecl ? {
129- let qualifiedName = tryQualify ( type : type )
131+ guard let qualifiedName = tryQualify ( type : TypeSyntax ( type ) ) else {
132+ return nil
133+ }
130134 return typeDeclByQualifiedName [ qualifiedName ]
131135 }
132136
@@ -139,36 +143,35 @@ class TypeDeclResolver {
139143 ///
140144 /// Supported inputs:
141145 /// - IdentifierTypeSyntax (e.g. `Method`) — resolved relative to the lexical scope, preferring the innermost enclosing type.
142- /// - MemberTypeSyntax (e.g. `Networking.API.Method`) — resolved by recursively building the fully qualified name.
146+ /// - MemberTypeSyntax (e.g. `Networking.API.Method`) — resolved relative to the lexical scope before falling back to the fully qualified name.
143147 ///
144148 /// Resolution strategy:
145- /// 1. If the node is IdentifierTypeSyntax, call `lookupType(for:)` which attempts scope-aware qualification via `tryQualify`.
146- /// 2. Otherwise, attempt to build a fully qualified name with `qualifiedComponents` and look it up with `lookupType(fullyQualified:)`.
149+ /// Build the qualified name with `qualifiedComponents`, then attempt scope-aware qualification via `tryQualify`.
147150 ///
148151 /// - Parameter type: The SwiftSyntax node representing a type appearance in source code.
149152 /// - Returns: The nominal declaration (enum/class/actor/struct) if found, otherwise nil.
150153 func resolve ( _ type : TypeSyntax ) -> TypeDecl ? {
151- if let id = type . as ( IdentifierTypeSyntax . self ) {
152- return lookupType ( for : id )
153- }
154- if let components = type . qualifiedComponents {
155- return lookupType ( fullyQualified : components )
154+ if let qualifiedName = tryQualify ( type : type ) {
155+ return lookupType ( fullyQualified : qualifiedName )
156156 }
157157 return nil
158158 }
159159
160+ func resolveExtensionTarget ( _ type : TypeSyntax ) -> TypeDecl ? {
161+ guard let qualifiedName = type . qualifiedComponents else {
162+ return nil
163+ }
164+ return lookupType ( fullyQualified : qualifiedName )
165+ }
166+
160167 /// Resolves a type usage node to a type alias declaration
161168 ///
162169 /// - Parameter type: The SwiftSyntax node representing a type appearance in source code.
163170 /// - Returns: The type alias declaration if found, otherwise nil.
164171 func resolveTypeAlias ( _ type : TypeSyntax ) -> TypeAliasDeclSyntax ? {
165- if let id = type . as ( IdentifierTypeSyntax . self ) {
166- let qualifiedName = tryQualify ( type : id )
172+ if let qualifiedName = tryQualify ( type : type ) {
167173 return typeAliasByQualifiedName [ qualifiedName ]
168174 }
169- if let components = type . qualifiedComponents {
170- return typeAliasByQualifiedName [ components ]
171- }
172175 return nil
173176 }
174177
0 commit comments