From 5ec895ec6d2b88078e8be74b41a03d92135bf44e Mon Sep 17 00:00:00 2001 From: Dimitris Koutsogiorgas Date: Sun, 7 Jan 2024 19:19:14 +0200 Subject: [PATCH] Always qualify ambiguous types --- src/main/java/io/outfoxx/swiftpoet/CodeWriter.kt | 16 ++++++++++------ .../io/outfoxx/swiftpoet/test/FileSpecTests.kt | 16 ++++++++++++---- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/outfoxx/swiftpoet/CodeWriter.kt b/src/main/java/io/outfoxx/swiftpoet/CodeWriter.kt index befd0290..7ac14662 100644 --- a/src/main/java/io/outfoxx/swiftpoet/CodeWriter.kt +++ b/src/main/java/io/outfoxx/swiftpoet/CodeWriter.kt @@ -29,7 +29,7 @@ private val NO_MODULE = String() internal class CodeWriter( out: Appendable, private val indent: String = DEFAULT_INDENT, - internal val importedTypes: Map = emptyMap(), + internal val importedTypes: Map> = emptyMap(), private val importedModules: Set = emptySet() ) : Closeable { @@ -40,7 +40,7 @@ internal class CodeWriter( private var comment = false private var moduleStack = mutableListOf(NO_MODULE) private val typeSpecStack = mutableListOf() - private val importableTypes = mutableMapOf() + private val importableTypes = mutableMapOf>() private val referencedTypes = mutableMapOf() private var trailingNewline = false @@ -336,7 +336,11 @@ internal class CodeWriter( } val topLevelTypeName = typeName.topLevelTypeName() val simpleName = topLevelTypeName.simpleName - importableTypes.putIfAbsent(simpleName, topLevelTypeName) + val typeNames = importableTypes.getOrDefault(simpleName, emptyList()).toMutableList() + if (!typeNames.contains(topLevelTypeName)) { + typeNames.add(topLevelTypeName) + } + importableTypes[simpleName] = typeNames } /** @@ -372,7 +376,7 @@ internal class CodeWriter( */ private fun resolveImport(typeName: DeclaredTypeName): String { val topLevelTypeName = typeName.topLevelTypeName() - return if (importedTypes.values.any { it == topLevelTypeName }) { + return if (importedTypes[typeName.topLevelTypeName().simpleName]?.count() == 1 && importedTypes.values.flatMap { it }.any { it == topLevelTypeName }) { typeName.simpleNames.joinToString(".") } else { typeName.canonicalName @@ -443,7 +447,7 @@ internal class CodeWriter( /** * Returns the non-colliding importable types and module names for all referenced types. */ - private fun generateImports(): Pair, Set> { + private fun generateImports(): Pair>, Set> { return importableTypes to referencedTypes.values.map { it.moduleName }.toSet() } @@ -456,7 +460,7 @@ internal class CodeWriter( fun collectImports( indent: String, emitStep: (importsCollector: CodeWriter) -> Unit, - ): Pair, Set> = + ): Pair>, Set> = CodeWriter(NullAppendable, indent) .use { importsCollector -> diff --git a/src/test/java/io/outfoxx/swiftpoet/test/FileSpecTests.kt b/src/test/java/io/outfoxx/swiftpoet/test/FileSpecTests.kt index deb84355..4f4715a8 100644 --- a/src/test/java/io/outfoxx/swiftpoet/test/FileSpecTests.kt +++ b/src/test/java/io/outfoxx/swiftpoet/test/FileSpecTests.kt @@ -268,7 +268,7 @@ class FileSpecTests { class Test { - let a: Array + let a: Swift.Array let b: Foundation.Array } @@ -290,7 +290,13 @@ class FileSpecTests { ) .addProperty( PropertySpec.varBuilder( - "order", + "some_module_order", + typeName("some_module.SortOrder") + ).build() + ) + .addProperty( + PropertySpec.varBuilder( + "some_other_module_order", typeName("some_other_module.SortOrder") ).build() ) @@ -308,12 +314,14 @@ class FileSpecTests { equalTo( """ import Foundation + import some_module import some_other_module struct SomeType { var foundation_order: Foundation.SortOrder - var order: SortOrder + var some_module_order: some_module.SortOrder + var some_other_module_order: some_other_module.SortOrder } @@ -357,7 +365,7 @@ class FileSpecTests { struct SomeType { - var foundation_order: SortOrder + var foundation_order: Foundation.SortOrder var order: some_other_module.SortOrder }