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

Fix incorrect importing of local types #105

Merged
merged 1 commit into from
Dec 20, 2023
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
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