Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always qualify ambiguous types #110

Merged
merged 1 commit into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better to do the collision check inside and still only export the actually imported types? Rather than a list with possible collisions?

Not actually sure if that idea works.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm I will give it a shot! I see what you are suggesting.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kdubb I attempted it and tried to use day with referencedTypeNames but that broke a bunch of the tests.

I think this change gives me the most confidence given all tests pass and the ones that changed made sense ot me.

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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes sense because above we do:

      .addProperty("a", typeName("Swift.Array"))
      .addProperty("b", typeName("Foundation.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