@@ -2246,6 +2246,14 @@ internal struct ParserResultsComposed {
2246
2246
associatedTypes = Self.extractAssociatedTypes(parserResult)
2247
2247
parsedTypes = parserResult.types
2248
2248
2249
+ var moduleAndTypeNameCollisions: Set<String> = []
2250
+
2251
+ for type in parsedTypes where !type.isExtension && type.parent == nil {
2252
+ if let module = type.module, type.localName == module {
2253
+ moduleAndTypeNameCollisions.insert(module)
2254
+ }
2255
+ }
2256
+
2249
2257
// set definedInType for all methods and variables
2250
2258
parsedTypes
2251
2259
.forEach { type in
@@ -2255,16 +2263,23 @@ internal struct ParserResultsComposed {
2255
2263
}
2256
2264
2257
2265
// map all known types to their names
2258
- parsedTypes
2259
- .filter { !$0.isExtension }
2260
- .forEach {
2261
- typeMap[$0.globalName] = $0
2262
- if let module = $0.module {
2263
- var typesByModules = modules[module, default: [:]]
2264
- typesByModules[$0.name] = $0
2265
- modules[module] = typesByModules
2266
- }
2266
+
2267
+ for type in parsedTypes where !type.isExtension && type.parent == nil {
2268
+ let name = type.name
2269
+ // If a type name has the `<module>.` prefix, and the type `<module>.<module>` is undefined, we can safely remove the `<module>.` prefix
2270
+ if let module = type.module, name.hasPrefix(module), name.dropFirst(module.count).hasPrefix( " . " ), !moduleAndTypeNameCollisions.contains(module) {
2271
+ type.localName.removeFirst(module.count + 1)
2272
+ }
2273
+ }
2274
+
2275
+ for type in parsedTypes where !type.isExtension {
2276
+ typeMap[type.globalName] = type
2277
+ if let module = type.module {
2278
+ var typesByModules = modules[module, default: [:]]
2279
+ typesByModules[type.name] = type
2280
+ modules[module] = typesByModules
2267
2281
}
2282
+ }
2268
2283
2269
2284
/// Resolve typealiases
2270
2285
let typealiases = Array(unresolvedTypealiases.values)
@@ -2274,15 +2289,25 @@ internal struct ParserResultsComposed {
2274
2289
2275
2290
/// Map associated types
2276
2291
associatedTypes.forEach {
2277
- typeMap[$0.key] = $0.value.type
2292
+ if let globalName = $0.value.type?.globalName,
2293
+ let type = typeMap[globalName] {
2294
+ typeMap[$0.key] = type
2295
+ } else {
2296
+ typeMap[$0.key] = $0.value.type
2297
+ }
2278
2298
}
2279
2299
2280
2300
types = unifyTypes()
2281
2301
}
2282
2302
2283
- private func resolveExtensionOfNestedType(_ type: Type) {
2303
+ mutating private func resolveExtensionOfNestedType(_ type: Type) {
2284
2304
var components = type.localName.components(separatedBy: " . " )
2285
- let rootName = type.module ?? components.removeFirst() // Module/parent name
2305
+ let rootName: String
2306
+ if type.parent != nil, let module = type.module {
2307
+ rootName = module
2308
+ } else {
2309
+ rootName = components.removeFirst()
2310
+ }
2286
2311
if let moduleTypes = modules[rootName], let baseType = moduleTypes[components.joined(separator: " . " )] ?? moduleTypes[type.localName] {
2287
2312
type.localName = baseType.localName
2288
2313
type.module = baseType.module
@@ -2299,6 +2324,14 @@ internal struct ParserResultsComposed {
2299
2324
}
2300
2325
}
2301
2326
}
2327
+ // Parent extensions should always be processed before `type`, as this affects the globalName of `type`.
2328
+ for parent in type.parentTypes where parent.isExtension && parent.localName.contains( " . " ) {
2329
+ let oldName = parent.globalName
2330
+ resolveExtensionOfNestedType(parent)
2331
+ if oldName != parent.globalName {
2332
+ rewriteChildren(of: parent)
2333
+ }
2334
+ }
2302
2335
}
2303
2336
2304
2337
// if it had contained types, they might have been fully defined and so their name has to be noted in uniques
@@ -2317,19 +2350,14 @@ internal struct ParserResultsComposed {
2317
2350
.forEach { (type: Type) in
2318
2351
let oldName = type.globalName
2319
2352
2320
- let hasDotInLocalName = type.localName.contains( " . " ) as Bool
2321
- if let _ = type.parent, hasDotInLocalName {
2353
+ if type.localName.contains( " . " ) {
2322
2354
resolveExtensionOfNestedType(type)
2323
- }
2324
-
2325
- if let resolved = resolveGlobalName(for: oldName, containingType: type.parent, unique: typeMap, modules: modules, typealiases: resolvedTypealiases, associatedTypes: associatedTypes)?.name {
2355
+ } else if let resolved = resolveGlobalName(for: oldName, containingType: type.parent, unique: typeMap, modules: modules, typealiases: resolvedTypealiases, associatedTypes: associatedTypes)?.name {
2326
2356
var moduleName: String = " "
2327
2357
if let module = type.module {
2328
2358
moduleName = " \\ (module). "
2329
2359
}
2330
2360
type.localName = resolved.replacingOccurrences(of: moduleName, with: " " )
2331
- } else {
2332
- return
2333
2361
}
2334
2362
2335
2363
// nothing left to do
@@ -3509,7 +3537,8 @@ public final class TemplateContext: NSObject, SourceryModel, NSCoding, Diffable
3509
3537
" based " : types.based,
3510
3538
" inheriting " : types.inheriting,
3511
3539
" implementing " : types.implementing,
3512
- " protocolCompositions " : types.protocolCompositions
3540
+ " protocolCompositions " : types.protocolCompositions,
3541
+ " typealiases " : types.typealiases
3513
3542
] as [String : Any],
3514
3543
" functions " : functions,
3515
3544
" type " : types.typesByName,
@@ -3547,6 +3576,9 @@ public final class Typealias: NSObject, Typed, SourceryModel, Diffable {
3547
3576
3548
3577
/// module in which this typealias was declared
3549
3578
public var module: String?
3579
+
3580
+ /// Imports that existed in the file that contained this typealias declaration
3581
+ public var imports: [Import] = []
3550
3582
3551
3583
/// typealias annotations
3552
3584
public var annotations: Annotations = [:]
@@ -3661,6 +3693,12 @@ public final class Typealias: NSObject, Typed, SourceryModel, Diffable {
3661
3693
}; self.typeName = typeName
3662
3694
self.type = aDecoder.decode(forKey: " type " )
3663
3695
self.module = aDecoder.decode(forKey: " module " )
3696
+ guard let imports: [Import] = aDecoder.decode(forKey: " imports " ) else {
3697
+ withVaList([ " imports " ]) { arguments in
3698
+ NSException.raise(NSExceptionName.parseErrorException, format: " Key '%@' not found. " , arguments: arguments)
3699
+ }
3700
+ fatalError()
3701
+ }; self.imports = imports
3664
3702
guard let annotations: Annotations = aDecoder.decode(forKey: " annotations " ) else {
3665
3703
withVaList([ " annotations " ]) { arguments in
3666
3704
NSException.raise(NSExceptionName.parseErrorException, format: " Key '%@' not found. " , arguments: arguments)
@@ -3689,6 +3727,7 @@ public final class Typealias: NSObject, Typed, SourceryModel, Diffable {
3689
3727
aCoder.encode(self.typeName, forKey: " typeName " )
3690
3728
aCoder.encode(self.type, forKey: " type " )
3691
3729
aCoder.encode(self.module, forKey: " module " )
3730
+ aCoder.encode(self.imports, forKey: " imports " )
3692
3731
aCoder.encode(self.annotations, forKey: " annotations " )
3693
3732
aCoder.encode(self.documentation, forKey: " documentation " )
3694
3733
aCoder.encode(self.parent, forKey: " parent " )
@@ -8506,6 +8545,7 @@ extension TypeName: TypeNameAutoJSExport {}
8506
8545
var typeName: TypeName { get }
8507
8546
var type: Type? { get }
8508
8547
var module: String? { get }
8548
+ var imports: [Import] { get }
8509
8549
var annotations: Annotations { get }
8510
8550
var documentation: Documentation { get }
8511
8551
var parent: Type? { get }
0 commit comments