Repository: evant/kotlin-inject
Pull request #41 (2020-08-15 23:48:09)

Fixed bug with scoped type alias variable naming


Patch:
diff --git a/kotlin-inject-compiler-core/src/main/kotlin/me/tatarka/inject/compiler/InjectGenerator.kt b/kotlin-inject-compiler-core/src/main/kotlin/me/tatarka/inject/compiler/InjectGenerator.kt
index 9eebb98..1968ac2 100644
--- a/kotlin-inject-compiler-core/src/main/kotlin/me/tatarka/inject/compiler/InjectGenerator.kt
+++ b/kotlin-inject-compiler-core/src/main/kotlin/me/tatarka/inject/compiler/InjectGenerator.kt
@@ -80,7 +80,7 @@ class InjectGenerator(provider: AstProvider, private val options: Options) :
 
                         addProperty(
                             PropertySpec.builder(
-                                type.asElement().simpleName.asScopedProp(),
+                                type.simpleName.asScopedProp(),
                                 type.asTypeName()
                             ).apply {
                                 if (context.scopeInterface != null) {
@@ -143,7 +143,7 @@ class InjectGenerator(provider: AstProvider, private val options: Options) :
                 for (type in context.collector.scoped) {
                     addProperty(
                         PropertySpec.builder(
-                            type.asElement().simpleName.asScopedProp(),
+                            type.simpleName.asScopedProp(),
                             type.asTypeName()
                         ).build()
                     )
@@ -335,7 +335,7 @@ class InjectGenerator(provider: AstProvider, private val options: Options) :
         if (result.name != null) {
             codeBlock.add("(%L as Inject%N).", result.name, result.astClass.name)
         }
-        codeBlock.add("%N", key.type.asElement().simpleName.asScopedProp())
+        codeBlock.add("%N", key.type.simpleName.asScopedProp())
         return codeBlock.build()
     }
 


Test patch:
diff --git a/integration-tests/src/test/kotlin/me/tatarka/inject/test/TypeAliasTest.kt b/integration-tests/src/test/kotlin/me/tatarka/inject/test/TypeAliasTest.kt
index a55f540..a0b7727 100644
--- a/integration-tests/src/test/kotlin/me/tatarka/inject/test/TypeAliasTest.kt
+++ b/integration-tests/src/test/kotlin/me/tatarka/inject/test/TypeAliasTest.kt
@@ -29,6 +29,13 @@ typealias NamedFoo2 = NamedFoo
     @Provides fun foo2(): NamedFoo2 = NamedFoo("2")
 }
 
+@CustomScope @Component abstract class ScopedConstructorAliasedComponent {
+    abstract val aliasedFoo: AliasedFoo
+
+    @CustomScope @Provides fun foo1(): NamedFoo1 = NamedFoo("1")
+    @CustomScope @Provides fun foo2(): NamedFoo2 = NamedFoo("2")
+}
+
 @Target(AnnotationTarget.TYPE)
 annotation class FooAnnotation
 
@@ -63,4 +70,14 @@ class QualifierTest {
             assertThat(component.aliasedFoo.foo2.name).isEqualTo("2")
         }
     }
+
+    @Test
+    fun generates_a_component_that_constructs_different_scoped_values_based_on_the_type_alias_name() {
+        val component = ScopedConstructorAliasedComponent::class.create()
+
+        assertAll {
+            assertThat(component.aliasedFoo.foo1.name).isEqualTo("1")
+            assertThat(component.aliasedFoo.foo2.name).isEqualTo("2")
+        }
+    }
 }
\ No newline at end of file