Skip to content

Commit

Permalink
Handle typealias to typealias
Browse files Browse the repository at this point in the history
  • Loading branch information
eygraber committed Nov 22, 2024
1 parent 319a8f8 commit 7e2924a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package me.tatarka.inject.test

import assertk.assertThat
import assertk.assertions.isNotNull
import kotlin.test.Test

class TypeAliasTest {

@Test
fun can_generate_for_typealias_on_typealias_function() {
val component = TypeAliasesComponent::class.create()

assertThat(component.typeAliasFoo()).isNotNull()
}

@Test
fun can_generate_for_typealias_on_typealias_to_typealias_function() {
val component = TypeAliasesComponent::class.create()

assertThat(component.typeAliasToTypeAliasFoo()).isNotNull()
}

@Test
fun can_generate_for_typealias_on_typealias_to_typealias_to_typealias_function() {
val component = TypeAliasesComponent::class.create()

assertThat(component.typeAliasToTypeAliasToTypeAliasFoo()).isNotNull()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package me.tatarka.inject.test

import me.tatarka.inject.annotations.Component
import me.tatarka.inject.annotations.Inject

@Inject
class TypeAliasBar

typealias TypeAliasToTypeAliasBar = TypeAliasBar
typealias TypeAliasToTypeAliasToTypeAliasBar = TypeAliasToTypeAliasBar

@Inject
fun TypeAliasFoo(
bar: TypeAliasBar,
typeAliasToBar: TypeAliasToTypeAliasBar,
typeAliasToTypeAliasToBar: TypeAliasToTypeAliasToTypeAliasBar,
) {}
typealias TypeAliasFoo = () -> Unit

typealias TypeAliasToTypeAliasFoo = TypeAliasFoo
typealias TypeAliasToTypeAliasToTypeAliasFoo = TypeAliasToTypeAliasFoo

@Component
abstract class TypeAliasesComponent {
abstract fun typeAliasFoo(): TypeAliasFoo
abstract fun typeAliasToTypeAliasFoo(): TypeAliasToTypeAliasFoo
abstract fun typeAliasToTypeAliasToTypeAliasFoo(): TypeAliasToTypeAliasToTypeAliasFoo
}
Original file line number Diff line number Diff line change
Expand Up @@ -382,8 +382,10 @@ class TypeResultResolver(private val provider: AstProvider, private val options:
val resolveType = key.type.resolvedType()
val args = resolveType.arguments.dropLast(1)
if (key.type.isTypeAlias()) {
val resolvedTypeAlias = key.type.resolveToHighestTypeAlias()

// Check to see if we have a function matching the type alias
val functions = provider.findFunctions(key.type.packageName, key.type.simpleName)
val functions = provider.findFunctions(resolvedTypeAlias.packageName, resolvedTypeAlias.simpleName)
val injectedFunction = functions.find { it.isInject() }
if (injectedFunction != null) {
return NamedFunction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,14 @@ private fun <T> renderTree(node: T, visitor: TreeVisitor<T>, indent: String, isL
fun AstType.isSet(): Boolean = packageName == "kotlin.collections" && simpleName == "Set"
fun AstType.isMap(): Boolean = packageName == "kotlin.collections" && simpleName == "Map"
fun AstType.isPair(): Boolean = packageName == "kotlin" && simpleName == "Pair"
fun AstType.isFunctionOrTypeAliasOfFunction() = isFunction() || isTypeAlias() && resolvedType().isFunction()
fun AstType.isFunctionOrTypeAliasOfFunction(): Boolean = isFunction() || isTypeAlias() &&
resolvedType().isFunctionOrTypeAliasOfFunction()

tailrec fun AstType.resolveToHighestTypeAlias(): AstType {
check(isTypeAlias()) {
"resolveToHighestTypeAlias should only be called on a typealias AstType"
}

val resolvedType = resolvedType()
return if (resolvedType.isTypeAlias()) resolvedType.resolveToHighestTypeAlias() else this
}

0 comments on commit 7e2924a

Please sign in to comment.