Values through a binding: a const read once, a callback by name, Readonly<T> and ?. on a present value read as what they are (523 silent sites to 356) #135

Merged
sionzee merged 141 commits from values-through-bindings into main 2026-11-22 17:33:43 +01:00
Owner

Based on spelling-harness (#134), and contains it: the diff shown is this piece alone.

#134 measured where a rule goes silent when the same code is written another way, and fixed what one normalisation of the tree could fix. What it left was on the list with reasons, and four groups were most of it:

await saveLater(text);                      const pending = saveLater(text);  await pending;        // a value through a const
button.addEventListener("click", () => f());   const onClick = () => f();  button.addEventListener("click", onClick);   // a callback by name
function total(prices: Record<string, number>)     function total(prices: Readonly<Record<string, number>>)                // Readonly<T> around a type
doc.title.trim();                           doc?.title.trim();    // where `doc` is a parameter that cannot be absent

In each row a rule that reported the left form was silent on the right one, or reported the right one for nothing.

What this adds

Written another way Silent sites Reports the fixture does not have
a value through a const 200 → 126 288 → 247 (138 are missing-mut, by design)
x?.m() on a value that is there 73 → 28 102 → 73
a callback passed by name 52 → 40 104 → 64
Readonly<T> around a parameter's type 38 → 2 87 → 13
the whole list 523 → 356 903 → 716

A const read once is its value, as a question rules ask (analyze/values.rs: value_of, function_of), not a rewrite of the tree. The first attempt moved the value to where it is read and broke four things: rules that are about the binding (a Result kept in a local, missing-mut), code that builds spans assuming a node contains its children, messages that then quoted the moved value, and line numbers. The answer is given only where nothing can come between: the const is read exactly once, in the next statement, evaluated first there, not in a branch, a loop or a nested function. So this.items.push(await load()) stays different from const r = await load(); this.items.push(r), which reads this.items after the wait.

A function passed by name is found the same way. A listener passed by a name nothing else reads cannot be removed either, so listener-cleanup reports it like the arrow written in place; a function handed by name to unsafe(...) is guarded in its body, which removes 40 of 43 false unhandled-failure reports.

Readonly<T> on a parameter is opened in the normalisation, keeping the written span. Readonly<Mut<T>> is still a loan: TypeScript keeps the brand, and Readonly only blocks assigning its fields.

x?.m() is x.m() where the file shows x cannot be absent: a parameter or variable whose written type has no undefined or null (aliases of the file opened; imports, type parameters and unknown count as "maybe"), or one bound to new, a literal or a function.

Evidence

  • Nothing lost. Every finding of the base on the fixtures, the three examples, an adopter's project and nineteen held-out projects is still there. On four open-source code bases one finding goes, and it was wrong: a null that only leaves through JSON.stringify, which the rule already allows when written in place.
  • An adopter's project: 14 new findings, all no-object-dictionary on parameters typed Readonly<Record<string, V>> and read by a key from data: the rule did not see through Readonly. All read by the implementer: 13 are what the rule is for, one is a false positive the rule gives the same way without Readonly.
  • Twin cases of mine (one program written both ways): a dropped Result awaited through a const and a listener passed by a name used once are now reported like their twins. Correct code stays silent both ways, and where something happens between the const and its read (a write, an await, a second call) nothing new is reported.
  • The transform is untouched: safe-ts emit is byte-identical to the base on the demo, std's tests and files full of these spellings.
  • Cost: none measurable. Per-file analysis on that project's largest entry 202 ms and 207 ms, whole-program pass 602 ms and 594 ms (minimum of 15 interleaved runs).

What turns red on upgrade

Three expectations in this repository's own fixtures changed, each the twin of a form already reported:

  • const pending = saveAsync(order); await pending; drops the Result exactly as await saveAsync(order); does: must-use-result.
  • A listener passed by a name read nowhere else: listener-cleanup.
  • A dictionary behind Readonly<Record<...>>: no-object-dictionary.

What is left

The 126 sites through a const that remain are rules that do not ask the question yet. The machinery is there; about 35 rules still match their shape where the value is written. The largest is must-use-result's kept-Result analysis (18), which is a pass of its own. Each line of the list says so.

  • The reverse direction did not move (33 sites): a rule that only follows the binding still misses the value written in place.
  • A named function treated as its callback written in place in general: the propagation analysis gives a named function its own entry, and the fixtures pin that.
  • ?. on an import, a call's result, a member read, and globals that may really be absent (window, process): 28 sites.
  • Copies of a member path (const items = this.items; items.push(x)): aliasing, a piece of its own.

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

Based on `spelling-harness` (#134), and contains it: the diff shown is this piece alone. #134 measured where a rule goes silent when the same code is written another way, and fixed what one normalisation of the tree could fix. What it left was on the list with reasons, and four groups were most of it: ```ts await saveLater(text); const pending = saveLater(text); await pending; // a value through a const button.addEventListener("click", () => f()); const onClick = () => f(); button.addEventListener("click", onClick); // a callback by name function total(prices: Record<string, number>) function total(prices: Readonly<Record<string, number>>) // Readonly<T> around a type doc.title.trim(); doc?.title.trim(); // where `doc` is a parameter that cannot be absent ``` In each row a rule that reported the left form was silent on the right one, or reported the right one for nothing. ## What this adds | Written another way | Silent sites | Reports the fixture does not have | |---|---|---| | a value through a `const` | 200 → 126 | 288 → 247 (138 are `missing-mut`, by design) | | `x?.m()` on a value that is there | 73 → 28 | 102 → 73 | | a callback passed by name | 52 → 40 | 104 → 64 | | `Readonly<T>` around a parameter's type | 38 → 2 | 87 → 13 | | **the whole list** | **523 → 356** | **903 → 716** | **A `const` read once is its value, as a question rules ask** (`analyze/values.rs`: `value_of`, `function_of`), not a rewrite of the tree. The first attempt moved the value to where it is read and broke four things: rules that are about the binding (a `Result` kept in a local, `missing-mut`), code that builds spans assuming a node contains its children, messages that then quoted the moved value, and line numbers. The answer is given only where nothing can come between: the `const` is read exactly once, in the next statement, evaluated first there, not in a branch, a loop or a nested function. So `this.items.push(await load())` stays different from `const r = await load(); this.items.push(r)`, which reads `this.items` after the wait. **A function passed by name** is found the same way. A listener passed by a name nothing else reads cannot be removed either, so `listener-cleanup` reports it like the arrow written in place; a function handed by name to `unsafe(...)` is guarded in its body, which removes 40 of 43 false `unhandled-failure` reports. **`Readonly<T>` on a parameter is opened** in the normalisation, keeping the written span. `Readonly<Mut<T>>` is still a loan: TypeScript keeps the brand, and `Readonly` only blocks assigning its fields. **`x?.m()` is `x.m()` where the file shows `x` cannot be absent**: a parameter or variable whose written type has no `undefined` or `null` (aliases of the file opened; imports, type parameters and `unknown` count as "maybe"), or one bound to `new`, a literal or a function. ## Evidence - **Nothing lost.** Every finding of the base on the fixtures, the three examples, an adopter's project and nineteen held-out projects is still there. On four open-source code bases one finding goes, and it was wrong: a `null` that only leaves through `JSON.stringify`, which the rule already allows when written in place. - **An adopter's project:** 14 new findings, all `no-object-dictionary` on parameters typed `Readonly<Record<string, V>>` and read by a key from data: the rule did not see through `Readonly`. All read by the implementer: 13 are what the rule is for, one is a false positive the rule gives the same way without `Readonly`. - **Twin cases of mine** (one program written both ways): a dropped `Result` awaited through a `const` and a listener passed by a name used once are now reported like their twins. Correct code stays silent both ways, and where something happens between the `const` and its read (a write, an `await`, a second call) nothing new is reported. - **The transform is untouched**: `safe-ts emit` is byte-identical to the base on the demo, std's tests and files full of these spellings. - **Cost:** none measurable. Per-file analysis on that project's largest entry 202 ms and 207 ms, whole-program pass 602 ms and 594 ms (minimum of 15 interleaved runs). ## What turns red on upgrade Three expectations in this repository's own fixtures changed, each the twin of a form already reported: - `const pending = saveAsync(order); await pending;` drops the `Result` exactly as `await saveAsync(order);` does: `must-use-result`. - A listener passed by a name read nowhere else: `listener-cleanup`. - A dictionary behind `Readonly<Record<...>>`: `no-object-dictionary`. ## What is left **The 126 sites through a `const` that remain are rules that do not ask the question yet.** The machinery is there; about 35 rules still match their shape where the value is written. The largest is `must-use-result`'s kept-`Result` analysis (18), which is a pass of its own. Each line of the list says so. - The reverse direction did not move (33 sites): a rule that only follows the binding still misses the value written in place. - A named function treated as its callback written in place in general: the propagation analysis gives a named function its own entry, and the fixtures pin that. - `?.` on an import, a call's result, a member read, and globals that may really be absent (`window`, `process`): 28 sites. - Copies of a member path (`const items = this.items; items.push(x)`): aliasing, a piece of its own. Created with Claude Opus 5.5 (implementation) and Claude Fable 5.2 (review and acceptance tests) in Claude Code.
Values through a const, functions by name, Readonly<T> and ?. on a present name: one answer each
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m18s
Tests / test (pull_request) Successful in 11m13s
f7145fd618
Open: the spelling harness left four groups of silent sites (a value through a `const`, a
callback passed by name, `Readonly<T>` around a parameter's type, `x?.m()` on a name that is
always there), about 400 of 523, each the lack of one piece of shared machinery.

Closes, once each:
- `analyze/values.rs`: what a `const` read once stands for (read in the next statement,
  evaluated first, only constants before it, no branch, loop or function between, a callee path
  only when the value doesn't wait) and the function a name passed once names. A query, not a
  rewrite: rules about the binding still see it, spans and fixes stay the code as written. The
  analyzer's walk visits such a value where it is read (awaited, returned, dropped there);
  bounded-concurrency, the loans of aliased-mut, the test-assertion chains,
  pinned-constant-assert and listener-cleanup ask it; a function by name handed to
  unsafe(...) is guarded in its body.
- `spelling::normalize` on the checker's tree (`Tree::Read`): `Readonly<T>` as a parameter's
  type is `T` (override-parameter reads the view through `Normalized::read_only`); `?.` is
  taken off a name whose written type, or value, shows it is there. The transform reads
  `Tree::Spelled`: `safe-ts emit` is byte-identical.

misses.txt: silent 523 -> 356, new 903 -> 716; every remaining line of the five groups says why
the machinery doesn't reach it. Fixture changes: kept_results:29 (`const p = f(); await p;`
drops the Result, as `await f();` does), hidden_any:136,138 (a listener by a name read
nowhere else can't be removed), the migration probe's line 42 (a null going out through
JSON.stringify is allowed, as written in place).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee changed target branch from spelling-harness to main 2026-11-22 17:32:26 +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
01c9c4582f
Co-Authored-By: Claude Fable 5.2 <[email protected]>
sionzee deleted branch values-through-bindings 2026-11-22 17:33:43 +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!135
No description provided.