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 Jun 29, 2024
1 parent d3c4d9b commit e360911
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.google.testing.junit.testparameterinjector.TestParameterInjector
import dev.whosnickdoglio.lint.shared.anvil.CONTRIBUTES_BINDING
import dev.whosnickdoglio.lint.shared.anvil.CONTRIBUTES_MULTI_BINDING
import dev.whosnickdoglio.lint.shared.anvil.CONTRIBUTES_TO
import dev.whosnickdoglio.stubs.anvilAnnotations
import dev.whosnickdoglio.stubs.daggerAnnotations
import dev.whosnickdoglio.stubs.javaxAnnotations
import org.junit.Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.google.testing.junit.testparameterinjector.TestParameter
import com.google.testing.junit.testparameterinjector.TestParameterInjector
import dev.whosnickdoglio.lint.shared.anvil.CONTRIBUTES_BINDING
import dev.whosnickdoglio.lint.shared.anvil.CONTRIBUTES_MULTI_BINDING
import dev.whosnickdoglio.stubs.anvilAnnotations
import dev.whosnickdoglio.stubs.javaxAnnotations
import org.junit.Test
import org.junit.runner.RunWith
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package dev.whosnickdoglio.anvil.detectors

import com.android.tools.lint.checks.infrastructure.TestFiles
import com.android.tools.lint.checks.infrastructure.TestLintTask
import dev.whosnickdoglio.stubs.anvilAnnotations
import dev.whosnickdoglio.stubs.daggerAnnotations
import org.junit.Test

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import dev.whosnickdoglio.lint.shared.anvil.CONTRIBUTES_SUBCOMPONENT_FACTORY
import dev.whosnickdoglio.lint.shared.anvil.CONTRIBUTES_TO
import dev.whosnickdoglio.lint.shared.anvil.MERGE_COMPONENT
import dev.whosnickdoglio.lint.shared.anvil.MERGE_SUBCOMPONENT
import dev.whosnickdoglio.stubs.anvilAnnotations
import org.junit.Test
import org.junit.runner.RunWith

Expand Down
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 @@ -34,7 +37,7 @@ internal class ComponentMustBeAbstractDetector :
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 @@ -62,6 +65,16 @@ internal class ComponentMustBeAbstractDetector :
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,22 +8,29 @@ 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.anvilAnnotations
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
fun `kotlin abstract class component does not show error`() {
TestLintTask.lint()
.files(
daggerAnnotations,
anvilAnnotations,
TestFiles.kotlin(
"""
import $componentAnnotation
Expand All @@ -40,10 +47,11 @@ class ComponentMustBeAbstractDetectorTest {
}

@Test
fun `java kotlin abstract class component does not show error`() {
fun `java abstract class component does not show error`() {
TestLintTask.lint()
.files(
daggerAnnotations,
anvilAnnotations,
TestFiles.java(
"""
import $componentAnnotation;
Expand All @@ -64,6 +72,7 @@ class ComponentMustBeAbstractDetectorTest {
TestLintTask.lint()
.files(
daggerAnnotations,
anvilAnnotations,
TestFiles.kotlin(
"""
import $componentAnnotation
Expand All @@ -84,6 +93,7 @@ class ComponentMustBeAbstractDetectorTest {
TestLintTask.lint()
.files(
daggerAnnotations,
anvilAnnotations,
TestFiles.java(
"""
import $componentAnnotation;
Expand All @@ -104,6 +114,7 @@ class ComponentMustBeAbstractDetectorTest {
TestLintTask.lint()
.files(
daggerAnnotations,
anvilAnnotations,
TestFiles.java(
"""
import $componentAnnotation;
Expand Down Expand Up @@ -134,7 +145,8 @@ class ComponentMustBeAbstractDetectorTest {
@@ -4 +4
- class MyComponent {}
+ interface MyComponent {}
""".trimIndent(),
"""
.trimIndent(),
)
}

Expand All @@ -143,6 +155,7 @@ class ComponentMustBeAbstractDetectorTest {
TestLintTask.lint()
.files(
daggerAnnotations,
anvilAnnotations,
TestFiles.kotlin(
"""
import $componentAnnotation
Expand Down Expand Up @@ -173,7 +186,8 @@ class ComponentMustBeAbstractDetectorTest {
@@ -4 +4
- class MyComponent
+ interface MyComponent
""".trimIndent(),
"""
.trimIndent(),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* Copyright (C) 2024 Nicholas Doglio
* SPDX-License-Identifier: MIT
*/
package dev.whosnickdoglio.anvil.detectors
package dev.whosnickdoglio.stubs

import com.android.tools.lint.checks.infrastructure.TestFile
import com.android.tools.lint.checks.infrastructure.TestFiles

val anvilAnnotations: TestFile =
public val anvilAnnotations: TestFile =
TestFiles.kotlin(
"""
package com.squareup.anvil.annotations
Expand Down

0 comments on commit e360911

Please sign in to comment.