Skip to content

Commit d0a5bbc

Browse files
authored
Merge pull request #40 from evant/bugfix/nested-with
Fix code generation with nested with
2 parents a8f292c + a7cc2eb commit d0a5bbc

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

integration-tests/src/test/kotlin/me/tatarka/inject/test/Models.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import me.tatarka.inject.annotations.Scope
55

66
interface IFoo
77

8+
interface IBar
9+
810
var fooConstructorCount = 0
911

1012
@Inject class Foo : IFoo {
@@ -19,6 +21,8 @@ var fooConstructorCount = 0
1921

2022
@Inject data class Bar(val foo: Foo) : IFoo
2123

24+
@Inject data class BarImpl(val foo: IFoo): IBar
25+
2226
@Inject class Baz: IFoo
2327

2428
class NamedFoo(val name: String)

integration-tests/src/test/kotlin/me/tatarka/inject/test/NestedComponentTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,19 @@ import kotlin.test.Test
1919
@Component abstract class SimpleChildComponent1(@Component val parent: ParentComponent) {
2020
abstract val namedFoo: NamedFoo
2121

22+
@Provides
23+
val BarImpl.binds: IBar
24+
get() = this
25+
2226
abstract val foo: IFoo
2327
}
2428

2529
@Component abstract class SimpleChildComponent2(@Component val parent: SimpleChildComponent1) {
2630
abstract val namedFoo: NamedFoo
2731

2832
abstract val foo: IFoo
33+
34+
abstract val bar: IBar
2935
}
3036

3137
class NestedComponentTest {
@@ -46,5 +52,6 @@ class NestedComponentTest {
4652

4753
assertThat(component.namedFoo.name).isEqualTo("parent")
4854
assertThat(component.foo).isNotNull()
55+
assertThat(component.bar).isNotNull()
4956
}
5057
}

kotlin-inject-compiler-core/src/main/kotlin/me/tatarka/inject/compiler/InjectGenerator.kt

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,16 +251,29 @@ class InjectGenerator(provider: AstProvider, private val options: Options) :
251251

252252
val receiverParamType = method.receiverParameterType
253253
val changeScope = providesResult.name != null && receiverParamType != null
254+
val changeScopeName = providesResult.name?.let {
255+
if (context.parentScopeName != null) {
256+
it.removePrefix("${context.parentScopeName}.")
257+
} else {
258+
it
259+
}
260+
}
254261

255262
if (providesResult.name != null) {
256263
if (changeScope) {
257-
codeBlock.add("with(%L)", providesResult.name)
264+
codeBlock.add("with(%L)", changeScopeName)
258265
codeBlock.beginControlFlow(" {")
259266
} else {
260-
codeBlock.add("%L.", providesResult.name)
267+
codeBlock.add("%L.", changeScopeName)
261268
}
262269
}
263270

271+
val context = if (changeScope) {
272+
context.withParentScopeName(providesResult.name)
273+
} else {
274+
context
275+
}
276+
264277
when (method) {
265278
is AstProperty -> {
266279
if (receiverParamType != null) {
@@ -481,12 +494,18 @@ class InjectGenerator(provider: AstProvider, private val options: Options) :
481494
val cycleDetector: CycleDetector,
482495
val scopeInterface: AstClass? = null,
483496
val args: List<Pair<AstType, String>> = emptyList(),
484-
val skipScoped: AstType? = null
497+
val skipScoped: AstType? = null,
498+
val parentScopeName: String? = null
485499
) {
486500
fun withoutScoped(scoped: AstType) = copy(skipScoped = scoped)
487501

488502
fun withSource(source: AstElement) = copy(source = source)
489503

504+
fun withParentScopeName(name: String?): Context {
505+
if (name == null) return this
506+
return copy(parentScopeName = if (parentScopeName == null) name else "$parentScopeName.$name")
507+
}
508+
490509
fun withArgs(args: List<Pair<AstType, String>>) = copy(args = args)
491510

492511
fun <T> use(source: AstElement, f: (context: Context) -> T): T {

0 commit comments

Comments
 (0)