// Runs every v2 registry example through the v2 checker: each "bad" must be reported with its own// code, each "good" must pass. Part of npm test (via docs.mjs --check).import fs from "node:fs";import path from "node:path";import { fileURLToPath } from "node:url";import { V2_REGISTRY } from "../packages/cli/src/v2/registry.js";import { checkV2 } from "../packages/cli/src/v2/rules.js";export const testV2Registry = () => { const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), ".."); const dir = path.join(repoRoot, ".examples-tmp", "v2-registry"); fs.rmSync(dir, { recursive: true, force: true }); fs.mkdirSync(dir, { recursive: true }); const files = []; for (const rule of V2_REGISTRY) { for (const kind of ["bad", "good"]) { const text = rule[kind]; const named = /^\/\/ file: src\/(\S+)/.exec(text); const sub = path.join(dir, `${rule.code}-${kind}`); fs.mkdirSync(sub, { recursive: true }); const file = path.join(sub, named ? named[1] : "main.ts"); fs.writeFileSync(file, `${text}\nexport {};\n`); files.push({ rule, kind, file }); } } const { diagnostics } = checkV2(dir, { files: files.map((f) => f.file) }); const problems = []; for (const { rule, kind, file } of files) { const rel = path.relative(dir, file); const codes = diagnostics.filter((d) => d.file === rel).map((d) => `${d.code}${d.tsCode ? `/TS${d.tsCode}` : ""} ${d.message}`); if (kind === "bad" && !codes.some((c) => c.startsWith(rule.code))) problems.push(`${rule.code} bad example is not reported as ${rule.code}: ${codes.join("; ") || "no diagnostics"}`); if (kind === "good" && codes.length > 0) problems.push(`${rule.code} good example is rejected: ${codes.join("; ")}`); } return problems;};if (process.argv[1] === fileURLToPath(import.meta.url)) { const problems = testV2Registry(); console.log(problems.length ? problems.join("\n") : "v2 registry examples: all bad ones rejected with their code, all good ones accepted"); process.exit(problems.length ? 1 : 0);}