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

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

ScaffoldSearchDetector.kt164 lines · 7.8 KiBRaw
1package com.vayunmathur.lint
2
3import com.android.tools.lint.detector.api.Category
4import com.android.tools.lint.detector.api.Detector
5import com.android.tools.lint.detector.api.Implementation
6import com.android.tools.lint.detector.api.Issue
7import com.android.tools.lint.detector.api.JavaContext
8import com.android.tools.lint.detector.api.Scope
9import com.android.tools.lint.detector.api.Severity
10import com.android.tools.lint.detector.api.SourceCodeScanner
11import com.intellij.psi.PsiMethod
12import org.jetbrains.kotlin.psi.KtFile
13import org.jetbrains.uast.UCallExpression
14
15/**
16 * Flags standalone search bars in app UI.
17 *
18 * Screen-level search is owned by the scaffold: `AppScaffold` (and the
19 * wrappers built on it — `DetailScaffold`, `DetailLazyColumn`,
20 * `LazyListScaffold`) take a `search: ScaffoldSearch?` parameter and render
21 * the shared inline filter field AS the top app bar, in its title slot.
22 * Rendering `CommonSearchBar`, `SearchBar`, or `SearchBarInputField`
23 * anywhere in app UI — as a content row, a first lazy item, or hand-built
24 * into a custom top bar's title slot — is flagged here, as is a raw
25 * Material 3 `SearchBar` behind those wrappers.
26 *
27 * TextField-as-search: a `TextField` / `OutlinedTextField` call is flagged
28 * only when it is reliably identifiable as search from its own source —
29 * a string literal containing "search", a `R.string.*search*` /
30 * `UiR.string.*search*` resource reference, or an `IconSearch` leading
31 * icon in the call. Ordinary form fields never match, which is deliberate:
32 * an unreliable guess must not break form screens.
33 *
34 * Scope: app packages only (`com.vayunmathur.*` outside
35 * `com.vayunmathur.library.*`). The shared library is exempt: the scaffolds
36 * and their field are built there (`ListPage`'s internal search included).
37 * TV text fields (`TvTextField`) are a different chrome family and are not
38 * flagged.
39 *
40 * Legitimate non-screen search — a bottom-sheet/drawer-local picker
41 * (launcher), a dialog-local filter (a timezone/ingredient picker), an
42 * expandable SearchBar whose overlay IS the screen's results surface
43 * (photos, youpipe), a map/document/browser overlay with no scaffold —
44 * opts out per file with:
45 * `// SCAFFOLD SEARCH EXCEPTION: <reason>`.
46 */
47class ScaffoldSearchDetector : Detector(), SourceCodeScanner {
48
49 override fun getApplicableMethodNames(): List<String> = SEARCH_CALLS.toList()
50
51 override fun visitMethodCall(context: JavaContext, node: UCallExpression, method: PsiMethod) {
52 if (!isSearchCall(context, node, method)) return
53
54 val packageName = context.uastFile?.packageName ?: return
55 if (!packageName.startsWith(APP_PACKAGE_PREFIX)) return
56 if (isLibraryPackage(packageName)) return
57
58 val ktFile = context.uastFile?.sourcePsi as? KtFile
59 if (ktFile != null && CommentOnlyMatcher.containsMarkerInComments(ktFile, EXCEPTION_MARKER)) return
60
61 context.report(
62 ISSUE,
63 node,
64 context.getLocation(node),
65 "Screen-level search is owned by the scaffold: pass " +
66 "`search = ScaffoldSearch(query, onQueryChange, placeholder)` to " +
67 "AppScaffold / DetailScaffold / DetailLazyColumn / LazyListScaffold " +
68 "instead of rendering `${node.methodName}` in app UI. " +
69 "Dialog/sheet/drawer-local filters and expandable overlay search " +
70 "that genuinely have no scaffold host use the exception marker " +
71 "`$EXCEPTION_MARKER <reason>`.",
72 )
73 }
74
75 private fun isSearchCall(context: JavaContext, node: UCallExpression, method: PsiMethod): Boolean {
76 val name = method.name
77 val containing = method.containingClass?.qualifiedName ?: return false
78 val inScope = containing.startsWith(LIBRARY_PREFIX) || containing.startsWith(MATERIAL3_PREFIX)
79 if (!inScope) return false
80 if (name in DIRECT_SEARCH_CALLS) return true
81 if (name in TEXT_FIELD_CALLS) return looksLikeSearch(node)
82 return false
83 }
84
85 /**
86 * Reliable search identification for a raw text field, from the call's
87 * own source only: a "search" string literal, a `*.string.*search*`
88 * resource reference, or a search leading icon.
89 */
90 private fun looksLikeSearch(node: UCallExpression): Boolean {
91 val source = node.asSourceString()
92 if (source.contains(ICON_SEARCH)) return true
93 if (SEARCH_RESOURCE.containsMatchIn(source)) return true
94 return STRING_LITERAL.findAll(source).any { SEARCH_WORD.containsMatchIn(it.groupValues[1]) }
95 }
96
97 companion object {
98 private const val APP_PACKAGE_PREFIX = "com.vayunmathur."
99 private const val LIBRARY_PREFIX = "com.vayunmathur.library"
100 private const val MATERIAL3_PREFIX = "androidx.compose.material3"
101 private const val ICON_SEARCH = "IconSearch"
102
103 const val EXCEPTION_MARKER = "// SCAFFOLD SEARCH EXCEPTION:"
104
105 private val DIRECT_SEARCH_CALLS = setOf(
106 "CommonSearchBar",
107 "SearchBar",
108 "SearchBarInputField",
109 )
110
111 private val TEXT_FIELD_CALLS = setOf(
112 "TextField",
113 "OutlinedTextField",
114 )
115
116 private val SEARCH_CALLS = DIRECT_SEARCH_CALLS + TEXT_FIELD_CALLS
117
118 // A `search` token at a word/segment start, never embedded in
119 // another word: "Research topic" / `R.string.research_topic` are
120 // ordinary form fields, while "Search stars" / `R.string.search_stars`
121 // (and "Searching…") identify search.
122 private val SEARCH_WORD = Regex("""\bsearch""", RegexOption.IGNORE_CASE)
123 private val SEARCH_RESOURCE =
124 Regex("""\.\s*string\s*\.\s*(?:[A-Za-z0-9]+_)*search""", RegexOption.IGNORE_CASE)
125 private val STRING_LITERAL = Regex("\"([^\"]*)\"")
126
127 private fun isLibraryPackage(packageName: String): Boolean =
128 packageName == LIBRARY_PREFIX || packageName.startsWith("$LIBRARY_PREFIX.")
129
130 val ISSUE: Issue = Issue.create(
131 id = "ScaffoldSearch",
132 briefDescription = "Standalone search bar in app UI; scaffold owns screen-level search",
133 explanation = """
134 Screen-level search belongs to the scaffold object. AppScaffold and \
135 the shared wrappers built on it (DetailScaffold, DetailLazyColumn, \
136 LazyListScaffold) accept a `search: ScaffoldSearch?` parameter and \
137 render the shared inline filter field AS the top app bar, in \
138 its title slot (title replaced), above the refresh box. Rendering a CommonSearchBar, \
139 SearchBar, or SearchBarInputField in app UI is flagged here, \
140 wherever it sits — content row, first lazy item, or a custom top \
141 bar's title slot. A raw TextField/OutlinedTextField is flagged only \
142 when its own source reliably identifies it as search (a "search" \
143 string literal, a `*.string.*search*` resource, or an IconSearch \
144 leading icon); ordinary form fields are never flagged.
145
146 The shared library modules (com.vayunmathur.library.*) are exempt \
147 because the scaffolds and their field are built there. TV text \
148 fields are a different chrome family and are not flagged.
149
150 Dialog/sheet/drawer-local filters, expandable overlay search whose \
151 overlay is the results surface, and other genuinely non-screen \
152 search opt out per file with the marker comment \
153 `// SCAFFOLD SEARCH EXCEPTION: <reason>`.
154 """.trimIndent(),
155 category = Category.CORRECTNESS,
156 priority = 6,
157 severity = Severity.ERROR,
158 implementation = Implementation(
159 ScaffoldSearchDetector::class.java,
160 Scope.JAVA_FILE_SCOPE,
161 ),
162 )
163 }
164}