Skip to content

Commit

Permalink
Merge pull request #1241 from square/egor/220421/unwrap-type-alias-un…
Browse files Browse the repository at this point in the history
…used-params

Properly unwrap KSTypeAlias with an unused type parameter
  • Loading branch information
Egorand committed Apr 21, 2022
2 parents 6a83bdc + 93cf7df commit 07bd6f5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
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

0 comments on commit 07bd6f5

Please sign in to comment.