Vue support: components, templates and composables #1

Merged
sionzee merged 3 commits from vue-support into main 2026-10-30 00:08:22 +01:00
Owner

Adds Vue 3 support: .vue files are checked like .ts files, every existing rule also runs inside templates, and 18 Vue-specific rules are added. Design and decisions: notes/vue.md.

How it works

  • sfc.rs turns a component into one virtual TypeScript program: <script> at module level (shared by every instance), <script setup> per instance (its bindings are exempt from no-mutable-global, but its functions stay top-level so failures are still followed across files), then the template as statements. A source map sends diagnostics, code frames and quick fixes back into the .vue file; fixes that would touch generated code are dropped.
  • template.rs is our own template parser (Rust, so the CLI and WASM stay Node-free). Handlers become detached callbacks, so uncaught-propagation, must-use-result and floating-rejection work in templates unchanged. {{ JSON.parse(x) }} is reported as unhandled-failure.
  • defineProps() is treated as borrowed, so props.x.y = … / v-model="props.x" get a props-specific no-param-mutation.
  • vue.rs holds the script-side rules; they also run in .ts files importing from vue (composables).

New rules

vue-sfc, vue-ref-value, vue-lost-reactivity, vue-setup-after-await, vue-async-race, vue-side-effect-in-render, vue-render-cost, vue-deep-watch, vue-prefer-shallow-ref, vue-inject-key, vue-async-component, vue-v-for-key, vue-v-if-with-v-for, vue-exhaustive-v-if, vue-v-html, vue-unsafe-url, vue-ssr-browser-global, vue-hydration-mismatch. listener-cleanup now covers component setup. SSR rules apply only with "safe-ts": { "vue": { "ssr": true } } in package.json or a Nuxt dependency.

Tooling and packages

  • Vite: safeTs() before vue() checks components and rewrites their script blocks in place. examples/vue/ is a small working app, covered by scripts/vite-check.mjs.
  • @evobug/safe-ts-vue: useTask, AsyncState, useListener/useInterval, injectRequired, safeUrl/safeHref.
  • @evobug/safe-ts-feature-vue-router: push/replace/isReady reject, resolve throws (written from vue-router 4, not verified against an installed copy).
  • Docs: docs/guide/vue.md, one page per rule, README sections.

