Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix type argument mapping when processing typealiases with KSP
- Loading branch information
Showing
2 changed files
with
36 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,9 +75,17 @@ internal fun KSType.toTypeName( | |
} else { | ||
decl.typeParameters.toTypeParameterResolver(typeParamResolver) | ||
} | ||
val mappedArgs = arguments.map { it.toTypeName(typeParamResolver) } | ||
|
||
val abbreviatedType = decl.type.resolve() | ||
val resolvedType = decl.type.resolve() | ||
val mappedArgs = mapTypeAliasArgsToAbbreviatedTypeArgs( | ||
typeParamResolver = typeParamResolver, | ||
typeAliasTypeParams = decl.typeParameters, | ||
typeAliasTypeArgs = arguments, | ||
abbreviatedTypeParams = resolvedType.declaration.typeParameters, | ||
abbreviatedTypeArgs = resolvedType.arguments, | ||
) | ||
This comment has been minimized.
Sorry, something went wrong. |
||
|
||
val abbreviatedType = resolvedType | ||
.toTypeName(extraResolver) | ||
.copy(nullable = isMarkedNullable) | ||
.rawType() | ||
|
@@ -95,6 +103,30 @@ internal fun KSType.toTypeName( | |
return type.copy(nullable = isMarkedNullable) | ||
} | ||
|
||
@KotlinPoetKspPreview | ||
private fun mapTypeAliasArgsToAbbreviatedTypeArgs( | ||
typeParamResolver: TypeParameterResolver, | ||
typeAliasTypeParams: List<KSTypeParameter>, | ||
typeAliasTypeArgs: List<KSTypeArgument>, | ||
abbreviatedTypeParams: List<KSTypeParameter>, | ||
abbreviatedTypeArgs: List<KSTypeArgument>, | ||
): 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() | ||
} | ||
} | ||
.map(typeAliasTypeArgs::get) | ||
This comment has been minimized.
Sorry, something went wrong.
ting-yuan
|
||
} | ||
return orderedAbbreviatedTypeArgs.map { it.toTypeName(typeParamResolver) } | ||
} | ||
|
||
/** | ||
* Returns a [TypeVariableName] representation of this [KSTypeParameter]. | ||
* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This might need to be done recursively.