Feature packs describe globals; the Node and browser packs report theirs #57

Merged
sionzee merged 3 commits from evobug/pack-globals into main 2026-11-16 21:34:40 +01:00
Owner

What

Feature packs can describe globals, and the Node and browser packs now do.

  • Format. safe-ts.json takes a globals part, shaped like a module without specifiers:
    functions (fetch, new URL, WebAssembly.compile), classes (extends may list several
    bases, for lib.dom's mixins), creates (fetch gives a Response; a property too:
    Crypto#subtle is a SubtleCrypto) and instances (localStorage is a Storage). A bare
    class name is a global class. Documented in docs/guide/libraries.md (pack format, and a new
    "Globals" section).
  • Matching. It applies to names nothing in the file declares or imports (also as window.x,
    self.x, globalThis.x), to what they give ((await fetch(url)).json(), also through
    unsafe / unsafeAsync after isErr narrowing), and to values declared with a global type: a
    parameter, local or field (res: Response, canvas: HTMLCanvasElement, private store: Storage)
    or a cast (el as HTMLCanvasElement). A local or imported name or type of the same name doesn't
    match. A global method is never matched by its name alone. This reuses the module machinery in
    features.rs (resolve, creates, instances, class chains) with an Origin::Global in the
    analyzer. package_type and the new global_type share the type unwrapping (named_types).
  • Fallback. Without a pack the hard-coded list (JSON.parse, new URL, structuredClone,
    atob, ...) applies as before. Where an enabled pack lists the same global, the pack's entry is
    reported instead, with its error (`new URL()` (global) can throw `TypeError`). The built-in
    "can't fail" cases stay quiet either way (new URL("./x", import.meta.url), JSON.stringify of a
    plain object, encodeURIComponent of a literal).
  • Generated packs.
    • Browser: safe-ts.json is generated by packages/feature-browser/test/pack.ts from
      browser-api.json (the 184 throws/rejects members and their errors texts) plus
      lib.dom.d.ts (bases, what functions, methods and properties return, global variables). I chose
      a generated file over reading the map at load time because the checker would otherwise need
      lib.dom.d.ts from the user's TypeScript. browser-api.json stays the one source of truth: a new
      manifest.test.ts check fails when the committed file isn't what the script makes. Added to
      files.
    • Node: pack() in test/inventory.ts also emits globals. node-api.json gained errors and
      creates for Node's globals, and test/triggers.ts makes them fail on the running Node (now 413
      of 448 failing members verified).
  • Several packs. Lookups now search every enabled pack. Before, the first pack that described a
    module hid the others' classes and instances, which broke Node + browser. For a global both
    describe, the first pack in "features" supplies the error text.
  • Precision fix. decode() on a TextDecoder made without fatal: true isn't reported: such a
    decoder can't throw. This was the only new finding on the RTSS client, and it was wrong.
  • The "no rules yet" warning is gone for the browser pack, and "not reported yet" is removed from
    README and libraries.md.

Default on or opt-in

Opt-in. The Vite plugin knows the code runs in a browser, but turning the pack on there would add
findings to every existing app on upgrade, and with check: "error" it would fail builds.
Nothing else turns a pack on implicitly either ("Enabling a pack is explicit"). The docs say to
enable it for browser code.

