Skip to content

Commit

Permalink
Make ComponentMustBeAbstractDetector aware of Anvil Component annotat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
WhosNickDoglio committed Apr 21, 2024
1 parent dbb76bc commit 9b63b79
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ import com.android.tools.lint.detector.api.Scope
import com.android.tools.lint.detector.api.Severity
import com.android.tools.lint.detector.api.SourceCodeScanner
import com.android.tools.lint.detector.api.TextFormat
import dev.whosnickdoglio.lint.shared.anvil.CONTRIBUTES_SUBCOMPONENT
import dev.whosnickdoglio.lint.shared.anvil.MERGE_COMPONENT
import dev.whosnickdoglio.lint.shared.anvil.MERGE_SUBCOMPONENT
import dev.whosnickdoglio.lint.shared.dagger.COMPONENT
import dev.whosnickdoglio.lint.shared.dagger.SUBCOMPONENT
import org.jetbrains.uast.UAnnotation
Expand All @@ -32,7 +35,7 @@ internal class ComponentMustBeAbstractDetector : Detector(), SourceCodeScanner {
override fun createUastHandler(context: JavaContext): UElementHandler =
object : UElementHandler() {
override fun visitAnnotation(node: UAnnotation) {
if (node.qualifiedName == COMPONENT || node.qualifiedName == SUBCOMPONENT) {
if (node.qualifiedName in componentAnnotations) {
val component = node.uastParent as? UClass ?: return

if (!context.evaluator.isAbstract(component)) {
Expand Down Expand Up @@ -60,6 +63,16 @@ internal class ComponentMustBeAbstractDetector : Detector(), SourceCodeScanner {
private val implementation =
Implementation(ComponentMustBeAbstractDetector::class.java, Scope.JAVA_FILE_SCOPE)

internal val componentAnnotations = setOf(
// Vanilla Dagger
COMPONENT,
SUBCOMPONENT,
// Anvil
MERGE_COMPONENT,
MERGE_SUBCOMPONENT,
CONTRIBUTES_SUBCOMPONENT
)

internal val ISSUE =
Issue.create(
id = "ComponentMustBeAbstract",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@ import com.android.tools.lint.checks.infrastructure.TestFiles
import com.android.tools.lint.checks.infrastructure.TestLintTask
import com.google.testing.junit.testparameterinjector.TestParameter
import com.google.testing.junit.testparameterinjector.TestParameterInjector
import dev.whosnickdoglio.lint.shared.dagger.COMPONENT
import dev.whosnickdoglio.lint.shared.dagger.SUBCOMPONENT
import com.google.testing.junit.testparameterinjector.TestParameterValuesProvider
import dev.whosnickdoglio.stubs.daggerAnnotations
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(TestParameterInjector::class)
class ComponentMustBeAbstractDetectorTest {
@TestParameter(value = [COMPONENT, SUBCOMPONENT])
private class ComponentMustBeAbstractTestParameterValueProvider :
TestParameterValuesProvider() {
override fun provideValues(context: Context?): MutableList<*> =
ComponentMustBeAbstractDetector.componentAnnotations.toMutableList()
}

@TestParameter(valuesProvider = ComponentMustBeAbstractTestParameterValueProvider::class)
lateinit var componentAnnotation: String

@Test
Expand Down Expand Up @@ -134,7 +139,8 @@ class ComponentMustBeAbstractDetectorTest {
@@ -4 +4
- class MyComponent {}
+ interface MyComponent {}
""".trimIndent(),
"""
.trimIndent(),
)
}

Expand Down Expand Up @@ -173,7 +179,8 @@ class ComponentMustBeAbstractDetectorTest {
@@ -4 +4
- class MyComponent
+ interface MyComponent
""".trimIndent(),
"""
.trimIndent(),
)
}
}

0 comments on commit 9b63b79

Please sign in to comment.