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

Properly unwrap KSTypeAlias with an unused type parameter #1241

Merged
merged 1 commit into from Apr 21, 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
43 changes: 20 additions & 23 deletions interop/ksp/src/main/kotlin/com/squareup/kotlinpoet/ksp/ksTypes.kt
Expand Up @@ -77,12 +77,11 @@ internal fun KSType.toTypeName(
}

val resolvedType = decl.type.resolve()
val mappedArgs = mapTypeAliasArgsToAbbreviatedTypeArgs(
val mappedArgs = mapTypeArgumentsFromTypeAliasToAbbreviatedType(
typeParamResolver = typeParamResolver,
typeAliasTypeParams = decl.typeParameters,
typeAliasTypeArgs = arguments,
abbreviatedTypeParams = resolvedType.declaration.typeParameters,
abbreviatedTypeArgs = resolvedType.arguments,
typeAlias = decl,
typeAliasTypeArguments = arguments,
abbreviatedType = resolvedType,
)

val abbreviatedType = resolvedType
Expand All @@ -104,27 +103,25 @@ internal fun KSType.toTypeName(
}

@KotlinPoetKspPreview
private fun mapTypeAliasArgsToAbbreviatedTypeArgs(
private fun mapTypeArgumentsFromTypeAliasToAbbreviatedType(
typeParamResolver: TypeParameterResolver,
typeAliasTypeParams: List<KSTypeParameter>,
typeAliasTypeArgs: List<KSTypeArgument>,
abbreviatedTypeParams: List<KSTypeParameter>,
abbreviatedTypeArgs: List<KSTypeArgument>,
typeAlias: KSTypeAlias,
typeAliasTypeArguments: List<KSTypeArgument>,
abbreviatedType: KSType,
): List<TypeName> {
val orderedAbbreviatedTypeArgs = if (typeAliasTypeParams.size < 2) {
// egor: If there's only one type parameter, KSP might use different names for it in typealias vs abbreviated type
// (not sure why), so we'll return early - order doesn't matter when there are less than 2 parameters.
abbreviatedTypeArgs
} else {
abbreviatedTypeParams
.map { abbreviatedTypeParam ->
typeAliasTypeParams.indexOfFirst { typeAliasTypeParam ->
abbreviatedTypeParam.name.asString() == typeAliasTypeParam.name.asString()
}
return abbreviatedType.arguments
.map { typeArgument ->
// Check if type argument is a reference to a typealias type parameter, and not an actual type.
val typeAliasTypeParameterIndex = typeAlias.typeParameters.indexOfFirst { typeAliasTypeParameter ->
typeAliasTypeParameter.name.asString() == typeArgument.type.toString()
}
.map(typeAliasTypeArgs::get)
}
return orderedAbbreviatedTypeArgs.map { it.toTypeName(typeParamResolver) }
if (typeAliasTypeParameterIndex >= 0) {
typeAliasTypeArguments[typeAliasTypeParameterIndex]
} else {
typeArgument
}
}
.map { it.toTypeName(typeParamResolver) }
}

/**
Expand Down
Expand Up @@ -269,13 +269,15 @@ class TestProcessorTest {
typealias TypeAliasName = String
typealias GenericTypeAlias = List<String>
typealias GenericMapTypeAlias<V, K> = Map<K, V>
typealias T1Unused<T1, T2> = Map<T2, String>

@ExampleAnnotation
class Example {
fun aliases(
aliasedName: TypeAliasName,
genericAlias: GenericTypeAlias,
genericMapAlias: GenericMapTypeAlias<String, Int>,
t1Unused: T1Unused<String, Int>,
) {
}
}
Expand All @@ -302,6 +304,7 @@ class TestProcessorTest {
aliasedName: String,
genericAlias: List<String>,
genericMapAlias: Map<Int, String>,
t1Unused: Map<Int, String>,
): Unit {
}
}
Expand Down