Skip to content

Commit

Permalink
Fix incorrect importing of local types
Browse files Browse the repository at this point in the history
  • Loading branch information
kdubb committed Dec 20, 2023
1 parent 4ccf88c commit 7f2df80
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/main/java/io/outfoxx/swiftpoet/CodeWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ internal class CodeWriter(
}

private fun referenceTypeName(typeName: DeclaredTypeName) {
if (typeName.moduleName.isEmpty()) {
return
}
referencedTypes[typeName.canonicalName] = typeName
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/io/outfoxx/swiftpoet/ImportSpec.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ class ImportSpec internal constructor(
internal val doc = CodeBlock.builder()
internal val guardTest = CodeBlock.builder()

init {
require(name.isNotBlank()) { "name is empty" }
}

fun addDoc(format: String, vararg args: Any) = apply {
doc.add(format, *args)
}
Expand Down
33 changes: 33 additions & 0 deletions src/test/java/io/outfoxx/swiftpoet/test/FileSpecTests.kt
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,40 @@ class FileSpecTests {
)
)
}
@Test
@DisplayName("Emits local module types without import")
fun testLocalTypesAreNotImported() {
val type =
TypeSpec.structBuilder("SomeType")
.addProperty(
PropertySpec.varBuilder(
"myStuff",
typeName(".MyStuff")
).build()
)
.build()

val testFile = FileSpec.builder("Test", "Test")
.addType(type)
.build()

val out = StringWriter()
testFile.writeTo(out)

assertThat(
out.toString(),
equalTo(
"""
struct SomeType {
var myStuff: MyStuff
}
""".trimIndent()
)
)
}
@Test
@DisplayName("Generates guarded imports")
fun testGenGuardedImports() {
Expand Down

0 comments on commit 7f2df80

Please sign in to comment.