Skip to content

Commit

Permalink
Copy required annotations to generated code
Browse files Browse the repository at this point in the history
Copy all annotations from `expect fun` functions annotated with `@CreateComponent` to the generated `actual fun` functions to avoid a Kotlin compiler warning.

Fixes #72
  • Loading branch information
vRallev committed Nov 15, 2024
1 parent 439e5b4 commit 73b18ba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.gradle.api.tasks.SourceTask
import org.gradle.api.tasks.compile.JavaCompile
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.KotlinMultiplatformExtension
import org.jetbrains.kotlin.gradle.tasks.KotlinCompilationTask
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
import org.jlleitschuh.gradle.ktlint.KtlintExtension
import java.io.File
Expand All @@ -33,9 +34,12 @@ internal open class KotlinPlugin : Plugin<Project> {
}

private fun Project.configureKotlinCompile() {
tasks.withType(KotlinJvmCompile::class.java).configureEach {
it.compilerOptions.jvmTarget.set(JvmTarget.fromTarget(javaVersion.toString()))
it.compilerOptions.allWarningsAsErrors.set(ci)
tasks.withType(KotlinCompilationTask::class.java).configureEach {
// TODO: ci
it.compilerOptions.allWarningsAsErrors.set(true)
if (it is KotlinJvmCompile) {
it.compilerOptions.jvmTarget.set(JvmTarget.fromTarget(javaVersion.toString()))
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.squareup.kotlinpoet.KModifier.ACTUAL
import com.squareup.kotlinpoet.ParameterSpec
import com.squareup.kotlinpoet.ParameterizedTypeName.Companion.parameterizedBy
import com.squareup.kotlinpoet.asTypeName
import com.squareup.kotlinpoet.ksp.toAnnotationSpec
import com.squareup.kotlinpoet.ksp.toClassName
import com.squareup.kotlinpoet.ksp.toTypeName
import com.squareup.kotlinpoet.ksp.writeTo
Expand Down Expand Up @@ -61,6 +62,15 @@ internal class CreateComponentProcessor(
resolver
.getSymbolsWithAnnotation(MergeComponent.CreateComponent::class)
.filterIsInstance<KSFunctionDeclaration>()
.filter {
// We copy all annotations from the expect function, including @CreateComponent,
// to the actual functions. Without doing so the Kotlin compiler would print a
// warning.
//
// Without this filter we'd try to process the generated actual function, which
// leads to errors.
Modifier.ACTUAL !in it.modifiers
}
.onEach { function ->
checkIsPublic(function) {
"Factory functions for components annotated with `@CreateComponent` must be public."
Expand All @@ -82,8 +92,6 @@ internal class CreateComponentProcessor(
.toClassName()
val generatedComponent = component.peerClass("KotlinInject${component.simpleName}")

function.requireContainingFile()

val parametersAsSpec = function.parameters.map {
ParameterSpec
.builder(
Expand All @@ -109,6 +117,7 @@ internal class CreateComponentProcessor(
)
}
}
.addAnnotations(function.annotations.map { it.toAnnotationSpec() }.toList())
.addModifiers(ACTUAL)
.addParameters(parametersAsSpec)
.returns(component)
Expand Down

0 comments on commit 73b18ba

Please sign in to comment.