safe-ts fix takes the fix that keeps the result; prefer-q off in files a Vite config elsewhere loads; added imports in the file's quotes #165

Merged
sionzee merged 2 commits from fix-ux-upgrade into main 2026-11-24 14:22:27 +01:00
Owner

Three pieces of an adopter's feedback about quick fixes during the 0.5.1 upgrade. Each was reproduced on main (b1e856ff) first.

1. safe-ts fix strict-truthiness skipped nearly every site ("several fixes")

safe-ts fix <rule> skipped any site with more than one fix unless --fix picked one. For strict-truthiness most sites have two, and for an optional string one of them keeps the meaning exactly:

export function label(name?: string): string {
  return name ? name : '-'   // Test for undefined and '' (the same as now) | Test for undefined only ('' counts as true)
}

Changed: fixing::pick takes, among several fixes, the one --fix picks, else the only one whose title says (the same as now) (Fix::keeps_meaning, SAME_AS_NOW in lib.rs). That title is already the documented contract in docs/guide/quick-fixes.md, and tests/meaning.rs runs those fixes. The harness now also fails a title that says (the same as now) when its FIXES entry isn't Same, so safe-ts fix can't take a fix that doesn't keep the result. Where --fix picks nothing at a site, the meaning-keeping fix is still taken. A site that is still skipped prints why: each fix changes the result for some input (a number: Compare with 0 (NaN counts as true), Test for a positive number) or more than one fix keeps the result. The report ends with the --fix for each kind of site left and how many sites offer it. --json gives "skipped": "none-keeps-meaning" | "several-keep-meaning".

Numbers are still skipped on purpose: no fix keeps NaN counting as false, so applying one without being asked would change behaviour silently.

On the adopter's apps/client (419 files):

fixed skipped no fix
main 4 88 4
this branch 12 80 4
then --fix "Compare with 0" +76 4 4

The last row is the command the report now prints for 76 of the 80 sites left. The adopter's tree was type-checked with tsc -p src and tsc -p . on a copy, before and after both --write runs. The set of errors is identical each time (82/83 errors, all from the copy's environment: two Vue copies, one missing optional module). So no fix adds a type error.

2. prefer-q's fix wrote q into files Vite loads untransformed

prefer-q already isn't reported and offers no q in a file a Vite/Vitest config imports. But Project::untransformed_by only asked configs in the file's directory and the directories above it. A config in another directory was never asked: apps/web/vite.config.ts importing ../../scripts/reload.ts loads that file untransformed too.

// scripts/reload.ts, imported by apps/client/vite.config.ts
export function load(name: string): Result<string, string> {
  const text = read(name)
  if (isErr(text)) return text   // main: prefer-q, fix writes `const text = q(read(name))`: throws when Vite loads it
  return text
}

Fixed: after the configs above, every other Vite/Vitest config of the project is asked (project_configs: a walk of the project root that skips node_modules, dist and similar, reads at most 5000 directories, and is cached for 5 s like the other file lists). This reuses the previous attempt's patch, plus the bound on the walk. needs-transform, and the fixes that leave out q and the prelude, get the same answer. Still reported: prefer-map-err in that file, with help that names mapErr and not q. New fixture untransformed/apps/web/vite.config.ts → untransformed/shared/reload.ts. On main it reports prefer-q at line 13; here it reports only prefer-map-err.

3. prefer-is-over-tag imports is with double quotes in a prelude package

The prelude half no longer reproduces on main. With types: ["@evobug_com/safe-ts-prelude"] (JSONC tsconfig with extends, as the adopter has), the fix writes is(...) without an import. That has been true since #93 (0.5.0), and the adopter saw it with 0.4.0. The quote half does reproduce: a file without imports got " and ; by default, e.g. import { is as safeIs } from "…" where a parameter hides the prelude's is in a single-quoted file.

