Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839// For an input whose output differs from the oracle only by `;`/space swaps, finds which `;` changes the JS AST.import fs from "node:fs";import { createRequire } from "node:module";const root = new URL("../..", import.meta.url).pathname.replace(/\/$/, "");const ts = createRequire(root + "/package.json")("typescript");process.env.TYPESTRIP_ERASE_WASM ??= "build/erase.wasm";const { blankSourceFile } = await import(root + "/vendor/ts-blank-space/out/index.js");const { transformSync } = await import(root + "/src/index.js");const SK = ts.SyntaxKind;function jsAst(code) { const sf = ts.createSourceFile("out.js", code, ts.ScriptTarget.ESNext, false, ts.ScriptKind.JS); const out = []; (function visit(n) { if (n.kind === SK.EmptyStatement || n.kind === SK.SemicolonClassElement) return; out.push(n.kind); if (ts.isIdentifier(n) || ts.isPrivateIdentifier(n) || ts.isLiteralExpression(n) || ts.isTemplateLiteralToken?.(n)) out.push(JSON.stringify(n.text)); ts.forEachChild(n, visit); out.push(-1); })(sf); return out.join(",");}const src = process.argv[2] === "-f" ? fs.readFileSync(process.argv[3], "utf8") : process.argv[2];const sf = ts.createSourceFile("input.ts", src, ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS);const want = blankSourceFile(sf, () => {});const got = transformSync(src);const wantAst = jsAst(want);if (jsAst(got) === wantAst) { console.log("AST matches"); process.exit(0); }const diffs = [];for (let i = 0; i < got.length; i++) if (got[i] !== want[i]) diffs.push(i);console.log(`${diffs.length} differing positions`);for (const i of diffs) { const fixed = got.slice(0, i) + want[i] + got.slice(i + 1); if (jsAst(fixed) === wantAst) { console.log(`culprit at ${i}: got ${JSON.stringify(got[i])} want ${JSON.stringify(want[i])}\n src : ${JSON.stringify(src.slice(Math.max(0, i - 80), i + 60))}\n want: ${JSON.stringify(want.slice(Math.max(0, i - 80), i + 60))}\n got : ${JSON.stringify(got.slice(Math.max(0, i - 80), i + 60))}`); process.exit(0); }}console.log("no single-position fix; first diffs:", diffs.slice(0, 5).map((i) => `${i}:${JSON.stringify(src.slice(i - 30, i + 30))}`).join("\n"));