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 Aliases as TypeArgument #1321

Merged
merged 1 commit into from Jul 26, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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 {
Egorand marked this conversation as resolved.
Show resolved Hide resolved
}
}

""".trimIndent(),
)
}

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