Rule precision from a 0.4.0 upgrade, and std's latest() #92

Merged
sionzee merged 1 commit from rule-precision-0-4 into main 2026-11-20 16:28:11 +01:00
Owner

An adopter's upgrade to 0.4.0 turned up six precision problems and one missing std helper. I reproduced each one with a probe before changing anything. Item 5 was already handled, so it only gets better help text and docs.

What changed

  1. catch-on-result reported a try that is a PropagateError boundary. readSave / migrate return a Result and also throw CorruptSave, and transitionWorld throws SimFault. The PropagateError map already showed these kinds caught in that try, but the rule only asked whether the callee returns a Result. The rule now skips a try when a call in it propagates a tracked kind that this catch takes (fn_propagation_with + is_caught, the same logic the map uses). It still reports a try around nothing that throws, and a catch that lets the kind through (if (e instanceof Other) ...; throw e).

  2. bounded-concurrency: keysOf(FROZEN) was flagged, Object.entries(FROZEN) wasn't. The rule page says the keys of a fixed table are bounded, so keysOf was the inconsistent one. std's keysOf (imported by name or through import * as dict) over a frozen or as const table now counts as fixed length. keysOf(param) is still reported.

  3. q-needs-result-return ignored the return type of the function a literal is returned from. Methods weren't the problem: const x: Fake = { async fetch() {...} } already worked. The gap was return { ... }. A returned literal (from a return or an arrow's expression body) now gets its callbacks' context from the declared return type, through Promise<T> for an async function. I also fixed a nearby hole: a callback that declares its own return type ((a): number => { q(...) } inside an annotated const) used to be excused by the outer context. It is now checked against its own type.

  4. no-mutable-global: an emitter subscribed with a listener passed in. Set.add and push into a module array were already caught, and so was a listener written in place. target.addEventListener("x", f) / bus.on("x", f) with f a parameter wasn't. The old fixture even excused it on purpose ("can be removed with off"), but nothing checked that anything removed it. Such a subscription is now reported (also this.onTick.bind(this)), except:

    • when the file removes the same listener from the same emitter (off / removeListener / removeEventListener). The listener is matched by its binding (a parameter of another function with the same name doesn't count) or, for this.onTick, as written;
    • when the unsubscriber subscribe(f) returns is kept;
    • for a module-level function, which is the same listener each time and holds nothing per call.

    Subscribing at module level, as in a startup-only new EventTarget(), was never reported and still isn't.

  5. no-for-in over Object.create(null) dictionaries: already fixed in 0.3.0 (#70). Both a visible null-prototype binding and the if (!Object.hasOwn(o, k)) continue; guard are accepted. The premise that for...in over a null-prototype object doesn't allocate is wrong. The rule page's own measurements (and V8) show that for...in over a dictionary-mode object collects its keys into a new array on every walk (97 B for 4 keys). So there is no allocation-free option, and a type can't prove an object has no prototype. A library's dictionaries are made inside the library, so the checker can't see them being made. I changed only the help and the page: the help now names the hasOwn guard as the form for a library's dictionary (no more memory than for...in), and steers to Object.keys + lookup rather than Object.values / entries. The page has a section with an example.

  6. stable-shape with three or more conditional spreads. It is not loosened. The finding now says "This literal leaves out N keys with conditional spreads: up to 2^N shapes ...". The help names the form: every key written, declared key: Option<T>. The rule page has a ✅ example of it. "Assigned after the literal" is not a valid alternative. It makes the same number of shapes, and the rule already reports o.x = ... on a literal without x. The help and the skill say so.

  7. std latest(work, signal?) in packages/std/src/async.ts, with a Superseded error class. Its semantics follow useTask:

    • run(...args) aborts the previous run's signal and gives the new run a fresh one;
    • it resolves to what work resolved to, or to err(Superseded) when a newer run or abort() came first, whatever the older run resolved or rejected with;
    • a rejection of the latest run is a bug and run rejects with it;
    • the optional owner signal aborts the run in progress and makes later runs resolve to Superseded without starting.

    latest-wins-counter now names latest in a file that imports neither Vue/React/Pinia nor their SAFE-TS packages, and useTask (mentioning latest) otherwise. unused-task-signal also checks latest callbacks.

Docs: the rule pages for all of the above, docs/guide/async.md (new "Latest run wins" section), docs/guide/std-helpers.md, the rule tables in docs/README.md and the adoption guide, the skill (tool-gaps.md, patterns-core.md, patterns-vue.md, census.md, final-pass.md), and CHANGELOG.

Tested

  • Each item: a probe before and after (safe-ts check on small programs).
  • Fixtures: new idiomatic_rules/boundary.ts (with propagates / caught annotations) and idiomatic_rules/latest.ts; new cases in bounded_concurrency, question, shapes and subscribers (the old "named listener" exemption became the cases above).
  • std: 4 new latest tests in packages/std/test/async.test.ts.
  • cargo nextest run: 280 passed. npm run test:std: 371 passed. node scripts/docs-typecheck.mjs: 611 examples compile. The docs test (734 examples, bad/good blocks) passes. npm run typecheck passes. safe-ts check packages/std/src/async.ts: no problems.

Left out

  • A PropagateError thrown inside latest's work is reported as escaping a callback (uncaught-propagation), not followed through run's rejection. That is conservative, and the docs say to return the failure as an Err. Modelling latest like mapLimit in the propagation pass would be a separate change.
  • PixiJS's GraphicsContext extends eventemitter3's EventEmitter in its .d.ts, but the checker only follows packs' class chains, so a module-level one still needs "safe-ts": { "emitters": ["pixi.js GraphicsContext"] } to be seen. Reading emitter ancestry from a package's declarations would be a separate change.
  • A module-level const search = latest(...) called from functions isn't reported by no-mutable-global, because the checker doesn't look into std's closures. The docs examples create latest inside the function that owns it.

Created with Claude Opus 5.5 in Claude Code.

An adopter's upgrade to 0.4.0 turned up six precision problems and one missing std helper. I reproduced each one with a probe before changing anything. Item 5 was already handled, so it only gets better help text and docs. ## What changed 1. **`catch-on-result` reported a `try` that is a `PropagateError` boundary.** `readSave` / `migrate` return a `Result` and also throw `CorruptSave`, and `transitionWorld` throws `SimFault`. The PropagateError map already showed these kinds caught in that `try`, but the rule only asked whether the callee returns a `Result`. The rule now skips a `try` when a call in it propagates a tracked kind that this `catch` takes (`fn_propagation_with` + `is_caught`, the same logic the map uses). It still reports a `try` around nothing that throws, and a `catch` that lets the kind through (`if (e instanceof Other) ...; throw e`). 2. **`bounded-concurrency`: `keysOf(FROZEN)` was flagged, `Object.entries(FROZEN)` wasn't.** The rule page says the keys of a fixed table are bounded, so `keysOf` was the inconsistent one. std's `keysOf` (imported by name or through `import * as dict`) over a frozen or `as const` table now counts as fixed length. `keysOf(param)` is still reported. 3. **`q-needs-result-return` ignored the return type of the function a literal is returned from.** Methods weren't the problem: `const x: Fake = { async fetch() {...} }` already worked. The gap was `return { ... }`. A returned literal (from a `return` or an arrow's expression body) now gets its callbacks' context from the declared return type, through `Promise<T>` for an `async` function. I also fixed a nearby hole: a callback that declares its own return type (`(a): number => { q(...) }` inside an annotated `const`) used to be excused by the outer context. It is now checked against its own type. 4. **`no-mutable-global`: an emitter subscribed with a listener passed in.** `Set.add` and `push` into a module array were already caught, and so was a listener written in place. `target.addEventListener("x", f)` / `bus.on("x", f)` with `f` a parameter wasn't. The old fixture even excused it on purpose ("can be removed with `off`"), but nothing checked that anything removed it. Such a subscription is now reported (also `this.onTick.bind(this)`), except: - when the file removes the same listener from the same emitter (`off` / `removeListener` / `removeEventListener`). The listener is matched by its binding (a parameter of another function with the same name doesn't count) or, for `this.onTick`, as written; - when the unsubscriber `subscribe(f)` returns is kept; - for a module-level function, which is the same listener each time and holds nothing per call. Subscribing at module level, as in a startup-only `new EventTarget()`, was never reported and still isn't. 5. **`no-for-in` over `Object.create(null)` dictionaries: already fixed in 0.3.0 (#70).** Both a visible null-prototype binding and the `if (!Object.hasOwn(o, k)) continue;` guard are accepted. The premise that `for...in` over a null-prototype object doesn't allocate is wrong. The rule page's own measurements (and V8) show that `for...in` over a dictionary-mode object collects its keys into a new array on every walk (97 B for 4 keys). So there is no allocation-free option, and a type can't prove an object has no prototype. A library's dictionaries are made inside the library, so the checker can't see them being made. I changed only the help and the page: the help now names the `hasOwn` guard as the form for a library's dictionary (no more memory than `for...in`), and steers to `Object.keys` + lookup rather than `Object.values` / `entries`. The page has a section with an example. 6. **`stable-shape` with three or more conditional spreads.** It is not loosened. The finding now says "This literal leaves out N keys with conditional spreads: up to 2^N shapes ...". The help names the form: every key written, declared `key: Option<T>`. The rule page has a ✅ example of it. "Assigned after the literal" is not a valid alternative. It makes the same number of shapes, and the rule already reports `o.x = ...` on a literal without `x`. The help and the skill say so. 7. **std `latest(work, signal?)`** in `packages/std/src/async.ts`, with a `Superseded` error class. Its semantics follow `useTask`: - `run(...args)` aborts the previous run's signal and gives the new run a fresh one; - it resolves to what `work` resolved to, or to `err(Superseded)` when a newer run or `abort()` came first, whatever the older run resolved or rejected with; - a rejection of the latest run is a bug and `run` rejects with it; - the optional owner `signal` aborts the run in progress and makes later runs resolve to `Superseded` without starting. `latest-wins-counter` now names `latest` in a file that imports neither Vue/React/Pinia nor their SAFE-TS packages, and `useTask` (mentioning `latest`) otherwise. `unused-task-signal` also checks `latest` callbacks. Docs: the rule pages for all of the above, `docs/guide/async.md` (new "Latest run wins" section), `docs/guide/std-helpers.md`, the rule tables in `docs/README.md` and the adoption guide, the skill (`tool-gaps.md`, `patterns-core.md`, `patterns-vue.md`, `census.md`, `final-pass.md`), and CHANGELOG. ## Tested - Each item: a probe before and after (`safe-ts check` on small programs). - Fixtures: new `idiomatic_rules/boundary.ts` (with `propagates` / `caught` annotations) and `idiomatic_rules/latest.ts`; new cases in `bounded_concurrency`, `question`, `shapes` and `subscribers` (the old "named listener" exemption became the cases above). - std: 4 new `latest` tests in `packages/std/test/async.test.ts`. - `cargo nextest run`: 280 passed. `npm run test:std`: 371 passed. `node scripts/docs-typecheck.mjs`: 611 examples compile. The docs test (734 examples, bad/good blocks) passes. `npm run typecheck` passes. `safe-ts check packages/std/src/async.ts`: no problems. ## Left out - A `PropagateError` thrown inside `latest`'s work is reported as escaping a callback (`uncaught-propagation`), not followed through `run`'s rejection. That is conservative, and the docs say to return the failure as an `Err`. Modelling `latest` like `mapLimit` in the propagation pass would be a separate change. - PixiJS's `GraphicsContext` extends eventemitter3's `EventEmitter` in its `.d.ts`, but the checker only follows packs' class chains, so a module-level one still needs `"safe-ts": { "emitters": ["pixi.js GraphicsContext"] }` to be seen. Reading emitter ancestry from a package's declarations would be a separate change. - A module-level `const search = latest(...)` called from functions isn't reported by `no-mutable-global`, because the checker doesn't look into std's closures. The docs examples create `latest` inside the function that owns it. Created with Claude Opus 5.5 in Claude Code.
Rule precision from a 0.4.0 upgrade, and std's latest()
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m54s
1fee685904
- catch-on-result: a try whose callee also throws a tracked PropagateError that the
  catch takes is a boundary, not reported.
- bounded-concurrency: std's keysOf over a frozen or as-const table is bounded, like
  Object.keys/entries/values of it.
- q-needs-result-return: a literal returned from a function takes its callbacks' types
  from the declared return type (methods, arrow values, Promise<T> of async, arrow
  bodies); a callback with its own return type is checked against that.
- no-mutable-global: an emitter subscribed from a function with a listener passed in
  (parameter, this.onX, .bind) is reported unless the file removes it, the subscribe
  result is kept, or it is a module-level function.
- stable-shape: three or more conditional spreads get a message that counts the shapes
  and names the Option<T> form.
- no-for-in: help names the hasOwn guard for library dictionaries.
- std: latest(work, signal?) with Superseded; latest-wins-counter names it outside
  Vue/React, unused-task-signal checks its callbacks.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee force-pushed rule-precision-0-4 from 1fee685904
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m54s
to c48408c6c4
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m53s
2026-11-20 16:28:07 +01:00
Compare
sionzee deleted branch rule-precision-0-4 2026-11-20 16:28:11 +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!92
No description provided.