Skip to content

Commit

Permalink
Merge pull request #1242 from square/egor/220421/unwrap-type-alias-re…
Browse files Browse the repository at this point in the history
…cursion

Unwrap nested KSTypeAliases recursively
  • Loading branch information
Egorand committed Apr 22, 2022
2 parents 07bd6f5 + 5d34a1f commit 161fc9a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
39 changes: 23 additions & 16 deletions interop/ksp/src/main/kotlin/com/squareup/kotlinpoet/ksp/ksTypes.kt
Expand Up @@ -70,25 +70,34 @@ internal fun KSType.toTypeName(
}
is KSTypeParameter -> typeParamResolver[decl.name.getShortName()]
is KSTypeAlias -> {
val extraResolver = if (decl.typeParameters.isEmpty()) {
typeParamResolver
} else {
decl.typeParameters.toTypeParameterResolver(typeParamResolver)
}
var typeAlias: KSTypeAlias = decl
var arguments = arguments

val resolvedType = decl.type.resolve()
val mappedArgs = mapTypeArgumentsFromTypeAliasToAbbreviatedType(
typeParamResolver = typeParamResolver,
typeAlias = decl,
typeAliasTypeArguments = arguments,
abbreviatedType = resolvedType,
)
var resolvedType: KSType
var mappedArgs: List<KSTypeArgument>
var extraResolver: TypeParameterResolver
while (true) {
resolvedType = typeAlias.type.resolve()
mappedArgs = mapTypeArgumentsFromTypeAliasToAbbreviatedType(
typeAlias = typeAlias,
typeAliasTypeArguments = arguments,
abbreviatedType = resolvedType,
)
extraResolver = if (typeAlias.typeParameters.isEmpty()) {
typeParamResolver
} else {
typeAlias.typeParameters.toTypeParameterResolver(typeParamResolver)
}

typeAlias = resolvedType.declaration as? KSTypeAlias ?: break
arguments = mappedArgs
}

val abbreviatedType = resolvedType
.toTypeName(extraResolver)
.copy(nullable = isMarkedNullable)
.rawType()
.withTypeArguments(mappedArgs)
.withTypeArguments(mappedArgs.map { it.toTypeName(typeParamResolver) })

val aliasArgs = typeArguments.map { it.toTypeName(typeParamResolver) }

Expand All @@ -104,11 +113,10 @@ internal fun KSType.toTypeName(

@KotlinPoetKspPreview
private fun mapTypeArgumentsFromTypeAliasToAbbreviatedType(
typeParamResolver: TypeParameterResolver,
typeAlias: KSTypeAlias,
typeAliasTypeArguments: List<KSTypeArgument>,
abbreviatedType: KSType,
): List<TypeName> {
): List<KSTypeArgument> {
return abbreviatedType.arguments
.map { typeArgument ->
// Check if type argument is a reference to a typealias type parameter, and not an actual type.
Expand All @@ -121,7 +129,6 @@ private fun mapTypeArgumentsFromTypeAliasToAbbreviatedType(
typeArgument
}
}
.map { it.toTypeName(typeParamResolver) }
}

/**
Expand Down
Expand Up @@ -270,6 +270,8 @@ class TestProcessorTest {
typealias GenericTypeAlias = List<String>
typealias GenericMapTypeAlias<V, K> = Map<K, V>
typealias T1Unused<T1, T2> = Map<T2, String>
typealias A1<T1, T2> = A2<T2, T1>
typealias A2<T2, T3> = Map<T3, T2>
@ExampleAnnotation
class Example {
Expand All @@ -278,6 +280,7 @@ class TestProcessorTest {
genericAlias: GenericTypeAlias,
genericMapAlias: GenericMapTypeAlias<String, Int>,
t1Unused: T1Unused<String, Int>,
a1: A1<String, Int>,
) {
}
}
Expand Down Expand Up @@ -305,6 +308,7 @@ class TestProcessorTest {
genericAlias: List<String>,
genericMapAlias: Map<Int, String>,
t1Unused: Map<Int, String>,
a1: Map<String, Int>,
): Unit {
}
}
Expand Down

0 comments on commit 161fc9a

Please sign in to comment.