fetch-signal: report fetch() that nothing can stop #6

Merged
sionzee merged 1 commit from rules/fetch-signal into main 2026-11-03 22:00:54 +01:00
Owner

What

New rule fetch-signal (warn in recommended.json): a fetch(...) with no signal in its init.

Without a signal nothing can stop the request. Browsers set no time limit and Node waits 5 minutes for headers, so a server that accepts the connection and never answers hangs everything that awaits it (a request handler, a queue worker, a spinner).

  • Reported: fetch(url) and fetch(url, init) where url is visibly a URL (a string or template, new URL(...), a binding typed string/URL or set to one) and the init visibly has no signal. That covers an object literal, a const object, and spreads of objects the checker can see. new Request(url) without a signal, passed to fetch, is reported too. Calls through globalThis.fetch, window.fetch and self.fetch count.
  • Accepted: any signal property, whether written in place or coming from a spread or a const that has one. Also accepted is anything the checker can't see into: init: RequestInit or request: Request parameters, RequestInfo, fetch(event.request), function results, computed keys, and a local binding named fetch.
  • Quick fixes: Pass the signal in scope (only when a binding named signal is visible) and Time out after 10 s (signal: AbortSignal.timeout(10_000)). The edit adds a new init, prepends to an existing literal, or spreads a const init into a new object.
  • Implemented as its own small module (crates/safe_ts_core/src/fetch_signal.rs) next to lints.rs, run in the same place.

The feature packs already map fetch (feature-browser says it rejects with TypeError). This rule doesn't use them: it is about the request never finishing, not about it failing.

Also in this PR

  • vue-async-race bug: it searched the watcher's source text for signal/AbortController, comments included. A comment mentioning "signal" silenced it, and that includes // oxlint-disable-next-line safe-ts/fetch-signal. It now leaves comments out of that search (Rules::code in vue.rs). I found this because annotating the vue_sfc fixture with // expect: fetch-signal made vue-async-race vanish on the next line.
  • Examples that are now reported, rightly: the settleAll/firstOk examples in docs/guide/std-helpers.md fetched without a signal. They now take a signal parameter and pass it. The JSDoc examples in packages/std/src/async.ts are updated to match. Two fetch calls in the vue_sfc fixture are now annotated // expect: fetch-signal.
  • examples/demo, examples/vue, the docs Vue demo components and the playground presets report exactly what they did before (diffed per file and location).

