-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added annotation to ignore @Preview compose functions, tried to fix j…
…acoco for :core-ui module but failed
- Loading branch information
Showing
29 changed files
with
288 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
@Suppress("DSL_SCOPE_VIOLATION") | ||
plugins { | ||
id("java-library") | ||
id("kotlin") | ||
alias(libs.plugins.detekt) apply false | ||
} | ||
|
||
java { | ||
sourceCompatibility = JavaVersion.VERSION_17 | ||
targetCompatibility = JavaVersion.VERSION_17 | ||
} | ||
|
||
dependencies { | ||
compileOnly(libs.detekt.api) | ||
compileOnly(libs.lint.api) | ||
} | ||
|
||
tasks.jar { | ||
manifest { | ||
// Format is | ||
// attributes(mapOf("Lint-Registry-v2" to "<fully-qualified-class-name-of-your-issue-registry>")) | ||
attributes(mapOf("Lint-Registry-v2" to "app.books.tanga.codechecks.registry.LintRegistry")) | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
...hecks/src/main/java/app/books/tanga/codechecks/MissingExcludePreviewAnnotationDetector.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package app.books.tanga.codechecks | ||
|
||
import com.android.tools.lint.client.api.UElementHandler | ||
import com.android.tools.lint.detector.api.Category | ||
import com.android.tools.lint.detector.api.Detector | ||
import com.android.tools.lint.detector.api.Implementation | ||
import com.android.tools.lint.detector.api.Issue | ||
import com.android.tools.lint.detector.api.JavaContext | ||
import com.android.tools.lint.detector.api.Scope | ||
import com.android.tools.lint.detector.api.Severity | ||
import com.android.tools.lint.detector.api.TextFormat | ||
import org.jetbrains.uast.UElement | ||
import org.jetbrains.uast.UMethod | ||
|
||
/** | ||
* A custom lint detector that checks for missing `@ExcludeFromJacocoGeneratedReport` | ||
* annotations on Jetpack Compose preview methods. | ||
* | ||
* This detector scans through method declarations in Kotlin files and reports any `@Preview` annotated methods | ||
* that are not also annotated with `@ExcludeFromJacocoGeneratedReport`. This ensures that preview methods | ||
* are properly excluded from JaCoCo coverage reports. | ||
* | ||
* Taken from here: https://github.com/AdamMc331/TOA/blob/96248ce1e8c27817779d4785a2f73d4100a1ea90/lint-checks/src/main/java/com/adammcneilly/toa/lint/MissingExcludePreviewAnnotationDetector.kt | ||
*/ | ||
@Suppress("UnstableApiUsage") | ||
class MissingExcludePreviewAnnotationDetector : Detector(), Detector.UastScanner { | ||
|
||
/** | ||
* Specifies that this detector is interested in method declarations. | ||
*/ | ||
override fun getApplicableUastTypes(): List<Class<out UElement>> = listOf(UMethod::class.java) | ||
|
||
/** | ||
* Creates a handler for processing UAST method nodes. | ||
*/ | ||
override fun createUastHandler(context: JavaContext): UElementHandler = PreviewMethodElementHandler(context) | ||
|
||
/** | ||
* An inner class to handle method nodes detected by the detector. | ||
*/ | ||
private class PreviewMethodElementHandler(private val context: JavaContext) : UElementHandler() { | ||
|
||
/** | ||
* Visits each method node in the UAST. | ||
* | ||
* Checks if the method is a Compose preview method and whether it's properly | ||
* annotated to be excluded from JaCoCo reports. | ||
*/ | ||
override fun visitMethod(node: UMethod) { | ||
val isComposePreviewMethod = node.hasAnnotation(COMPOSE_PREVIEW_ANNOTATION) | ||
val isExcludedFromJacoco = node.hasAnnotation(EXCLUDE_FROM_JACOCO_ANNOTATION) | ||
|
||
if (isComposePreviewMethod && isExcludedFromJacoco.not()) { | ||
context.report( | ||
issue = ISSUE_MISSING_EXCLUDE_PREVIEW_ANNOTATION, | ||
location = context.getLocation(node), | ||
message = ISSUE_MISSING_EXCLUDE_PREVIEW_ANNOTATION.getExplanation(TextFormat.TEXT), | ||
) | ||
} | ||
} | ||
} | ||
|
||
companion object { | ||
private const val COMPOSE_PREVIEW_ANNOTATION = "androidx.compose.ui.tooling.preview.Preview" | ||
private const val EXCLUDE_FROM_JACOCO_ANNOTATION = | ||
"app.books.tanga.coreui.common.ExcludeFromJacocoGeneratedReport" | ||
|
||
/** | ||
* Definition of the lint issue checked by this detector. | ||
*/ | ||
@Suppress("MaxLineLength") | ||
internal val ISSUE_MISSING_EXCLUDE_PREVIEW_ANNOTATION = Issue.create( | ||
id = "MissingExcludePreviewAnnotation", | ||
briefDescription = "Jetpack Compose previews should be excluded from JaCoCo Reports.", | ||
explanation = "Any methods annotated with @Preview should also have " + | ||
"the @ExcludeFromJacocoGeneratedReport annotation.", | ||
category = Category.CUSTOM_LINT_CHECKS, | ||
severity = Severity.ERROR, | ||
implementation = Implementation( | ||
MissingExcludePreviewAnnotationDetector::class.java, | ||
Scope.JAVA_FILE_SCOPE, | ||
), | ||
priority = 10, | ||
) | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
code-checks/src/main/java/app/books/tanga/codechecks/PreviewAnnotationRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package app.books.tanga.codechecks | ||
|
||
import io.gitlab.arturbosch.detekt.api.CodeSmell | ||
import io.gitlab.arturbosch.detekt.api.Config | ||
import io.gitlab.arturbosch.detekt.api.Debt | ||
import io.gitlab.arturbosch.detekt.api.Entity | ||
import io.gitlab.arturbosch.detekt.api.Issue | ||
import io.gitlab.arturbosch.detekt.api.Rule | ||
import io.gitlab.arturbosch.detekt.api.Severity | ||
import org.jetbrains.kotlin.psi.KtAnnotationEntry | ||
import org.jetbrains.kotlin.psi.KtFunction | ||
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType | ||
|
||
class PreviewAnnotationRule(config: Config) : Rule(config) { | ||
|
||
override val issue = Issue( | ||
javaClass.simpleName, | ||
Severity.CodeSmell, | ||
"Compose functions annotated with @Preview must also be annotated with @ExcludeFromJacocoGeneratedReport", | ||
Debt.FIVE_MINS | ||
) | ||
|
||
override fun visitAnnotationEntry(annotationEntry: KtAnnotationEntry) { | ||
super.visitAnnotationEntry(annotationEntry) | ||
|
||
if (annotationEntry.shortName?.asString() != "Preview") return | ||
|
||
val owner = annotationEntry.getStrictParentOfType<KtFunction>() ?: return | ||
if (owner.annotationEntries.none { it.shortName?.asString() == "ExcludeFromJacocoGeneratedReport" }) { | ||
report( | ||
CodeSmell( | ||
issue, | ||
Entity.from(annotationEntry), | ||
message = "The @Preview function `${owner.name}` should also be " + | ||
"annotated with @ExcludeFromJacocoGeneratedReport" | ||
) | ||
) | ||
} | ||
} | ||
} |
Oops, something went wrong.