feature-node: match methods by the class of the value, not by name #26

Merged
sionzee merged 1 commit from fix/feature-node-method-receivers into main 2026-11-04 21:22:25 +01:00
Owner

What

The Node pack matched methods by name across a whole module. A name that fails on one class was reported on any value from that module:

  • fs.createReadStream(p).read() was reported as if it were FileHandle#read.
  • The new ZIP names from #23 (open, stream, content, ...) were reported on anything a node:zlib chain returns, e.g. createGzip().pipe(dest).open().

To limit the damage, the generator dropped every name used twice in a module, which also lost real failures: zip.get(), zip.close(), zip.add(), zipBuffer.get(), gunzip.toArray().

How it works now

Pack format (safe-ts.json, generated by inventory.ts --write-pack). New per-module fields:

  • classes: per class, its failing statics (ZipFile.open()), methods (zip.get()) and extends ("node:stream Transform"). A class is listed when it or a base has failing members.
  • creates: callable → the class of its (awaited) result. Keys can be fn, ns.fn, Class.static, Class#method or new Alias. Example: ZipFile.open → ZipFile, ZipFile#get → ZipEntry, createGzip → Gzip, fs.promises.open → FileHandle.
  • instances: exported values of a class (performance).
  • methods (name-only, unchanged meaning) now only holds members that belong to no class (fs.promises.readFile).

node-api.json has two new sections:

  • extends: each class's nearest base. Written from the running Node by inventory.ts --write-extends. The manifest test checks it is current. node inventory.ts says when it changed.
  • creates: hand-written. I checked every entry by calling it on Node 26.13. The manifest test checks that each one names a mapped callable and an existing class, and checks exported instances at runtime.

Checker (features.rs, analyze.rs, project.rs):

  • A receiver's origin now records how it is reached from the import (Get/Call/New steps, also across files). FeatureSet::lookup_method resolves that path to a class through creates/instances.
  • A value of a known class is checked against its class and its bases, across modules. It never falls back to name matching.
  • Other values are checked against the name-only methods, as before. Other packs (zod, vue-router) use only methods and behave as before.
  • new X() is now followed as an origin (before, new values had none). For other packs, that means name-only methods and automatic mode can now apply to values made with new from a package.
  • safe-ts coverage uses the same lookup.

Counts: 189 functions as before. Methods went from 231 name-only to 35 name-only plus 215 class members (212 methods, 3 statics).

Collisions in other modules

Same module. I checked every name main matched by name alone against every other class in the same module, inherited methods included (now possible with extends). One real collision: node:fs read. It rejects on Dir/FileHandle, but ReadStream#read (from Readable) never fails. Fixed by this PR and covered by a fixture.

Across chains. These names were matched on any value from their module, including values that leave the module through a chain (pipe, user objects built from module values):

  • node:sqlite open, get, run, all, exec, prepare, iterate
  • node:buffer write
  • node:stream/web read/write
  • node:wasi start
  • node:inspector/promises post
  • node:worker_threads postMessage
  • node:crypto final/sign
  • node:http2 request
  • node:util decode
  • node:module resolve
  • node:perf_hooks measure
  • node:readline/promises question/commit
  • node:zlib open/stream/content/toBuffer

They are all class members now, so they only match receivers of that class.

Coverage kept. Every class method main matched by name is still reachable: through new, a creates factory, or an instances export (performance, createRequire()).

Tests

New fixture crates/safe_ts_core/tests/fixtures/node_classes:

  • Must not be flagged: gzip stream .on()/.write()/.end(), createHash().update().digest(), fs.createReadStream(p).read(), and createGzip().pipe(destination).open().
  • Must be flagged: ZipFile.open/openSync, zip.get/getSync/close, entry.content/contentSync, zipBuffer.get(), a ZipFile opened in another file, gunzip.toArray() (through Readable), open() → handle.read(), fs.promises.open() → .read(), signer.sign(), performance.measure(), createRequire(...).resolve().

Against main's checker and pack, this fixture fails with 2 false positives and 5 misses.

