Compiler foundation: scanner-based tokenizer, typed match, semantic check, source maps, ? operator
- tokens.js: ts.createScanner with regex and template rescans
- preprocess.js: match lowers to __tsus_match(subject)({ Tag: arm }) (plus
_or and _lit forms), await detected on the AST; ? lowers to hoisted early
returns found through the parser's recovery of 'expr?'
- runtime: Option/Result as tagged unions; mapped-type arms enforce
exhaustiveness and narrow payloads; ? helpers
- program.js: ts.createProgram over a virtual host mapping .tsus to lowered
TS; TS errors mapped back and wrapped as TSUS2xx/TSUS30x with tsCode
- build emits .js.map pointing at the .tsus source
- example 16: pin readFile's utf8 overload (real type error the checker found)
- runner: optional exact diagnostic positions in example.json
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Changed files
18 files changed1,729 additions397 deletionsagainst 8396947
- Changedexamples/16-boundary-fs/src/main.tsus+5−3
- Changedpackage-lock.json+20−0
- Changedpackage.json+3−0
- Changedpackages/cli/bin/tsus.js+2−1
- Changedpackages/cli/package.json+2−0
- Changedpackages/cli/src/build.js+34−49
- Changedpackages/cli/src/check.js+14−7
- Addedpackages/cli/src/codes.js+49−0
- Addedpackages/cli/src/compile.js+129−0
- Changedpackages/cli/src/preprocess.js+672−268
- Addedpackages/cli/src/program.js+244−0
- Addedpackages/cli/src/sourcemap.js+193−0
- Addedpackages/cli/src/tokens.js+142−0
- Changedpackages/plugin/core.js+21−29
- Changedpackages/plugin/webpack.cjs+21−0
- Changedpackages/runtime/index.d.ts+101−13
- Changedpackages/runtime/index.js+65−27
- Changedscripts/run-examples.mjs+12−0
Changedexamples/16-boundary-fs/src/main.tsus+5−3
| @@ -1,13 +1,15 @@ | ||
| import { readFile } from "node:fs/promises"; | ||
| import { wrapAsync } from "@tsus/runtime/boundary"; | ||
| -const safeRead = wrapAsync(readFile); | ||
| +// readFile is overloaded (string or Buffer); pin the utf8 overload so the | ||
| +// Ok payload is typed as string. The semantic checker caught the old version. | ||
| +const safeRead = wrapAsync((file: string) => readFile(file, "utf8")); | ||
| const here = new URL("data.txt", import.meta.url).pathname; | ||
| const nowhere = new URL("missing.txt", import.meta.url).pathname; | ||
| -const found = await safeRead(here, "utf8"); | ||
| -const missing = await safeRead(nowhere, "utf8"); | ||
| +const found = await safeRead(here); | ||
| +const missing = await safeRead(nowhere); | ||
| console.log(found.map((text) => text.trim()).unwrapOr("<read failed>")); | ||
| console.log(missing.map((text) => text.trim()).unwrapOr("<read failed>")); | ||
Changedpackage-lock.json+20−0
| @@ -11,6 +11,9 @@ | ||
| "dependencies": { | ||
| "typescript": "^5.5.0", | ||
| "zod": "^3.23.0" | ||
| + }, | ||
| + "devDependencies": { | ||
| + "@types/node": "^22.20.5" | ||
| } | ||
| }, | ||
| "node_modules/@tsus/cli": { | ||
| @@ -25,6 +28,16 @@ | ||
| "resolved": "packages/runtime", | ||
| "link": true | ||
| }, | ||
| + "node_modules/@types/node": { | ||
| + "version": "22.20.5", | ||
| + "resolved": "https://registry.npmjs.org/@types/node/-/node-22.20.5.tgz", | ||
| + "integrity": "sha512-U2+DNr+wSjpsTS/wZGYHq7GcwfuSmKiKvoPvK22zwTlRhU91yOniN4qRR5KhIjvif7ysw/dz/hKmfDH0Ris4aA==", | ||
| + "dev": true, | ||
| + "license": "MIT", | ||
| + "dependencies": { | ||
| + "undici-types": "~6.21.0" | ||
| + } | ||
| + }, | ||
| "node_modules/typescript": { | ||
| "version": "5.9.3", | ||
| "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.9.3.tgz", | ||
| @@ -37,6 +50,13 @@ | ||
| "engines": { | ||
| "node": ">=14.17" | ||
| } | ||
| + }, | ||
| + "node_modules/undici-types": { | ||
| + "version": "6.21.0", | ||
| + "resolved": "https://registry.npmjs.org/undici-types/-/undici-types-6.21.0.tgz", | ||
| + "integrity": "sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==", | ||
| + "dev": true, | ||
| + "license": "MIT" | ||
| }, | ||
| "node_modules/zod": { | ||
| "version": "3.25.76", | ||
Changedpackage.json+3−0
| @@ -11,5 +11,8 @@ | ||
| "dependencies": { | ||
| "typescript": "^5.5.0", | ||
| "zod": "^3.23.0" | ||
| + }, | ||
| + "devDependencies": { | ||
| + "@types/node": "^22.20.5" | ||
| } | ||
| } | ||
Changedpackages/cli/bin/tsus.js+2−1
| @@ -19,7 +19,8 @@ | ||
| const renderHuman = (diagnostics) => { | ||
| for (const d of diagnostics) { | ||
| - console.error(`error ${d.code}: ${d.message}`); | ||
| + const ts = d.tsCode ? ` [TS${d.tsCode}]` : ""; | ||
| + console.error(`error ${d.code}${ts}: ${d.message}`); | ||
| console.error(` --> ${d.file}:${d.line}:${d.col}`); | ||
| console.error(` hint: ${d.hint}`); | ||
| } | ||
Changedpackages/cli/package.json+2−0
| @@ -8,6 +8,8 @@ | ||
| "exports": { | ||
| "./preprocess": "./src/preprocess.js", | ||
| "./check": "./src/check.js", | ||
| + "./compile": "./src/compile.js", | ||
| + "./program": "./src/program.js", | ||
| "./build": "./src/build.js" | ||
| }, | ||
| "dependencies": { | ||
Changedpackages/cli/src/build.js+34−49
| @@ -1,9 +1,9 @@ | ||
| -// tsus build: src/**/*.{ts,tsus} -> dist/**/*.js (plain ESM). | ||
| +// tsus build: src/**/*.{ts,tsus} -> dist/**/*.js (plain ESM) + source maps. | ||
| +// tsus check: the same pipeline without emitting. | ||
| import fs from "node:fs"; | ||
| import path from "node:path"; | ||
| -import ts from "typescript"; | ||
| -import { preprocess, PreprocessError } from "./preprocess.js"; | ||
| -import { checkSource } from "./check.js"; | ||
| +import { compileTsus, emitJs, relativeSourcePath } from "./compile.js"; | ||
| +import { semanticCheck } from "./program.js"; | ||
| const walk = (dir) => { | ||
| const out = []; | ||
| @@ -18,15 +18,6 @@ | ||
| return out; | ||
| }; | ||
| -// Rewrite relative import/export specifiers ending in .tsus or .ts to .js. | ||
| -const rewriteSpecifiers = (code) => | ||
| - code.replace( | ||
| - /(\b(?:from|import)\s*\(?\s*)(["'])(\.{1,2}\/[^"']*?)\.(?:tsus|ts)(\2)/g, | ||
| - (_m, lead, quote, spec) => `${lead}${quote}${spec}.js${quote}`, | ||
| - ); | ||
| - | ||
| -const RUNTIME_IMPORT = `import { __tsus_match } from "@tsus/runtime";\n`; | ||
| - | ||
| export const collectSources = (rootDir) => { | ||
| const srcDir = path.join(rootDir, "src"); | ||
| if (!fs.existsSync(srcDir)) { | ||
| @@ -35,39 +26,36 @@ | ||
| return { srcDir, files: walk(srcDir) }; | ||
| }; | ||
| -// Preprocess + check every .tsus file. Returns diagnostics (empty = clean). | ||
| -export const checkProject = (rootDir) => { | ||
| +const isTsFile = (file) => file.endsWith(".ts"); | ||
| + | ||
| +// Syntax-level failures make the lowered code meaningless to type-check, so | ||
| +// the semantic pass only runs once every file preprocesses and parses. | ||
| +const BLOCKING = new Set(["TSUS000", "TSUS006"]); | ||
| + | ||
| +// Preprocess + rule-check every .tsus file, then type-check the whole | ||
| +// project. Returns diagnostics (empty = clean) and the per-file compilations. | ||
| +export const checkProject = (rootDir, { semantic = true } = {}) => { | ||
| const { srcDir, files } = collectSources(rootDir); | ||
| const diagnostics = []; | ||
| + const compiled = new Map(); | ||
| for (const file of files) { | ||
| if (!file.endsWith(".tsus")) { | ||
| continue; | ||
| } | ||
| const rel = path.relative(rootDir, file); | ||
| - const source = fs.readFileSync(file, "utf8"); | ||
| - try { | ||
| - const { code } = preprocess(source); | ||
| - diagnostics.push(...checkSource(rel, code)); | ||
| - } catch (error) { | ||
| - if (error instanceof PreprocessError) { | ||
| - diagnostics.push({ | ||
| - code: error.code, | ||
| - message: `match macro error: ${error.message}`, | ||
| - hint: "Arms look like: Pattern(binding) => expression, separated by commas.", | ||
| - file: rel, | ||
| - line: 1, | ||
| - col: 1, | ||
| - }); | ||
| - } else { | ||
| - throw error; | ||
| - } | ||
| - } | ||
| + const c = compileTsus(fs.readFileSync(file, "utf8"), rel); | ||
| + compiled.set(file, c); | ||
| + diagnostics.push(...c.diagnostics); | ||
| } | ||
| - return { srcDir, files, diagnostics }; | ||
| + if (semantic && !diagnostics.some((d) => BLOCKING.has(d.code))) { | ||
| + diagnostics.push(...semanticCheck({ rootDir, tsFiles: files.filter(isTsFile), compiled })); | ||
| + } | ||
| + diagnostics.sort((a, b) => a.file.localeCompare(b.file) || a.line - b.line || a.col - b.col); | ||
| + return { srcDir, files, diagnostics, compiled }; | ||
| }; | ||
| export const buildProject = (rootDir) => { | ||
| - const { srcDir, files, diagnostics } = checkProject(rootDir); | ||
| + const { srcDir, files, diagnostics, compiled } = checkProject(rootDir); | ||
| if (diagnostics.length > 0) { | ||
| return { diagnostics, emitted: [] }; | ||
| } | ||
| @@ -85,22 +73,19 @@ | ||
| emitted.push(dest); | ||
| continue; | ||
| } | ||
| - let tsCode = fs.readFileSync(file, "utf8"); | ||
| - if (isTsus) { | ||
| - const { code, changed } = preprocess(tsCode); | ||
| - tsCode = changed ? RUNTIME_IMPORT + code : code; | ||
| - } | ||
| - tsCode = rewriteSpecifiers(tsCode); | ||
| - const output = ts.transpileModule(tsCode, { | ||
| - compilerOptions: { | ||
| - module: ts.ModuleKind.ESNext, | ||
| - target: ts.ScriptTarget.ES2022, | ||
| - }, | ||
| - }).outputText; | ||
| const dest = path.join(distDir, rel.replace(/\.(tsus|ts)$/, ".js")); | ||
| + const sourceText = isTsus ? compiled.get(file).source : fs.readFileSync(file, "utf8"); | ||
| + const { js, map } = emitJs({ | ||
| + code: isTsus ? compiled.get(file).code : sourceText, | ||
| + toSource: isTsus ? compiled.get(file).toSource : (o) => o, | ||
| + sourceText, | ||
| + sourcePath: relativeSourcePath(dest, file), | ||
| + outName: path.basename(dest), | ||
| + }); | ||
| fs.mkdirSync(path.dirname(dest), { recursive: true }); | ||
| - fs.writeFileSync(dest, output); | ||
| - emitted.push(dest); | ||
| + fs.writeFileSync(dest, js); | ||
| + fs.writeFileSync(`${dest}.map`, `${JSON.stringify(map)}\n`); | ||
| + emitted.push(dest, `${dest}.map`); | ||
| } | ||
| return { diagnostics: [], emitted }; | ||
| }; | ||
Changedpackages/cli/src/check.js+14−7
| @@ -25,7 +25,9 @@ | ||
| }, | ||
| }; | ||
| -export const checkSource = (fileName, code) => { | ||
| +// `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. | ||
| +export const checkSource = (fileName, code, locate) => { | ||
| const sourceFile = ts.createSourceFile( | ||
| fileName, | ||
| code, | ||
| @@ -35,15 +37,21 @@ | ||
| ); | ||
| 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) => { | ||
| - 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, | ||
| + ...where(node.getStart(sourceFile)), | ||
| }); | ||
| }; | ||
| @@ -72,14 +80,13 @@ | ||
| 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.", | ||
| + tsCode: d.code, | ||
| file: fileName, | ||
| - line: line + 1, | ||
| - col: character + 1, | ||
| + ...where(d.start ?? 0), | ||
| }); | ||
| } | ||
Addedpackages/cli/src/codes.js+49−0
| @@ -0,0 +1,49 @@ | ||
| +// Stable diagnostic codes owned by the compiler (preprocessor + semantic | ||
| +// pass). The strictness rules (TSUS001-005) live next to their checks in | ||
| +// check.js. TSUS2xx wrap a TypeScript diagnostic and always carry its numeric | ||
| +// code as `tsCode`. | ||
| + | ||
| +export const CODES = { | ||
| + TSUS000: { | ||
| + message: "syntax error", | ||
| + hint: "The file does not parse as TypeScript after macro expansion.", | ||
| + }, | ||
| + TSUS006: { | ||
| + message: "malformed match expression", | ||
| + hint: "Arms look like: Variant(binding) => expression, \"literal\" => expression, or _ => expression, separated by commas.", | ||
| + }, | ||
| + TSUS200: { | ||
| + message: "type error", | ||
| + hint: "The original TypeScript diagnostic is in tsCode and the message.", | ||
| + }, | ||
| + TSUS201: { | ||
| + message: "non-exhaustive match", | ||
| + hint: "Add an arm for every missing variant, or a _ arm to cover the rest.", | ||
| + }, | ||
| + TSUS202: { | ||
| + message: "match arm does not fit the subject", | ||
| + hint: "Each pattern must be a variant tag (or literal value) the subject's type can actually hold.", | ||
| + }, | ||
| + TSUS203: { | ||
| + message: "match subject is not matchable", | ||
| + hint: "Variant patterns need a tagged union (a `tag` field); literal patterns need a string, number, bigint, or boolean subject.", | ||
| + }, | ||
| + TSUS300: { | ||
| + message: "? used outside a function that returns Result or Option", | ||
| + hint: "Declare the enclosing function's return type as Result<T, E>, Option<T>, or Promise<...> of one; ? cannot be used at top level or inside match arms, which are closures.", | ||
| + }, | ||
| + TSUS301: { | ||
| + message: "? applied to a value that is not a Result or Option", | ||
| + hint: "Only Result and Option values can be propagated with ?. In async code, await first: `const x = await load()?;`.", | ||
| + }, | ||
| + TSUS302: { | ||
| + message: "? propagates a value the function cannot return", | ||
| + hint: "An Err must match the function's error type, and None can only propagate out of a function returning Option. Convert first (mapErr, okOr).", | ||
| + }, | ||
| + TSUS303: { | ||
| + message: "? in a position that may not run", | ||
| + hint: "? is lowered to an early return before the statement, so it cannot sit in a ?: branch, the right side of && || ??, or a loop condition. Move it into its own statement.", | ||
| + }, | ||
| +}; | ||
| + | ||
| +export const describe = (code) => CODES[code] ?? { message: code, hint: "" }; | ||
Addedpackages/cli/src/compile.js+129−0
| @@ -0,0 +1,129 @@ | ||
| +// Per-file compilation shared by `tsus build`, `tsus check`, and the bundler | ||
| +// plugins: preprocess, run the strictness rules, and emit JS with a source | ||
| +// map that points at the original .tsus file. | ||
| +import path from "node:path"; | ||
| +import ts from "typescript"; | ||
| +import { preprocess } from "./preprocess.js"; | ||
| +import { checkSource } from "./check.js"; | ||
| +import { lineStarts, positionOf, decodeMappings, encodeMappings } from "./sourcemap.js"; | ||
| + | ||
| +// Preprocess one .tsus source and run the syntactic rules over the result. | ||
| +// Every diagnostic carries a 1-based line/col in the .tsus file. | ||
| +export const compileTsus = (source, fileName) => { | ||
| + const pre = preprocess(source); | ||
| + const starts = lineStarts(source); | ||
| + const locateSource = (offset) => { | ||
| + const { line, character } = positionOf(starts, offset); | ||
| + return { line: line + 1, col: character + 1 }; | ||
| + }; | ||
| + const locate = (offset) => locateSource(pre.toSource(offset)); | ||
| + const diagnostics = [ | ||
| + ...pre.diagnostics.map(({ offset, ...d }) => ({ ...d, file: fileName, ...locateSource(offset) })), | ||
| + ...checkSource(fileName, pre.code, locate), | ||
| + ]; | ||
| + return { ...pre, source, fileName, locate, locateSource, diagnostics }; | ||
| +}; | ||
| + | ||
| +const rewriteSpecifier = (spec) => | ||
| + /^\.{1,2}\//.test(spec) && /\.(tsus|ts)$/.test(spec) && !spec.endsWith(".d.ts") | ||
| + ? spec.replace(/\.(tsus|ts)$/, ".js") | ||
| + : spec; | ||
| + | ||
| +// TS transformer: relative `.tsus`/`.ts` import specifiers become `.js`, so | ||
| +// the emitted dist tree resolves on its own. Done on the AST rather than the | ||
| +// text so source map columns stay right. | ||
| +const specifierTransformer = (context) => { | ||
| + const f = context.factory; | ||
| + const visit = (node) => { | ||
| + if (ts.isImportDeclaration(node) && ts.isStringLiteral(node.moduleSpecifier)) { | ||
| + const next = rewriteSpecifier(node.moduleSpecifier.text); | ||
| + if (next !== node.moduleSpecifier.text) { | ||
| + return f.updateImportDeclaration( | ||
| + node, | ||
| + node.modifiers, | ||
| + node.importClause, | ||
| + f.createStringLiteral(next), | ||
| + node.attributes, | ||
| + ); | ||
| + } | ||
| + } | ||
| + if (ts.isExportDeclaration(node) && node.moduleSpecifier && ts.isStringLiteral(node.moduleSpecifier)) { | ||
| + const next = rewriteSpecifier(node.moduleSpecifier.text); | ||
| + if (next !== node.moduleSpecifier.text) { | ||
| + return f.updateExportDeclaration( | ||
| + node, | ||
| + node.modifiers, | ||
| + node.isTypeOnly, | ||
| + node.exportClause, | ||
| + f.createStringLiteral(next), | ||
| + node.attributes, | ||
| + ); | ||
| + } | ||
| + } | ||
| + if ( | ||
| + ts.isCallExpression(node) && | ||
| + node.expression.kind === ts.SyntaxKind.ImportKeyword && | ||
| + node.arguments.length > 0 && | ||
| + ts.isStringLiteral(node.arguments[0]) | ||
| + ) { | ||
| + const next = rewriteSpecifier(node.arguments[0].text); | ||
| + if (next !== node.arguments[0].text) { | ||
| + return f.updateCallExpression(node, node.expression, node.typeArguments, [ | ||
| + f.createStringLiteral(next), | ||
| + ...node.arguments.slice(1), | ||
| + ]); | ||
| + } | ||
| + } | ||
| + return ts.visitEachChild(node, visit, context); | ||
| + }; | ||
| + return (sf) => ts.visitNode(sf, visit); | ||
| +}; | ||
| + | ||
| +const COMPILER_OPTIONS = { | ||
| + module: ts.ModuleKind.ESNext, | ||
| + target: ts.ScriptTarget.ES2022, | ||
| + sourceMap: true, | ||
| + inlineSources: false, | ||
| +}; | ||
| + | ||
| +// Emit JS for already-lowered TypeScript `code`. `toSource` maps offsets in | ||
| +// `code` back to `sourceText` (identity for plain .ts). Returns the JS (with | ||
| +// a sourceMappingURL comment unless `inlineMap`) and a v3 source map whose | ||
| +// single source is `sourcePath` (relative to the output file). | ||
| +export const emitJs = ({ code, toSource, sourceText, sourcePath, outName, rewriteImports = true, inlineMap = false }) => { | ||
| + const out = ts.transpileModule(code, { | ||
| + compilerOptions: COMPILER_OPTIONS, | ||
| + fileName: "module.ts", | ||
| + transformers: rewriteImports ? { before: [specifierTransformer] } : undefined, | ||
| + }); | ||
| + const js = out.outputText.replace(/\n?\/\/# sourceMappingURL=.*\s*$/, "\n"); | ||
| + const raw = JSON.parse(out.sourceMapText); | ||
| + const genStarts = lineStarts(code); | ||
| + const srcStarts = lineStarts(sourceText); | ||
| + const lines = decodeMappings(raw.mappings).map((segs) => | ||
| + segs.map((seg) => { | ||
| + if (seg.length < 4) { | ||
| + return seg; | ||
| + } | ||
| + const line = Math.min(seg[2], genStarts.length - 1); | ||
| + const offset = Math.min(genStarts[line] + seg[3], code.length); | ||
| + const pos = positionOf(srcStarts, toSource(offset)); | ||
| + return [seg[0], 0, pos.line, pos.character]; | ||
| + }), | ||
| + ); | ||
| + const map = { | ||
| + version: 3, | ||
| + file: outName, | ||
| + sources: [sourcePath], | ||
| + sourcesContent: [sourceText], | ||
| + names: [], | ||
| + mappings: encodeMappings(lines), | ||
| + }; | ||
| + const comment = inlineMap | ||
| + ? `//# sourceMappingURL=data:application/json;base64,${Buffer.from(JSON.stringify(map)).toString("base64")}` | ||
| + : `//# sourceMappingURL=${outName}.map`; | ||
| + return { js: `${js}${comment}\n`, map }; | ||
| +}; | ||
| + | ||
| +export const relativeSourcePath = (fromFile, toFile) => | ||
| + path.relative(path.dirname(fromFile), toFile).split(path.sep).join("/"); | ||
Changedpackages/cli/src/preprocess.js+672−268
This diff has 1,007 lines, more than this page shows. View the file or the raw file.
Addedpackages/cli/src/program.js+244−0
| @@ -0,0 +1,244 @@ | ||
| +// 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 hold | ||
| +import 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; | ||
| +}; | ||
Addedpackages/cli/src/sourcemap.js+193−0
| @@ -0,0 +1,193 @@ | ||
| +// Position bookkeeping for the preprocessor. | ||
| +// | ||
| +// Every lowering pass writes its output through an emitter that records, for | ||
| +// each run of output text, where it came from in the input: | ||
| +// | ||
| +// - copy(text, srcStart): the text is a verbatim slice of the input starting at | ||
| +// srcStart, so every offset inside maps exactly. | ||
| +// - synth(text, anchor): generated glue (helper names, arrows, temporaries); | ||
| +// every offset inside maps to the single input offset `anchor`. | ||
| +// | ||
| +// Passes chain (tsus -> match-lowered -> try-lowered -> with runtime import), | ||
| +// so an output offset is mapped back by composing each pass's lookup. | ||
| + | ||
| +export const createEmitter = () => { | ||
| + let out = ""; | ||
| + const segments = []; | ||
| + const push = (text, src, exact) => { | ||
| + if (text.length === 0) { | ||
| + return; | ||
| + } | ||
| + segments.push({ gen: out.length, len: text.length, src, exact }); | ||
| + out += text; | ||
| + }; | ||
| + return { | ||
| + copy: (text, srcStart) => push(text, srcStart, true), | ||
| + synth: (text, anchor) => push(text, anchor, false), | ||
| + // Splice another emitter's output in; its segments already point at the | ||
| + // same input, so only their output offsets shift. | ||
| + append: (other) => { | ||
| + const base = out.length; | ||
| + for (const s of other.segments) { | ||
| + segments.push({ ...s, gen: s.gen + base }); | ||
| + } | ||
| + out += other.text(); | ||
| + }, | ||
| + text: () => out, | ||
| + segments, | ||
| + length: () => out.length, | ||
| + }; | ||
| +}; | ||
| + | ||
| +// Turns an emitter's segments into an offset lookup (output -> input). | ||
| +export const segmentLookup = (segments) => (offset) => { | ||
| + if (segments.length === 0) { | ||
| + return offset; | ||
| + } | ||
| + let lo = 0; | ||
| + let hi = segments.length - 1; | ||
| + while (lo < hi) { | ||
| + const mid = (lo + hi + 1) >> 1; | ||
| + if (segments[mid].gen <= offset) { | ||
| + lo = mid; | ||
| + } else { | ||
| + hi = mid - 1; | ||
| + } | ||
| + } | ||
| + const s = segments[lo]; | ||
| + if (offset < s.gen) { | ||
| + return s.src; | ||
| + } | ||
| + if (!s.exact) { | ||
| + return s.src; | ||
| + } | ||
| + return s.src + Math.min(offset - s.gen, s.len); | ||
| +}; | ||
| + | ||
| +export const lineStarts = (text) => { | ||
| + const starts = [0]; | ||
| + for (let i = 0; i < text.length; i += 1) { | ||
| + const ch = text.charCodeAt(i); | ||
| + if (ch === 10) { | ||
| + starts.push(i + 1); | ||
| + } else if (ch === 13) { | ||
| + if (text.charCodeAt(i + 1) === 10) { | ||
| + i += 1; | ||
| + } | ||
| + starts.push(i + 1); | ||
| + } | ||
| + } | ||
| + return starts; | ||
| +}; | ||
| + | ||
| +// Zero-based { line, character } for an offset. | ||
| +export const positionOf = (starts, offset) => { | ||
| + let lo = 0; | ||
| + let hi = starts.length - 1; | ||
| + while (lo < hi) { | ||
| + const mid = (lo + hi + 1) >> 1; | ||
| + if (starts[mid] <= offset) { | ||
| + lo = mid; | ||
| + } else { | ||
| + hi = mid - 1; | ||
| + } | ||
| + } | ||
| + return { line: lo, character: offset - starts[lo] }; | ||
| +}; | ||
| + | ||
| +// --- Source Map v3 VLQ --- | ||
| + | ||
| +const B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||
| +const B64_INDEX = new Map([...B64].map((c, i) => [c, i])); | ||
| + | ||
| +const encodeVlq = (value) => { | ||
| + let v = value < 0 ? (-value << 1) | 1 : value << 1; | ||
| + let out = ""; | ||
| + do { | ||
| + let digit = v & 31; | ||
| + v >>>= 5; | ||
| + if (v > 0) { | ||
| + digit |= 32; | ||
| + } | ||
| + out += B64[digit]; | ||
| + } while (v > 0); | ||
| + return out; | ||
| +}; | ||
| + | ||
| +// Returns lines of segments: [genCol, srcIndex, srcLine, srcCol, nameIndex?] | ||
| +// with absolute (not delta) values. | ||
| +export const decodeMappings = (mappings) => { | ||
| + const lines = []; | ||
| + let srcIndex = 0; | ||
| + let srcLine = 0; | ||
| + let srcCol = 0; | ||
| + let nameIndex = 0; | ||
| + for (const lineText of mappings.split(";")) { | ||
| + const segs = []; | ||
| + let genCol = 0; | ||
| + if (lineText !== "") { | ||
| + for (const segText of lineText.split(",")) { | ||
| + const values = []; | ||
| + let shift = 0; | ||
| + let acc = 0; | ||
| + for (const ch of segText) { | ||
| + const digit = B64_INDEX.get(ch); | ||
| + acc += (digit & 31) << shift; | ||
| + if (digit & 32) { | ||
| + shift += 5; | ||
| + } else { | ||
| + values.push(acc & 1 ? -(acc >>> 1) : acc >>> 1); | ||
| + acc = 0; | ||
| + shift = 0; | ||
| + } | ||
| + } | ||
| + genCol += values[0]; | ||
| + const seg = [genCol]; | ||
| + if (values.length >= 4) { | ||
| + srcIndex += values[1]; | ||
| + srcLine += values[2]; | ||
| + srcCol += values[3]; | ||
| + seg.push(srcIndex, srcLine, srcCol); | ||
| + if (values.length >= 5) { | ||
| + nameIndex += values[4]; | ||
| + seg.push(nameIndex); | ||
| + } | ||
| + } | ||
| + segs.push(seg); | ||
| + } | ||
| + } | ||
| + lines.push(segs); | ||
| + } | ||
| + return lines; | ||
| +}; | ||
| + | ||
| +export const encodeMappings = (lines) => { | ||
| + let srcIndex = 0; | ||
| + let srcLine = 0; | ||
| + let srcCol = 0; | ||
| + let nameIndex = 0; | ||
| + return lines | ||
| + .map((segs) => { | ||
| + let genCol = 0; | ||
| + return segs | ||
| + .map((seg) => { | ||
| + let out = encodeVlq(seg[0] - genCol); | ||
| + genCol = seg[0]; | ||
| + if (seg.length >= 4) { | ||
| + out += encodeVlq(seg[1] - srcIndex); | ||
| + out += encodeVlq(seg[2] - srcLine); | ||
| + out += encodeVlq(seg[3] - srcCol); | ||
| + srcIndex = seg[1]; | ||
| + srcLine = seg[2]; | ||
| + srcCol = seg[3]; | ||
| + if (seg.length >= 5) { | ||
| + out += encodeVlq(seg[4] - nameIndex); | ||
| + nameIndex = seg[4]; | ||
| + } | ||
| + } | ||
| + return out; | ||
| + }) | ||
| + .join(","); | ||
| + }) | ||
| + .join(";"); | ||
| +}; | ||
Addedpackages/cli/src/tokens.js+142−0
| @@ -0,0 +1,142 @@ | ||
| +// Tokenizer for .tsus source, built on TypeScript's own scanner. | ||
| +// | ||
| +// ts.createScanner knows strings, comments, numeric literals and every | ||
| +// operator, but two things depend on parser context and need a rescan: | ||
| +// | ||
| +// - `/` starts either a division or a regex literal. We decide from the | ||
| +// previous significant token (an expression just ended -> division) and call | ||
| +// reScanSlashToken for the regex case. | ||
| +// - `}` either closes a block or resumes a template literal after `${ ... }`. | ||
| +// We track the brace depth at each TemplateHead and call reScanTemplateToken | ||
| +// when the matching `}` arrives. | ||
| +// | ||
| +// The result is a flat array of tokens with source offsets. Trivia (spaces, | ||
| +// comments) is skipped; callers copy it from the source text between tokens. | ||
| +import ts from "typescript"; | ||
| + | ||
| +const K = ts.SyntaxKind; | ||
| + | ||
| +// Keywords after which a `/` starts a regex rather than dividing. | ||
| +const REGEX_AFTER_KEYWORD = new Set([ | ||
| + K.ReturnKeyword, | ||
| + K.TypeOfKeyword, | ||
| + K.InstanceOfKeyword, | ||
| + K.InKeyword, | ||
| + K.OfKeyword, | ||
| + K.NewKeyword, | ||
| + K.DeleteKeyword, | ||
| + K.VoidKeyword, | ||
| + K.ThrowKeyword, | ||
| + K.CaseKeyword, | ||
| + K.DoKeyword, | ||
| + K.ElseKeyword, | ||
| + K.YieldKeyword, | ||
| + K.AwaitKeyword, | ||
| + K.ExtendsKeyword, | ||
| +]); | ||
| + | ||
| +// Tokens that end an expression: a following `/` is division. | ||
| +const ENDS_EXPRESSION = new Set([ | ||
| + K.Identifier, | ||
| + K.PrivateIdentifier, | ||
| + K.NumericLiteral, | ||
| + K.BigIntLiteral, | ||
| + K.StringLiteral, | ||
| + K.RegularExpressionLiteral, | ||
| + K.NoSubstitutionTemplateLiteral, | ||
| + K.TemplateTail, | ||
| + K.CloseParenToken, | ||
| + K.CloseBracketToken, | ||
| + K.CloseBraceToken, | ||
| + K.PlusPlusToken, | ||
| + K.MinusMinusToken, | ||
| + K.ThisKeyword, | ||
| + K.SuperKeyword, | ||
| + K.TrueKeyword, | ||
| + K.FalseKeyword, | ||
| + K.NullKeyword, | ||
| +]); | ||
| + | ||
| +const isKeyword = (kind) => kind >= K.FirstKeyword && kind <= K.LastKeyword; | ||
| + | ||
| +const slashStartsRegex = (prev) => { | ||
| + if (prev === undefined) { | ||
| + return true; | ||
| + } | ||
| + if (ENDS_EXPRESSION.has(prev.kind)) { | ||
| + return false; | ||
| + } | ||
| + if (isKeyword(prev.kind)) { | ||
| + // Reserved operators-as-words (return, typeof, ...) precede regexes; any | ||
| + // other keyword here is being used as a name (`type / 2`). | ||
| + return REGEX_AFTER_KEYWORD.has(prev.kind); | ||
| + } | ||
| + return true; | ||
| +}; | ||
| + | ||
| +export const tokenize = (text) => { | ||
| + const scanner = ts.createScanner(ts.ScriptTarget.Latest, true, ts.LanguageVariant.Standard, text); | ||
| + const tokens = []; | ||
| + // Brace depth at which each open template substitution started. | ||
| + const templateDepths = []; | ||
| + let braceDepth = 0; | ||
| + while (true) { | ||
| + let kind = scanner.scan(); | ||
| + if (kind === K.EndOfFileToken) { | ||
| + break; | ||
| + } | ||
| + const prev = tokens.length > 0 ? tokens[tokens.length - 1] : undefined; | ||
| + if ((kind === K.SlashToken || kind === K.SlashEqualsToken) && slashStartsRegex(prev)) { | ||
| + kind = scanner.reScanSlashToken(); | ||
| + } else if (kind === K.OpenBraceToken) { | ||
| + braceDepth += 1; | ||
| + } else if (kind === K.CloseBraceToken) { | ||
| + const top = templateDepths.length > 0 ? templateDepths[templateDepths.length - 1] : -1; | ||
| + if (top === braceDepth) { | ||
| + kind = scanner.reScanTemplateToken(false); | ||
| + if (kind === K.TemplateTail) { | ||
| + templateDepths.pop(); | ||
| + } | ||
| + } else { | ||
| + braceDepth -= 1; | ||
| + } | ||
| + } else if (kind === K.TemplateHead) { | ||
| + templateDepths.push(braceDepth); | ||
| + } | ||
| + tokens.push({ | ||
| + kind, | ||
| + start: scanner.getTokenStart(), | ||
| + end: scanner.getTokenEnd(), | ||
| + text: scanner.getTokenText(), | ||
| + newlineBefore: scanner.hasPrecedingLineBreak(), | ||
| + }); | ||
| + } | ||
| + return tokens; | ||
| +}; | ||
| + | ||
| +const OPENERS = new Set([K.OpenParenToken, K.OpenBracketToken, K.OpenBraceToken, K.TemplateHead]); | ||
| +const CLOSERS = new Set([K.CloseParenToken, K.CloseBracketToken, K.CloseBraceToken, K.TemplateTail]); | ||
| + | ||
| +export const isOpener = (kind) => OPENERS.has(kind); | ||
| +export const isCloser = (kind) => CLOSERS.has(kind); | ||
| + | ||
| +// Index of the token that closes the bracket opened at tokens[i], or -1. | ||
| +// Template literals count as a bracket pair (head ... tail), so braces inside | ||
| +// `${}` never confuse the count. | ||
| +export const findClose = (tokens, i, limit = tokens.length) => { | ||
| + let depth = 0; | ||
| + for (let j = i; j < limit; j += 1) { | ||
| + const kind = tokens[j].kind; | ||
| + if (OPENERS.has(kind)) { | ||
| + depth += 1; | ||
| + } else if (CLOSERS.has(kind)) { | ||
| + depth -= 1; | ||
| + if (depth === 0) { | ||
| + return j; | ||
| + } | ||
| + } | ||
| + } | ||
| + return -1; | ||
| +}; | ||
| + | ||
| +export { K as SyntaxKind }; | ||
Changedpackages/plugin/core.js+21−29
| @@ -1,13 +1,14 @@ | ||
| -// Shared transform behind every bundler adapter: preprocess the match macro, | ||
| -// enforce the strictness rules, strip types. Emits plain JS but leaves import | ||
| +// Shared transform behind every bundler adapter: preprocess the match macro | ||
| +// and ? operator, enforce the strictness rules, strip types. Emits plain JS | ||
| +// with an inline source map back to the .tsus file, but leaves import | ||
| // specifiers untouched so the host bundler resolves .tsus -> .tsus chains | ||
| // through its own pipeline (each hop hits this transform again). | ||
| -import ts from "typescript"; | ||
| -import { preprocess, PreprocessError } from "@tsus/cli/preprocess"; | ||
| -import { checkSource } from "@tsus/cli/check"; | ||
| +// | ||
| +// Bundlers transform one file at a time, so this path has no semantic | ||
| +// type-check; run `tsus check` (or `tsus check --json` in CI) for that. | ||
| +import path from "node:path"; | ||
| +import { compileTsus, emitJs } from "@tsus/cli/compile"; | ||
| -const RUNTIME_IMPORT = `import { __tsus_match } from "@tsus/runtime";\n`; | ||
| - | ||
| export class TsusTransformError extends Error { | ||
| constructor(message, diagnostics) { | ||
| super(message); | ||
| @@ -17,29 +18,20 @@ | ||
| } | ||
| export const transformSource = (source, fileName) => { | ||
| - let pre; | ||
| - try { | ||
| - pre = preprocess(source); | ||
| - } catch (error) { | ||
| - if (error instanceof PreprocessError) { | ||
| - throw new TsusTransformError(`${fileName}: ${error.message}`, [ | ||
| - { code: error.code, message: error.message, file: fileName }, | ||
| - ]); | ||
| - } | ||
| - throw error; | ||
| - } | ||
| - const diagnostics = checkSource(fileName, pre.code); | ||
| - if (diagnostics.length > 0) { | ||
| - const summary = diagnostics | ||
| + const compiled = compileTsus(source, fileName); | ||
| + if (compiled.diagnostics.length > 0) { | ||
| + const summary = compiled.diagnostics | ||
| .map((d) => `${d.code} ${d.file}:${d.line}:${d.col} ${d.message}`) | ||
| .join("\n"); | ||
| - throw new TsusTransformError(summary, diagnostics); | ||
| + throw new TsusTransformError(summary, compiled.diagnostics); | ||
| } | ||
| - const tsCode = pre.changed ? RUNTIME_IMPORT + pre.code : pre.code; | ||
| - return ts.transpileModule(tsCode, { | ||
| - compilerOptions: { | ||
| - module: ts.ModuleKind.ESNext, | ||
| - target: ts.ScriptTarget.ES2022, | ||
| - }, | ||
| - }).outputText; | ||
| + return emitJs({ | ||
| + code: compiled.code, | ||
| + toSource: compiled.toSource, | ||
| + sourceText: source, | ||
| + sourcePath: path.basename(fileName), | ||
| + outName: path.basename(fileName).replace(/\.tsus$/, ".js"), | ||
| + rewriteImports: false, | ||
| + inlineMap: true, | ||
| + }).js; | ||
| }; | ||
Changedpackages/plugin/webpack.cjs+21−0
| @@ -1,8 +1,29 @@ | ||
| // Webpack loader. CommonJS entry (webpack requires loaders synchronously) | ||
| // that hands off to the ESM core via the async loader API. Works in Next.js | ||
| // through next.config webpack(config) — see examples/27-nextjs. | ||
| +const fs = require("node:fs"); | ||
| +const path = require("node:path"); | ||
| + | ||
| +// The loader's real logic lives in ESM files webpack cannot see through the | ||
| +// dynamic import, so its persistent cache (Next.js keeps one in .next/cache) | ||
| +// would keep serving output from an older compiler. Declaring them as build | ||
| +// dependencies invalidates that cache when the compiler or runtime changes. | ||
| +const compilerFiles = () => { | ||
| + const dirs = [__dirname, path.join(__dirname, "..", "cli", "src"), path.join(__dirname, "..", "runtime")]; | ||
| + return dirs.flatMap((dir) => | ||
| + fs.existsSync(dir) | ||
| + ? fs.readdirSync(dir).filter((f) => f.endsWith(".js")).map((f) => path.join(dir, f)) | ||
| + : [], | ||
| + ); | ||
| +}; | ||
| + | ||
| module.exports = function tsusLoader(source) { | ||
| const callback = this.async(); | ||
| + if (typeof this.addBuildDependency === "function") { | ||
| + for (const file of compilerFiles()) { | ||
| + this.addBuildDependency(file); | ||
| + } | ||
| + } | ||
| import("./core.js") | ||
| .then(({ transformSource }) => { | ||
| callback(null, transformSource(source, this.resourcePath)); | ||
Changedpackages/runtime/index.d.ts+101−13
| @@ -1,17 +1,29 @@ | ||
| -export interface Option<T> { | ||
| - readonly tag: "Some" | "None"; | ||
| - isSome(): boolean; | ||
| - isNone(): boolean; | ||
| +// Option and Result are discriminated unions on `tag`, so the match macro's | ||
| +// typed arms (below) can key on the tag and narrow each arm to its variant. | ||
| + | ||
| +interface OptionMethods<T> { | ||
| + isSome(): this is SomeVariant<T>; | ||
| + isNone(): this is NoneVariant<T>; | ||
| map<U>(f: (value: T) => U): Option<U>; | ||
| andThen<U>(f: (value: T) => Option<U>): Option<U>; | ||
| unwrapOr(fallback: T): T; | ||
| okOr<E>(error: E): Result<T, E>; | ||
| } | ||
| -export interface Result<T, E> { | ||
| - readonly tag: "Ok" | "Err"; | ||
| - isOk(): boolean; | ||
| - isErr(): boolean; | ||
| +export interface SomeVariant<T> extends OptionMethods<T> { | ||
| + readonly tag: "Some"; | ||
| + readonly value: T; | ||
| +} | ||
| + | ||
| +export interface NoneVariant<T = never> extends OptionMethods<T> { | ||
| + readonly tag: "None"; | ||
| +} | ||
| + | ||
| +export type Option<T> = SomeVariant<T> | NoneVariant<T>; | ||
| + | ||
| +interface ResultMethods<T, E> { | ||
| + isOk(): this is OkVariant<T, E>; | ||
| + isErr(): this is ErrVariant<T, E>; | ||
| map<U>(f: (value: T) => U): Result<U, E>; | ||
| mapErr<F>(f: (error: E) => F): Result<T, F>; | ||
| andThen<U>(f: (value: T) => Result<U, E>): Result<U, E>; | ||
| @@ -19,15 +31,91 @@ | ||
| ok(): Option<T>; | ||
| } | ||
| +export interface OkVariant<T, E = never> extends ResultMethods<T, E> { | ||
| + readonly tag: "Ok"; | ||
| + readonly value: T; | ||
| +} | ||
| + | ||
| +export interface ErrVariant<T, E> extends ResultMethods<T, E> { | ||
| + readonly tag: "Err"; | ||
| + readonly error: E; | ||
| +} | ||
| + | ||
| +export type Result<T, E> = OkVariant<T, E> | ErrVariant<T, E>; | ||
| + | ||
| export declare const Some: <T>(value: T) => Option<T>; | ||
| -export declare const None: Option<never>; | ||
| +export declare const None: NoneVariant<never>; | ||
| export declare const Ok: <T, E = never>(value: T) => Result<T, E>; | ||
| export declare const Err: <E, T = never>(error: E) => Result<T, E>; | ||
| export type Tagged<K extends string, V> = { readonly tag: K; readonly value: V }; | ||
| export declare const tag: <K extends string, V>(t: K, value: V) => Tagged<K, V>; | ||
| -export declare const __tsus_match: ( | ||
| - value: unknown, | ||
| - arms: Array<[unknown, (...args: never[]) => unknown]>, | ||
| -) => never; | ||
| +// --- match --- | ||
| +// | ||
| +// Arms are an object keyed by the subject's tag values. A mapped type over | ||
| +// the union's `tag` field makes every variant a required key (that is the | ||
| +// exhaustiveness check), rejects keys the union cannot hold, and types each | ||
| +// arm's parameter as that variant's payload. Works for Option, Result, and | ||
| +// any user union with a string `tag` field. The match's type is the union of | ||
| +// the arms' types. | ||
| + | ||
| +/** The tag values of a tagged union. */ | ||
| +export type TagOf<V> = V extends { readonly tag: infer K extends string } ? K : never; | ||
| +/** The member of union V whose tag is K. */ | ||
| +export type VariantOf<V, K> = Extract<V, { readonly tag: K }>; | ||
| +/** What an arm binds: `value` if present (Some, Ok, Tagged), else `error` (Err), else the variant itself. */ | ||
| +export type Payload<V> = V extends { readonly value: infer P } | ||
| + ? P | ||
| + : V extends { readonly error: infer E } | ||
| + ? E | ||
| + : V; | ||
| + | ||
| +// The arm results here are `any` only so that a match which fails this | ||
| +// constraint (the real error) does not also report an `unknown` result | ||
| +// everywhere the match is used. A match that type-checks takes its type from | ||
| +// the arms actually written, through ArmResult. | ||
| +export type MatchArms<V> = { [K in TagOf<V>]: (payload: Payload<VariantOf<V, K>>) => any }; | ||
| +type NoExtraArms<V, A> = { [K in Exclude<keyof A, TagOf<V>>]: never }; | ||
| +type ArmResult<A> = A[keyof A] extends infer F ? (F extends (...args: never[]) => infer R ? R : never) : never; | ||
| + | ||
| +export declare const __tsus_match: <V extends { readonly tag: string }>( | ||
| + value: V, | ||
| +) => <A extends MatchArms<V>>(arms: A & NoExtraArms<V, A>) => ArmResult<A>; | ||
| + | ||
| +export declare const __tsus_match_or: <V extends { readonly tag: string }>( | ||
| + value: V, | ||
| +) => <A extends Partial<MatchArms<V>>, W>( | ||
| + arms: A & NoExtraArms<V, A>, | ||
| + otherwise: (value: V) => W, | ||
| +) => ArmResult<A> | W; | ||
| + | ||
| +// Literal patterns (`"/" => ...`) cannot key an object without colliding | ||
| +// with `_` and losing number/string distinctions, so they lower to a list of | ||
| +// [literal, handler] pairs. Each literal must be a value of the subject's | ||
| +// type; the wildcard is required unless the literals cover a finite union. | ||
| +type Literal = string | number | bigint | boolean; | ||
| +type CaseLiterals<C> = C extends readonly (readonly [infer L, unknown])[] ? L : never; | ||
| +type CaseResult<C> = C extends readonly (readonly [unknown, () => infer R])[] ? R : never; | ||
| + | ||
| +export declare const __tsus_match_lit: <V extends Literal>( | ||
| + value: V, | ||
| +) => <const C extends readonly (readonly [V, () => unknown])[], W = never>( | ||
| + cases: C, | ||
| + ...otherwise: [Exclude<V, CaseLiterals<C>>] extends [never] ? [] : [otherwise: (value: V) => W] | ||
| +) => CaseResult<C> | W; | ||
| + | ||
| +// --- the ? operator --- | ||
| + | ||
| +// `any` for the same reason as MatchArms: when ? is applied to a non-Result | ||
| +// (TSUS301), the unwrapped value should not cascade into more errors. | ||
| +type Tryable = Result<any, any> | Option<any>; | ||
| +type Bail = { readonly tag: "Err" } | { readonly tag: "None" }; | ||
| + | ||
| +export declare const __tsus_try: <V extends Tryable>(value: V) => V; | ||
| +export declare const __tsus_isBail: <V extends Tryable>(value: V) => value is Extract<V, Bail>; | ||
| +/** Re-types an Err/None so it fits any Result/Option with the same error type. */ | ||
| +export declare const __tsus_bail: <V>( | ||
| + value: V, | ||
| +) => V extends { readonly tag: "Err"; readonly error: infer E } ? ErrVariant<never, E> : NoneVariant<never>; | ||
| +export declare const __tsus_unwrap: <V extends Tryable>(value: V) => Payload<Exclude<V, Bail>>; | ||
Changedpackages/runtime/index.js+65−27
| @@ -47,33 +47,71 @@ | ||
| export const tag = (t, value) => ({ tag: t, value }); | ||
| -// Compiled form of the match macro. Arms are [pattern, handler] pairs where | ||
| -// pattern is a variant tag string, "_" for wildcard, or { lit: value }. | ||
| -export const __tsus_match = (value, arms) => { | ||
| - for (const [pattern, handler] of arms) { | ||
| - if (pattern === "_") { | ||
| - return handler(value); | ||
| +// --- compiled forms of the match macro --- | ||
| +// | ||
| +// The preprocessor lowers `match (subject) { ... }` to one of these, curried | ||
| +// so TypeScript fixes the subject's type before it checks the arms: | ||
| +// | ||
| +// __tsus_match(subject)({ Some: (x) => ..., None: () => ... }) | ||
| +// __tsus_match_or(subject)({ Some: (x) => ... }, () => wildcard) | ||
| +// __tsus_match_lit(subject)([["/", () => ...], ...], () => wildcard) | ||
| +// | ||
| +// A variant arm receives the variant's payload: `value` if it has one (Some, | ||
| +// Ok, Tagged), else `error` (Err), else the variant object itself. | ||
| + | ||
| +const payload = (v) => ("value" in v ? v.value : "error" in v ? v.error : v); | ||
| + | ||
| +const noArm = (value) => | ||
| + new Error(`TSUS100: non-exhaustive match, no arm matched ${JSON.stringify(value)}. Add a _ arm.`); | ||
| + | ||
| +const dispatch = (value, arms, otherwise) => { | ||
| + const t = value !== null && typeof value === "object" ? value.tag : undefined; | ||
| + if (typeof t === "string" && Object.hasOwn(arms, t)) { | ||
| + return arms[t](payload(value)); | ||
| + } | ||
| + if (otherwise) { | ||
| + return otherwise(value); | ||
| + } | ||
| + throw noArm(value); | ||
| +}; | ||
| + | ||
| +export const __tsus_match = (value) => (arms) => dispatch(value, arms); | ||
| + | ||
| +export const __tsus_match_or = (value) => (arms, otherwise) => dispatch(value, arms, otherwise); | ||
| + | ||
| +export const __tsus_match_lit = (value) => (cases, otherwise) => { | ||
| + for (const [literal, handler] of cases) { | ||
| + if (value === literal) { | ||
| + return handler(); | ||
| } | ||
| - if (typeof pattern === "object" && pattern !== null && "lit" in pattern) { | ||
| - if (value === pattern.lit) { | ||
| - return handler(value); | ||
| - } | ||
| - continue; | ||
| - } | ||
| - if (value !== null && typeof value === "object" && value.tag === pattern) { | ||
| - if (pattern === "None") { | ||
| - return handler(); | ||
| - } | ||
| - if (pattern === "Err") { | ||
| - return handler(value.error); | ||
| - } | ||
| - if ("value" in value) { | ||
| - return handler(value.value); | ||
| - } | ||
| - return handler(value); | ||
| - } | ||
| } | ||
| - throw new Error( | ||
| - `TSUS100: non-exhaustive match, no arm matched ${JSON.stringify(value)}. Add a _ arm.`, | ||
| - ); | ||
| + if (otherwise) { | ||
| + return otherwise(value); | ||
| + } | ||
| + throw noArm(value); | ||
| +}; | ||
| + | ||
| +// --- compiled form of the ? operator --- | ||
| +// | ||
| +// const x = load()?; | ||
| +// becomes | ||
| +// const t = __tsus_try(load()); if (__tsus_isBail(t)) return __tsus_bail(t); const x = t.value; | ||
| +// | ||
| +// __tsus_try and __tsus_bail are identities at runtime; they exist for the | ||
| +// type checker. An Err or None carries no payload of the success type, so the | ||
| +// same object is returned as-is from the enclosing function. | ||
| + | ||
| +export const __tsus_try = (value) => value; | ||
| + | ||
| +export const __tsus_isBail = (value) => value.tag === "Err" || value.tag === "None"; | ||
| + | ||
| +export const __tsus_bail = (value) => value; | ||
| + | ||
| +// Emitted only for a ? the compiler already rejected (TSUS300/TSUS303), so a | ||
| +// checked build never contains it; reaching it means the checker was bypassed. | ||
| +export const __tsus_unwrap = (value) => { | ||
| + if (value.tag === "Ok" || value.tag === "Some") { | ||
| + return value.value; | ||
| + } | ||
| + throw new Error(`TSUS300: ? propagated ${value.tag} outside a Result/Option function`); | ||
| }; | ||
Changedscripts/run-examples.mjs+12−0
| @@ -86,6 +86,18 @@ | ||
| if (missing.length > 0) { | ||
| failure = `missing expected codes ${missing.join(", ")}; saw ${[...seen].join(", ") || "none"}`; | ||
| } | ||
| + // Optional exact expectations: each listed diagnostic must be reported, | ||
| + // matching every field it names (code, file, line, col, tsCode). This | ||
| + // pins positions mapped back to the .tsus source. | ||
| + for (const want of manifest.diagnostics ?? []) { | ||
| + const hit = (parsed.diagnostics ?? []).some((d) => | ||
| + Object.entries(want).every(([key, value]) => d[key] === value), | ||
| + ); | ||
| + if (!hit && failure === "") { | ||
| + const seenAt = (parsed.diagnostics ?? []).map((d) => `${d.code}@${d.line}:${d.col}`).join(", "); | ||
| + failure = `expected diagnostic ${JSON.stringify(want)}; saw ${seenAt || "none"}`; | ||
| + } | ||
| + } | ||
| } | ||
| } else { | ||
| const rb = runNode([cliBin, "build", dir], repoRoot); | ||
Checks
No checks reported for this commit.
Runs
No runs for this commit.