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

Fix binds validation #200

Merged
merged 1 commit into from
Mar 21, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,17 @@ internal val KSClassDeclaration.primarySuperType: KSType?
.map { it.resolve() }
.filterNot { it.isAny }
.firstOrNull()

internal fun KSClassDeclaration.hasSuperType(ksType: KSType): Boolean {
superTypes
.map { it.resolve() }
.forEach {
if (it == ksType) return true

val declaration = it.declaration
if (declaration is KSClassDeclaration && declaration.hasSuperType(ksType)) {
return true
}
}
return false
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.google.devtools.ksp.symbol.KSType
import com.moriatsushi.koject.Binds
import com.moriatsushi.koject.processor.analytics.findAnnotation
import com.moriatsushi.koject.processor.analytics.findArgumentByName
import com.moriatsushi.koject.processor.analytics.hasSuperType
import com.moriatsushi.koject.processor.analytics.isNothing
import com.moriatsushi.koject.processor.analytics.name
import com.moriatsushi.koject.processor.analytics.primarySuperType
Expand All @@ -23,13 +24,13 @@ internal fun KSClassDeclaration.findBindAnnotation(): BindsAnnotation? {
val annotation = findAnnotation<Binds>() ?: return null
val argument = annotation.findArgumentByName<KSType>("to")
val toType = if (argument == null || argument.isNothing) {
primarySuperType
?: throwNotFoundSuperTypeException(this)
primarySuperType ?: throwNotFoundSuperTypeException(this)
} else {
superTypes
.map { it.resolve() }
.find { it == argument }
?: throwNotIncludedSuperTypeException(this, argument)
if (hasSuperType(argument)) {
argument
} else {
throwNotIncludedSuperTypeException(this, argument)
}
}
return BindsAnnotation(
toTypeName = toType.toTypeName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ class AppProcessorBindsTest {
assertCompileSucceed(result)
}

@Test
fun success_nestedSupertype() {
val complication = compilationFactory.create(folder)
complication.sources = listOf(nestedSuperType)
val result = complication.compile()

assertCompileSucceed(result)
}

@Test
fun failed_notFoundSupertype() {
val complication = compilationFactory.create(folder)
Expand Down Expand Up @@ -82,6 +91,24 @@ class AppProcessorBindsTest {
""",
)

private val nestedSuperType = SourceFile.kotlin(
"Test.kt",
"""
package com.testpackage

import com.moriatsushi.koject.Binds
import com.moriatsushi.koject.Provides

@Binds(to = Type2::class)
@Provides
class SampleImpl: Type1

interface Type1: Type2

interface Type2
""",
)

private val notFoundSupertypeCode = SourceFile.kotlin(
"Test.kt",
"""
Expand Down