check-safety/check_anyusage.go
Alex Dunmow c4d01de82c feat: concise output + diff-scoped any check
Reporter is now a collector: checks declare a verdict (OK/Skip/Warn/Fail/
Fatal) plus findings, and a single central render() prints output. Default
output is silent on pass/skip — only FAIL/WARN/ERR checks print, followed by
one tally line, so a clean run is two lines. --verbose restores full per-check
output. All ~30 checks were converted to this API; orphaned guidance/label
helpers (printPerTargetOKLines, per-check *Help blocks, colors_format.go) were
removed.

The any-usage check (2e) now defaults to only the unstaged working-tree diff
(changed lines), via a new per-repo git-diff index in changedlines.go; --all-any
restores the full scan. Not-a-git-repo / no-diff warns on nothing.

Golden fixtures regenerated; integration tests updated to the new format; added
unit tests for the diff index.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-04 01:16:56 +08:00

65 lines
1.8 KiB
Go

package main
func init() {
register(Check{
Seq: 24,
ID: "2e",
Title: "Warn on any usage in Go and TypeScript",
Run: func(ctx *ScanContext, rep *Reporter) {
// By default only warn on `any` introduced in the unstaged working-tree
// diff; --all-any scans every source file.
var keep lineKeeper
if !ctx.allAny {
idx := newDiffIndex()
keep = idx.isChanged
}
var anyWarnings []anyUsageWarning
for _, target := range ctx.backendTargets {
for _, w := range checkGoAnyUsage(target.root, keep) {
w.file = prefixDisplayPath(target.displayOrRoot(), w.file)
anyWarnings = append(anyWarnings, w)
}
}
for _, target := range ctx.pluginTargets {
for _, w := range checkGoAnyUsage(target.root, keep) {
w.file = prefixDisplayPath(target.display, w.file)
anyWarnings = append(anyWarnings, w)
}
}
for _, target := range collectESLintDirectiveTargets(ctx.repoRoot, ctx.frontendTargets) {
for _, w := range checkTypeScriptAnyUsage(target.dir, keep) {
w.file = prefixDisplayPath(target.label, w.file)
anyWarnings = append(anyWarnings, w)
}
}
if len(anyWarnings) == 0 {
if ctx.allAny {
rep.OK("no any usage found")
} else {
rep.OK("no any usage in changed lines")
}
return
}
scope := "any usage"
if !ctx.allAny {
scope = "any usage in changed lines"
}
limit := min(len(anyWarnings), maxAnyWarningsPrinted)
extra := ""
if !ctx.allAny {
extra = ", --all-any to scan all"
}
rep.Warn("%d %s (showing %d%s)", len(anyWarnings), scope, limit, extra)
for _, w := range anyWarnings[:limit] {
rep.Findingf("%s:%d [%s] %s", w.file, w.line, w.lang, w.snippet)
}
if len(anyWarnings) > limit {
rep.Findingf("... %d more omitted", len(anyWarnings)-limit)
}
},
})
}