From bafc4427e9b20c10969cee90bcf396114cfd0a7f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Brais=20Gab=C3=ADn?= Date: Sun, 4 Aug 2024 02:06:56 +0200 Subject: [PATCH] Improve code position (#234) --- .../schwarz/it/lightsaber/checkers/UnusedInject.kt | 5 ++++- .../schwarz/it/lightsaber/checkers/UnusedScopes.kt | 10 ++++++++-- .../it/lightsaber/checkers/UnusedInjectKtTest.kt | 6 ++++-- .../it/lightsaber/checkers/UnusedScopesKtTest.kt | 4 ++-- 4 files changed, 18 insertions(+), 7 deletions(-) diff --git a/lightsaber/src/main/java/schwarz/it/lightsaber/checkers/UnusedInject.kt b/lightsaber/src/main/java/schwarz/it/lightsaber/checkers/UnusedInject.kt index b941715e..822403dd 100644 --- a/lightsaber/src/main/java/schwarz/it/lightsaber/checkers/UnusedInject.kt +++ b/lightsaber/src/main/java/schwarz/it/lightsaber/checkers/UnusedInject.kt @@ -40,7 +40,10 @@ internal class UnusedInjectKsp : LightsaberKspRule { val injectName = inject.parent as KSClassDeclaration Finding( "The @Inject in `${injectName.qualifiedName!!.asString()}` constructor is unused because there is a @Provides defined in `${parent.qualifiedName!!.asString()}.${provide.simpleName.getShortName()}`.", - inject.location.toCodePosition(), + inject.annotations + .single { it.annotationType.resolve().declaration.qualifiedName!!.asString() == Inject::class.qualifiedName!! } + .location + .toCodePosition(), inject::hasSuppress, ) } diff --git a/lightsaber/src/main/java/schwarz/it/lightsaber/checkers/UnusedScopes.kt b/lightsaber/src/main/java/schwarz/it/lightsaber/checkers/UnusedScopes.kt index 04fbe551..8acb5836 100644 --- a/lightsaber/src/main/java/schwarz/it/lightsaber/checkers/UnusedScopes.kt +++ b/lightsaber/src/main/java/schwarz/it/lightsaber/checkers/UnusedScopes.kt @@ -46,7 +46,10 @@ internal fun checkUnusedScopes( .map { (component, scope) -> Finding( "The scope `$scope` on component `$component` is not used.", - component.getScopeCodePosition(daggerProcessingEnv, scope.scopeAnnotation()!!.annotationTypeElement()!!.toString()), + component.getScopeCodePosition( + daggerProcessingEnv, + scope.scopeAnnotation()!!.annotationTypeElement()!!.toString(), + ), component.componentPath().currentComponent()::hasSuppress, ) } @@ -87,7 +90,10 @@ internal class UnusedScopesKsp : LightsaberKspRule { val annotationName = scopes.find { classDeclaration.hasAnnotation(it) } Finding( "The `@$annotationName` scope is unused because `${classDeclaration.qualifiedName!!.asString()}` doesn't contain any constructor annotated with `@Inject`.", - classDeclaration.location.toCodePosition(), + classDeclaration.annotations + .single { it.annotationType.resolve().declaration.qualifiedName!!.asString() == annotationName } + .location + .toCodePosition(), classDeclaration::hasSuppress, ) } diff --git a/lightsaber/src/test/kotlin/schwarz/it/lightsaber/checkers/UnusedInjectKtTest.kt b/lightsaber/src/test/kotlin/schwarz/it/lightsaber/checkers/UnusedInjectKtTest.kt index 5a168e04..304fd98c 100644 --- a/lightsaber/src/test/kotlin/schwarz/it/lightsaber/checkers/UnusedInjectKtTest.kt +++ b/lightsaber/src/test/kotlin/schwarz/it/lightsaber/checkers/UnusedInjectKtTest.kt @@ -15,7 +15,7 @@ import schwarz.it.lightsaber.utils.extension internal class UnusedInjectKtTest { @ParameterizedTest - @CsvSource("kapt,4,14", "ksp,5,") + @CsvSource("kapt,4,14", "ksp,6,") fun injectNotUsed( @ConvertWith(CompilerArgumentConverter::class) compiler: KotlinCompiler, line: Int, @@ -43,7 +43,9 @@ internal class UnusedInjectKtTest { import javax.inject.Inject - class Foo @Inject constructor() + class Foo + @Inject + constructor() """.trimIndent(), ) diff --git a/lightsaber/src/test/kotlin/schwarz/it/lightsaber/checkers/UnusedScopesKtTest.kt b/lightsaber/src/test/kotlin/schwarz/it/lightsaber/checkers/UnusedScopesKtTest.kt index 3e2fac7b..f3e540a4 100644 --- a/lightsaber/src/test/kotlin/schwarz/it/lightsaber/checkers/UnusedScopesKtTest.kt +++ b/lightsaber/src/test/kotlin/schwarz/it/lightsaber/checkers/UnusedScopesKtTest.kt @@ -39,7 +39,7 @@ internal class UnusedScopesKtTest { } @ParameterizedTest - @CsvSource("kapt,5,14", "ksp,6,") + @CsvSource("kapt,5,14", "ksp,5,") fun singletonNoInject_Errors( @ConvertWith(CompilerArgumentConverter::class) compiler: KotlinCompiler, line: Int, @@ -121,7 +121,7 @@ internal class UnusedScopesKtTest { } @ParameterizedTest - @CsvSource("kapt,5,14", "ksp,6,") + @CsvSource("kapt,5,14", "ksp,5,") fun customScopeNoInject_Error( @ConvertWith(CompilerArgumentConverter::class) compiler: KotlinCompiler, line: Int,