Skip to content

Commit c59a133

Browse files
Make ComponentMustBeAbstractDetector aware of Anvil Component annotations
1 parent 947ff34 commit c59a133

File tree

7 files changed

+40
-9
lines changed

7 files changed

+40
-9
lines changed

lint/anvil/src/test/java/dev/whosnickdoglio/anvil/detectors/ContributesBindingMustHaveSuperDetectorTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import com.google.testing.junit.testparameterinjector.TestParameterInjector
1111
import dev.whosnickdoglio.lint.shared.anvil.CONTRIBUTES_BINDING
1212
import dev.whosnickdoglio.lint.shared.anvil.CONTRIBUTES_MULTI_BINDING
1313
import dev.whosnickdoglio.lint.shared.anvil.CONTRIBUTES_TO
14+
import dev.whosnickdoglio.stubs.anvilAnnotations
1415
import dev.whosnickdoglio.stubs.daggerAnnotations
1516
import dev.whosnickdoglio.stubs.javaxAnnotations
1617
import org.junit.Test

lint/anvil/src/test/java/dev/whosnickdoglio/anvil/detectors/MissingContributesBindingDetectorTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.google.testing.junit.testparameterinjector.TestParameter
1010
import com.google.testing.junit.testparameterinjector.TestParameterInjector
1111
import dev.whosnickdoglio.lint.shared.anvil.CONTRIBUTES_BINDING
1212
import dev.whosnickdoglio.lint.shared.anvil.CONTRIBUTES_MULTI_BINDING
13+
import dev.whosnickdoglio.stubs.anvilAnnotations
1314
import dev.whosnickdoglio.stubs.javaxAnnotations
1415
import org.junit.Test
1516
import org.junit.runner.RunWith

lint/anvil/src/test/java/dev/whosnickdoglio/anvil/detectors/MissingContributesToDetectorTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package dev.whosnickdoglio.anvil.detectors
66

77
import com.android.tools.lint.checks.infrastructure.TestFiles
88
import com.android.tools.lint.checks.infrastructure.TestLintTask
9+
import dev.whosnickdoglio.stubs.anvilAnnotations
910
import dev.whosnickdoglio.stubs.daggerAnnotations
1011
import org.junit.Test
1112

lint/anvil/src/test/java/dev/whosnickdoglio/anvil/detectors/NoAnvilInJavaDetectorTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import dev.whosnickdoglio.lint.shared.anvil.CONTRIBUTES_SUBCOMPONENT_FACTORY
1515
import dev.whosnickdoglio.lint.shared.anvil.CONTRIBUTES_TO
1616
import dev.whosnickdoglio.lint.shared.anvil.MERGE_COMPONENT
1717
import dev.whosnickdoglio.lint.shared.anvil.MERGE_SUBCOMPONENT
18+
import dev.whosnickdoglio.stubs.anvilAnnotations
1819
import org.junit.Test
1920
import org.junit.runner.RunWith
2021

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
@@ -34,7 +37,7 @@ internal class ComponentMustBeAbstractDetector :
3437
override fun createUastHandler(context: JavaContext): UElementHandler =
3538
object : UElementHandler() {
3639
override fun visitAnnotation(node: UAnnotation) {
37-
if (node.qualifiedName == COMPONENT || node.qualifiedName == SUBCOMPONENT) {
40+
if (node.qualifiedName in componentAnnotations) {
3841
val component = node.uastParent as? UClass ?: return
3942

4043
if (!context.evaluator.isAbstract(component)) {
@@ -62,6 +65,16 @@ internal class ComponentMustBeAbstractDetector :
6265
private val implementation =
6366
Implementation(ComponentMustBeAbstractDetector::class.java, Scope.JAVA_FILE_SCOPE)
6467

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

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,29 @@ 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
12+
import dev.whosnickdoglio.stubs.anvilAnnotations
1313
import dev.whosnickdoglio.stubs.daggerAnnotations
1414
import org.junit.Test
1515
import org.junit.runner.RunWith
1616

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

2228
@Test
2329
fun `kotlin abstract class component does not show error`() {
2430
TestLintTask.lint()
2531
.files(
2632
daggerAnnotations,
33+
anvilAnnotations,
2734
TestFiles.kotlin(
2835
"""
2936
import $componentAnnotation
@@ -40,10 +47,11 @@ class ComponentMustBeAbstractDetectorTest {
4047
}
4148

4249
@Test
43-
fun `java kotlin abstract class component does not show error`() {
50+
fun `java abstract class component does not show error`() {
4451
TestLintTask.lint()
4552
.files(
4653
daggerAnnotations,
54+
anvilAnnotations,
4755
TestFiles.java(
4856
"""
4957
import $componentAnnotation;
@@ -64,6 +72,7 @@ class ComponentMustBeAbstractDetectorTest {
6472
TestLintTask.lint()
6573
.files(
6674
daggerAnnotations,
75+
anvilAnnotations,
6776
TestFiles.kotlin(
6877
"""
6978
import $componentAnnotation
@@ -84,6 +93,7 @@ class ComponentMustBeAbstractDetectorTest {
8493
TestLintTask.lint()
8594
.files(
8695
daggerAnnotations,
96+
anvilAnnotations,
8797
TestFiles.java(
8898
"""
8999
import $componentAnnotation;
@@ -104,6 +114,7 @@ class ComponentMustBeAbstractDetectorTest {
104114
TestLintTask.lint()
105115
.files(
106116
daggerAnnotations,
117+
anvilAnnotations,
107118
TestFiles.java(
108119
"""
109120
import $componentAnnotation;
@@ -134,7 +145,8 @@ class ComponentMustBeAbstractDetectorTest {
134145
@@ -4 +4
135146
- class MyComponent {}
136147
+ interface MyComponent {}
137-
""".trimIndent(),
148+
"""
149+
.trimIndent(),
138150
)
139151
}
140152

@@ -143,6 +155,7 @@ class ComponentMustBeAbstractDetectorTest {
143155
TestLintTask.lint()
144156
.files(
145157
daggerAnnotations,
158+
anvilAnnotations,
146159
TestFiles.kotlin(
147160
"""
148161
import $componentAnnotation
@@ -173,7 +186,8 @@ class ComponentMustBeAbstractDetectorTest {
173186
@@ -4 +4
174187
- class MyComponent
175188
+ interface MyComponent
176-
""".trimIndent(),
189+
"""
190+
.trimIndent(),
177191
)
178192
}
179193
}

lint/anvil/src/test/java/dev/whosnickdoglio/anvil/detectors/Stubs.kt renamed to lint/test-stubs/src/main/java/dev/whosnickdoglio/stubs/AnvilStubs.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
* Copyright (C) 2024 Nicholas Doglio
33
* SPDX-License-Identifier: MIT
44
*/
5-
package dev.whosnickdoglio.anvil.detectors
5+
package dev.whosnickdoglio.stubs
66

77
import com.android.tools.lint.checks.infrastructure.TestFile
88
import com.android.tools.lint.checks.infrastructure.TestFiles
99

10-
val anvilAnnotations: TestFile =
10+
public val anvilAnnotations: TestFile =
1111
TestFiles.kotlin(
1212
"""
1313
package com.squareup.anvil.annotations

0 commit comments

Comments
 (0)