Skip to content

Commit

Permalink
Check for error types in toClassName too (#1890)
Browse files Browse the repository at this point in the history
* Check for error types in toClassName too

* PR number chicken and egg problem
  • Loading branch information
ZacSweers committed Apr 17, 2024
1 parent cdb5426 commit 52e43f2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Change Log
* Fix: `MemberName`s without a package are now correctly imported (#1841)
* Fix: Throw if primary constructor delegates to other constructors (#1859).
* Fix: Aliased imports with nested class (#1876).
* Fix: Check for error types in `KSType.toClassName()` (#1890).

## Version 1.16.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,15 @@ import com.squareup.kotlinpoet.TypeVariableName
import com.squareup.kotlinpoet.WildcardTypeName
import com.squareup.kotlinpoet.tags.TypeAliasTag

private fun KSType.requireNotErrorType() {
require(!isError) {
"Error type '$this' is not resolvable in the current round of processing."
}
}

/** Returns the [ClassName] representation of this [KSType] IFF it's a [KSClassDeclaration]. */
public fun KSType.toClassName(): ClassName {
requireNotErrorType()
val decl = declaration
check(decl is KSClassDeclaration) {
"Declaration was not a KSClassDeclaration: $this"
Expand All @@ -61,9 +68,7 @@ internal fun KSType.toTypeName(
typeParamResolver: TypeParameterResolver,
typeArguments: List<KSTypeArgument>,
): TypeName {
require(!isError) {
"Error type '$this' is not resolvable in the current round of processing."
}
requireNotErrorType()
val type = when (val decl = declaration) {
is KSClassDeclaration -> {
decl.toClassName().withTypeArguments(arguments.map { it.toTypeName(typeParamResolver) })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.google.devtools.ksp.symbol.KSDeclaration
import com.google.devtools.ksp.symbol.KSType
import com.google.devtools.ksp.symbol.KSTypeArgument
import com.google.devtools.ksp.symbol.Nullability
import com.squareup.kotlinpoet.ksp.toClassName
import com.squareup.kotlinpoet.ksp.toTypeName
import kotlin.test.assertFailsWith
import org.junit.Test
Expand Down Expand Up @@ -77,10 +78,16 @@ class KsTypesTest {
}
}

val exception = assertFailsWith<IllegalArgumentException> {
val exception1 = assertFailsWith<IllegalArgumentException> {
type.toClassName()
}
assertThat(exception1).hasMessageThat()
.contains("is not resolvable in the current round of processing")

val exception2 = assertFailsWith<IllegalArgumentException> {
type.toTypeName()
}
assertThat(exception).hasMessageThat()
assertThat(exception2).hasMessageThat()
.contains("is not resolvable in the current round of processing")
}
}

0 comments on commit 52e43f2

Please sign in to comment.