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

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

TodoEvasionDetector.kt127 lines · 5.6 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.Location
10import com.android.tools.lint.detector.api.Scope
11import com.android.tools.lint.detector.api.Severity
12import com.android.tools.lint.detector.api.SourceCodeScanner
13import org.jetbrains.kotlin.psi.KtFile
14import org.jetbrains.uast.UElement
15import org.jetbrains.uast.UFile
16import java.io.File
17
18/**
19 * Catches TODO-marker shapes that evade detekt's `ForbiddenComment` ban.
20 *
21 * detekt matches exact comment substrings, so each of these live shapes slips
22 * past `ForbiddenComment` (`FIXME:`/`STOPSHIP:`/`TODO:`) untouched:
23 * - scoped markers with a parenthesised owner — `TODO(scope): ...`,
24 * `TODO(b/12345): ...`
25 * - bare `TODO` / `FIXME` words in section banners with no colon
26 * (`// ---- TODO / FIXME ----`)
27 *
28 * The base ban stays blocking in detekt; these evasion shapes land here as a
29 * an error like the rest of the ban. Comment-only via
30 * [CommentOnlyMatcher]: an identifier or string literal that happens to
31 * contain `TODO` must not trip this check.
32 */
33class TodoEvasionDetector : Detector(), SourceCodeScanner {
34
35 override fun getApplicableUastTypes(): List<Class<out UElement>> =
36 listOf(UFile::class.java)
37
38 override fun createUastHandler(context: JavaContext): UElementHandler =
39 object : UElementHandler() {
40 override fun visitFile(node: UFile) {
41 val packageName = node.packageName
42 if (packageName.isEmpty()) return
43 if (!packageName.startsWith("com.vayunmathur.")) return
44 if (LintPackageExclusions.isExcluded(packageName)) return
45 // Rule-definition sources carry marker shapes in KDoc as examples
46 // by necessity; scanning them would self-flag.
47 if (ExceptionMarkerFormatDetector.isLintRulesPackage(packageName)) return
48 val ktFile = node.sourcePsi as? KtFile ?: return
49 val source = ktFile.text
50 val file = context.file ?: return
51 for (comment in CommentOnlyMatcher.commentsIn(ktFile)) {
52 val text = comment.text
53 if (BASE_BANNED.any { text.contains(it) }) continue
54 val base = comment.textRange.startOffset
55 val scoped = SCOPED_MARKER.find(text)
56 if (scoped != null) {
57 report(
58 context, node, file, source,
59 base + scoped.range.first, scoped.value.length,
60 "TODO marker with a parenthesised scope (`${scoped.value}`) evades " +
61 "the detekt `ForbiddenComment` ban, which matches `TODO:` exactly. " +
62 "Resolve it or file a tracked task — bare markers are banned " +
63 "repo-wide with no scope carve-out.",
64 )
65 continue
66 }
67 val bare = BARE_MARKER.find(text)
68 if (bare != null) {
69 report(
70 context, node, file, source,
71 base + bare.range.first, bare.value.length,
72 "Bare `${bare.value}` marker with no colon evades the detekt " +
73 "`ForbiddenComment` ban. Resolve it or file a tracked task — " +
74 "bare markers are banned repo-wide.",
75 )
76 }
77 }
78 }
79 }
80
81 private fun report(
82 context: JavaContext,
83 node: UFile,
84 file: File,
85 source: String,
86 start: Int,
87 length: Int,
88 message: String,
89 ) {
90 context.report(
91 ISSUE,
92 node,
93 Location.create(file, source, start, start + length),
94 message,
95 )
96 }
97
98 companion object {
99 /** Already blocked by detekt `ForbiddenComment`; never double-report. */
100 private val BASE_BANNED = listOf("FIXME:", "STOPSHIP:", "TODO:")
101
102 /** `TODO(owner):` / `TODO(b/123):` — scoped markers the ban misses. */
103 private val SCOPED_MARKER = Regex("""\bTODO\([^()\n]*\):?""")
104
105 /** Bare `TODO` / `FIXME` banner words with no colon (and no scope paren). */
106 private val BARE_MARKER = Regex("""\b(?:TODO|FIXME)\b(?!:)(?!\()""")
107
108 val ISSUE: Issue = Issue.create(
109 id = "TodoEvasion",
110 briefDescription = "TODO marker shape evades the marker ban",
111 explanation = """
112 detekt's `ForbiddenComment` ban matches `TODO:`, `FIXME:` and `STOPSHIP:` \
113 exactly, so scoped markers (`TODO(owner):`, `TODO(b/123):`) and bare banner \
114 words (`TODO`, `FIXME` with no colon) slip past it untouched. Those shapes \
115 are banned here too: resolve the item or file a tracked task rather than \
116 leaving an untracked marker in the tree.
117 """.trimIndent(),
118 category = Category.PRODUCTIVITY,
119 priority = 5,
120 severity = Severity.ERROR,
121 implementation = Implementation(
122 TodoEvasionDetector::class.java,
123 Scope.JAVA_FILE_SCOPE,
124 ),
125 )
126 }
127}