Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

JavaScript · 137 lines · 4.9 KiB · at eb4a6ea

// tsus build: src/**/*.{ts,tsus} -> dist/**/*.js (plain ESM) + source maps +// .d.ts declarations.// tsus check: the same pipeline without emitting.import fs from "node:fs";import path from "node:path";import { compileTsus, emitJs, relativeSourcePath, specifierTransformer } from "./compile.js";import { emitDeclarations, semanticCheck, virtualName } from "./program.js";import { runRules } from "./rules/index.js";const walk = (dir) => {  const out = [];  for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {    const full = path.join(dir, entry.name);    if (entry.isDirectory()) {      out.push(...walk(full));    } else {      out.push(full);    }  }  return out;};export const collectSources = (rootDir) => {  const srcDir = path.join(rootDir, "src");  if (!fs.existsSync(srcDir)) {    throw new Error(`no src/ directory in ${rootDir}`);  }  return { srcDir, files: walk(srcDir) };};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"]);// The promise and Result rules need to know what a call returns. Per file,// they only see names declared in the file or imported from a relative// module, so a call through a parameter (`api.save()`, `deps.log()`) or a// package was invisible to them. With the project's program they use real// types, so their answers replace the per-file ones.const TYPED = new Set(["TSUS411", "TSUS412"]);const typedDiagnostics = (rootDir, compiled, program) => {  const out = new Map();  for (const [file, c] of compiled) {    const fileName = path.relative(rootDir, file);    const found = runRules({      fileName,      code: c.code,      original: c.source,      toSource: c.toSource,      path: file,      program,      programFileName: virtualName(file),    });    out.set(fileName, found.filter((d) => TYPED.has(d.code)));  }  return out;};const replaceTyped = (diagnostics, typed) => {  const kept = diagnostics.filter((d) => !(TYPED.has(d.code) && typed.has(d.file)));  diagnostics.length = 0;  diagnostics.push(...kept, ...[...typed.values()].flat());};// 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 c = compileTsus(fs.readFileSync(file, "utf8"), rel, { path: file });    compiled.set(file, c);    diagnostics.push(...c.diagnostics);  }  if (semantic && !diagnostics.some((d) => BLOCKING.has(d.code))) {    let program;    diagnostics.push(...semanticCheck({ rootDir, tsFiles: files.filter(isTsFile), compiled, onProgram: (p) => (program = p) }));    if (program) {      replaceTyped(diagnostics, typedDiagnostics(rootDir, compiled, program));    }  }  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, compiled } = checkProject(rootDir);  if (diagnostics.length > 0) {    return { diagnostics, emitted: [] };  }  const distDir = path.join(rootDir, "dist");  const emitted = [];  for (const file of files) {    const rel = path.relative(srcDir, file);    const isTsus = file.endsWith(".tsus");    const isTs = file.endsWith(".ts") && !file.endsWith(".d.ts");    if (!isTsus && !isTs) {      // Asset (data files, etc.): copy through so runtime-relative reads work.      const dest = path.join(distDir, rel);      fs.mkdirSync(path.dirname(dest), { recursive: true });      fs.copyFileSync(file, dest);      emitted.push(dest);      continue;    }    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, js);    fs.writeFileSync(`${dest}.map`, `${JSON.stringify(map)}\n`);    emitted.push(dest, `${dest}.map`);  }  const declarations = emitDeclarations({    rootDir,    srcDir,    distDir,    tsFiles: files.filter(isTsFile),    compiled,    transformer: specifierTransformer,  });  emitted.push(...declarations.written);  return { diagnostics: declarations.diagnostics, emitted };};