Tests

  • New fixture crates/safe_ts_core/tests/fixtures/fetch_signal/main.ts: 8 reported cases and 13 accepted ones.
  • Two fix tests in tests/fixes.rs check the fix titles and the exact text after applying each fix.
  • Rebased onto main at 04a5576 (head b13cae3). What had to change: only the conflicts in the rule lists (analyze.rs now runs megamorphic::check and then fetch_signal::check; fetch-signal sits next to no-accumulating-copy in the oxlint plugin and recommended.json) and in tests/fixes.rs, where main's import-attributes fix test and the two fetch-signal tests are both kept. No rule code, fixtures or docs needed changes: the new rules on main (no-accumulating-copy, stable-shape's call-site check, the tighter Vue URL rules) don't fire on this PR's fixtures or doc examples, and #5's call-following doesn't change their diagnostics.
  • After the rebase:
    • cargo nextest run --workspace: 104 passed, 0 failed (the earlier features failure was fixed on main by #23).
    • npm run test:std: 130 passed, 0 failed.
    • npm run test:vite: passed.
    • npm run typecheck: passed.
    • npm run test:docs: passed (docs typecheck, wasm build, VitePress build).
    • safe-ts check on examples/demo (22 problems, the demo's intended ones) and examples/vue (no problems): no fetch-signal reports.

Left out / uncertain

  • Only the global fetch is checked. Wrappers (ky, axios, a project's http.get) are not, and neither are functions forwarding a RequestInit they received; their callers aren't followed.
  • The rule doesn't check that the signal ever aborts. A new AbortController().signal that is never aborted passes.
  • The time limit in the quick fix (10 s) is a guess the user should adjust; the rule page says so.
  • In a Vue watcher, applying the timeout fix also silences vue-async-race, because that rule still counts any signal in the code as cancellation. A timeout does not cancel the stale run. I've noted this but haven't changed that heuristic beyond the comment fix.

🤖 Generated with Claude Code

## What New rule `fetch-signal` (warn in `recommended.json`): a `fetch(...)` with no `signal` in its init. Without a signal nothing can stop the request. Browsers set no time limit and Node waits 5 minutes for headers, so a server that accepts the connection and never answers hangs everything that awaits it (a request handler, a queue worker, a spinner). - **Reported:** `fetch(url)` and `fetch(url, init)` where `url` is visibly a URL (a string or template, `new URL(...)`, a binding typed `string`/`URL` or set to one) and the init visibly has no `signal`. That covers an object literal, a `const` object, and spreads of objects the checker can see. `new Request(url)` without a signal, passed to `fetch`, is reported too. Calls through `globalThis.fetch`, `window.fetch` and `self.fetch` count. - **Accepted:** any `signal` property, whether written in place or coming from a spread or a `const` that has one. Also accepted is anything the checker can't see into: `init: RequestInit` or `request: Request` parameters, `RequestInfo`, `fetch(event.request)`, function results, computed keys, and a local binding named `fetch`. - **Quick fixes:** *Pass the `signal` in scope* (only when a binding named `signal` is visible) and *Time out after 10 s* (`signal: AbortSignal.timeout(10_000)`). The edit adds a new init, prepends to an existing literal, or spreads a `const` init into a new object. - Implemented as its own small module (`crates/safe_ts_core/src/fetch_signal.rs`) next to `lints.rs`, run in the same place. The feature packs already map `fetch` (feature-browser says it rejects with `TypeError`). This rule doesn't use them: it is about the request never finishing, not about it failing. ### Also in this PR - **`vue-async-race` bug:** it searched the watcher's source text for `signal`/`AbortController`, comments included. A comment mentioning "signal" silenced it, and that includes `// oxlint-disable-next-line safe-ts/fetch-signal`. It now leaves comments out of that search (`Rules::code` in vue.rs). I found this because annotating the `vue_sfc` fixture with `// expect: fetch-signal` made `vue-async-race` vanish on the next line. - **Examples that are now reported, rightly:** the `settleAll`/`firstOk` examples in `docs/guide/std-helpers.md` fetched without a signal. They now take a `signal` parameter and pass it. The JSDoc examples in `packages/std/src/async.ts` are updated to match. Two `fetch` calls in the `vue_sfc` fixture are now annotated `// expect: fetch-signal`. - `examples/demo`, `examples/vue`, the docs Vue demo components and the playground presets report exactly what they did before (diffed per file and location). ## Tests - New fixture `crates/safe_ts_core/tests/fixtures/fetch_signal/main.ts`: 8 reported cases and 13 accepted ones. - Two fix tests in `tests/fixes.rs` check the fix titles and the exact text after applying each fix. - Rebased onto `main` at 04a5576 (head b13cae3). What had to change: only the conflicts in the rule lists (`analyze.rs` now runs `megamorphic::check` and then `fetch_signal::check`; `fetch-signal` sits next to `no-accumulating-copy` in the oxlint plugin and `recommended.json`) and in `tests/fixes.rs`, where main's `import-attributes` fix test and the two `fetch-signal` tests are both kept. No rule code, fixtures or docs needed changes: the new rules on main (`no-accumulating-copy`, stable-shape's call-site check, the tighter Vue URL rules) don't fire on this PR's fixtures or doc examples, and #5's call-following doesn't change their diagnostics. - After the rebase: - `cargo nextest run --workspace`: 104 passed, 0 failed (the earlier `features` failure was fixed on main by #23). - `npm run test:std`: 130 passed, 0 failed. - `npm run test:vite`: passed. - `npm run typecheck`: passed. - `npm run test:docs`: passed (docs typecheck, wasm build, VitePress build). - `safe-ts check` on `examples/demo` (22 problems, the demo's intended ones) and `examples/vue` (no problems): no `fetch-signal` reports. ## Left out / uncertain - Only the global `fetch` is checked. Wrappers (`ky`, `axios`, a project's `http.get`) are not, and neither are functions forwarding a `RequestInit` they received; their callers aren't followed. - The rule doesn't check that the signal ever aborts. A `new AbortController().signal` that is never aborted passes. - The time limit in the quick fix (10 s) is a guess the user should adjust; the rule page says so. - In a Vue watcher, applying the timeout fix also silences `vue-async-race`, because that rule still counts any `signal` in the code as cancellation. A timeout does not cancel the stale run. I've noted this but haven't changed that heuristic beyond the comment fix. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
sionzee force-pushed rules/fetch-signal from 4405e2c8bc to b13cae3c59
Some checks failed
Benchmarks / bench (pull_request) Has been cancelled
2026-11-03 21:58:09 +01:00
Compare
sionzee force-pushed rules/fetch-signal from b13cae3c59
Some checks failed
Benchmarks / bench (pull_request) Has been cancelled
to 10fcd161b5
Some checks failed
Benchmarks / bench (pull_request) Failing after 6m59s
2026-11-03 21:59:25 +01:00
Compare
sionzee deleted branch rules/fetch-signal 2026-11-03 22:00:54 +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!6
No description provided.