Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / lint-rules / src / main / java / com / vayunmathur / lint / RawScaffoldInAppDetector.kt
RawScaffoldInAppDetector.kt110 lines · 5.5 KiBRaw
| 1 | package com.vayunmathur.lint |
| 2 | |
| 3 | import com.android.tools.lint.detector.api.Category |
| 4 | import com.android.tools.lint.detector.api.Detector |
| 5 | import com.android.tools.lint.detector.api.Implementation |
| 6 | import com.android.tools.lint.detector.api.Issue |
| 7 | import com.android.tools.lint.detector.api.JavaContext |
| 8 | import com.android.tools.lint.detector.api.Scope |
| 9 | import com.android.tools.lint.detector.api.Severity |
| 10 | import com.android.tools.lint.detector.api.SourceCodeScanner |
| 11 | import com.intellij.psi.PsiMethod |
| 12 | import org.jetbrains.kotlin.psi.KtFile |
| 13 | import org.jetbrains.uast.UCallExpression |
| 14 | |
| 15 | /** |
| 16 | * Flags a raw `Scaffold` used directly in an app screen. |
| 17 | * |
| 18 | * Two calls are flagged: the Material 3 `androidx.compose.material3.Scaffold` |
| 19 | * and the thin library wrapper `com.vayunmathur.library.ui.Scaffold` (defined in |
| 20 | * `MaterialComponents.kt`). App screens must instead build on a shared scaffold — |
| 21 | * `AppScaffold` / `DetailScaffold` / `DetailLazyColumn` / `ListPage` / |
| 22 | * `LazyListScaffold` / `TabbedPagerScaffold` (or `TopAppBarOverlay` for |
| 23 | * full-bleed) — which own the window insets and content padding once, in one |
| 24 | * place. A raw Scaffold in a screen re-derives that padding by hand, and that is |
| 25 | * where the ad-hoc padding bugs come from. |
| 26 | * |
| 27 | * Scope: only app modules are flagged. The shared library modules |
| 28 | * (`com.vayunmathur.library.*`) are excluded because the shared scaffolds are |
| 29 | * BUILT on the raw Scaffold there. Note this exclusion is intentionally just the |
| 30 | * library prefix, not the broader [LintPackageExclusions] set — games ARE apps |
| 31 | * and should be flagged. |
| 32 | * |
| 33 | * Per-file opt-out: a file carrying the marker comment `// RAW SCAFFOLD EXCEPTION` |
| 34 | * is skipped, for documented cases such as music's nested scaffolds. |
| 35 | */ |
| 36 | class RawScaffoldInAppDetector : Detector(), SourceCodeScanner { |
| 37 | |
| 38 | override fun getApplicableMethodNames(): List<String> = listOf("Scaffold") |
| 39 | |
| 40 | override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) { |
| 41 | val isRawScaffold = |
| 42 | context.evaluator.isMemberInClass(method, MATERIAL3_SCAFFOLD_CLASS) || |
| 43 | context.evaluator.isMemberInClass(method, LIBRARY_SCAFFOLD_CLASS) |
| 44 | if (!isRawScaffold) return |
| 45 | |
| 46 | val packageName = context.uastFile?.packageName ?: return |
| 47 | // App-only rule: skip the shared library modules (where the shared |
| 48 | // scaffolds are built on raw Scaffold), and anything outside our apps. |
| 49 | if (!packageName.startsWith(APP_PACKAGE_PREFIX)) return |
| 50 | if (isLibraryPackage(packageName)) return |
| 51 | |
| 52 | // Documented per-file opt-out. |
| 53 | // Comment-only: a marker inside a string literal must not opt the file out. |
| 54 | val ktFile = context.uastFile?.sourcePsi as? KtFile |
| 55 | if (ktFile != null && CommentOnlyMatcher.containsMarkerInComments(ktFile, EXCEPTION_MARKER)) return |
| 56 | |
| 57 | context.report( |
| 58 | ISSUE, |
| 59 | node, |
| 60 | context.getLocation(node), |
| 61 | "App screens must use a shared scaffold (AppScaffold / DetailScaffold / " + |
| 62 | "DetailLazyColumn / ListPage / LazyListScaffold / TabbedPagerScaffold, or " + |
| 63 | "TopAppBarOverlay for full-bleed) instead of a raw Scaffold, which is where " + |
| 64 | "ad-hoc padding bugs come from. If this screen genuinely needs a raw Scaffold " + |
| 65 | "(e.g. nested scaffolds), add the `$EXCEPTION_MARKER` marker comment to the file.", |
| 66 | ) |
| 67 | } |
| 68 | |
| 69 | companion object { |
| 70 | private const val MATERIAL3_SCAFFOLD_CLASS = "androidx.compose.material3.ScaffoldKt" |
| 71 | private const val LIBRARY_SCAFFOLD_CLASS = "com.vayunmathur.library.ui.MaterialComponentsKt" |
| 72 | |
| 73 | private const val APP_PACKAGE_PREFIX = "com.vayunmathur." |
| 74 | private const val LIBRARY_PREFIX = "com.vayunmathur.library" |
| 75 | |
| 76 | private const val EXCEPTION_MARKER = "// RAW SCAFFOLD EXCEPTION" |
| 77 | |
| 78 | private fun isLibraryPackage(packageName: String): Boolean = |
| 79 | packageName == LIBRARY_PREFIX || packageName.startsWith("$LIBRARY_PREFIX.") |
| 80 | |
| 81 | val ISSUE: Issue = Issue.create( |
| 82 | id = "RawScaffoldInApp", |
| 83 | briefDescription = "Raw Scaffold used in an app screen", |
| 84 | explanation = """ |
| 85 | App screens must not use a raw `Scaffold` — neither \ |
| 86 | `androidx.compose.material3.Scaffold` nor the library wrapper \ |
| 87 | `com.vayunmathur.library.ui.Scaffold`. They must build on a shared scaffold \ |
| 88 | instead: AppScaffold, DetailScaffold, DetailLazyColumn, ListPage, \ |
| 89 | LazyListScaffold, or TabbedPagerScaffold (or TopAppBarOverlay for full-bleed \ |
| 90 | screens). |
| 91 | |
| 92 | The shared scaffolds own window insets and content padding once, in one place. \ |
| 93 | A raw Scaffold in a screen re-derives that padding by hand, and that hand-rolled \ |
| 94 | padding is where the ad-hoc inset/padding bugs come from. |
| 95 | |
| 96 | The shared library modules (com.vayunmathur.library.*) are excluded because the \ |
| 97 | shared scaffolds are built on the raw Scaffold there. If a screen genuinely needs \ |
| 98 | a raw Scaffold (for example music's nested scaffolds), add the marker comment \ |
| 99 | `// RAW SCAFFOLD EXCEPTION: <reason>` to the file to opt it out. |
| 100 | """.trimIndent(), |
| 101 | category = Category.CORRECTNESS, |
| 102 | priority = 6, |
| 103 | severity = Severity.ERROR, |
| 104 | implementation = Implementation( |
| 105 | RawScaffoldInAppDetector::class.java, |
| 106 | Scope.JAVA_FILE_SCOPE, |
| 107 | ), |
| 108 | ) |
| 109 | } |
| 110 | } |