safe-ts fix <rule>, and quick fixes that don't break files #93

Merged
sionzee merged 1 commit from cli-fix into main 2026-11-20 16:32:32 +01:00
Owner

Problem

An adopter who upgraded a 729-file monorepo to 0.4.0 hit three problems:

  1. There was no way to apply one rule's fixes from the CLI. oxlint --fix-suggestions applies the first suggestion of every rule at once, and some bad fixes got in. They wrote a 300-line codemod instead.
  2. prefer-is-over-tag's fix added import { is } from "@evobug_com/safe-ts-std/enum" in files where the prelude already gives is (92 files). The import always used double quotes.
  3. prefer-q's fix wrote q into files Vite loads without the transform (vite.config.ts and what it imports). q throws there at runtime.

Solution

safe-ts fix <rule> <files or dirs>... [--write] [--fix "<title prefix>"] [--json] (crates/safe_ts_core/src/fixing.rs, crates/safe_ts_cli/src/fix.rs)

  • It uses the checker's own diagnostics and fixes, the ones the language server offers (Project::check_file, failures followed into other files). The program resolves as for check.
  • It prints a diff, and --write changes the files, like migrate. The report goes to stderr. --json prints the counts, every site, and each changed file's diff.
  • A site with one fix gets it. A site with several fixes is skipped and listed with its fix titles, unless --fix picks one by the start of its title.
  • Each round applies the fixes whose edits don't overlap, re-checks, and repeats until nothing changes (at most 20 rounds). If a round leaves the file unparsable, it is undone and its fixes are tried one at a time.
  • Legacy files are left out. The idiomatic rules are on for the run; a rule the project turns off stays off. A changed file is an overlay, so files checked after it see the new text.
  • It goes through the generic Diagnostic.fixes pipeline, so the other lane's cross-file strict-truthiness fixes will show up without changes here.

Imports added by fixes (fixes.rs)

  • ImportContext now records the file's import style (quote, semicolon). ensure, ensure_type, local_for and the codemods' add_imports / unions.rs imports all write in that style.
  • local_for, used by prefer-is-over-tag and prefer-q, didn't check the prelude. It now returns the prelude's name with no import, and only renames to safeIs when a local is hides the prelude's.
  • prefer-is-over-tag keeps the tag in the quotes it was written with.

Files that run untransformed (Project::untransformed_by, untransformed.rs)

  • The checker looks for vite.config.*, vitest.config.* and vitest.workspace.* in the file's directory and the directories above it. It follows what each config imports (runtime imports only) and caches that for 5 s, like global_type_files.
  • It skips a config when the package.json next to it uses --configLoader native, the same exemption the existing config warning uses.
  • Which config loads a file is passed into the analysis and is part of the analysis cache key.
  • New rule needs-transform (error), modelled on prelude-in-page. It reports q(...), parseJson<T> / validate<T> / fromFields<T> / arbitrary<T>, and prelude values used without an import.
    • q fixes write the plain form: const x = make(); if (isErr(x)) return x;, mapErr for q(x, f), return make() for return q(make()).
    • Prelude names get an import fix.
  • In those files:
    • ImportContext.prelude is off, so fixes import the std names they use.
    • must-use-result offers const result = f(); if (isErr(result)) return result instead of q(…).
    • unhandled-failure drops its q(unsafe(…)) option, and "Use the safe version" drops its q form.
    • prefer-q isn't reported, and prefer-map-err's help names only mapErr.
    • Safety net in diagnostics_for: any fix whose result has more q, parseJson<T> or unimported prelude uses is dropped, whichever rule built it.

Docs: CLI help; adoption guide (new section "Applying one rule's quick fixes", and the Vite config section); editor page; new rule page docs/rules/needs-transform.md with checked examples (a new vite-config fence word in tests/docs.rs and docs-typecheck.mjs); the prefer-q, prefer-map-err and prefer-is-over-tag pages; README (layout, CLI, rule table, quick-fix table, prelude notes); docs index; sidebar; CHANGELOG; skill (references/tool-gaps.md, references/patterns-core.md: use safe-ts fix one rule at a time and review the diff, never --fix-suggestions). I didn't touch the skill's SKILL.md or the census.

