Rebasely Beta: data may be purged

justintypescript-ultra-strict Public

TSUS411/TSUS412 use the project's types in tsus check, build and fix

Found by the Rebasely on-call pilot: a floating promise or an ignored Result from a call
through a parameter (void api.beat(), api.heartbeat(...), store.save(row)) was not reported,
because the rules ran per file before the type-check and their binder only sees local and
relative-import declarations. On the pilot's probe, 1 of 4 floating promises was reported.
checkProject now hands the semantic pass's ts.Program to the rules and replaces their
TSUS411/TSUS412 answers with the typed ones (runRules already supported a program; nothing
passed one). examples/58-typed-promise-rules pins the three cases.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
eb4a6ea5c0fed22173ef721b747dfbb09ea3a186
Parent 2e2b18f Browse filesHistory

Changed files

5 files changed77 additions6 deletionsagainst 2e2b18f

  1. ChangedREADME.md+3−2
  2. Addedexamples/58-typed-promise-rules/example.json+9−0
  3. Addedexamples/58-typed-promise-rules/src/main.tsus+22−0
  4. Changedpackages/cli/src/build.js+38−2
  5. Changedpackages/cli/src/program.js+5−2
ChangedREADME.md+3−2
@@ -4,7 +4,7 @@
> **This repo is a thought experiment, not a production tool.** The compiler works, type-checks, and every example passes, but the macro syntax is lowered by a preprocessor with known edge cases, the bundler plugins skip semantic checking, there are no published packages, and there is no stability promise. It exists to make the proposal concrete enough to argue with. Do not build anything you care about on it yet.
-**Status:** working prototype. `packages/cli` implements `tsus check` and `tsus build`: a tokenizer built on `ts.createScanner`, a preprocessor that lowers `match` and the `?` operator with a source map back to the `.tsus` file, and a full `ts.createProgram` type-check over the project. `match` is exhaustiveness-checked at compile time, `tsus build` emits `.js.map` files that point at the `.tsus` source and `.d.ts` declarations for plain TypeScript consumers, `packages/runtime` implements `Option`/`Result`, the typed match helpers and the boundary layer, `packages/plugin` provides webpack/Vite/esbuild/Bun adapters, the checker enforces 18 strictness rules with machine-applicable fixes (`tsus fix`, `tsus explain`, `tsus migrate`, `tsus init`; see [For agents](#for-agents)), and all 54 projects under `examples/` pass (`npm install && npm test`). `LEARNINGS.md` documents what broke along the way, how that shaped the design, and the known limitations.
+**Status:** working prototype. `packages/cli` implements `tsus check` and `tsus build`: a tokenizer built on `ts.createScanner`, a preprocessor that lowers `match` and the `?` operator with a source map back to the `.tsus` file, and a full `ts.createProgram` type-check over the project. `match` is exhaustiveness-checked at compile time, `tsus build` emits `.js.map` files that point at the `.tsus` source and `.d.ts` declarations for plain TypeScript consumers, `packages/runtime` implements `Option`/`Result`, the typed match helpers and the boundary layer, `packages/plugin` provides webpack/Vite/esbuild/Bun adapters, the checker enforces 18 strictness rules with machine-applicable fixes (`tsus fix`, `tsus explain`, `tsus migrate`, `tsus init`; see [For agents](#for-agents)), and all 55 projects under `examples/` pass (`npm install && npm test`). `LEARNINGS.md` documents what broke along the way, how that shaped the design, and the known limitations.
TypeScript's early design decisions assumed humans would write and maintain it, so a lot of features exist to make a human's life easier. We valued speed over quality. With AI writing most of our code, we can flip that tradeoff and force the language to be declarative and verifiable, like Rust.
@@ -163,7 +163,7 @@
| TSUS412 | no unused `Result` | none |
| TSUS413 | no `@ts-ignore`, `@ts-expect-error`, `@ts-nocheck` | safe: delete |
-The rules run on the macro-expanded source with the TypeScript parser and a per-file binder, so names resolve through real scopes (a shadowed parameter is not a parameter). TSUS410 to TSUS412 follow relative `.tsus` and `.ts` imports to see what an imported function returns or what an imported error type is. When a full `ts.Program` is handed to the checker, they use its types instead.
+The rules run on the macro-expanded source with the TypeScript parser and a per-file binder, so names resolve through real scopes (a shadowed parameter is not a parameter). TSUS410 to TSUS412 follow relative `.tsus` and `.ts` imports to see what an imported function returns or what an imported error type is. `tsus check`, `tsus build` and `tsus fix` also hand TSUS411 and TSUS412 the project's `ts.Program`, so a call through a parameter or an object (`api.save()`, `deps.log()`) or into a package is judged by its real type; the bundler plugins, which see one file at a time, keep the per-file answer (`examples/58-typed-promise-rules`).
**Boundary files.** A file named `*.boundary.tsus` is where the outside world is let in: `as` casts (after a runtime check) and default exports are allowed there, and nothing else is relaxed. Cloudflare Workers' `export default { fetch }` lives in one (`examples/23-cloudflare-worker`), as does a checked JSON config loader (`examples/53-boundary-file`).
@@ -210,6 +210,7 @@
| `50-banned-floating-promise` | TSUS411 |
| `51-banned-unused-result` | TSUS412 |
| `52-banned-ts-suppression` | TSUS413 |
+| `58-typed-promise-rules` | TSUS411 and TSUS412 on calls through a parameter, which need the project's types |
Rejection examples can also pin exact positions (`"diagnostics": [{ "code", "file", "line", "col", "tsCode" }]` in `example.json`), which tests the source map back to the `.tsus` file.
View file
Addedexamples/58-typed-promise-rules/example.json+9−0
@@ -0,0 +1,9 @@
+{
+ "expect": "check-fail",
+ "codes": ["TSUS411", "TSUS412"],
+ "diagnostics": [
+ { "code": "TSUS411", "file": "src/main.tsus", "line": 9, "col": 8 },
+ { "code": "TSUS411", "file": "src/main.tsus", "line": 10, "col": 3 },
+ { "code": "TSUS412", "file": "src/main.tsus", "line": 15, "col": 3 }
+ ]
+}
View file
Addedexamples/58-typed-promise-rules/src/main.tsus+22−0
@@ -0,0 +1,22 @@
+// Calls through a parameter: the per-file binder cannot see what api.beat() or
+// store.save() returns, so TSUS411 and TSUS412 need the project's types.
+import { Ok, type Result } from "@tsus/runtime";
+
+type Api = { readonly beat: () => Promise<number> };
+type Store = { readonly save: (row: string) => Result<true, "full"> };
+
+export const tick = (api: Api): number => {
+ void api.beat();
+ api.beat();
+ return 1;
+};
+
+export const persist = (store: Store, row: string): string => {
+ store.save(row);
+ return "saved";
+};
+
+export const fine = async (api: Api, store: Store): Promise<Result<true, "full">> => {
+ await api.beat();
+ return store.save("r1").andThen(() => Ok(true));
+};
View file
Changedpackages/cli/src/build.js+38−2
@@ -4,7 +4,8 @@
import fs from "node:fs";
import path from "node:path";
import { compileTsus, emitJs, relativeSourcePath, specifierTransformer } from "./compile.js";
-import { emitDeclarations, semanticCheck } from "./program.js";
+import { emitDeclarations, semanticCheck, virtualName } from "./program.js";
+import { runRules } from "./rules/index.js";
const walk = (dir) => {
const out = [];
@@ -33,6 +34,37 @@
// 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 } = {}) => {
@@ -49,7 +81,11 @@
diagnostics.push(...c.diagnostics);
}
if (semantic && !diagnostics.some((d) => BLOCKING.has(d.code))) {
- diagnostics.push(...semanticCheck({ rootDir, tsFiles: files.filter(isTsFile), compiled }));
+ 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 };
View file
Changedpackages/cli/src/program.js+5−2
@@ -29,7 +29,7 @@
const MATCH_CODES = new Set(["TSUS201", "TSUS202", "TSUS203"]);
const IMPLICIT_ANY = new Set([7006, 7031]);
-const virtualName = (file) => `${file}.ts`;
+export const virtualName = (file) => `${file}.ts`;
// The deepest node whose first token starts exactly at pos.
const nodeStartingAt = (sf, pos) => {
@@ -120,10 +120,13 @@
const rootsOf = (tsFiles, compiled) => [...tsFiles, ...[...compiled.keys()].map(virtualName)];
-export const semanticCheck = ({ rootDir, tsFiles, compiled }) => {
+// onProgram (optional) receives the checked ts.Program, so the type-aware
+// strictness rules can run against the same types.
+export const semanticCheck = ({ rootDir, tsFiles, compiled, onProgram }) => {
const host = projectHost(rootDir, compiled, COMPILER_OPTIONS);
const roots = rootsOf(tsFiles, compiled);
const program = ts.createProgram({ rootNames: roots, options: COMPILER_OPTIONS, host });
+ onProgram?.(program);
const checker = program.getTypeChecker();
const diagnostics = [];
View file

Checks

No checks reported for this commit.

Runs

No runs for this commit.