|
| 1 | +// `rx-tsc` (bin/tsc.js) on a copy of examples/shop: a clean program passes; a TypeScript type error, a parse error and a |
| 2 | +// checker error each fail it, placed on the .rx. Same prerequisites as test/run.js. |
| 3 | +// node test/tsc.js VERBOSE=1 prints each run's output |
| 4 | +const { spawnSync } = require("node:child_process"); |
| 5 | +const fs = require("node:fs"); |
| 6 | +const path = require("node:path"); |
| 7 | + |
| 8 | +const repo = path.resolve(__dirname, "../../.."); |
| 9 | +const ws = path.join(__dirname, "../.test/tsc"); |
| 10 | +fs.rmSync(ws, { recursive: true, force: true }); |
| 11 | +fs.mkdirSync(ws, { recursive: true }); |
| 12 | +for (const f of fs.readdirSync(path.join(repo, "examples/shop"))) fs.copyFileSync(path.join(repo, "examples/shop", f), path.join(ws, f)); |
| 13 | +fs.writeFileSync(path.join(ws, "api.d.ts"), [ |
| 14 | + "export declare function checkout(items: { id: number; qty: number }[], coupon: string): Promise<{ id: number }>;", |
| 15 | + "export declare function fetchProducts(): Promise<{ id: number; name: string; price: number }[]>;", |
| 16 | + "export declare function validateCoupon(code: string): Promise<number>;", |
| 17 | + "", |
| 18 | +].join("\n")); |
| 19 | +fs.symlinkSync(path.join(repo, "ref/node_modules"), path.join(ws, "node_modules"), "dir"); |
| 20 | +fs.writeFileSync(path.join(ws, "tsconfig.json"), JSON.stringify({ |
| 21 | + compilerOptions: { jsx: "preserve", jsxImportSource: "@solidjs/web", strict: true, noImplicitAny: false, target: "ES2022", module: "ESNext", moduleResolution: "Bundler", allowImportingTsExtensions: true, noEmit: true, skipLibCheck: true, lib: ["ES2022", "DOM"], types: [] }, |
| 22 | + include: ["*.rx", "*.ts"], |
| 23 | +}, null, 2)); |
| 24 | + |
| 25 | +const original = Object.fromEntries(fs.readdirSync(ws).filter(f => f.endsWith(".rx")).map(f => [f, fs.readFileSync(path.join(ws, f), "utf8")])); |
| 26 | +function run(edit) { |
| 27 | + for (const [f, t] of Object.entries(original)) fs.writeFileSync(path.join(ws, f), t); |
| 28 | + if (edit) fs.writeFileSync(path.join(ws, edit.file), edit.text(original[edit.file])); |
| 29 | + const r = spawnSync(process.execPath, [path.join(__dirname, "../bin/tsc.js")], { cwd: ws, encoding: "utf8" }); |
| 30 | + return { status: r.status, out: r.stdout + r.stderr }; |
| 31 | +} |
| 32 | + |
| 33 | +let pass = 0, fail = 0; |
| 34 | +function check(name, ok, got) { |
| 35 | + if (ok) pass++; else fail++; |
| 36 | + console.log(`${ok ? " ✓" : " ✗"} ${name}${!ok || process.env.VERBOSE ? `\n ${JSON.stringify(got).slice(0, 2000)}` : ""}`); |
| 37 | +} |
| 38 | + |
| 39 | +const clean = run(); |
| 40 | +check("the shop passes", clean.status === 0 && clean.out.trim() === "", clean); |
| 41 | + |
| 42 | +const type = run({ file: "cart.rx", text: t => t.replace("qtyOf(p.id)", 'qtyOf("x")') }); |
| 43 | +const typeLine = original["cart.rx"].slice(0, original["cart.rx"].indexOf("qtyOf(p.id)")).split("\n").length; |
| 44 | +check("a type error fails, on the .rx line", type.status !== 0 && new RegExp(`cart\\.rx\\(${typeLine},\\d+\\): error TS2345`).test(type.out), type); |
| 45 | + |
| 46 | +const parse = run({ file: "catalog.rx", text: t => t + "\nexport fn broken( {\n" }); |
| 47 | +check("a parse error fails (no projection, so TypeScript alone would pass it)", parse.status !== 0 && /catalog\.rx:\d+:\d+ - error PARSE/.test(parse.out), parse); |
| 48 | + |
| 49 | +const write = run({ file: "main.rx", text: t => t + "\naction wipe() { products := [] }\n" }); |
| 50 | +check("a checker error fails, with its rule", write.status !== 0 && /main\.rx:\d+:\d+ - error WRITE_TO_NON_SOURCE: .*\(MO-2\)/.test(write.out), write); |
| 51 | + |
| 52 | +console.log(`\n${pass} passed, ${fail} failed`); |
| 53 | +process.exit(fail ? 1 : 0); |
0 commit comments