Tests

  • cargo nextest run --workspace: 290 passed. New tests:
    • tests/fixes.rs: quote and semicolon style; no import with the prelude, and safeIs when a local is hides it; untransformed q written out (plain, mapErr, return); prelude import fix; must-use-result and unhandled-failure without q in untransformed files.
    • Fixture tests/fixtures/untransformed (a Vite config importing scripts/plugin.ts, and app code that isn't reached and still gets prefer-q).
    • Unit tests in fixing.rs for overlap and fix choice.
    • crates/safe_ts_cli/tests/fix.rs: diff, --write, legacy left out, re-run is a no-op, several fixes skipped vs --fix, --json, unknown rule.
  • cargo test --doc -p safe_ts_core: ok.
  • node scripts/docs-typecheck.mjs: 607 documentation examples compile.
  • npm run build, npm run test:std (367 pass), npm run test:vite (passed).
  • By hand, on a temp project (prelude on, single quotes, no semicolons, a vite.config.ts importing a script that uses q and prelude names): fix prefer-is-over-tag gives !is(light, 'Green') with no import. fix needs-transform adds import { err, isErr } from '...' and writes q out without semicolons. check on the three example Vite configs reports no needs-transform.

Not covered, and why

  • safe-ts migrate throw-to-result still writes q(...) for callers, also in files a Vite config imports. The codemods analyze files on their own, without the project's config lookup, and they are a separate tool from the quick fixes. Follow-up if wanted.
  • Configs are only looked for above the file (up to the root, or the nearest package.json outside it). A shared package that an app's config imports from another directory isn't flagged from the package's side. The existing check warning about the config still covers it when that app is checked. import() in the config and configs under other names (vite --config x.ts) aren't followed. The rule page says so.
  • vite_config_warning (the run-level warning) is unchanged. It still shows next to the new per-site findings.

Created with Claude Opus 5.5 in Claude Code.

## Problem An adopter who upgraded a 729-file monorepo to 0.4.0 hit three problems: 1. There was no way to apply one rule's fixes from the CLI. `oxlint --fix-suggestions` applies the first suggestion of every rule at once, and some bad fixes got in. They wrote a 300-line codemod instead. 2. `prefer-is-over-tag`'s fix added `import { is } from "@evobug_com/safe-ts-std/enum"` in files where the prelude already gives `is` (92 files). The import always used double quotes. 3. `prefer-q`'s fix wrote `q` into files Vite loads without the transform (`vite.config.ts` and what it imports). `q` throws there at runtime. ## Solution **`safe-ts fix <rule> <files or dirs>... [--write] [--fix "<title prefix>"] [--json]`** (`crates/safe_ts_core/src/fixing.rs`, `crates/safe_ts_cli/src/fix.rs`) - It uses the checker's own diagnostics and fixes, the ones the language server offers (`Project::check_file`, failures followed into other files). The program resolves as for `check`. - It prints a diff, and `--write` changes the files, like `migrate`. The report goes to stderr. `--json` prints the counts, every site, and each changed file's diff. - A site with one fix gets it. A site with several fixes is skipped and listed with its fix titles, unless `--fix` picks one by the start of its title. - Each round applies the fixes whose edits don't overlap, re-checks, and repeats until nothing changes (at most 20 rounds). If a round leaves the file unparsable, it is undone and its fixes are tried one at a time. - Legacy files are left out. The idiomatic rules are on for the run; a rule the project turns off stays off. A changed file is an overlay, so files checked after it see the new text. - It goes through the generic `Diagnostic.fixes` pipeline, so the other lane's cross-file `strict-truthiness` fixes will show up without changes here. **Imports added by fixes** (`fixes.rs`) - `ImportContext` now records the file's import style (quote, semicolon). `ensure`, `ensure_type`, `local_for` and the codemods' `add_imports` / `unions.rs` imports all write in that style. - `local_for`, used by `prefer-is-over-tag` and `prefer-q`, didn't check the prelude. It now returns the prelude's name with no import, and only renames to `safeIs` when a local `is` hides the prelude's. - `prefer-is-over-tag` keeps the tag in the quotes it was written with. **Files that run untransformed** (`Project::untransformed_by`, `untransformed.rs`) - The checker looks for `vite.config.*`, `vitest.config.*` and `vitest.workspace.*` in the file's directory and the directories above it. It follows what each config imports (runtime imports only) and caches that for 5 s, like `global_type_files`. - It skips a config when the package.json next to it uses `--configLoader native`, the same exemption the existing config warning uses. - Which config loads a file is passed into the analysis and is part of the analysis cache key. - New rule **`needs-transform`** (error), modelled on `prelude-in-page`. It reports `q(...)`, `parseJson<T>` / `validate<T>` / `fromFields<T>` / `arbitrary<T>`, and prelude values used without an import. - `q` fixes write the plain form: `const x = make(); if (isErr(x)) return x;`, `mapErr` for `q(x, f)`, `return make()` for `return q(make())`. - Prelude names get an import fix. - In those files: - `ImportContext.prelude` is off, so fixes import the std names they use. - `must-use-result` offers `const result = f(); if (isErr(result)) return result` instead of `q(…)`. - `unhandled-failure` drops its `q(unsafe(…))` option, and "Use the safe version" drops its `q` form. - `prefer-q` isn't reported, and `prefer-map-err`'s help names only `mapErr`. - Safety net in `diagnostics_for`: any fix whose result has more `q`, `parseJson<T>` or unimported prelude uses is dropped, whichever rule built it. **Docs**: CLI help; adoption guide (new section "Applying one rule's quick fixes", and the Vite config section); editor page; new rule page `docs/rules/needs-transform.md` with checked examples (a new `vite-config` fence word in `tests/docs.rs` and `docs-typecheck.mjs`); the `prefer-q`, `prefer-map-err` and `prefer-is-over-tag` pages; README (layout, CLI, rule table, quick-fix table, prelude notes); docs index; sidebar; CHANGELOG; skill (`references/tool-gaps.md`, `references/patterns-core.md`: use `safe-ts fix` one rule at a time and review the diff, never `--fix-suggestions`). I didn't touch the skill's SKILL.md or the census. ## Tests - `cargo nextest run --workspace`: 290 passed. New tests: - `tests/fixes.rs`: quote and semicolon style; no import with the prelude, and `safeIs` when a local `is` hides it; untransformed `q` written out (plain, `mapErr`, `return`); prelude import fix; `must-use-result` and `unhandled-failure` without `q` in untransformed files. - Fixture `tests/fixtures/untransformed` (a Vite config importing `scripts/plugin.ts`, and app code that isn't reached and still gets `prefer-q`). - Unit tests in `fixing.rs` for overlap and fix choice. - `crates/safe_ts_cli/tests/fix.rs`: diff, `--write`, legacy left out, re-run is a no-op, several fixes skipped vs `--fix`, `--json`, unknown rule. - `cargo test --doc -p safe_ts_core`: ok. - `node scripts/docs-typecheck.mjs`: 607 documentation examples compile. - `npm run build`, `npm run test:std` (367 pass), `npm run test:vite` (passed). - By hand, on a temp project (prelude on, single quotes, no semicolons, a `vite.config.ts` importing a script that uses `q` and prelude names): `fix prefer-is-over-tag` gives `!is(light, 'Green')` with no import. `fix needs-transform` adds `import { err, isErr } from '...'` and writes `q` out without semicolons. `check` on the three example Vite configs reports no `needs-transform`. ## Not covered, and why - `safe-ts migrate throw-to-result` still writes `q(...)` for callers, also in files a Vite config imports. The codemods analyze files on their own, without the project's config lookup, and they are a separate tool from the quick fixes. Follow-up if wanted. - Configs are only looked for above the file (up to the root, or the nearest package.json outside it). A shared package that an app's config imports from another directory isn't flagged from the package's side. The existing `check` warning about the config still covers it when that app is checked. `import()` in the config and configs under other names (`vite --config x.ts`) aren't followed. The rule page says so. - `vite_config_warning` (the run-level warning) is unchanged. It still shows next to the new per-site findings. Created with Claude Opus 5.5 in Claude Code.
safe-ts fix <rule>, and quick fixes that don't break files
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m13s
8c607f0cb3
- `safe-ts fix <rule> <files or dirs>... [--write] [--fix "<title prefix>"] [--json]`:
  applies one rule's quick fixes (the language server's), non-overlapping edits per round,
  re-checking until nothing changes. Sites with several fixes are skipped and listed unless
  --fix picks one; legacy files are left out; a round that breaks parsing is undone.
- New rule `needs-transform`: q(...), parseJson<T> and friends, and prelude names without an
  import, in files a vite.config.* / vitest.config.* imports (they load without the
  transform). Fixes write q out (`if (isErr(x)) return x`, mapErr) or add the import.
- In those files, fixes don't write q (must-use-result offers the plain form), import std
  names even with the prelude on, and prefer-q isn't reported.
- prefer-is-over-tag's fix no longer imports `is` when the prelude gives it, and keeps the
  tag's quotes. Imports added by fixes and codemods use the file's quotes and semicolons.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
sionzee force-pushed cli-fix from 8c607f0cb3
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m13s
to 4115ae9b75
All checks were successful
Benchmarks / bench (pull_request) Successful in 3m56s
2026-11-20 16:28:23 +01:00
Compare
sionzee deleted branch cli-fix 2026-11-20 16:32:32 +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!93
No description provided.