Skip to content

Commit 607f605

Browse files
authored
Merge pull request #41 from solidjs/claude/project-thread-k44bg2
Add rx-tsc: type-check .rx from the command line
2 parents 5bcdbe6 + 308da10 commit 607f605

8 files changed

Lines changed: 131 additions & 5 deletions

File tree

‎.github/workflows/editor.yml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ jobs:
4747
npm ci
4848
node test/run.js
4949
RXC=$GITHUB_WORKSPACE/compiler/target/release/rxc node test/run.js
50+
node test/tsc.js
5051
- name: Package the extension
5152
working-directory: tooling/vscode-rx
5253
run: |

‎README.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,19 @@ and VSCodium. It brings highlighting and the language server. Other editors
148148
can run the language server on its own: `npx @reactivescript/language-server
149149
--stdio` ([`tooling/rx-language-server`](tooling/rx-language-server)).
150150

151+
**In CI.** Vite compiles `.rx` without type-checking it, as it does `.ts`.
152+
`rx-tsc` is `tsc` with `.rx` in the program: the same check the editor runs,
153+
plus the checker's errors with their rules.
154+
155+
```sh
156+
npm i -D @reactivescript/language-server typescript
157+
npx rx-tsc --noEmit
158+
```
159+
160+
It reads `tsconfig.json`, whose `include` must name the `.rx` files
161+
(`"include": ["src"]` or `["src/**/*.rx", "src/**/*.ts"]`), with Solid's JSX:
162+
`"jsx": "preserve"`, `"jsxImportSource": "@solidjs/web"`.
163+
151164
How releases are made: [`tooling/RELEASING.md`](tooling/RELEASING.md) and
152165
[`compiler/README.md`](compiler/README.md).
153166

‎tooling/rx-language-server/README.md‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ node bin/server.js --stdio
3838
`initializationOptions: { typescript: { tsdk } }` picks TypeScript's `lib`
3939
folder; without it, the server uses its own `typescript`.
4040

41+
## Type-checking from the command line
42+
43+
`rx-tsc` (`bin/tsc.js`) is `tsc` with `.rx` modules in the program, for CI.
44+
It is Volar's tsc runner (the one `vue-tsc` uses) with this package's
45+
language plugin, so it checks the same projection the editor does, and places
46+
type errors on the `.rx`. It takes `tsc`'s arguments and reads
47+
`tsconfig.json`, whose `include` must name the `.rx` files.
48+
49+
```sh
50+
npx rx-tsc --noEmit
51+
```
52+
53+
After TypeScript's errors it prints the crate's diagnostics for every `.rx`
54+
module in the program (parse errors, else the checker's, with their rules),
55+
and exits non-zero if there are any. A module that does not parse has no
56+
projection, so TypeScript alone would pass it. Under `--watch` only
57+
TypeScript's errors are reported.
58+
4159
## Editors
4260

4361
**VS Code** (and Cursor, VSCodium): the extension in
@@ -92,10 +110,11 @@ instead (or `rx-language-server --stdio` once installed).
92110
```sh
93111
cargo build --release --manifest-path ../../compiler/Cargo.toml
94112
npm install && node test/run.js # VERBOSE=1 prints each answer
113+
node test/tsc.js # rx-tsc: a clean program, a type, a parse and a checker error
95114
```
96115

97116
`test/run.js` drives the server over LSP on a copy of `examples/shop` (Solid's
98117
types from `ref/node_modules`, via `ref/scripts/solid-next.sh`): hover, go to
99118
definition across modules, references, completion while typing (in code and in
100119
markup), a parse error, a TypeScript type error and a crate diagnostic, each
101-
placed on the `.rx`.
120+
placed on the `.rx`. `test/tsc.js` runs `rx-tsc` on the same copy.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env node
2+
// `rx-tsc`: TypeScript's own `tsc`, with `.rx` modules in the program (spec/13 TY-1a), for CI and the command line. It
3+
// is Volar's tsc runner (the one `vue-tsc` uses) with the language server's `.rx` plugin, so the editor and the command
4+
// line check the same projection. It takes tsc's arguments (`rx-tsc --noEmit`, `-p`, `--watch`) and reads the
5+
// tsconfig.json's `include`, which must name the `.rx` files. Type errors are placed on the `.rx`.
6+
//
7+
// The crate's own diagnostics (parse errors, else the checker's, with their rules) are reported after TypeScript's, for
8+
// every `.rx` module in the program: a module that does not parse has no projection, so TypeScript alone would pass it.
9+
const path = require("node:path");
10+
const { runTsc } = require("@volar/typescript/lib/quickstart/runTsc");
11+
const { rxLanguagePlugin, analyses } = require("../src/index.js");
12+
13+
const tsc = require.resolve("typescript/lib/tsc");
14+
15+
process.on("exit", code => {
16+
const watching = process.argv.includes("--watch") || process.argv.includes("-w");
17+
if (watching) return;
18+
let errors = 0;
19+
for (const { file, result } of analyses()) {
20+
for (const d of result.diagnostics ?? []) {
21+
errors++;
22+
const where = d.range ? `${d.range.start.line}:${d.range.start.column + 1}` : "1:1";
23+
process.stdout.write(`${path.relative(process.cwd(), file)}:${where} - error ${d.code}: ${d.message} (${d.rule})\n`);
24+
}
25+
}
26+
if (errors) {
27+
process.stdout.write(`\nFound ${errors} ReactiveScript error${errors === 1 ? "" : "s"}.\n`);
28+
if (!code) process.exitCode = 1;
29+
}
30+
});
31+
32+
runTsc(tsc, [".rx"], ts => [rxLanguagePlugin(ts)]);

‎tooling/rx-language-server/package-lock.json‎

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tooling/rx-language-server/package.json‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@reactivescript/language-server",
33
"version": "0.1.0",
4-
"description": "The .rx language server (spec/11 CP-8): Volar over the crate's TSX projection, TypeScript's answers mapped back onto .rx, the crate's own diagnostics.",
4+
"description": "The .rx language server (spec/11 CP-8): Volar over the crate's TSX projection, TypeScript's answers mapped back onto .rx, the crate's own diagnostics; rx-tsc type-checks .rx from the command line.",
55
"license": "MIT",
66
"repository": {
77
"type": "git",
@@ -10,7 +10,8 @@
1010
},
1111
"type": "commonjs",
1212
"bin": {
13-
"rx-language-server": "./bin/server.js"
13+
"rx-language-server": "./bin/server.js",
14+
"rx-tsc": "./bin/tsc.js"
1415
},
1516
"main": "./src/index.js",
1617
"files": [
@@ -21,7 +22,7 @@
2122
"node": ">=18"
2223
},
2324
"scripts": {
24-
"test": "node test/run.js",
25+
"test": "node test/run.js && node test/tsc.js",
2526
"prepack": "node scripts/wasm.js"
2627
},
2728
"dependencies": {

‎tooling/rx-language-server/src/index.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,7 @@ function rxDiagnostics() {
9595
};
9696
}
9797

98-
module.exports = { rxLanguagePlugin, rxDiagnostics, analysis };
98+
/** Every `.rx` module analyzed so far, with its last analysis (the command line reports their crate diagnostics). */
99+
const analyses = () => [...cache].filter(([f]) => isModule(f)).map(([file, { result }]) => ({ file, result }));
100+
101+
module.exports = { rxLanguagePlugin, rxDiagnostics, analysis, analyses };
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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

Comments
 (0)