aboutsummaryrefslogtreecommitdiff
path: root/scripts
diff options
context:
space:
mode:
authoramankumarsingh77 <amankumarsingh7702@gmail.com>2026-07-28 19:10:30 +0530
committerAman Kumar <145339995+amankumarsingh77@users.noreply.github.com>2026-08-02 19:12:46 +0530
commit0fa62d8177f2216b95739e3d36f87fc1a95886c5 (patch)
treea81b2e461d4cf3a7af7c36d89fa6c9355e3f04bb /scripts
parentf9dcdf7f6dcdf2979693a9f179eda2b9995d57ae (diff)
chore: fail the build on unguarded type assertions
A clean tsc does not prove the absence of casts. This scans src/ for as any, ts-ignore, non-null assertions, inline object casts, and double casts, wired into a new npm run check that CI runs as a single step. The escape hatch is an external allowlist module rather than an inline comment marker: comments are not used in this codebase, and an entry carries a count, so adding a new cast to an already-exempted file still fails until someone raises a number that shows up in the diff. Exactly one exemption is needed today, for the dynamic sharp import. scripts/ was gitignored wholesale, so the two new files are un-ignored individually and everything else dropped there stays ignored. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'scripts')
-rw-r--r--scripts/boundary-allowlist.ts16
-rw-r--r--scripts/check-boundaries.ts105
2 files changed, 121 insertions, 0 deletions
diff --git a/scripts/boundary-allowlist.ts b/scripts/boundary-allowlist.ts
new file mode 100644
index 0000000..be6816f
--- /dev/null
+++ b/scripts/boundary-allowlist.ts
@@ -0,0 +1,16 @@
+export type Exemption = {
+ readonly path: string;
+ readonly rule: string;
+ readonly count: number;
+ readonly reason: string;
+};
+
+export const ALLOWLIST: ReadonlyArray<Exemption> = [
+ {
+ path: "src/util/sharp-shim.ts",
+ rule: "inline-object-cast",
+ count: 1,
+ reason:
+ "sharp is an optional dependency resolved by a dynamic import, so its shape is unknown at compile time; the cast only claims an optional .default and every value read off it is re-checked with typeof before use",
+ },
+];
diff --git a/scripts/check-boundaries.ts b/scripts/check-boundaries.ts
new file mode 100644
index 0000000..5dff96d
--- /dev/null
+++ b/scripts/check-boundaries.ts
@@ -0,0 +1,105 @@
+import { readdirSync, readFileSync, statSync } from "node:fs";
+import { join } from "node:path";
+import { ALLOWLIST, type Exemption } from "./boundary-allowlist";
+
+export type { Exemption };
+
+export type Violation = {
+ readonly path: string;
+ readonly line: number;
+ readonly rule: string;
+ readonly text: string;
+};
+
+const RULES: ReadonlyArray<{ readonly rule: string; readonly re: RegExp }> = [
+ { rule: "as-any", re: /\bas\s+any\b/ },
+ { rule: "ts-ignore", re: /@ts-(ignore|expect-error|nocheck)/ },
+ { rule: "non-null-assertion", re: /[A-Za-z_$)\]]!\s*(?:[.[(;,)\]}]|$)/ },
+ { rule: "inline-object-cast", re: /\bas\s+\{/ },
+ { rule: "double-cast", re: /\bas\s+unknown\s+as\b/ },
+];
+
+export type SourceFile = { readonly path: string; readonly text: string };
+
+const normalise = (path: string): string => path.split("\\").join("/");
+
+const key = (path: string, rule: string): string => `${normalise(path)}\u0000${rule}`;
+
+const budgets = (exemptions: ReadonlyArray<Exemption>): Map<string, number> => {
+ const out = new Map<string, number>();
+ for (const e of exemptions) out.set(key(e.path, e.rule), (out.get(key(e.path, e.rule)) ?? 0) + e.count);
+ return out;
+};
+
+const rawHits = (files: ReadonlyArray<SourceFile>): ReadonlyArray<Violation> => {
+ const out: Violation[] = [];
+ for (const file of files) {
+ for (const [i, line] of file.text.split("\n").entries()) {
+ for (const { rule, re } of RULES) {
+ if (re.test(line)) out.push({ path: file.path, line: i + 1, rule, text: line.trim() });
+ }
+ }
+ }
+ return out;
+};
+
+export const checkSource = (
+ files: ReadonlyArray<SourceFile>,
+ exemptions: ReadonlyArray<Exemption> = ALLOWLIST,
+): ReadonlyArray<Violation> => {
+ const remaining = budgets(exemptions);
+ const out: Violation[] = [];
+ for (const hit of rawHits(files)) {
+ const k = key(hit.path, hit.rule);
+ const budget = remaining.get(k) ?? 0;
+ if (budget > 0) {
+ remaining.set(k, budget - 1);
+ continue;
+ }
+ out.push(hit);
+ }
+ return out;
+};
+
+export const unusedExemptions = (
+ files: ReadonlyArray<SourceFile>,
+ exemptions: ReadonlyArray<Exemption> = ALLOWLIST,
+): ReadonlyArray<Exemption> => {
+ const seen = new Map<string, number>();
+ for (const hit of rawHits(files)) {
+ const k = key(hit.path, hit.rule);
+ seen.set(k, (seen.get(k) ?? 0) + 1);
+ }
+ return exemptions.filter((e) => (seen.get(key(e.path, e.rule)) ?? 0) < e.count);
+};
+
+const walk = (dir: string): ReadonlyArray<string> =>
+ readdirSync(dir)
+ .sort()
+ .flatMap((name) => {
+ const p = join(dir, name);
+ return statSync(p).isDirectory() ? walk(p) : p.endsWith(".ts") ? [p] : [];
+ });
+
+const main = (): void => {
+ const files = walk("src").map((path) => ({ path: normalise(path), text: readFileSync(path, "utf8") }));
+ const violations = checkSource(files);
+ const stale = unusedExemptions(files);
+
+ for (const e of ALLOWLIST) {
+ console.log(`boundary check: exemption ${e.path} [${e.rule}] x${e.count} — ${e.reason}`);
+ }
+ for (const e of stale) {
+ console.log(`boundary check: exemption ${e.path} [${e.rule}] is stale, the code it covered is gone — delete it`);
+ }
+
+ if (violations.length === 0) {
+ console.log(`boundary check: clean (${files.length} files, ${ALLOWLIST.length} exemption(s))`);
+ return;
+ }
+ for (const v of violations) console.error(`${v.path}:${v.line} [${v.rule}] ${v.text}`);
+ console.error(`\nboundary check: ${violations.length} violation(s)`);
+ process.exitCode = 1;
+};
+
+if (process.argv[1]?.endsWith("check-boundaries.ts")) main();