Modern Apps git

← Modern-Apps.git

lint: enforce one widget class per widget file

Commit
fd06e1e91d020d9a753f274a85ef7a907d8e29d8
Author
Vayun Mathur <[email protected]>
When
2026-11-19 03:48 UTC (17 days ago)
Parents
c649198
PackageStructureDetector now checks every file directly in an
app widget root: it must declare a MaWidget/GlanceAppWidget
subclass (not merely its receiver) at top level, so helper-only
files can no longer land in widget/. Widget helpers nest inside
the widget class or its file; non-Glance app helpers belong to
their canonical root.

docs/package-structure.md §3.2 and root AGENTS.md state the
one-widget-one-file rule in the same terms. All twelve widget
files in the tree already satisfy it after the one-file mergers.

Verified: :lint-rules:compileKotlin plus lintDev for calculator,
clock, email, photos, calendar, weather, screentime, and todo
BUILD SUCCESSFUL.

Diff

AGENTS.md
diff --git a/AGENTS.md b/AGENTS.mdindex add3e04fbf..3273d90dd8 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -72,7 +72,7 @@ The backend (relay server for findfamily/everysync/office/voxels) lives outside - Every app with networking has exactly one public `network.Networking` entry point. It owns `NetworkClient`, the narrowest `TrustBundle`, URLs, endpoint paths, auth headers, retries, and status mapping. No package outside `network/` (and `library.network`) may construct raw API requests. - Every native library has exactly one guarded wrapper in `platform/` named `<Feature>Native`; it is the only `System.loadLibrary` caller for that library and exposes availability instead of letting initialization fail. JNI-frozen FQNs are the only package exceptions and must carry the verbatim JNI marker. - Every app has exactly one preferences owner in `data/` named `<App>Prefs`; only it opens the app’s DataStore/SharedPreferences, and it recovers from corruption with documented defaults. Persisted/external decoding goes through non-throwing helpers in `data/` or `domain/`; Room converters never throw.-- Every exported entry point has one validated contract in `intents/`; entry points consume validated values only. Regexes must be Android-ICU-valid (escape literal braces outside quantifiers/character classes). Widgets render from one immutable snapshot and use fallbacks for missing keys/items.+- Every exported entry point has one validated contract in `intents/`; entry points consume validated values only. Regexes must be Android-ICU-valid (escape literal braces outside quantifiers/character classes). Widgets render from one immutable snapshot and use fallbacks for missing keys/items. One widget, one file: every file in `widget/` declares a `MaWidget` subclass at top level together with its receiver; no helper-only files in `widget/` (helpers nest inside the widget; non-Glance app helpers belong to their own root). - Escape hatches verbatim: `// PACKAGE STRUCTURE EXCEPTION (JNI)`, `// RAW SCAFFOLD EXCEPTION: <reason>`, `// TOAST EXCEPTION: <reason>`, `// RAW ANIMATION EXCEPTION: <reason>`. - detekt rules live in `detekt.yml` (ui/domain/odf/service scoping rationale inline). Fix code, not config, to green a module. 
docs/package-structure.md
diff --git a/docs/package-structure.md b/docs/package-structure.mdindex 3c4355f8b2..3044fbeb71 100644--- a/docs/package-structure.md+++ b/docs/package-structure.md@@ -100,6 +100,17 @@ forbidden: boundary. A widget receiver, widget class, action callback, and immutable widget snapshot all belong directly in `widget`. +One widget, one file: each widget lives in exactly one file directly in+`widget`, and that file declares the widget class (a `MaWidget` subclass)+at top level together with its receiver. Every file in `widget` must+declare such a widget class at top level — there are no helper-only+files in `widget`. Widget helper code (snapshot types, action+callbacks, private composables, formatting helpers) is nested inside+the widget class or kept in the same widget file; helpers that serve+app (non-Glance) concerns do not belong in `widget` at all and move to+the canonical root for that concern (`ui`, `data`, `domain`,+`platform`).+ ### 3.3 App-root files  When the base package `com.vayunmathur.<app>` is not empty, it may contain
lint-rules/src/main/java/com/vayunmathur/lint/PackageStructureDetector.kt
diff --git a/lint-rules/src/main/java/com/vayunmathur/lint/PackageStructureDetector.kt b/lint-rules/src/main/java/com/vayunmathur/lint/PackageStructureDetector.ktindex 1052474b99..8c7673d3ce 100644--- a/lint-rules/src/main/java/com/vayunmathur/lint/PackageStructureDetector.kt+++ b/lint-rules/src/main/java/com/vayunmathur/lint/PackageStructureDetector.kt@@ -59,6 +59,18 @@ class PackageStructureDetector : Detector(), SourceCodeScanner {                                 context.getLocation(node),                                 subpackageViolation,                             )+                            return+                        }+                        if (segment == "widget" && segments.size == 1) {+                            val widgetViolation = validateWidgetFile(node, packageName)+                            if (widgetViolation != null) {+                                context.report(+                                    ISSUE,+                                    node,+                                    context.getLocation(node),+                                    widgetViolation,+                                )+                            }                         }                         return                     }@@ -203,6 +215,34 @@ class PackageStructureDetector : Detector(), SourceCodeScanner {             "viewmodels", "widget", "widgets",         ) +        /**+         * One widget, one file: every file directly in the `widget` root must+         * declare a widget class (a `MaWidget` / `GlanceAppWidget` subclass,+         * not merely its receiver) at top level. Helper-only files in+         * `widget` are violations; widget helpers nest inside the widget+         * class or its file, and non-Glance app helpers belong to their+         * own canonical root. See docs/package-structure.md §3.2.+         */+        private fun validateWidgetFile(node: UFile, packageName: String): String? {+            val ktFile = node.sourcePsi as? KtFile ?: return null+            val hasWidgetClass = ktFile.declarations+                .filterIsInstance<KtClass>()+                .any { clazz ->+                    clazz.superTypeListEntries.any { entry ->+                        val text = entry.text+                        text.contains(Regex("""\bMaWidget\s*[<(]""")) ||+                            text.contains(Regex("""\bGlanceAppWidget\s*\("""))+                    }+                }+            if (hasWidgetClass) return null+            return "File in widget package `$packageName` declares no widget class at " ++                "top level. One widget, one file: every file in `widget` must declare " ++                "its `MaWidget` subclass (with its receiver) at top level; helper-only " ++                "files are forbidden — nest helpers inside the widget or move " ++                "non-Glance app helpers to their canonical root. " ++                "See docs/package-structure.md §3.2."+        }+         private fun validateSubpackages(packageName: String, segments: List<String>): String? {             val root = segments.firstOrNull() ?: return null             if (root == "widget" && segments.size > 1) {