Fixed: import_style falls back to the file's code when it has no import: the quote of its first string literal (not a JSX attribute's, not in a Vue template) and whether its first top-level declaration or expression statement ends with ;. Every fix that adds a std import goes through ImportContext (ensure, ensure_type, local_for, import_line, loans' Mut import), so they all follow it. strict-truthiness's "" now follows the file's quotes too ('' in a single-quoted file, in the fix and its title).

Tests

  • cargo nextest run --workspace: all pass. New tests: fixing::tests::picking_the_fix_that_keeps_the_meaning, CLI the_fix_that_keeps_the_meaning_is_taken_without_a_choice, fixes::an_added_import_follows_the_quotes_of_a_file_without_imports, and the untransformed fixture.
  • npm run test:rewrites (full): passes. misses.txt: miss prefer-map-err hoist-const goes from 2 to 5. That is the new fixture's prefer-map-err site under the same known hoist-const gap (the rule matches the shape where the value is written); no existing site moved.
  • npm run test:soundness: 16 of 110 classes escape, exactly escaping.txt.
  • node scripts/docs-typecheck.mjs: only the dompurify errors this worktree's install lacks; vue-tsc not installed here, so the VitePress build was not run.

Docs: docs/guide/adoption.md (Applying one rule's quick fixes), docs/guide/quick-fixes.md, docs/rules/strict-truthiness.md, prefer-q.md, needs-transform.md, prefer-is-over-tag.md, README, CLI usage, and CHANGELOG (Changed + 2 Fixed).

🤖 Generated with Claude Code

Three pieces of an adopter's feedback about quick fixes during the 0.5.1 upgrade. Each was reproduced on main (b1e856ff) first. ## 1. `safe-ts fix strict-truthiness` skipped nearly every site ("several fixes") `safe-ts fix <rule>` skipped any site with more than one fix unless `--fix` picked one. For strict-truthiness most sites have two, and for an optional string one of them keeps the meaning exactly: ```ts export function label(name?: string): string { return name ? name : '-' // Test for undefined and '' (the same as now) | Test for undefined only ('' counts as true) } ``` **Changed:** `fixing::pick` takes, among several fixes, the one `--fix` picks, else **the only one whose title says `(the same as now)`** (`Fix::keeps_meaning`, `SAME_AS_NOW` in lib.rs). That title is already the documented contract in docs/guide/quick-fixes.md, and `tests/meaning.rs` runs those fixes. The harness now also fails a title that says `(the same as now)` when its `FIXES` entry isn't `Same`, so `safe-ts fix` can't take a fix that doesn't keep the result. Where `--fix` picks nothing at a site, the meaning-keeping fix is still taken. A site that is still skipped prints why: `each fix changes the result for some input` (a number: *Compare with 0 (NaN counts as true)*, *Test for a positive number*) or `more than one fix keeps the result`. The report ends with the `--fix` for each kind of site left and how many sites offer it. `--json` gives `"skipped": "none-keeps-meaning" | "several-keep-meaning"`. Numbers are still skipped on purpose: no fix keeps `NaN` counting as false, so applying one without being asked would change behaviour silently. **On the adopter's `apps/client`** (419 files): | | fixed | skipped | no fix | |---|---|---|---| | main | 4 | 88 | 4 | | this branch | 12 | 80 | 4 | | then `--fix "Compare with 0"` | +76 | 4 | 4 | The last row is the command the report now prints for 76 of the 80 sites left. The adopter's tree was type-checked with `tsc -p src` and `tsc -p .` on a copy, before and after both `--write` runs. The set of errors is identical each time (82/83 errors, all from the copy's environment: two Vue copies, one missing optional module). So no fix adds a type error. ## 2. `prefer-q`'s fix wrote `q` into files Vite loads untransformed `prefer-q` already isn't reported and offers no `q` in a file a Vite/Vitest config imports. But `Project::untransformed_by` only asked configs in the file's directory and the directories above it. A config in another directory was never asked: `apps/web/vite.config.ts` importing `../../scripts/reload.ts` loads that file untransformed too. ```ts // scripts/reload.ts, imported by apps/client/vite.config.ts export function load(name: string): Result<string, string> { const text = read(name) if (isErr(text)) return text // main: prefer-q, fix writes `const text = q(read(name))`: throws when Vite loads it return text } ``` **Fixed:** after the configs above, every other Vite/Vitest config of the project is asked (`project_configs`: a walk of the project root that skips `node_modules`, `dist` and similar, reads at most 5000 directories, and is cached for 5 s like the other file lists). This reuses the previous attempt's patch, plus the bound on the walk. `needs-transform`, and the fixes that leave out `q` and the prelude, get the same answer. **Still reported:** `prefer-map-err` in that file, with help that names `mapErr` and not `q`. New fixture `untransformed/apps/web/vite.config.ts` → `untransformed/shared/reload.ts`. On main it reports `prefer-q` at line 13; here it reports only `prefer-map-err`. ## 3. `prefer-is-over-tag` imports `is` with double quotes in a prelude package The prelude half no longer reproduces on main. With `types: ["@evobug_com/safe-ts-prelude"]` (JSONC tsconfig with `extends`, as the adopter has), the fix writes `is(...)` without an import. That has been true since #93 (0.5.0), and the adopter saw it with 0.4.0. The quote half does reproduce: a file **without imports** got `"` and `;` by default, e.g. `import { is as safeIs } from "…"` where a parameter hides the prelude's `is` in a single-quoted file. **Fixed:** `import_style` falls back to the file's code when it has no import: the quote of its first string literal (not a JSX attribute's, not in a Vue template) and whether its first top-level declaration or expression statement ends with `;`. Every fix that adds a std import goes through `ImportContext` (`ensure`, `ensure_type`, `local_for`, `import_line`, loans' `Mut` import), so they all follow it. strict-truthiness's `""` now follows the file's quotes too (`''` in a single-quoted file, in the fix and its title). ## Tests - `cargo nextest run --workspace`: all pass. New tests: `fixing::tests::picking_the_fix_that_keeps_the_meaning`, CLI `the_fix_that_keeps_the_meaning_is_taken_without_a_choice`, `fixes::an_added_import_follows_the_quotes_of_a_file_without_imports`, and the `untransformed` fixture. - `npm run test:rewrites` (full): passes. `misses.txt`: `miss prefer-map-err hoist-const` goes from 2 to 5. That is the new fixture's `prefer-map-err` site under the same known `hoist-const` gap (the rule matches the shape where the value is written); no existing site moved. - `npm run test:soundness`: 16 of 110 classes escape, exactly `escaping.txt`. - `node scripts/docs-typecheck.mjs`: only the `dompurify` errors this worktree's install lacks; vue-tsc not installed here, so the VitePress build was not run. Docs: `docs/guide/adoption.md` (Applying one rule's quick fixes), `docs/guide/quick-fixes.md`, `docs/rules/strict-truthiness.md`, `prefer-q.md`, `needs-transform.md`, `prefer-is-over-tag.md`, README, CLI usage, and CHANGELOG (Changed + 2 Fixed). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Quick fixes during an upgrade: safe-ts fix takes the fix that keeps the result, prefer-q off in files a Vite config elsewhere loads, imports in the file's quotes
Some checks failed
Benchmarks / bench (pull_request) Successful in 4m8s
Tests / test (pull_request) Has been cancelled
814533964d
- `safe-ts fix <rule>`: among several fixes, the one `--fix` picks, else the only one whose
  title says "(the same as now)" (`Fix::keeps_meaning`); a site still skipped says why, the
  report ends with the `--fix` per kind of site left, `--json` gives `"skipped"`. The meaning
  harness fails a "(the same as now)" title its table doesn't classify as Same.
- `Project::untransformed_by` also asks every Vite/Vitest config of the project (bounded walk),
  so a file an app's config imports from another directory runs untransformed: no `prefer-q`,
  no `q` in fixes there.
- An import a fix adds to a file without imports follows the quotes and semicolons of its
  code; strict-truthiness writes `''` in such files.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee force-pushed fix-ux-upgrade from 814533964d
Some checks failed
Benchmarks / bench (pull_request) Successful in 4m8s
Tests / test (pull_request) Has been cancelled
to b21a4bd430
All checks were successful
Benchmarks / bench (pull_request) Successful in 5m50s
Tests / test (pull_request) Successful in 30m49s
2026-11-24 13:37:12 +01:00
Compare
Merge main into fix-ux-upgrade
Some checks failed
Tests / test (pull_request) Has been cancelled
Benchmarks / bench (pull_request) Has been cancelled
dd116913c8
Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee deleted branch fix-ux-upgrade 2026-11-24 14:22:27 +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!165
No description provided.