One spelling for every rule: a harness that rewrites each fixture 39 ways, and a normalisation that takes 2,094 silent sites down to 300 #134

Merged
sionzee merged 140 commits from spelling-harness into main 2026-11-22 17:33:37 +01:00
Owner

Based on after-await (#133), and contains it: the diff shown is this piece alone.

A rule recognises the code it was written against. Written another way, the same code often passed in silence:

JSON.parse(text);        JSON["parse"](text);        (JSON).parse(text);        JSON.parse.call(JSON, text);
import * as fs from "node:fs";  fs.readFileSync(p);       const read = readFileSync;  read(p);
import { parse } from "./index.ts";   // which only passes it on from "./parse.ts"
type Loaded = Promise<Result<number, string>>;   async function load(): Loaded { ... }

Each line is one program spelled several ways; a rule that reports the first form should report them all. An agent does not pick spellings to dodge a rule, but it produces every one of them, and each rule had its own, narrower idea of what a call or a type looks like. Nobody had measured how often that mattered.

The harness

crates/safe_ts_core/tests/rewrites.rs rewrites every fixture one meaning-preserving way at one site at a time (39 transformations: a const copy of a callee or a receiver, a namespace import, x?.m(), x["m"](), .call and .apply, a re-export, an arrow for a declaration and back, a callback by name, a value through a const, destructuring, a type alias, Readonly<T>, for...of for forEach, if/else for ?:, and more), keeps the variants tsc accepts, and compares each variant's findings with the fixture's, line by line. About 43,000 variants. A finding the variant loses is a miss.

At the start: 2,094 sites where a rule went silent, across nearly every rule.

The count per rule and transformation is a ratchet in tests/rewrites/misses.txt, each line with its reason. The test fails when a count differs either way, so a new rule arrives with its spelling gaps written down, and a fix takes its line off. The normal run takes about 20 seconds; npm run test:rewrites checks every site and runs in CI.

The fix: one spelling, for every rule

spelling::normalize rewrites the parsed tree once, before the semantic analysis, so that every rule reads the same thing:

  • o["m"] is o.m; parentheses are gone; f.call(t, a) and f.apply(t, [a]) are f(a);
  • a member of a namespace import, a default import and a const copy of a name are the name;
  • a name imported through a module of the project that only passes it on is the name where it is declared;
  • a type alias of the file is read as its body where a type is written;
  • x?.m() is x.m() where x cannot be absent by how it is written; x = x + e is x += e.

On the same fixtures, 2,094 sites became 300. On today's tree, with the fixtures of the 26 rules added since, the list holds 364 (and 159 more where the finding lost is away from the rewritten line).

What the user wrote still decides three things, read off the tree as parsed, each with a test that compares both trees on every fixture:

  • the text of messages and quick fixes (a fix must not spell out an alias the author wrote, or edit an import the tree added);
  • what is used (unused-code and unused-private count the references that are written; an opened alias or a replaced copy is still a use);
  • what a file imports (the module graph, cycles, the unresolved-import warning).

Found on the way

  • The editor went stale. A cached analysis depended on other modules without that being in its key. It now records what it asked and the answers, and asks again once per check.
  • Six quick fixes never parsed: Wrap in unsafe(() => ...) around a call that contains await. Found by applying every fix to every variant; it is unsafeAsync(async () => ...) now.
  • Rules that read source text where they meant the tree: a search for .runAllTimers(, for mut(, for Promise in an annotation, a receiver keyed by its text. Each now reads the tree.
  • The harness found on its own, as misses under two transformations, the narrow receiver lookup in the event rules that #133 fixes.

Evidence

  • No finding lost. Seventeen held-out projects from the earlier pieces: identical to the base on every rule.
  • An adopter's project: the differences are all from following re-exports: no-param-mutation 76 to 168 and prefer-constructor-tag 4 to 181, every one in code that imports through a re-exporting index (13 read by me, all real; the implementer checked all by pattern and read 36); wide-param +2, a parameter read only through a dead const copy; one json-type message that now names the module a type really comes from. Nothing else moves, the three examples are identical.
  • The transform is untouched: safe-ts emit gives byte-identical output on the demo, on std's tests and on a file full of rewritten spellings.
  • Cost: per-file analysis on that project's largest entry 186 ms before, 189 ms after; whole-program pass 466 ms and 478 ms (shipped profile, minimum of 15 interleaved runs, measured before the last two merges). Reading what is used off the tree as parsed then added a second scope analysis: +1.4% on a whole check of that entry. The work-count guard moves by four counts, all names the tests import through a re-exporting module.
  • Soundness corpus: unchanged, 18 of 98 escape.

What turns red on upgrade

Rules now see code they missed. A project that imports through an index file, uses namespace imports, or copies functions into consts will get findings on code that passed. On the adopter that is 271, all in two rules, all through one index.

What stays unseen

The list says, line by line. The large groups:

  • A value followed through a binding (const r = f(); use(r) where the rule reads use(f())): 26 sites for must-use-result, 16 for bounded-concurrency, 15 for aliased-mut. Needs each rule to follow the binding, not a rewrite.
  • A callback passed by name where the rule reads one written in place.
  • x?.m() where only the type says x is there: the tree cannot tell.
  • An alias of an alias, a member-path copy (const s = a.b; s.m()), and rules with an import lookup of their own (six lines).

Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review, acceptance tests and the merges) in Claude Code.

Based on `after-await` (#133), and contains it: the diff shown is this piece alone. A rule recognises the code it was written against. Written another way, the same code often passed in silence: ```ts JSON.parse(text); JSON["parse"](text); (JSON).parse(text); JSON.parse.call(JSON, text); import * as fs from "node:fs"; fs.readFileSync(p); const read = readFileSync; read(p); import { parse } from "./index.ts"; // which only passes it on from "./parse.ts" type Loaded = Promise<Result<number, string>>; async function load(): Loaded { ... } ``` Each line is one program spelled several ways; a rule that reports the first form should report them all. An agent does not pick spellings to dodge a rule, but it produces every one of them, and each rule had its own, narrower idea of what a call or a type looks like. Nobody had measured how often that mattered. ## The harness `crates/safe_ts_core/tests/rewrites.rs` rewrites every fixture one meaning-preserving way at one site at a time (39 transformations: a `const` copy of a callee or a receiver, a namespace import, `x?.m()`, `x["m"]()`, `.call` and `.apply`, a re-export, an arrow for a declaration and back, a callback by name, a value through a `const`, destructuring, a type alias, `Readonly<T>`, `for...of` for `forEach`, `if`/`else` for `?:`, and more), keeps the variants `tsc` accepts, and compares each variant's findings with the fixture's, line by line. About 43,000 variants. A finding the variant loses is a miss. **At the start: 2,094 sites** where a rule went silent, across nearly every rule. The count per rule and transformation is a ratchet in `tests/rewrites/misses.txt`, each line with its reason. The test fails when a count differs either way, so a new rule arrives with its spelling gaps written down, and a fix takes its line off. The normal run takes about 20 seconds; `npm run test:rewrites` checks every site and runs in CI. ## The fix: one spelling, for every rule `spelling::normalize` rewrites the parsed tree once, before the semantic analysis, so that every rule reads the same thing: - `o["m"]` is `o.m`; parentheses are gone; `f.call(t, a)` and `f.apply(t, [a])` are `f(a)`; - a member of a namespace import, a default import and a `const` copy of a name are the name; - a name imported through a module of the project that only passes it on is the name where it is declared; - a type alias of the file is read as its body where a type is written; - `x?.m()` is `x.m()` where `x` cannot be absent by how it is written; `x = x + e` is `x += e`. **On the same fixtures, 2,094 sites became 300.** On today's tree, with the fixtures of the 26 rules added since, the list holds 364 (and 159 more where the finding lost is away from the rewritten line). **What the user wrote still decides three things**, read off the tree as parsed, each with a test that compares both trees on every fixture: - the **text** of messages and quick fixes (a fix must not spell out an alias the author wrote, or edit an import the tree added); - what is **used** (`unused-code` and `unused-private` count the references that are written; an opened alias or a replaced copy is still a use); - what a file **imports** (the module graph, cycles, the unresolved-import warning). ## Found on the way - **The editor went stale.** A cached analysis depended on other modules without that being in its key. It now records what it asked and the answers, and asks again once per check. - **Six quick fixes never parsed**: *Wrap in `unsafe(() => ...)`* around a call that contains `await`. Found by applying every fix to every variant; it is `unsafeAsync(async () => ...)` now. - **Rules that read source text where they meant the tree**: a search for `.runAllTimers(`, for `mut(`, for `Promise` in an annotation, a receiver keyed by its text. Each now reads the tree. - The harness found on its own, as misses under two transformations, the narrow receiver lookup in the event rules that #133 fixes. ## Evidence - **No finding lost.** Seventeen held-out projects from the earlier pieces: identical to the base on every rule. - **An adopter's project:** the differences are all from following re-exports: `no-param-mutation` 76 to 168 and `prefer-constructor-tag` 4 to 181, every one in code that imports through a re-exporting index (13 read by me, all real; the implementer checked all by pattern and read 36); `wide-param` +2, a parameter read only through a dead `const` copy; one `json-type` message that now names the module a type really comes from. Nothing else moves, the three examples are identical. - **The transform is untouched**: `safe-ts emit` gives byte-identical output on the demo, on std's tests and on a file full of rewritten spellings. - **Cost:** per-file analysis on that project's largest entry 186 ms before, 189 ms after; whole-program pass 466 ms and 478 ms (shipped profile, minimum of 15 interleaved runs, measured before the last two merges). Reading what is used off the tree as parsed then added a second scope analysis: +1.4% on a whole check of that entry. The work-count guard moves by four counts, all names the tests import through a re-exporting module. - **Soundness corpus:** unchanged, 18 of 98 escape. ## What turns red on upgrade Rules now see code they missed. A project that imports through an index file, uses namespace imports, or copies functions into `const`s will get findings on code that passed. On the adopter that is 271, all in two rules, all through one index. ## What stays unseen The list says, line by line. The large groups: - **A value followed through a binding** (`const r = f(); use(r)` where the rule reads `use(f())`): 26 sites for `must-use-result`, 16 for `bounded-concurrency`, 15 for `aliased-mut`. Needs each rule to follow the binding, not a rewrite. - **A callback passed by name** where the rule reads one written in place. - **`x?.m()` where only the type says `x` is there**: the tree cannot tell. - **An alias of an alias**, a member-path copy (`const s = a.b; s.m()`), and rules with an import lookup of their own (six lines). Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review, acceptance tests and the merges) in Claude Code.
One spelling for every rule: a harness that rewrites each fixture, and a normalisation of the tree before analysis
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m22s
Tests / test (pull_request) Successful in 10m0s
ba308d09fb
Open: a rule recognised the code it was written against and went silent when the same code was
spelled another way (o["m"](), (o).m(), f.call(t), a namespace import, a const copy, a type
alias, a re-export, x?.m() on a value that is there). Nothing measured how often.

tests/rewrites.rs rewrites every fixture one meaning-preserving way at one site at a time (39
transformations), keeps the variants tsc accepts and compares findings line by line; the count
per rule and transformation is a ratchet in tests/rewrites/misses.txt, each line with its
reason. spelling::normalize rewrites the parsed tree once before semantic analysis, so every
rule reads one spelling; names imported through a module that only passes them on resolve to
where they are declared.

What the user wrote still decides three things, read off the tree as parsed: the text of
messages and fixes, what is used (unused-code, unused-private), and what a file imports. Tests
compare both trees on every fixture for each.

Found on the way and fixed: the editor's cache did not follow other modules; six quick fixes
wrapped a call that awaits in a plain arrow and did not parse; several rules read source text
where they meant the tree.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
sionzee changed target branch from after-await to main 2026-11-22 17:32:25 +01:00
Merge main: the piece below has landed
Some checks failed
Tests / test (pull_request) Has been cancelled
Benchmarks / bench (pull_request) Has been cancelled
0f6796fccb
Co-Authored-By: Claude Fable 5.2 <[email protected]>
sionzee deleted branch spelling-harness 2026-11-22 17:33:37 +01:00
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Evobug/Safe-Ts!134
No description provided.