Skip to content

Commit f1a92ad

Browse files
committed
improvement(audits): make baseline updates shrink-only and tolerate biome diagnostic exit codes
1 parent 7b1a67c commit f1a92ad

3 files changed

Lines changed: 64 additions & 22 deletions

File tree

‎scripts/check-explicit-any.ts‎

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
*
1818
* Run: `bun run check:explicit-any`
1919
*/
20-
import { readFileSync, writeFileSync } from 'node:fs'
20+
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
2121
import path from 'node:path'
2222
import { localBin } from './local-bin'
2323

@@ -56,7 +56,8 @@ function collect(): Baseline {
5656
],
5757
{ cwd: ROOT, stdout: 'pipe', stderr: 'pipe' }
5858
)
59-
if (result.exitCode !== 0) {
59+
// Hits are reported as diagnostics, so only a run that produced none counts as a failure.
60+
if (result.exitCode !== 0 && !result.stdout.toString().includes('::')) {
6061
console.error(`biome lint failed:\n${result.stderr.toString()}`)
6162
process.exit(1)
6263
}
@@ -86,21 +87,44 @@ function total(counts: Counts): number {
8687
}
8788

8889
const current = collect()
90+
const baseline: Baseline = existsSync(BASELINE)
91+
? (JSON.parse(readFileSync(BASELINE, 'utf8')) as Baseline)
92+
: current
93+
94+
/** Per-file counts clamped to the baseline, so an update can only lower them. */
95+
function shrunkTo(after: Counts, before: Counts): Counts {
96+
return Object.fromEntries(
97+
Object.entries(after)
98+
.map(([file, count]): [string, number] => [file, Math.min(count, before[file] ?? 0)])
99+
.filter(([, count]) => count > 0)
100+
)
101+
}
89102

90103
if (process.argv.includes('--update')) {
91-
const baseline: Baseline = {
92-
explicitAny: sorted(current.explicitAny),
93-
nonNullAssertion: sorted(current.nonNullAssertion),
104+
const next: Baseline = {
105+
explicitAny: sorted(shrunkTo(current.explicitAny, baseline.explicitAny ?? {})),
106+
nonNullAssertion: sorted(shrunkTo(current.nonNullAssertion, baseline.nonNullAssertion ?? {})),
94107
}
95-
writeFileSync(BASELINE, `${JSON.stringify(baseline, null, 2)}\n`)
108+
writeFileSync(BASELINE, `${JSON.stringify(next, null, 2)}\n`)
96109
console.log(
97-
`Wrote ${path.relative(ROOT, BASELINE)}: ${total(baseline.explicitAny)} explicit any, ` +
98-
`${total(baseline.nonNullAssertion)} non-null assertions`
110+
`Wrote ${path.relative(ROOT, BASELINE)}: ${total(next.explicitAny)} explicit any, ` +
111+
`${total(next.nonNullAssertion)} non-null assertions`
112+
)
113+
const raised = (Object.keys(METRICS) as Metric[]).flatMap((metric) =>
114+
Object.entries(current[metric])
115+
.filter(([file, count]) => count > (baseline[metric]?.[file] ?? 0))
116+
.map(
117+
([file, count]) =>
118+
` ${metric} ${file}: ${count} (baseline ${baseline[metric]?.[file] ?? 0})`
119+
)
99120
)
100-
process.exit(0)
121+
if (raised.length) {
122+
console.error(`✗ refused to raise the baseline for ${raised.length} file(s); fix them instead:`)
123+
console.error(raised.sort().join('\n'))
124+
}
125+
process.exit(raised.length ? 1 : 0)
101126
}
102127

103-
const baseline = JSON.parse(readFileSync(BASELINE, 'utf8')) as Baseline
104128
let regressed = 0
105129
let stale = 0
106130

‎scripts/check-file-names.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* Run: `bun run check:file-names`
3535
*/
3636
import { execFileSync } from 'node:child_process'
37-
import { readFileSync, writeFileSync } from 'node:fs'
37+
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
3838
import path from 'node:path'
3939

4040
const ROOT = path.resolve(import.meta.dir, '..')
@@ -187,14 +187,23 @@ const violations = sourceFiles().flatMap(check)
187187
const byKey = new Map(violations.map((violation) => [key(violation), violation]))
188188
const current = [...byKey.keys()].sort()
189189

190+
const baseline = new Set<string>(
191+
existsSync(BASELINE) ? JSON.parse(readFileSync(BASELINE, 'utf8')) : current
192+
)
193+
const added = current.filter((entry) => !baseline.has(entry))
194+
190195
if (process.argv.includes('--update')) {
191-
writeFileSync(BASELINE, `${JSON.stringify(current, null, 2)}\n`)
192-
console.log(`Wrote ${current.length} baseline entries to ${path.relative(ROOT, BASELINE)}`)
193-
process.exit(0)
196+
// Shrink-only: drop fixed entries, never admit a new one.
197+
const kept = current.filter((entry) => baseline.has(entry))
198+
writeFileSync(BASELINE, `${JSON.stringify(kept, null, 2)}\n`)
199+
console.log(`Wrote ${kept.length} baseline entries to ${path.relative(ROOT, BASELINE)}`)
200+
for (const entry of added) {
201+
const violation = byKey.get(entry) as Violation
202+
console.error(`✗ not baselined — rename: ${violation.file} → ${violation.expected}`)
203+
}
204+
process.exit(added.length ? 1 : 0)
194205
}
195206

196-
const baseline = new Set<string>(JSON.parse(readFileSync(BASELINE, 'utf8')))
197-
const added = current.filter((entry) => !baseline.has(entry))
198207
const currentSet = new Set(current)
199208
const stale = [...baseline].filter((entry) => !currentSet.has(entry))
200209

‎scripts/check-unused-exports.ts‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*
2525
* Run: `bun run check:unused-exports`
2626
*/
27-
import { readFileSync, writeFileSync } from 'node:fs'
27+
import { existsSync, readFileSync, writeFileSync } from 'node:fs'
2828
import path from 'node:path'
2929
import { localBin } from './local-bin'
3030

@@ -112,14 +112,23 @@ if (strict.length) {
112112

113113
const current = [...kindByEntry.keys()].sort()
114114

115+
const baseline = new Set<string>(
116+
existsSync(BASELINE) ? JSON.parse(readFileSync(BASELINE, 'utf8')) : current
117+
)
118+
const added = current.filter((entry) => !baseline.has(entry))
119+
115120
if (process.argv.includes('--update')) {
116-
writeFileSync(BASELINE, `${JSON.stringify(current, null, 2)}\n`)
117-
console.log(`Wrote ${current.length} baseline entries to ${path.relative(ROOT, BASELINE)}`)
118-
process.exit(strict.length ? 1 : 0)
121+
// Shrink-only: drop fixed entries, never admit a new one.
122+
const kept = current.filter((entry) => baseline.has(entry))
123+
writeFileSync(BASELINE, `${JSON.stringify(kept, null, 2)}\n`)
124+
console.log(`Wrote ${kept.length} baseline entries to ${path.relative(ROOT, BASELINE)}`)
125+
if (added.length) {
126+
console.error(`✗ refused to baseline ${added.length} new unused export(s); fix them instead:`)
127+
for (const entry of added) console.error(` ${entry}`)
128+
}
129+
process.exit(strict.length || added.length ? 1 : 0)
119130
}
120131

121-
const baseline = new Set<string>(JSON.parse(readFileSync(BASELINE, 'utf8')))
122-
const added = current.filter((entry) => !baseline.has(entry))
123132
const currentSet = new Set(current)
124133
const stale = [...baseline].filter((entry) => !currentSet.has(entry))
125134

0 commit comments

Comments
 (0)