Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / lint-rules / src / main / java / com / vayunmathur / lint / OneComposablePerFileDetector.kt

OneComposablePerFileDetector.kt102 lines · 4.9 KiBRaw
1package com.vayunmathur.lint
2
3import com.android.tools.lint.client.api.UElementHandler
4import com.android.tools.lint.detector.api.Category
5import com.android.tools.lint.detector.api.Detector
6import com.android.tools.lint.detector.api.Implementation
7import com.android.tools.lint.detector.api.Issue
8import com.android.tools.lint.detector.api.JavaContext
9import com.android.tools.lint.detector.api.Scope
10import com.android.tools.lint.detector.api.Severity
11import com.android.tools.lint.detector.api.SourceCodeScanner
12import org.jetbrains.kotlin.lexer.KtTokens
13import org.jetbrains.kotlin.psi.KtNamedFunction
14import org.jetbrains.uast.UElement
15import org.jetbrains.uast.UFile
16
17/**
18 * Flags a file that declares more than one *public* top-level `@Composable`.
19 *
20 * The convention is one public composable — a screen or a component — per file,
21 * under `ui/`, `ui/component/`, or `ui/dialogs/`. Overloads of that same
22 * composable (they share its name) and private helper composables may stay in
23 * the file; a second, differently-named public composable belongs in its own
24 * file. Keeping screens one-to-one with files is what makes the tree navigable
25 * and keeps diffs and imports honest as the app grows.
26 */
27class OneComposablePerFileDetector : Detector(), SourceCodeScanner {
28
29 override fun getApplicableUastTypes(): List<Class<out UElement>> = listOf(UFile::class.java)
30
31 override fun createUastHandler(context: JavaContext): UElementHandler =
32 object : UElementHandler() {
33 override fun visitFile(node: UFile) {
34 // App-screen rule only: skip shared library / e2ee / sdk / games /
35 // tools / personal modules (same exclusion set as PackageStructure).
36 // The library/ui Material+icon shim is many-composables-per-file by
37 // design, and games splits were deliberately deferred.
38 if (LintPackageExclusions.isExcluded(node.packageName)) return
39
40 // Names of public top-level composables already seen in this file.
41 // The first is allowed; each additional *distinct* name is a violation.
42 // Overloads reuse a name, so they never trip the check.
43 val seen = LinkedHashSet<String>()
44 for (clazz in node.classes) {
45 for (method in clazz.methods) {
46 val ktFun = method.sourcePsi as? KtNamedFunction ?: continue
47 if (!ktFun.isTopLevel) continue
48 if (ktFun.isPrivateOrInternal()) continue
49 val isComposable =
50 method.uAnnotations.any { it.qualifiedName == COMPOSABLE }
51 if (!isComposable) continue
52
53 val name = method.name
54 if (name in seen) continue
55 if (seen.isNotEmpty()) {
56 context.report(
57 ISSUE,
58 method,
59 context.getNameLocation(method),
60 "Only one public @Composable is allowed per file. Move " +
61 "`$name` to its own file under ui/, ui/component/, or " +
62 "ui/dialogs/ (overloads of the same composable and " +
63 "private helpers may stay).",
64 )
65 }
66 seen.add(name)
67 }
68 }
69 }
70 }
71
72 private fun KtNamedFunction.isPrivateOrInternal(): Boolean =
73 hasModifier(KtTokens.PRIVATE_KEYWORD) ||
74 hasModifier(KtTokens.INTERNAL_KEYWORD) ||
75 hasModifier(KtTokens.PROTECTED_KEYWORD)
76
77 companion object {
78 private const val COMPOSABLE = "androidx.compose.runtime.Composable"
79
80 val ISSUE: Issue = Issue.create(
81 id = "OneComposablePerFile",
82 briefDescription = "More than one public composable in a file",
83 explanation = """
84 Each file should contain exactly one public @Composable — its screen or \
85 component entry point. Overloads of that same composable and private helper \
86 composables may share the file, but a second, differently-named public \
87 composable belongs in its own file under ui/, ui/component/, or ui/dialogs/.
88
89 Keeping screens one-to-one with files makes the source tree map directly to \
90 the UI, keeps imports and diffs scoped to a single screen, and stops files \
91 from growing into grab-bags of unrelated screens.
92 """.trimIndent(),
93 category = Category.PRODUCTIVITY,
94 priority = 5,
95 severity = Severity.ERROR,
96 implementation = Implementation(
97 OneComposablePerFileDetector::class.java,
98 Scope.JAVA_FILE_SCOPE,
99 ),
100 )
101 }
102}