Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / lint-rules / src / main / java / com / vayunmathur / lint / DirectComposeAnimationDetector.kt
DirectComposeAnimationDetector.kt180 lines · 9.1 KiBRaw
| 1 | package com.vayunmathur.lint |
| 2 | |
| 3 | import com.android.tools.lint.client.api.UElementHandler |
| 4 | import com.android.tools.lint.detector.api.Category |
| 5 | import com.android.tools.lint.detector.api.Detector |
| 6 | import com.android.tools.lint.detector.api.Implementation |
| 7 | import com.android.tools.lint.detector.api.Issue |
| 8 | import com.android.tools.lint.detector.api.JavaContext |
| 9 | import com.android.tools.lint.detector.api.Scope |
| 10 | import com.android.tools.lint.detector.api.Severity |
| 11 | import com.android.tools.lint.detector.api.SourceCodeScanner |
| 12 | import org.jetbrains.kotlin.psi.KtFile |
| 13 | import org.jetbrains.uast.UElement |
| 14 | import org.jetbrains.uast.UImportStatement |
| 15 | |
| 16 | /** |
| 17 | * Flags an app module reaching for Compose's declarative animation APIs directly. |
| 18 | * |
| 19 | * Motion that is written at the call site carries no intent, so the same interaction ends up with a |
| 20 | * different duration and curve on every screen and the app stops feeling like one app. The shared |
| 21 | * helpers name the interaction instead - `animatedDp`, `FadeVisibility`, `itemMotion`, |
| 22 | * `sharedText`, `MorphPage` - and put the timing in one place where it can be tuned once. |
| 23 | * |
| 24 | * Only the declarative layer is flagged: the `AnimatedVisibility` / `fadeIn` / `animate*AsState` |
| 25 | * family, which is where that drift happens. Imperative motion built on |
| 26 | * [androidx.compose.animation.core.Animatable] is deliberately allowed - a game board wave or a |
| 27 | * camera flash is genuinely one-off, and wrapping it in the library would be a passthrough that |
| 28 | * names nothing. |
| 29 | * |
| 30 | * Scope: app modules only. `com.vayunmathur.library.*` is where the helpers live and is exempt, and |
| 31 | * so is `com.vayunmathur.launcher.*`, which drives its home screen from `animateBounds` and |
| 32 | * `Animatable` and declares the dependency explicitly for that reason. |
| 33 | * |
| 34 | * Per-file opt-out: a file carrying `// RAW ANIMATION EXCEPTION: <reason>` is skipped. |
| 35 | */ |
| 36 | class DirectComposeAnimationDetector : Detector(), SourceCodeScanner { |
| 37 | |
| 38 | override fun getApplicableUastTypes(): List<Class<out UElement>> = |
| 39 | listOf(UImportStatement::class.java) |
| 40 | |
| 41 | override fun createUastHandler(context: JavaContext): UElementHandler? { |
| 42 | val packageName = context.uastFile?.packageName ?: return null |
| 43 | if (!packageName.startsWith(APP_PACKAGE_PREFIX)) return null |
| 44 | if (isExemptPackage(packageName)) return null |
| 45 | |
| 46 | // Documented per-file opt-out, matching the repo's other escape hatches. |
| 47 | // Comment-only: a marker inside a string literal must not opt the file out. |
| 48 | val ktFile = context.uastFile?.sourcePsi as? KtFile |
| 49 | if (ktFile != null && CommentOnlyMatcher.containsMarkerInComments(ktFile, EXCEPTION_MARKER)) return null |
| 50 | |
| 51 | return object : UElementHandler() { |
| 52 | override fun visitImportStatement(node: UImportStatement) { |
| 53 | val reference = node.importReference?.asSourceString() ?: return |
| 54 | val banned = bannedSymbolIn(reference) ?: return |
| 55 | context.report( |
| 56 | ISSUE, |
| 57 | node, |
| 58 | context.getLocation(node as UElement), |
| 59 | "`$banned` must not be used directly in an app module. Use the motion helpers " + |
| 60 | "in `com.vayunmathur.library.ui` (animatedDp / animatedFloat / " + |
| 61 | "animatedColor / pressedShape / itemMotion / staggeredEntrance, " + |
| 62 | "FadeVisibility / PopVisibility / ExpandVisibility / BannerVisibility / " + |
| 63 | "SwappedContent / SwappedTopBar) or `com.vayunmathur.library.util` for " + |
| 64 | "navigation, shared-element and exit motion (sharedContainer / " + |
| 65 | "sharedText / sharedCrop / MorphPage / expandFromLine / dropsAway / " + |
| 66 | "sinksBelow), " + |
| 67 | "so the timing is named once instead of re-derived per screen. Imperative " + |
| 68 | "one-off motion on Animatable is allowed - mark the file with " + |
| 69 | "`$EXCEPTION_MARKER <reason>` if that is what this is.", |
| 70 | ) |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | companion object { |
| 76 | private const val APP_PACKAGE_PREFIX = "com.vayunmathur." |
| 77 | private const val LIBRARY_PREFIX = "com.vayunmathur.library" |
| 78 | |
| 79 | /** |
| 80 | * The launcher's home screen is built on `animateBounds` and `Animatable`, which have no |
| 81 | * shared equivalent, and it declares `androidx-compose-animation` explicitly for that. |
| 82 | * Exempted wholesale rather than per file so its drag-and-drop code is not littered with |
| 83 | * markers. |
| 84 | */ |
| 85 | private const val LAUNCHER_PREFIX = "com.vayunmathur.launcher" |
| 86 | |
| 87 | const val EXCEPTION_MARKER = "// RAW ANIMATION EXCEPTION:" |
| 88 | |
| 89 | private const val ANIMATION_PACKAGE = "androidx.compose.animation" |
| 90 | |
| 91 | /** |
| 92 | * The declarative surface. Matched on the imported name rather than the package, because |
| 93 | * these live across both `androidx.compose.animation` and `androidx.compose.animation.core` |
| 94 | * while their imperative neighbours - Animatable, tween, spring, the easings - stay allowed. |
| 95 | */ |
| 96 | private val BANNED_SYMBOLS = listOf( |
| 97 | "AnimatedVisibility", |
| 98 | "AnimatedContent", |
| 99 | "Crossfade", |
| 100 | "animateColorAsState", |
| 101 | "animateDpAsState", |
| 102 | "animateFloatAsState", |
| 103 | "animateIntAsState", |
| 104 | "animateIntOffsetAsState", |
| 105 | "animateOffsetAsState", |
| 106 | "animateSizeAsState", |
| 107 | "animateValueAsState", |
| 108 | "fadeIn", |
| 109 | "fadeOut", |
| 110 | "scaleIn", |
| 111 | "scaleOut", |
| 112 | "slideIn", |
| 113 | "slideInHorizontally", |
| 114 | "slideInVertically", |
| 115 | "slideOut", |
| 116 | "slideOutHorizontally", |
| 117 | "slideOutVertically", |
| 118 | "expandHorizontally", |
| 119 | "expandIn", |
| 120 | "expandVertically", |
| 121 | "shrinkHorizontally", |
| 122 | "shrinkOut", |
| 123 | "shrinkVertically", |
| 124 | "togetherWith", |
| 125 | ) |
| 126 | |
| 127 | private fun isExemptPackage(packageName: String): Boolean = |
| 128 | packageName == LIBRARY_PREFIX || |
| 129 | packageName.startsWith("$LIBRARY_PREFIX.") || |
| 130 | packageName == LAUNCHER_PREFIX || |
| 131 | packageName.startsWith("$LAUNCHER_PREFIX.") |
| 132 | |
| 133 | /** The banned symbol this import refers to, or null if the import is fine. */ |
| 134 | private fun bannedSymbolIn(reference: String): String? { |
| 135 | if (!reference.startsWith(ANIMATION_PACKAGE)) return null |
| 136 | // A star import pulls the whole declarative surface in with it. |
| 137 | if (reference == "$ANIMATION_PACKAGE.*") return "$ANIMATION_PACKAGE.*" |
| 138 | val symbol = reference.substringAfterLast('.') |
| 139 | return BANNED_SYMBOLS.firstOrNull { it == symbol }?.let { "$ANIMATION_PACKAGE...$it" } |
| 140 | } |
| 141 | |
| 142 | val ISSUE: Issue = Issue.create( |
| 143 | id = "DirectComposeAnimation", |
| 144 | briefDescription = "Compose animation API used directly in an app module", |
| 145 | explanation = """ |
| 146 | App modules must not use Compose's declarative animation APIs directly — \ |
| 147 | AnimatedVisibility, AnimatedContent, Crossfade, the `animate*AsState` family, or \ |
| 148 | the `fadeIn`/`scaleIn`/`slideIn`/`expandVertically` enter-exit builders. |
| 149 | |
| 150 | Motion written at a call site carries no intent. The same interaction picks up a \ |
| 151 | different duration and curve on each screen, and a spec written with no arguments \ |
| 152 | silently takes Compose's own default rather than the motion scheme the app is \ |
| 153 | themed with. The shared helpers name the interaction instead and hold the timing \ |
| 154 | in one place: `animatedDp`, `animatedFloat`, `animatedColor`, `pressedShape`, \ |
| 155 | `itemMotion` and `staggeredEntrance` in com.vayunmathur.library.ui, the \ |
| 156 | `FadeVisibility` / `PopVisibility` / `ExpandVisibility` / `BannerVisibility` / \ |
| 157 | `SwappedContent` / `SwappedTopBar` wrappers beside them, and `sharedText` / \ |
| 158 | `sharedContainer` / `sharedCrop` / `MorphPage` / `expandFromLine` / \ |
| 159 | `dropsAway` / `sinksBelow` in com.vayunmathur.library.util for navigation, \ |
| 160 | shared-element and exit motion. |
| 161 | |
| 162 | Imperative motion on androidx.compose.animation.core.Animatable is NOT flagged. A \ |
| 163 | game board wave or a camera capture flash is genuinely one-off, and wrapping it \ |
| 164 | would be a passthrough that names nothing. |
| 165 | |
| 166 | com.vayunmathur.library.* is exempt because the helpers are built there, and \ |
| 167 | com.vayunmathur.launcher.* is exempt because its home screen is built on \ |
| 168 | animateBounds and Animatable. For anything else that genuinely needs the raw API, \ |
| 169 | add the marker comment `// RAW ANIMATION EXCEPTION: <reason>` to the file. |
| 170 | """.trimIndent(), |
| 171 | category = Category.CORRECTNESS, |
| 172 | priority = 6, |
| 173 | severity = Severity.ERROR, |
| 174 | implementation = Implementation( |
| 175 | DirectComposeAnimationDetector::class.java, |
| 176 | Scope.JAVA_FILE_SCOPE, |
| 177 | ), |
| 178 | ) |
| 179 | } |
| 180 | } |