// The strictness checker. Runs on preprocessed (valid-TS) source of .tsus// files only; plain .ts dependencies are exempt by design.import ts from "typescript";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.", },};export const checkSource = (fileName, code) => { const sourceFile = ts.createSourceFile( fileName, code, ts.ScriptTarget.ES2022, true, ts.ScriptKind.TS, ); const diagnostics = []; const add = (node, code) => { const { line, character } = sourceFile.getLineAndCharacterOfPosition(node.getStart(sourceFile)); diagnostics.push({ code, message: RULES[code].message, hint: RULES[code].hint, file: fileName, line: line + 1, col: character + 1, }); }; 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) { const { line, character } = sourceFile.getLineAndCharacterOfPosition(d.start ?? 0); diagnostics.push({ code: "TSUS000", message: `syntax error: ${ts.flattenDiagnosticMessageText(d.messageText, " ")}`, hint: "The file does not parse as TypeScript after macro expansion.", file: fileName, line: line + 1, col: character + 1, }); } diagnostics.sort((a, b) => a.line - b.line || a.col - b.col); return diagnostics;};