// Semantic checking: one ts.Program over the whole project, where each .tsus// file is served from memory as its preprocessed TypeScript under the virtual// name `<file>.tsus.ts`. TypeScript's diagnostics are mapped back to .tsus// positions and wrapped in stable codes://// TSUS200 any TypeScript error (tsCode carries the TS code)// TSUS201 a match that misses variants (or a literal match with no _)// TSUS202 a match arm naming a variant / literal the subject cannot hold// TSUS203 a match subject that is not a tagged union / literal type// TSUS301 ? applied to something that is not a Result or Option// TSUS302 ? propagating an Err/None the function's return type cannot holdimport path from "node:path";import ts from "typescript";import { describe } from "./codes.js";export const COMPILER_OPTIONS = { strict: true, noEmit: true, target: ts.ScriptTarget.ES2022, module: ts.ModuleKind.ESNext, moduleResolution: ts.ModuleResolutionKind.Bundler, allowImportingTsExtensions: true, esModuleInterop: true, skipLibCheck: true, lib: ["lib.es2023.d.ts", "lib.dom.d.ts", "lib.dom.iterable.d.ts"],};const MISMATCH_CODES = new Set([2322, 2353, 2561]);const MATCH_CODES = new Set(["TSUS201", "TSUS202", "TSUS203"]);const IMPLICIT_ANY = new Set([7006, 7031]);const virtualName = (file) => `${file}.ts`;// The deepest node whose first token starts exactly at pos.const nodeStartingAt = (sf, pos) => { let found; const visit = (node) => { const start = node.getStart(sf); if (start > pos || node.end <= pos) { return; } if (start === pos) { found = node; } ts.forEachChild(node, visit); }; visit(sf); return found;};// Climbs from node to the enclosing __tsus_match*(subject)(...) call and// returns its subject expression.const matchSubjectOf = (node) => { for (let n = node; n; n = n.parent) { if ( ts.isCallExpression(n) && ts.isCallExpression(n.expression) && ts.isIdentifier(n.expression.expression) && n.expression.expression.text.startsWith("__tsus_match") ) { return n.expression.arguments[0]; } } return undefined;};const tagsOf = (checker, type, at) => { const tags = []; for (const member of type.isUnion() ? type.types : [type]) { const prop = checker.getPropertyOfType(member, "tag"); if (!prop) { continue; } const t = checker.getTypeOfSymbolAtLocation(prop, at); for (const lit of t.isUnion() ? t.types : [t]) { if (lit.isStringLiteral()) { tags.push(lit.value); } } } return tags;};export const semanticCheck = ({ rootDir, tsFiles, compiled }) => { const virtual = new Map(); for (const [file, c] of compiled) { virtual.set(virtualName(file), c); } const host = ts.createCompilerHost(COMPILER_OPTIONS, true); const baseGetSourceFile = host.getSourceFile.bind(host); const baseFileExists = host.fileExists.bind(host); const baseReadFile = host.readFile.bind(host); host.getSourceFile = (fileName, languageVersion, onError, shouldCreate) => { const v = virtual.get(fileName); if (v) { return ts.createSourceFile(fileName, v.code, languageVersion, true, ts.ScriptKind.TS); } return baseGetSourceFile(fileName, languageVersion, onError, shouldCreate); }; host.fileExists = (fileName) => virtual.has(fileName) || baseFileExists(fileName); host.readFile = (fileName) => (virtual.has(fileName) ? virtual.get(fileName).code : baseReadFile(fileName)); const cache = ts.createModuleResolutionCache(rootDir, (f) => host.getCanonicalFileName(f), COMPILER_OPTIONS); host.resolveModuleNameLiterals = (literals, containingFile, redirected, options) => literals.map((literal) => { const spec = literal.text; if (/^\.{1,2}\//.test(spec) && spec.endsWith(".tsus")) { const target = virtualName(path.resolve(path.dirname(containingFile), spec)); return { resolvedModule: virtual.has(target) ? { resolvedFileName: target, extension: ts.Extension.Ts, isExternalLibraryImport: false } : undefined, }; } return ts.resolveModuleName(spec, containingFile, options, host, cache, redirected); }); const roots = [...tsFiles, ...[...compiled.keys()].map(virtualName)]; const program = ts.createProgram({ rootNames: roots, options: COMPILER_OPTIONS, host }); const checker = program.getTypeChecker(); const diagnostics = []; for (const d of [...program.getOptionsDiagnostics(), ...program.getGlobalDiagnostics()]) { diagnostics.push({ code: "TSUS200", tsCode: d.code, message: ts.flattenDiagnosticMessageText(d.messageText, " "), hint: describe("TSUS200").hint, file: "", line: 1, col: 1, }); } for (const file of tsFiles) { const sf = program.getSourceFile(file); if (!sf) { continue; } for (const d of [...program.getSyntacticDiagnostics(sf), ...program.getSemanticDiagnostics(sf)]) { const { line, character } = sf.getLineAndCharacterOfPosition(d.start ?? 0); diagnostics.push({ code: "TSUS200", tsCode: d.code, message: ts.flattenDiagnosticMessageText(d.messageText, " "), hint: describe("TSUS200").hint, file: path.relative(rootDir, file), line: line + 1, col: character + 1, }); } } for (const [file, c] of compiled) { const sf = program.getSourceFile(virtualName(file)); if (!sf) { continue; } const rel = path.relative(rootDir, file); const reportedTry = new Set(); const fileDiagnostics = []; const semantic = [...program.getSemanticDiagnostics(sf)].sort((a, b) => (a.start ?? 0) - (b.start ?? 0)); for (const d of semantic) { const start = d.start ?? 0; const tsMessage = ts.flattenDiagnosticMessageText(d.messageText, " "); const src = c.toSource(start); let code = "TSUS200"; let message = tsMessage; const trySite = c.trySites.find((s) => s.argStart === start || s.returnStart === start); const matchSite = c.matchSites.find( (s) => s.braceStart === src || s.matchStart === src || s.subjectStart === src || s.patterns.some((p) => p.start === src), ); if (trySite && trySite.argStart === start && d.code === 2345) { code = "TSUS301"; message = `${describe(code).message}: ${tsMessage}`; reportedTry.add(trySite); } else if (trySite && trySite.returnStart === start) { if (reportedTry.has(trySite)) { continue; } code = "TSUS302"; message = `${describe(code).message}: ${tsMessage}`; reportedTry.add(trySite); } else if (matchSite) { const node = nodeStartingAt(sf, start); const subject = node ? matchSubjectOf(node) : undefined; const subjectType = subject ? checker.getTypeAtLocation(subject) : undefined; const typeText = subjectType ? checker.typeToString(subjectType) : "the subject"; const pattern = matchSite.patterns.find((p) => p.start === src); if (matchSite.kind === "tag" && src === matchSite.braceStart && d.code === 2345 && subjectType) { const covered = new Set(matchSite.patterns.map((p) => p.name)); const missing = tagsOf(checker, subjectType, subject).filter((t) => !covered.has(t)); if (missing.length > 0) { code = "TSUS201"; message = `non-exhaustive match on ${typeText}: no arm for ${missing.join(", ")}`; } } else if (matchSite.kind === "literal" && src === matchSite.matchStart && d.code === 2554) { code = "TSUS201"; message = `non-exhaustive match on ${typeText}: the literal arms do not cover every value; add a _ arm`; } else if (pattern && MISMATCH_CODES.has(d.code)) { code = "TSUS202"; message = matchSite.kind === "tag" ? `${pattern.name} is not a variant of ${typeText}` : `${pattern.name} is not a possible value of ${typeText}`; } else if (src === matchSite.subjectStart && d.code === 2345 && subject && subject.getStart(sf) === start) { code = "TSUS203"; message = matchSite.kind === "tag" ? `cannot match variants on ${typeText}: it has no string tag field` : `cannot match literals on ${typeText}: literal patterns need a string, number, bigint, or boolean subject`; } } fileDiagnostics.push({ code, tsCode: d.code, message, hint: describe(code).hint, file: rel, src, ...c.locate(start), }); } // Once a match is reported as broken (TSUS201-203), drop the implicit-any // noise TypeScript adds for the arms it could no longer type. const broken = c.matchSites.filter((s) => fileDiagnostics.some((d) => MATCH_CODES.has(d.code) && d.src >= s.matchStart && d.src < s.end), ); for (const { src, ...d } of fileDiagnostics) { const cascade = d.code === "TSUS200" && IMPLICIT_ANY.has(d.tsCode) && broken.some((s) => src >= s.matchStart && src < s.end); if (!cascade) { diagnostics.push(d); } } } return diagnostics;};