Skip to content

Commit

Permalink
Fix Aliases as TypeArgument
Browse files Browse the repository at this point in the history
  • Loading branch information
bitPogo committed Jul 12, 2022
1 parent 1887184 commit 041f923
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.squareup.kotlinpoet.ksp

import com.google.devtools.ksp.symbol.ClassKind
import com.google.devtools.ksp.symbol.KSClassDeclaration
import com.google.devtools.ksp.symbol.KSType
import com.google.devtools.ksp.symbol.KSTypeAlias
Expand Down Expand Up @@ -63,6 +64,12 @@ internal fun KSType.toTypeName(
}
val type = when (val decl = declaration) {
is KSClassDeclaration -> {
val arguments = if (decl.classKind == ClassKind.ANNOTATION_CLASS) {
arguments
} else {
typeArguments
}

decl.toClassName().withTypeArguments(arguments.map { it.toTypeName(typeParamResolver) })
}
is KSTypeParameter -> typeParamResolver[decl.name.getShortName()]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,47 @@ class TestProcessorTest {
)
}

@Test
fun aliasAsTypeArgument() {
val compilation = prepareCompilation(
kotlin(
"Example.kt",
"""
package test
import com.squareup.kotlinpoet.ksp.test.processor.ExampleAnnotation
typealias Alias997 = Map<String, Int>
@ExampleAnnotation
interface AliasAsTypeArgument {
fun bar(arg1: List<Alias997>)
}
"""
)
)

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

assertThat(generatedFileText).isEqualTo(
"""
package test
import kotlin.Unit
import kotlin.collections.List
public class AliasAsTypeArgument {
public fun bar(arg1: List<Alias997>): Unit {
}
}
""".trimIndent()
)
}

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

0 comments on commit 041f923

Please sign in to comment.