// The strictness checker. Runs on preprocessed (valid-TS) source of .tsus// files only; plain .ts dependencies are exempt by design.import ts from "typescript";import { runRules } from "./rules/index.js";const RULES = { TSUS001: { message: "class declarations are banned in .tsus files", hint: "Use plain object types and functions.", }, TSUS002: { message: "null is banned in .tsus files", hint: "Use Option<T> from @tsus/runtime, or fromNullable at the boundary.", }, TSUS003: { message: "undefined is banned in .tsus files", hint: "Use Option<T> from @tsus/runtime, or fromNullable at the boundary.", }, TSUS004: { message: "var is banned in .tsus files", hint: "Use const (preferred) or let.", }, TSUS005: { message: "throw is banned in .tsus files", hint: "Return Result<T, E> from @tsus/runtime, or wrapSync/wrapAsync at the boundary.", },};// `locate(offset)` maps an offset in `code` to a 1-based { line, col } in the// original .tsus file; without it, positions are reported in `code` itself.// options (all optional) feed the extended rules in ./rules: original (the// .tsus text), toSource (offset map from code to original), path (absolute// file path, enables cross-file rules), program (for type-aware rules).export const checkSource = (fileName, code, locate, options = {}) => { const sourceFile = ts.createSourceFile( fileName, code, ts.ScriptTarget.ES2022, true, ts.ScriptKind.TS, ); const diagnostics = []; const where = (offset) => { if (locate) { return locate(offset); } const { line, character } = sourceFile.getLineAndCharacterOfPosition(offset); return { line: line + 1, col: character + 1 }; }; const add = (node, code) => { diagnostics.push({ code, message: RULES[code].message, hint: RULES[code].hint, file: fileName, at: node.getStart(sourceFile), ...where(node.getStart(sourceFile)), }); }; const visit = (node) => { if (ts.isClassDeclaration(node) || ts.isClassExpression(node)) { add(node, "TSUS001"); } if (node.kind === ts.SyntaxKind.NullKeyword) { add(node, "TSUS002"); } if (ts.isIdentifier(node) && node.text === "undefined") { add(node, "TSUS003"); } if ( ts.isVariableDeclarationList(node) && (node.flags & (ts.NodeFlags.Let | ts.NodeFlags.Const)) === 0 ) { add(node, "TSUS004"); } if (ts.isThrowStatement(node)) { add(node, "TSUS005"); } ts.forEachChild(node, visit); }; visit(sourceFile); const parseErrors = sourceFile.parseDiagnostics ?? []; for (const d of parseErrors) { diagnostics.push({ code: "TSUS000", message: `syntax error: ${ts.flattenDiagnosticMessageText(d.messageText, " ")}`, hint: "The file does not parse as TypeScript after macro expansion.", tsCode: d.code, file: fileName, at: d.start ?? 0, ...where(d.start ?? 0), }); } // Hook: the extended rule set in ./rules adds its diagnostics and fixes. return runRules({ ...options, fileName, code, sourceFile, legacy: diagnostics });};