Skip to content

Commit 56d529e

Browse files
Make ComponentMustBeAbstractDetector aware of Anvil Component annotations
1 parent dbb76bc commit 56d529e

File tree

5 files changed

+29
-9
lines changed

5 files changed

+29
-9
lines changed

lint/dagger/src/main/java/dev/whosnickdoglio/dagger/detectors/ComponentMustBeAbstractDetector.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import com.android.tools.lint.detector.api.Scope
1515
import com.android.tools.lint.detector.api.Severity
1616
import com.android.tools.lint.detector.api.SourceCodeScanner
1717
import com.android.tools.lint.detector.api.TextFormat
18+
import dev.whosnickdoglio.lint.shared.anvil.CONTRIBUTES_SUBCOMPONENT
19+
import dev.whosnickdoglio.lint.shared.anvil.MERGE_COMPONENT
20+
import dev.whosnickdoglio.lint.shared.anvil.MERGE_SUBCOMPONENT
1821
import dev.whosnickdoglio.lint.shared.dagger.COMPONENT
1922
import dev.whosnickdoglio.lint.shared.dagger.SUBCOMPONENT
2023
import org.jetbrains.uast.UAnnotation
@@ -32,7 +35,7 @@ internal class ComponentMustBeAbstractDetector : Detector(), SourceCodeScanner {
3235
override fun createUastHandler(context: JavaContext): UElementHandler =
3336
object : UElementHandler() {
3437
override fun visitAnnotation(node: UAnnotation) {
35-
if (node.qualifiedName == COMPONENT || node.qualifiedName == SUBCOMPONENT) {
38+
if (node.qualifiedName in componentAnnotations) {
3639
val component = node.uastParent as? UClass ?: return
3740

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

66+
internal val componentAnnotations = setOf(
67+
// Vanilla Dagger
68+
COMPONENT,
69+
SUBCOMPONENT,
70+
// Anvil
71+
MERGE_COMPONENT,
72+
MERGE_SUBCOMPONENT,
73+
CONTRIBUTES_SUBCOMPONENT,
74+
)
75+
6376
internal val ISSUE =
6477
Issue.create(
6578
id = "ComponentMustBeAbstract",

lint/dagger/src/test/java/dev/whosnickdoglio/dagger/detectors/ComponentMustBeAbstractDetectorTest.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,20 @@ import com.android.tools.lint.checks.infrastructure.TestFiles
88
import com.android.tools.lint.checks.infrastructure.TestLintTask
99
import com.google.testing.junit.testparameterinjector.TestParameter
1010
import com.google.testing.junit.testparameterinjector.TestParameterInjector
11-
import dev.whosnickdoglio.lint.shared.dagger.COMPONENT
12-
import dev.whosnickdoglio.lint.shared.dagger.SUBCOMPONENT
11+
import com.google.testing.junit.testparameterinjector.TestParameterValuesProvider
1312
import dev.whosnickdoglio.stubs.daggerAnnotations
1413
import org.junit.Test
1514
import org.junit.runner.RunWith
1615

1716
@RunWith(TestParameterInjector::class)
1817
class ComponentMustBeAbstractDetectorTest {
19-
@TestParameter(value = [COMPONENT, SUBCOMPONENT])
18+
private class ComponentMustBeAbstractTestParameterValueProvider :
19+
TestParameterValuesProvider() {
20+
override fun provideValues(context: Context?): MutableList<*> =
21+
ComponentMustBeAbstractDetector.componentAnnotations.toMutableList()
22+
}
23+
24+
@TestParameter(valuesProvider = ComponentMustBeAbstractTestParameterValueProvider::class)
2025
lateinit var componentAnnotation: String
2126

2227
@Test
@@ -134,7 +139,8 @@ class ComponentMustBeAbstractDetectorTest {
134139
@@ -4 +4
135140
- class MyComponent {}
136141
+ interface MyComponent {}
137-
""".trimIndent(),
142+
"""
143+
.trimIndent(),
138144
)
139145
}
140146

@@ -173,7 +179,8 @@ class ComponentMustBeAbstractDetectorTest {
173179
@@ -4 +4
174180
- class MyComponent
175181
+ interface MyComponent
176-
""".trimIndent(),
182+
"""
183+
.trimIndent(),
177184
)
178185
}
179186
}

lint/shared/src/main/java/dev/whosnickdoglio/lint/shared/anvil/AnvilAnnotations.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2024 Nicholas Doglio
2+
* Copyright (C) 2023 Nicholas Doglio
33
* SPDX-License-Identifier: MIT
44
*/
55
package dev.whosnickdoglio.lint.shared.anvil

lint/shared/src/main/java/dev/whosnickdoglio/lint/shared/dagger/DaggerAnnotations.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2024 Nicholas Doglio
2+
* Copyright (C) 2023 Nicholas Doglio
33
* SPDX-License-Identifier: MIT
44
*/
55
package dev.whosnickdoglio.lint.shared.dagger

lint/shared/src/main/java/dev/whosnickdoglio/lint/shared/hilt/HiltAnnotations.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2024 Nicholas Doglio
2+
* Copyright (C) 2023 Nicholas Doglio
33
* SPDX-License-Identifier: MIT
44
*/
55
package dev.whosnickdoglio.lint.shared.hilt

0 commit comments

Comments
 (0)