Skip to content

Commit

Permalink
Update dependencies (#1240)
Browse files Browse the repository at this point in the history
* Update dependencies

* Fix type argument mapping when processing typealiases with KSP
  • Loading branch information
Egorand committed Apr 21, 2022
1 parent 7c2d9f4 commit 6a83bdc
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 10 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Expand Up @@ -40,7 +40,7 @@ allprojects {
subprojects {
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs += listOf("-Xopt-in=kotlin.RequiresOptIn")
freeCompilerArgs += listOf("-opt-in=kotlin.RequiresOptIn")
}
}
// Ensure "org.gradle.jvm.version" is set to "8" in Gradle metadata.
Expand Down
8 changes: 4 additions & 4 deletions gradle/libs.versions.toml
Expand Up @@ -13,14 +13,14 @@
# limitations under the License.

[versions]
kotlin = "1.6.10"
kct = "1.4.6"
ksp = "1.6.0-1.0.1"
kotlin = "1.6.21"
kct = "1.4.8"
ksp = "1.6.20-1.0.5"
ktlint = "0.42.1"

[plugins]
kotlin-jvm = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
dokka = { id = "org.jetbrains.dokka", version = "1.6.10" }
dokka = { id = "org.jetbrains.dokka", version = "1.6.20" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
spotless = { id = "com.diffplug.spotless", version = "6.2.1" }
mavenPublish = { id = "com.vanniktech.maven.publish", version = "0.18.0" }
Expand Down
5 changes: 4 additions & 1 deletion interop/kotlinx-metadata/build.gradle.kts
Expand Up @@ -22,7 +22,10 @@ tasks.jar {

tasks.compileTestKotlin {
kotlinOptions {
freeCompilerArgs = listOf("-Xjvm-default=all")
freeCompilerArgs = listOf(
"-Xjvm-default=all",
"-opt-in=com.squareup.kotlinpoet.metadata.KotlinPoetMetadataPreview",
)
}
}

Expand Down
36 changes: 34 additions & 2 deletions interop/ksp/src/main/kotlin/com/squareup/kotlinpoet/ksp/ksTypes.kt
Expand Up @@ -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,
)

val abbreviatedType = resolvedType
.toTypeName(extraResolver)
.copy(nullable = isMarkedNullable)
.rawType()
Expand All @@ -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)
}
return orderedAbbreviatedTypeArgs.map { it.toTypeName(typeParamResolver) }
}

/**
* Returns a [TypeVariableName] representation of this [KSTypeParameter].
*
Expand Down
2 changes: 1 addition & 1 deletion interop/ksp/test-processor/build.gradle.kts
Expand Up @@ -21,7 +21,7 @@ plugins {
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
kotlinOptions {
@Suppress("SuspiciousCollectionReassignment")
freeCompilerArgs += listOf("-Xopt-in=com.squareup.kotlinpoet.ksp.KotlinPoetKspPreview")
freeCompilerArgs += listOf("-opt-in=com.squareup.kotlinpoet.ksp.KotlinPoetKspPreview")
}
}

Expand Down
2 changes: 1 addition & 1 deletion kotlinpoet/build.gradle.kts
Expand Up @@ -22,7 +22,7 @@ tasks.jar {

tasks.compileTestKotlin {
kotlinOptions {
freeCompilerArgs = listOf("-Xopt-in=com.squareup.kotlinpoet.DelicateKotlinPoetApi")
freeCompilerArgs = listOf("-opt-in=com.squareup.kotlinpoet.DelicateKotlinPoetApi")
}
}

Expand Down

0 comments on commit 6a83bdc

Please sign in to comment.