New findings

  • The CHANGELOG has an Unreleased → Changed entry covering what is now reported and how to opt out.
  • Docs examples (the docs now also enable the browser pack): 5 new findings, all correct, all
    fetch() without handling (from the Node pack's globals). fetch-signal.md now wraps fetch in
    unsafeAsync. The react-uncached-promise.md bad example lists unhandled-failure too.
    Nothing new in the Vue or React sections.
  • examples/vue, examples/react, examples/demo and the playground presets, with the browser pack
    on: no new findings (they already use unsafeAsync for fetch and response.text()).
  • TwitchRTS (scratch copy, SAFE_TS_CACHE_DIR in /tmp): client, server, admin and packages all
    have 0 new findings after the TextDecoder fix (1 before it, a false positive). RTSS already wraps
    its browser calls. With unsafe stripped in a second copy, the packs match 7 global calls
    (3 fetch, 1 localStorage.setItem, 3 new URL now carrying the pack's text), all correct.
  • Cost: the browser pack's JSON (94 KB) adds about 5–8 ms per process. Release check of the RTSS
    client: 58 → 66 ms with both packs, unchanged without the browser pack.

Verification

cargo nextest run --workspace (194 passed, including the new pack_globals fixture: two packs,
shadowing, typed values, fields, casts, new, fallback replacement, unsafeAsync, TextDecoder),
cargo clippy -q --workspace --all-targets (no new warnings), npm run build, npm run test:std
(257), npm run typecheck, npm run typecheck:emit, node scripts/docs-typecheck.mjs,
npm run test:docs, npm run test:vite, npm run bench:check: all pass.

scripts/verify-packages.mjs has a new browser-pack check, which passes from the packed tarball.
The script as a whole fails at its earlier Vite step, the same way on main: automatic mode
analyzes the installed std tarball and reports match() (describe can throw). That's separate
from this PR.

Docs changed: README.md, CHANGELOG.md, docs/guide/libraries.md, docs/rules/unhandled-failure.md,
docs/rules/fetch-signal.md, docs/react/rules/react-uncached-promise.md, docs/package.json.

🤖 Generated with Claude Code

## What Feature packs can describe globals, and the Node and browser packs now do. - **Format.** `safe-ts.json` takes a `globals` part, shaped like a module without `specifiers`: `functions` (`fetch`, `new URL`, `WebAssembly.compile`), `classes` (`extends` may list several bases, for lib.dom's mixins), `creates` (`fetch` gives a `Response`; a property too: `Crypto#subtle` is a `SubtleCrypto`) and `instances` (`localStorage` is a `Storage`). A bare class name is a global class. Documented in docs/guide/libraries.md (pack format, and a new "Globals" section). - **Matching.** It applies to names nothing in the file declares or imports (also as `window.x`, `self.x`, `globalThis.x`), to what they give (`(await fetch(url)).json()`, also through `unsafe` / `unsafeAsync` after `isErr` narrowing), and to values declared with a global type: a parameter, local or field (`res: Response`, `canvas: HTMLCanvasElement`, `private store: Storage`) or a cast (`el as HTMLCanvasElement`). A local or imported name or type of the same name doesn't match. A global method is never matched by its name alone. This reuses the module machinery in features.rs (`resolve`, `creates`, `instances`, class chains) with an `Origin::Global` in the analyzer. `package_type` and the new `global_type` share the type unwrapping (`named_types`). - **Fallback.** Without a pack the hard-coded list (`JSON.parse`, `new URL`, `structuredClone`, `atob`, ...) applies as before. Where an enabled pack lists the same global, the pack's entry is reported instead, with its error (`` `new URL()` (global) can throw `TypeError` ``). The built-in "can't fail" cases stay quiet either way (`new URL("./x", import.meta.url)`, `JSON.stringify` of a plain object, `encodeURIComponent` of a literal). - **Generated packs.** - Browser: `safe-ts.json` is generated by `packages/feature-browser/test/pack.ts` from `browser-api.json` (the 184 `throws`/`rejects` members and their `errors` texts) plus `lib.dom.d.ts` (bases, what functions, methods and properties return, global variables). I chose a generated file over reading the map at load time because the checker would otherwise need lib.dom.d.ts from the user's TypeScript. `browser-api.json` stays the one source of truth: a new `manifest.test.ts` check fails when the committed file isn't what the script makes. Added to `files`. - Node: `pack()` in `test/inventory.ts` also emits `globals`. `node-api.json` gained `errors` and `creates` for Node's globals, and `test/triggers.ts` makes them fail on the running Node (now 413 of 448 failing members verified). - **Several packs.** Lookups now search every enabled pack. Before, the first pack that described a module hid the others' classes and instances, which broke Node + browser. For a global both describe, the first pack in `"features"` supplies the error text. - **Precision fix.** `decode()` on a `TextDecoder` made without `fatal: true` isn't reported: such a decoder can't throw. This was the only new finding on the RTSS client, and it was wrong. - The "no rules yet" warning is gone for the browser pack, and "not reported yet" is removed from README and libraries.md. ## Default on or opt-in Opt-in. The Vite plugin knows the code runs in a browser, but turning the pack on there would add findings to every existing app on upgrade, and with `check: "error"` it would fail builds. Nothing else turns a pack on implicitly either ("Enabling a pack is explicit"). The docs say to enable it for browser code. ## New findings - The CHANGELOG has an Unreleased → Changed entry covering what is now reported and how to opt out. - Docs examples (the docs now also enable the browser pack): 5 new findings, all correct, all `fetch()` without handling (from the Node pack's globals). `fetch-signal.md` now wraps `fetch` in `unsafeAsync`. The `react-uncached-promise.md` bad example lists `unhandled-failure` too. Nothing new in the Vue or React sections. - `examples/vue`, `examples/react`, `examples/demo` and the playground presets, with the browser pack on: no new findings (they already use `unsafeAsync` for `fetch` and `response.text()`). - TwitchRTS (scratch copy, `SAFE_TS_CACHE_DIR` in /tmp): client, server, admin and packages all have 0 new findings after the TextDecoder fix (1 before it, a false positive). RTSS already wraps its browser calls. With `unsafe` stripped in a second copy, the packs match 7 global calls (3 `fetch`, 1 `localStorage.setItem`, 3 `new URL` now carrying the pack's text), all correct. - Cost: the browser pack's JSON (94 KB) adds about 5–8 ms per process. Release `check` of the RTSS client: 58 → 66 ms with both packs, unchanged without the browser pack. ## Verification `cargo nextest run --workspace` (194 passed, including the new `pack_globals` fixture: two packs, shadowing, typed values, fields, casts, `new`, fallback replacement, `unsafeAsync`, TextDecoder), `cargo clippy -q --workspace --all-targets` (no new warnings), `npm run build`, `npm run test:std` (257), `npm run typecheck`, `npm run typecheck:emit`, `node scripts/docs-typecheck.mjs`, `npm run test:docs`, `npm run test:vite`, `npm run bench:check`: all pass. `scripts/verify-packages.mjs` has a new browser-pack check, which passes from the packed tarball. The script as a whole fails at its earlier Vite step, the same way on `main`: automatic mode analyzes the installed std tarball and reports `match()` (`describe` can throw). That's separate from this PR. Docs changed: README.md, CHANGELOG.md, docs/guide/libraries.md, docs/rules/unhandled-failure.md, docs/rules/fetch-signal.md, docs/react/rules/react-uncached-promise.md, docs/package.json. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
A pack's safe-ts.json takes a `globals` part (a module's shape without
specifiers: functions incl. `new X`, classes with several bases, creates incl.
properties, instances). It applies to names nothing in the file declares or
imports (also via window./self./globalThis.), to what they give (also through
unsafe/unsafeAsync), and to values declared with a global type (parameter,
local, field, `as` cast). A pack's entry replaces the built-in check of the
same global; without a pack the built-in list applies as before.

The browser pack gets a safe-ts.json generated from browser-api.json and
lib.dom.d.ts (test/pack.ts, checked by manifest.test.ts). The Node pack's
generator emits `globals` from node-api.json, which gains errors and creates
for Node's globals, checked by new triggers on the running Node.

Lookups now search every enabled pack (Node and browser both have globals),
and decode() on a TextDecoder made without `fatal: true` isn't reported.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Fixture pack_globals: commit its local packs
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m13s
5a64e7d60e
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merge remote-tracking branch 'origin/main' into up-57
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m10s
f0f57b4470
sionzee deleted branch evobug/pack-globals 2026-11-16 21:34:40 +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!57
No description provided.