Feature packs describe globals; the Node and browser packs report theirs #57
Loading…
Reference in a new issue
No description provided.
Delete branch "evobug/pack-globals"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
What
Feature packs can describe globals, and the Node and browser packs now do.
safe-ts.jsontakes aglobalspart, shaped like a module withoutspecifiers:functions(fetch,new URL,WebAssembly.compile),classes(extendsmay list severalbases, for lib.dom's mixins),
creates(fetchgives aResponse; a property too:Crypto#subtleis aSubtleCrypto) andinstances(localStorageis aStorage). A bareclass name is a global class. Documented in docs/guide/libraries.md (pack format, and a new
"Globals" section).
window.x,self.x,globalThis.x), to what they give ((await fetch(url)).json(), also throughunsafe/unsafeAsyncafterisErrnarrowing), and to values declared with a global type: aparameter, 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'tmatch. A global method is never matched by its name alone. This reuses the module machinery in
features.rs (
resolve,creates,instances, class chains) with anOrigin::Globalin theanalyzer.
package_typeand the newglobal_typeshare the type unwrapping (named_types).JSON.parse,new URL,structuredClone,atob, ...) applies as before. Where an enabled pack lists the same global, the pack's entry isreported 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.stringifyof aplain object,
encodeURIComponentof a literal).safe-ts.jsonis generated bypackages/feature-browser/test/pack.tsfrombrowser-api.json(the 184throws/rejectsmembers and theirerrorstexts) pluslib.dom.d.ts(bases, what functions, methods and properties return, global variables). I chosea 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.jsonstays the one source of truth: a newmanifest.test.tscheck fails when the committed file isn't what the script makes. Added tofiles.pack()intest/inventory.tsalso emitsglobals.node-api.jsongainederrorsandcreatesfor Node's globals, andtest/triggers.tsmakes them fail on the running Node (now 413of 448 failing members verified).
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.decode()on aTextDecodermade withoutfatal: trueisn't reported: such adecoder can't throw. This was the only new finding on the RTSS client, and it was wrong.
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
fetch()without handling (from the Node pack's globals).fetch-signal.mdnow wrapsfetchinunsafeAsync. Thereact-uncached-promise.mdbad example listsunhandled-failuretoo.Nothing new in the Vue or React sections.
examples/vue,examples/react,examples/demoand the playground presets, with the browser packon: no new findings (they already use
unsafeAsyncforfetchandresponse.text()).SAFE_TS_CACHE_DIRin /tmp): client, server, admin and packages allhave 0 new findings after the TextDecoder fix (1 before it, a false positive). RTSS already wraps
its browser calls. With
unsafestripped in a second copy, the packs match 7 global calls(3
fetch, 1localStorage.setItem, 3new URLnow carrying the pack's text), all correct.checkof the RTSSclient: 58 → 66 ms with both packs, unchanged without the browser pack.
Verification
cargo nextest run --workspace(194 passed, including the newpack_globalsfixture: 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.mjshas 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 modeanalyzes the installed std tarball and reports
match()(describecan throw). That's separatefrom 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