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 false positive with UnusedScope and @MergeComponent #245

Merged
merged 2 commits into from
Aug 23, 2024
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
1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ ksp_gradle_plugin = { module = "com.google.devtools.ksp:symbol-processing-gradle
ksp_api = { module = "com.google.devtools.ksp:symbol-processing-api", version.ref = "ksp" }
kotlin_gradle_plugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
ktlint = { module = "com.pinterest.ktlint:ktlint-cli", version = "1.3.1" }
anvil_annotations = { module = "com.squareup.anvil:annotations", version = "2.4.9" }

[plugins]
kotlin_plugin = { id = "org.jetbrains.kotlin", version.ref = "kotlin" }
Expand Down
1 change: 1 addition & 0 deletions lightsaber/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ dependencies {
testImplementation(libs.truth)
testImplementation(libs.kctfork.core)
testImplementation(libs.kctfork.ksp)
testImplementation(libs.anvil.annotations)
}

configure<com.diffplug.gradle.spotless.SpotlessExtension> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ internal fun checkUnusedScopes(
}
}

private val ignoreAnnotatedWith = listOf(
Component::class.qualifiedName!!,
Subcomponent::class.qualifiedName!!,
"com.squareup.anvil.annotations.MergeComponent",
"com.squareup.anvil.annotations.MergeSubcomponent",
)

internal class UnusedScopesKsp : LightsaberKspRule {
private val scopes: MutableSet<String> = mutableSetOf(Singleton::class.qualifiedName!!)
private val declarations: MutableList<KSClassDeclaration> = mutableListOf()
Expand All @@ -72,8 +79,7 @@ internal class UnusedScopesKsp : LightsaberKspRule {
.asSequence()
.flatMap { resolver.getSymbolsWithAnnotation(it) }
.filterIsInstance<KSClassDeclaration>()
.filterNot { it.hasAnnotation(Component::class.qualifiedName!!) }
.filterNot { it.hasAnnotation(Subcomponent::class.qualifiedName!!) },
.filterNot { declaration -> ignoreAnnotatedWith.any { declaration.hasAnnotation(it) } },
)

injects.addAll(
Expand Down Expand Up @@ -117,8 +123,7 @@ internal class UnusedScopesJavac(
scopes
.asSequence()
.flatMap { roundEnv.getElementsAnnotatedWith(elements.getTypeElement(it)) }
.filterNot { it.isAnnotatedWith(Component::class) }
.filterNot { it.isAnnotatedWith(Subcomponent::class) },
.filterNot { element -> ignoreAnnotatedWith.any { element.isAnnotatedWith(it) } },
)

injects.addAll(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ internal fun <T : Annotation> Element.isAnnotatedWith(klass: KClass<T>): Boolean
return getAnnotation(klass.java) != null
}

internal fun Element.isAnnotatedWith(annotationName: String): Boolean {
return annotationMirrors.any { it.annotationType.toString() == annotationName }
}

internal fun TypeElement.findAnnotationMirrors(annotationName: String): AnnotationMirror? {
return annotationMirrors.singleOrNull { it.annotationType.toString() == annotationName }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ internal class UnusedScopesKtTest {
}

@ParameterizedTest
@CsvSource("kapt,Component", "kapt,Subcomponent", "ksp,Component", "ksp,Subcomponent")
@CsvSource("kapt,Component", "kapt,Subcomponent", "kapt,MergeComponent(Singleton::class)", "kapt,MergeSubcomponent(Singleton::class)", "ksp,Component", "ksp,Subcomponent")
fun noReportAComponentNorSubcomponent(
@ConvertWith(CompilerArgumentConverter::class) compiler: KotlinCompiler,
type: String,
Expand All @@ -207,6 +207,8 @@ internal class UnusedScopesKtTest {
"""
package test

import com.squareup.anvil.annotations.MergeComponent
import com.squareup.anvil.annotations.MergeSubcomponent
import dagger.Component
import dagger.Subcomponent
import javax.inject.Inject
Expand Down