Manifest tests (npm run test:std) have two new checks: bases match the running Node, and creates is consistent.

Commands run:

  • cargo nextest run: 104 passed
  • npm run test:std: 132 passed (incl. 4 manifest tests)
  • npm run test:vite: passes
  • npm run test:docs: passes (new checked bad/good example in docs/guide/libraries.md)
  • npm run typecheck: passes
  • clippy: no new warnings

Left out / uncertain

  • Constructors are still not checked. new ZipBuffer(bytes) can throw, but no constructor is reported yet (as the pack already said). The fixture notes it.
  • creates is hand-written, so a factory missing from it leaves its values classless. That gives fewer reports, not wrong ones. Values that only arrive through callbacks or events (ServerResponse, server-side TLSSocket) have no origin, as before.
  • Quick-fix bug found while writing the fixture (not fixed here). The unsafeAsync(() => ...) fix on a call whose receiver contains await (await (await zip.get(n)).content()) produces an await inside a non-async arrow. The fixture splits those lines.
  • Docs. The guide's Node section, pack-format reference and "What the packs can't see" are updated, and the README table/bullets too.

🤖 Generated with Claude Code

## What The Node pack matched methods **by name across a whole module**. A name that fails on one class was reported on any value from that module: - `fs.createReadStream(p).read()` was reported as if it were `FileHandle#read`. - The new ZIP names from #23 (`open`, `stream`, `content`, ...) were reported on anything a `node:zlib` chain returns, e.g. `createGzip().pipe(dest).open()`. To limit the damage, the generator dropped every name used twice in a module, which also lost real failures: `zip.get()`, `zip.close()`, `zip.add()`, `zipBuffer.get()`, `gunzip.toArray()`. ## How it works now **Pack format** (`safe-ts.json`, generated by `inventory.ts --write-pack`). New per-module fields: - `classes`: per class, its failing `statics` (`ZipFile.open()`), `methods` (`zip.get()`) and `extends` (`"node:stream Transform"`). A class is listed when it or a base has failing members. - `creates`: callable → the class of its (awaited) result. Keys can be `fn`, `ns.fn`, `Class.static`, `Class#method` or `new Alias`. Example: `ZipFile.open` → ZipFile, `ZipFile#get` → ZipEntry, `createGzip` → Gzip, `fs.promises.open` → FileHandle. - `instances`: exported values of a class (`performance`). - `methods` (name-only, unchanged meaning) now only holds members that belong to no class (`fs.promises.readFile`). **node-api.json** has two new sections: - `extends`: each class's nearest base. Written from the running Node by `inventory.ts --write-extends`. The manifest test checks it is current. `node inventory.ts` says when it changed. - `creates`: hand-written. I checked every entry by calling it on Node 26.13. The manifest test checks that each one names a mapped callable and an existing class, and checks exported instances at runtime. **Checker** (`features.rs`, `analyze.rs`, `project.rs`): - A receiver's origin now records how it is reached from the import (`Get`/`Call`/`New` steps, also across files). `FeatureSet::lookup_method` resolves that path to a class through `creates`/`instances`. - A value of a known class is checked against its class and its bases, across modules. It never falls back to name matching. - Other values are checked against the name-only `methods`, as before. Other packs (zod, vue-router) use only `methods` and behave as before. - `new X()` is now followed as an origin (before, `new` values had none). For other packs, that means name-only `methods` and automatic mode can now apply to values made with `new` from a package. - `safe-ts coverage` uses the same lookup. Counts: 189 functions as before. Methods went from 231 name-only to 35 name-only plus 215 class members (212 methods, 3 statics). ## Collisions in other modules **Same module.** I checked every name main matched by name alone against every other class in the same module, inherited methods included (now possible with `extends`). One real collision: **node:fs `read`**. It rejects on `Dir`/`FileHandle`, but `ReadStream#read` (from Readable) never fails. Fixed by this PR and covered by a fixture. **Across chains.** These names were matched on *any* value from their module, including values that leave the module through a chain (`pipe`, user objects built from module values): - node:sqlite `open`, `get`, `run`, `all`, `exec`, `prepare`, `iterate` - node:buffer `write` - node:stream/web `read`/`write` - node:wasi `start` - node:inspector/promises `post` - node:worker_threads `postMessage` - node:crypto `final`/`sign` - node:http2 `request` - node:util `decode` - node:module `resolve` - node:perf_hooks `measure` - node:readline/promises `question`/`commit` - node:zlib `open`/`stream`/`content`/`toBuffer` They are all class members now, so they only match receivers of that class. **Coverage kept.** Every class method main matched by name is still reachable: through `new`, a `creates` factory, or an `instances` export (`performance`, `createRequire()`). ## Tests New fixture `crates/safe_ts_core/tests/fixtures/node_classes`: - **Must not be flagged:** gzip stream `.on()`/`.write()`/`.end()`, `createHash().update().digest()`, `fs.createReadStream(p).read()`, and `createGzip().pipe(destination).open()`. - **Must be flagged:** `ZipFile.open`/`openSync`, `zip.get`/`getSync`/`close`, `entry.content`/`contentSync`, `zipBuffer.get()`, a ZipFile opened in another file, `gunzip.toArray()` (through Readable), `open()` → `handle.read()`, `fs.promises.open()` → `.read()`, `signer.sign()`, `performance.measure()`, `createRequire(...).resolve()`. Against main's checker and pack, this fixture fails with 2 false positives and 5 misses. Manifest tests (`npm run test:std`) have two new checks: bases match the running Node, and `creates` is consistent. Commands run: - `cargo nextest run`: 104 passed - `npm run test:std`: 132 passed (incl. 4 manifest tests) - `npm run test:vite`: passes - `npm run test:docs`: passes (new checked bad/good example in `docs/guide/libraries.md`) - `npm run typecheck`: passes - clippy: no new warnings ## Left out / uncertain - **Constructors are still not checked.** `new ZipBuffer(bytes)` can throw, but no constructor is reported yet (as the pack already said). The fixture notes it. - **`creates` is hand-written**, so a factory missing from it leaves its values classless. That gives fewer reports, not wrong ones. Values that only arrive through callbacks or events (`ServerResponse`, server-side `TLSSocket`) have no origin, as before. - **Quick-fix bug found while writing the fixture (not fixed here).** The `unsafeAsync(() => ...)` fix on a call whose receiver contains `await` (`await (await zip.get(n)).content()`) produces an `await` inside a non-async arrow. The fixture splits those lines. - **Docs.** The guide's Node section, pack-format reference and "What the packs can't see" are updated, and the README table/bullets too. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
feature-node: match methods by the class of the value, not by name
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m3s
f462b08884
The pack listed methods by name per module, so a name that fails on one
class was reported on any value from that module: `fs.createReadStream(p)
.read()` as if it were a FileHandle's, and the new ZIP names (`open`,
`stream`, `content`) on anything a node:zlib chain returns. To avoid such
collisions the generator dropped every name used twice, which also lost
real failures (`zip.get()`, `zip.close()`, `gunzip.toArray()`).

The pack now has, per module:
- `classes`: failing statics and methods per class, and its base class.
  The bases come from the running Node (`inventory.ts --write-extends`,
  checked by the manifest test), so a gzip stream is a Readable.
- `creates`: which class a callable returns (`ZipFile.open` -> ZipFile,
  `ZipFile#get` -> ZipEntry, `createGzip` -> Gzip), and `instances` for
  exported instances (`performance`). Written by hand in node-api.json,
  each entry checked by calling it; the manifest test checks that they
  name mapped callables and existing classes.
- `methods`: only members that belong to no class (`fs.promises.readFile`).

The checker records how a receiver is reached from its module (`Get`,
`Call`, `New` steps, across files too) and resolves its class through the
packs. A value of a known class is checked against its class and bases
only; other values against the module's name-only `methods`, as before.
`new X()` is now followed as an origin.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee deleted branch fix/feature-node-method-receivers 2026-11-04 21:22:26 +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!26
No description provided.