aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEgor Andreevich <egor@squareup.com>2024-01-05 17:09:12 +0100
committerEgor Andreevich <github@egorand.dev>2024-01-07 04:36:57 -0500
commit4593621556298205a01785c7dd26109b77702675 (patch)
tree88a3e4b45838969ed45cd04d9e3c573aa14dc899
parentd44d63b9da3f13e16b0705b11fc3da10d3c206f5 (diff)
downloadkotlinpoet-4593621556298205a01785c7dd26109b77702675.tar.gz
Honour same-package import aliases
-rw-r--r--kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/CodeWriter.kt14
-rw-r--r--kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/FileSpecTest.kt31
2 files changed, 38 insertions, 7 deletions
diff --git a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/CodeWriter.kt b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/CodeWriter.kt
index ff1de015..d4489f24 100644
--- a/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/CodeWriter.kt
+++ b/kotlinpoet/src/commonMain/kotlin/com/squareup/kotlinpoet/CodeWriter.kt
@@ -445,8 +445,8 @@ internal class CodeWriter constructor(
return className.canonicalName
}
- // If the class is in the same package, we're done.
- if (packageName == className.packageName) {
+ // If the class is in the same package and there's no import alias for that class, we're done.
+ if (packageName == className.packageName && imports[className.canonicalName]?.alias == null) {
referencedNames.add(className.topLevelClassName().simpleName)
return className.simpleNames.joinToString(".")
}
@@ -722,11 +722,11 @@ internal class CodeWriter constructor(
importsCollector.close()
return CodeWriter(
- out,
- indent,
- memberImports + generatedImports.filterKeys { it !in memberImports },
- suggestedTypeImports,
- suggestedMemberImports,
+ out = out,
+ indent = indent,
+ imports = memberImports + generatedImports.filterKeys { it !in memberImports },
+ importedTypes = suggestedTypeImports,
+ importedMembers = suggestedMemberImports,
)
}
diff --git a/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/FileSpecTest.kt b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/FileSpecTest.kt
index c363f592..a855cddc 100644
--- a/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/FileSpecTest.kt
+++ b/kotlinpoet/src/commonTest/kotlin/com/squareup/kotlinpoet/FileSpecTest.kt
@@ -500,6 +500,37 @@ class FileSpecTest {
)
}
+ // https://github.com/square/kotlinpoet/issues/1696
+ @Test fun aliasedImportInSamePackage() {
+ val packageName = "com.mypackage"
+ val className = ClassName(packageName, "StringKey")
+ val source = FileSpec.builder(packageName, "K")
+ .addAliasedImport(className, "S")
+ .addType(
+ TypeSpec
+ .objectBuilder("K")
+ .addProperty(
+ PropertySpec.builder("test", className)
+ .initializer("%T(%L)", className, 0)
+ .build(),
+ )
+ .build(),
+ )
+ .build()
+ assertThat(source.toString()).isEqualTo(
+ """
+ |package com.mypackage
+ |
+ |import com.mypackage.StringKey as S
+ |
+ |public object K {
+ | public val test: S = S(0)
+ |}
+ |
+ """.trimMargin(),
+ )
+ }
+
@Test fun conflictingParentName() {
val source = FileSpec.builder("com.squareup.tacos", "A")
.addType(