1+ /**
2+ * Enforce the dashboard strict allowlist as a monotonic ratchet. When a module
3+ * joins the allowlist, add the same one-line entry to the baseline below in
4+ * that PR; baseline entries must never be removed.
5+ */
16import { spawnSync } from "node:child_process" ;
27import path from "node:path" ;
38import process from "node:process" ;
9+ import { pathToFileURL } from "node:url" ;
410
511const configPath = "tsconfig.dashboard-strict.json" ;
612const tscPath = path . resolve ( "node_modules/typescript/bin/tsc" ) ;
7- const showConfig = runTsc ( [ "-p" , configPath , "--showConfig" ] ) ;
8- if ( showConfig . status !== 0 ) {
9- process . stderr . write ( showConfig . output ) ;
10- process . exit ( 1 ) ;
11- }
1213
13- const config = JSON . parse ( showConfig . stdout ) ;
14- const strictFiles = new Set ( config . files . map ( normalizeFile ) ) ;
15- if (
16- strictFiles . size === 0 ||
17- config . compilerOptions ?. strict !== true ||
18- config . compilerOptions ?. noUncheckedIndexedAccess !== true ||
19- config . compilerOptions ?. exactOptionalPropertyTypes !== true
14+ export const DASHBOARD_STRICT_BASELINE_FILES = Object . freeze ( [
15+ "dashboard/apply-observability.ts" ,
16+ "dashboard/automerge-metrics.ts" ,
17+ "dashboard/dashboard-health.ts" ,
18+ "dashboard/error-safety.ts" ,
19+ "dashboard/exact-review-decision.ts" ,
20+ "dashboard/exact-review-publication-retry.ts" ,
21+ "dashboard/exact-review-queue-shared.ts" ,
22+ "dashboard/exact-review-read-model.ts" ,
23+ "dashboard/github-api.ts" ,
24+ "dashboard/operational-health.ts" ,
25+ "dashboard/review-observability.ts" ,
26+ "dashboard/review-run-telemetry.ts" ,
27+ "dashboard/state-blobs.ts" ,
28+ ] ) ;
29+
30+ export function assertDashboardStrictBaseline (
31+ files ,
32+ baselineFiles = DASHBOARD_STRICT_BASELINE_FILES ,
2033) {
21- throw new Error (
22- "dashboard strict config must enable all strict flags and list at least one file" ,
23- ) ;
34+ const strictFiles = new Set ( files . map ( normalizeFile ) ) ;
35+ const baseline = new Set ( baselineFiles . map ( normalizeFile ) ) ;
36+ const missing = [ ...baseline ] . filter ( ( file ) => ! strictFiles . has ( file ) ) ;
37+ if ( missing . length > 0 ) {
38+ throw new Error ( `dashboard strict config is missing baseline files:\n${ missing . join ( "\n" ) } ` ) ;
39+ }
40+
41+ const unrecorded = [ ...strictFiles ] . filter ( ( file ) => ! baseline . has ( file ) ) ;
42+ if ( unrecorded . length > 0 ) {
43+ throw new Error (
44+ `dashboard strict config has files missing from its baseline:\n${ unrecorded . join ( "\n" ) } ` ,
45+ ) ;
46+ }
2447}
2548
26- const check = runTsc ( [ "-p" , configPath , "--pretty" , "false" ] ) ;
27- if ( check . status === 0 ) process . exit ( 0 ) ;
49+ function main ( ) {
50+ const showConfig = runTsc ( [ "-p" , configPath , "--showConfig" ] ) ;
51+ if ( showConfig . status !== 0 ) {
52+ process . stderr . write ( showConfig . output ) ;
53+ process . exitCode = 1 ;
54+ return ;
55+ }
2856
29- const selected = [ ] ;
30- let includeContinuation = false ;
31- let recognizedDiagnostic = false ;
32- for ( const line of check . output . split ( / (?< = \n ) / ) ) {
33- const match = / ^ ( .+ ?) \( \d + , \d + \) : e r r o r T S \d + : / . exec ( line ) ;
34- if ( match ) {
35- recognizedDiagnostic = true ;
36- includeContinuation = strictFiles . has ( normalizeFile ( match [ 1 ] ) ) ;
37- } else if ( / ^ e r r o r T S \d + : / . test ( line ) ) {
38- recognizedDiagnostic = true ;
39- includeContinuation = true ;
57+ const config = JSON . parse ( showConfig . stdout ) ;
58+ if (
59+ config . compilerOptions ?. strict !== true ||
60+ config . compilerOptions ?. noUncheckedIndexedAccess !== true ||
61+ config . compilerOptions ?. exactOptionalPropertyTypes !== true
62+ ) {
63+ throw new Error ( "dashboard strict config must enable all strict flags" ) ;
4064 }
41- if ( includeContinuation ) selected . push ( line ) ;
42- }
4365
44- if ( selected . length > 0 || ! recognizedDiagnostic ) {
45- process . stderr . write ( selected . length > 0 ? selected . join ( "" ) : check . output ) ;
46- process . exitCode = 1 ;
66+ const configuredFiles = Array . isArray ( config . files ) ? config . files : [ ] ;
67+ assertDashboardStrictBaseline ( configuredFiles ) ;
68+ const strictFiles = new Set ( configuredFiles . map ( normalizeFile ) ) ;
69+
70+ const check = runTsc ( [ "-p" , configPath , "--pretty" , "false" ] ) ;
71+ if ( check . status === 0 ) return ;
72+
73+ const selected = [ ] ;
74+ let includeContinuation = false ;
75+ let recognizedDiagnostic = false ;
76+ for ( const line of check . output . split ( / (?< = \n ) / ) ) {
77+ const match = / ^ ( .+ ?) \( \d + , \d + \) : e r r o r T S \d + : / . exec ( line ) ;
78+ if ( match ) {
79+ recognizedDiagnostic = true ;
80+ includeContinuation = strictFiles . has ( normalizeFile ( match [ 1 ] ) ) ;
81+ } else if ( / ^ e r r o r T S \d + : / . test ( line ) ) {
82+ recognizedDiagnostic = true ;
83+ includeContinuation = true ;
84+ }
85+ if ( includeContinuation ) selected . push ( line ) ;
86+ }
87+
88+ if ( selected . length > 0 || ! recognizedDiagnostic ) {
89+ process . stderr . write ( selected . length > 0 ? selected . join ( "" ) : check . output ) ;
90+ process . exitCode = 1 ;
91+ }
4792}
4893
4994function normalizeFile ( file ) {
@@ -62,3 +107,5 @@ function runTsc(args) {
62107 output : `${ result . stdout } ${ result . stderr } ` ,
63108 } ;
64109}
110+
111+ if ( import . meta. url === pathToFileURL ( process . argv [ 1 ] ?? "" ) . href ) main ( ) ;
0 commit comments