Skip to content

Commit

Permalink
Always qualify ambiguous types
Browse files Browse the repository at this point in the history
  • Loading branch information
dnkoutso committed Jan 7, 2024
1 parent 5752524 commit 1542b64
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
16 changes: 10 additions & 6 deletions src/main/java/io/outfoxx/swiftpoet/CodeWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ private val NO_MODULE = String()
internal class CodeWriter(
out: Appendable,
private val indent: String = DEFAULT_INDENT,
internal val importedTypes: Map<String, DeclaredTypeName> = emptyMap(),
internal val importedTypes: Map<String, List<DeclaredTypeName>> = emptyMap(),
private val importedModules: Set<String> = emptySet()
) : Closeable {

Expand All @@ -40,7 +40,7 @@ internal class CodeWriter(
private var comment = false
private var moduleStack = mutableListOf(NO_MODULE)
private val typeSpecStack = mutableListOf<AnyTypeSpec>()
private val importableTypes = mutableMapOf<String, DeclaredTypeName>()
private val importableTypes = mutableMapOf<String, List<DeclaredTypeName>>()
private val referencedTypes = mutableMapOf<String, DeclaredTypeName>()
private var trailingNewline = false

Expand Down Expand Up @@ -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
}

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -443,7 +447,7 @@ internal class CodeWriter(
/**
* Returns the non-colliding importable types and module names for all referenced types.
*/
private fun generateImports(): Pair<Map<String, DeclaredTypeName>, Set<String>> {
private fun generateImports(): Pair<Map<String, List<DeclaredTypeName>>, Set<String>> {
return importableTypes to referencedTypes.values.map { it.moduleName }.toSet()
}

Expand All @@ -456,7 +460,7 @@ internal class CodeWriter(
fun collectImports(
indent: String,
emitStep: (importsCollector: CodeWriter) -> Unit,
): Pair<Map<String, DeclaredTypeName>, Set<String>> =
): Pair<Map<String, List<DeclaredTypeName>>, Set<String>> =
CodeWriter(NullAppendable, indent)
.use { importsCollector ->

Expand Down
16 changes: 12 additions & 4 deletions src/test/java/io/outfoxx/swiftpoet/test/FileSpecTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class FileSpecTests {
class Test {
let a: Array
let a: Swift.Array
let b: Foundation.Array
}
Expand All @@ -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()
)
Expand All @@ -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
}
Expand Down Expand Up @@ -357,7 +365,7 @@ class FileSpecTests {
struct SomeType {
var foundation_order: SortOrder
var foundation_order: Foundation.SortOrder
var order: some_other_module.SortOrder
}
Expand Down

0 comments on commit 1542b64

Please sign in to comment.