Enums in templates, and values that may be missing (second commit)

  • <Match :on="value"> from @evobug/safe-ts-vue/match: one slot per variant (#Ready="{ value }"), #_ for the rest. vue-match checks every variant has a slot and every slot is a variant. Variants come from the value's declaration (ref<T>(), defineProps<...>(), useTask, return types, v-for over a typed list) and the Enum<...> it names, here or in the file it is imported from.
  • The build compiles <Match> into a v-if chain and rewrites slot props into reads of the value, so nothing of it is left at runtime. The Match component renders whatever the transform can't rewrite (e.g. :on="find(id)").
  • vue-exhaustive-v-if now knows the variants: a chain covering all of them passes, a chain missing one names it.
  • vue-maybe-undefined: the template reads into a ref() without a value, an optional prop, defineModel() or inject() without a default, and no v-if / && / ?: / ?. checks it first.
  • vue-template-ref: useElement (typed without null) for elements the template always renders, read after mount. Use useOptionalElement for elements under v-if, useElements inside v-for.
  • vue-split-state / vue-ref-without-value: loading/error/data refs, or a rendered ref that stays undefined until loaded. Both point to useTask + AsyncState.
  • examples/vue uses <Match>; scripts/vite-check.mjs checks that it is compiled into a v-if chain.

Decisions (defaults, easy to change in notes/vue.md)

  • Options API and plain-JS components are reported (vue-sfc), not checked further.
  • app.config.errorHandler / onErrorCaptured don't count as handling.
  • Pinia needs no special case.
  • Module-level createRouter() + router.push() stays no-mutable-global; use useRouter().

Known gaps

  • Editor: oxlint 1.90 doesn't pass .vue files to JS plugins, so no safe-ts diagnostics in .vue in the editor (CLI and Vite work).
  • No vue-tsc for type-checking emitted components; Nuxt auto-imports aren't followed; emit isn't linked to the parent's handler; no Vue playground preset.

Testing

  • cargo test --no-fail-fast: all pass except the features fixture (createDecipheriv expectation), which already fails on main.
  • npm run test:std: 115/116; the feature-node inventory failure (Node 26.13 API drift) already fails on main.
  • Docs examples (tests/docs.rs, scripts/docs-typecheck.mjs), npm run test:vite, npm run typecheck, tsc -p packages/safe-ts-vue, and @evobug/safe-ts-vue's 41 tests pass.
  • package-lock.json changed via npm install --offline (new workspaces).

A Vue demo in the docs (third commit)

  • docs/guide/vue-demo.md runs one component written two ways, side by side in the browser, against a fake server (slow, fast, failing). Without safe-ts it crashes on a failed first load, shows an older response over a newer one, and keeps an error that never clears. With useTask and <Match> it does none of that.
  • The code and the safe-ts reports on the page are generated at build time from the files that run (a ```demo block in the VitePress config). The demo's components go through the safe-ts transform, so the browser runs the compiled v-if chain.
  • vue_demo_shows_its_problems (tests/docs.rs) keeps the plain version reporting vue-async-race, vue-maybe-undefined and vue-split-state, and the safe-ts version clean.
  • Fixes the docs site build, broken by the second commit: {{ user.name }} in inline code is rendered by VitePress. npm run build -w docs passes.

🤖 Generated with Claude Code

Adds Vue 3 support: `.vue` files are checked like `.ts` files, every existing rule also runs inside templates, and 18 Vue-specific rules are added. Design and decisions: `notes/vue.md`. ## How it works - `sfc.rs` turns a component into one virtual TypeScript program: `<script>` at module level (shared by every instance), `<script setup>` per instance (its bindings are exempt from `no-mutable-global`, but its functions stay top-level so failures are still followed across files), then the template as statements. A source map sends diagnostics, code frames and quick fixes back into the `.vue` file; fixes that would touch generated code are dropped. - `template.rs` is our own template parser (Rust, so the CLI and WASM stay Node-free). Handlers become detached callbacks, so `uncaught-propagation`, `must-use-result` and `floating-rejection` work in templates unchanged. `{{ JSON.parse(x) }}` is reported as `unhandled-failure`. - `defineProps()` is treated as borrowed, so `props.x.y = …` / `v-model="props.x"` get a props-specific `no-param-mutation`. - `vue.rs` holds the script-side rules; they also run in `.ts` files importing from `vue` (composables). ## New rules `vue-sfc`, `vue-ref-value`, `vue-lost-reactivity`, `vue-setup-after-await`, `vue-async-race`, `vue-side-effect-in-render`, `vue-render-cost`, `vue-deep-watch`, `vue-prefer-shallow-ref`, `vue-inject-key`, `vue-async-component`, `vue-v-for-key`, `vue-v-if-with-v-for`, `vue-exhaustive-v-if`, `vue-v-html`, `vue-unsafe-url`, `vue-ssr-browser-global`, `vue-hydration-mismatch`. `listener-cleanup` now covers component setup. SSR rules apply only with `"safe-ts": { "vue": { "ssr": true } }` in `package.json` or a Nuxt dependency. ## Tooling and packages - Vite: `safeTs()` before `vue()` checks components and rewrites their script blocks in place. `examples/vue/` is a small working app, covered by `scripts/vite-check.mjs`. - `@evobug/safe-ts-vue`: `useTask`, `AsyncState`, `useListener`/`useInterval`, `injectRequired`, `safeUrl`/`safeHref`. - `@evobug/safe-ts-feature-vue-router`: `push`/`replace`/`isReady` reject, `resolve` throws (written from vue-router 4, not verified against an installed copy). - Docs: `docs/guide/vue.md`, one page per rule, README sections. ## Enums in templates, and values that may be missing (second commit) - `<Match :on="value">` from `@evobug/safe-ts-vue/match`: one slot per variant (`#Ready="{ value }"`), `#_` for the rest. `vue-match` checks every variant has a slot and every slot is a variant. Variants come from the value's declaration (`ref<T>()`, `defineProps<...>()`, `useTask`, return types, `v-for` over a typed list) and the `Enum<...>` it names, here or in the file it is imported from. - The build compiles `<Match>` into a `v-if` chain and rewrites slot props into reads of the value, so nothing of it is left at runtime. The `Match` component renders whatever the transform can't rewrite (e.g. `:on="find(id)"`). - `vue-exhaustive-v-if` now knows the variants: a chain covering all of them passes, a chain missing one names it. - `vue-maybe-undefined`: the template reads into a `ref()` without a value, an optional prop, `defineModel()` or `inject()` without a default, and no `v-if` / `&&` / `?:` / `?.` checks it first. - `vue-template-ref`: `useElement` (typed without `null`) for elements the template always renders, read after mount. Use `useOptionalElement` for elements under `v-if`, `useElements` inside `v-for`. - `vue-split-state` / `vue-ref-without-value`: `loading`/`error`/`data` refs, or a rendered ref that stays undefined until loaded. Both point to `useTask` + `AsyncState`. - `examples/vue` uses `<Match>`; `scripts/vite-check.mjs` checks that it is compiled into a `v-if` chain. ## Decisions (defaults, easy to change in `notes/vue.md`) - Options API and plain-JS components are reported (`vue-sfc`), not checked further. - `app.config.errorHandler` / `onErrorCaptured` don't count as handling. - Pinia needs no special case. - Module-level `createRouter()` + `router.push()` stays `no-mutable-global`; use `useRouter()`. ## Known gaps - Editor: oxlint 1.90 doesn't pass `.vue` files to JS plugins, so no safe-ts diagnostics in `.vue` in the editor (CLI and Vite work). - No `vue-tsc` for type-checking emitted components; Nuxt auto-imports aren't followed; `emit` isn't linked to the parent's handler; no Vue playground preset. ## Testing - `cargo test --no-fail-fast`: all pass except the `features` fixture (`createDecipheriv` expectation), which already fails on `main`. - `npm run test:std`: 115/116; the `feature-node` inventory failure (Node 26.13 API drift) already fails on `main`. - Docs examples (`tests/docs.rs`, `scripts/docs-typecheck.mjs`), `npm run test:vite`, `npm run typecheck`, `tsc -p packages/safe-ts-vue`, and `@evobug/safe-ts-vue`'s 41 tests pass. - `package-lock.json` changed via `npm install --offline` (new workspaces). ## A Vue demo in the docs (third commit) - `docs/guide/vue-demo.md` runs one component written two ways, side by side in the browser, against a fake server (slow, fast, failing). Without safe-ts it crashes on a failed first load, shows an older response over a newer one, and keeps an error that never clears. With `useTask` and `<Match>` it does none of that. - The code and the safe-ts reports on the page are generated at build time from the files that run (a ```` ```demo ```` block in the VitePress config). The demo's components go through the safe-ts transform, so the browser runs the compiled `v-if` chain. - `vue_demo_shows_its_problems` (tests/docs.rs) keeps the plain version reporting `vue-async-race`, `vue-maybe-undefined` and `vue-split-state`, and the safe-ts version clean. - Fixes the docs site build, broken by the second commit: `{{ user.name }}` in inline code is rendered by VitePress. `npm run build -w docs` passes. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
- .vue files become one virtual TypeScript program (<script> at module
  level, <script setup> per instance, the template as statements); every
  diagnostic, code frame and quick fix maps back into the component
- every existing rule runs in templates: handlers are detached callbacks,
  rendered expressions are uses
- 18 vue-* rules: reactivity, setup/async, render cost, v-for/v-if,
  v-html and bound URLs, SSR (opt-in or Nuxt)
- Vite: safeTs() before vue() checks and rewrites script blocks in place
- @evobug/safe-ts-vue runtime helpers, vue-router feature pack
- docs: Vue guide, a page per rule; design notes in notes/vue.md

Co-Authored-By: Claude Opus 5.5 <[email protected]>
- <Match :on="value"> with a slot per variant (#_ for the rest), checked against
  the variants of the value's enum (vue-match). The variants come from the
  declaration (ref<T>(), defineProps, useTask, return types) and the Enum<...>
  it names, followed through imports. The build compiles <Match> into a v-if
  chain, slot props rewritten to reads of the value; the runtime Match
  component renders what it can't rewrite
- vue-exhaustive-v-if now knows the variants: a chain covering all of them
  passes, a chain missing one names it
- vue-maybe-undefined: template reads into ref() without a value, optional
  props, defineModel() and inject() without a default, outside a v-if/&&/?.
- vue-template-ref: useElement (typed without null) for elements the template
  always renders, read after mount; useOptionalElement, useElements otherwise
- vue-split-state, vue-ref-without-value: loading/error/data refs and
  undefined-until-loaded refs, pointing to useTask and AsyncState
- docs: guide sections and a page per rule; examples/vue uses <Match>

Co-Authored-By: Claude Opus 5.5 <[email protected]>
docs: a Vue demo, one component with and without safe-ts
All checks were successful
Pages / publish (push) Successful in 3m46s
a6fae04a39
A new guide page runs the same profile panel twice, side by side in the browser, against a
fake server: written the usual way (loading/error/data refs and an async watcher) and with
useTask and <Match>. Three tries show the crash, the async race and the error that never
clears, each next to the rule that reports it.

The code and the reports on the page are generated at build time from the files that run
(a ```demo block in the VitePress config), and the demo's components go through the safe-ts
transform, so the browser runs the compiled v-if chain. A test keeps the plain version
producing its rules and the safe-ts version clean.

Also fixes the docs build: two sentences had {{ }} in inline code, which VitePress renders.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee deleted branch vue-support 2026-10-30 00:09:02 +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!1
No description provided.