Skip to content

Commit

Permalink
Fix complex alias handling in KsTypes (#1929)
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacSweers authored Jun 11, 2024
1 parent fe3abfa commit df3e562
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Change Log

* **Fix**: Don't expand typealiases of function types to `LambdaTypeName`s in `KSTypeReference.toTypeName()`.
* **Fix**: Small double and float values were set to 0.0 in %L translation (#1919)
* **Fix**: Fix typealias type argument resolution in KSP2.
* **Enhancement**: Make enum entry references in `KSAnnotation.toAnnotationSpec()` and `KSClassDeclaration.toClassName()` more robust.
* Migrate `kotlinpoet-metadata` to stable `org.jetbrains.kotlin:kotlin-metadata-jvm` artifact for Metadata parsing.
* Promote `kotlinpoet-metadata` out of preview to stable.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,6 @@ public fun KSTypeReference.toTypeName(
returnType = elem.returnType.toTypeName(typeParamResolver),
).copy(nullable = type.isMarkedNullable, suspending = type.isSuspendFunctionType)
} else {
type.toTypeName(typeParamResolver, element?.typeArguments.orEmpty())
type.toTypeName(typeParamResolver, type.arguments)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,49 @@ class TestProcessorTest(private val useKsp2: Boolean) {
)
}

@Test
fun complexAliasing() {
val compilation = prepareCompilation(
kotlin(
"Example.kt",
"""
package test
import javax.inject.Provider
import com.squareup.kotlinpoet.ksp.test.processor.ExampleAnnotation
typealias DaggerProvider<T> = @JvmSuppressWildcards Provider<T>
interface SelectOptions
interface SelectHandler<T>
@ExampleAnnotation
class Example(
private val handlers: Map<Class<out SelectOptions>, DaggerProvider<SelectHandler<*>>>,
)
""",
),
)

val result = compilation.compile()
assertThat(result.exitCode).isEqualTo(KotlinCompilation.ExitCode.OK)
val generatedFileText = File(compilation.kspSourcesDir, "kotlin/test/TestExample.kt")
.readText()

assertThat(generatedFileText).isEqualTo(
"""
package test
import java.lang.Class
import kotlin.collections.Map
public class TestExample {
private val handlers: Map<Class<out SelectOptions>, DaggerProvider<SelectHandler<*>>> = TODO()
}
""".trimIndent(),
)
}

private fun prepareCompilation(vararg sourceFiles: SourceFile): KotlinCompilation {
return KotlinCompilation()
.apply {
Expand Down

0 comments on commit df3e562

Please sign in to comment.