safe-ts fix <rule>, and quick fixes that don't break files #93
Loading…
Reference in a new issue
No description provided.
Delete branch "cli-fix"
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?
Problem
An adopter who upgraded a 729-file monorepo to 0.4.0 hit three problems:
oxlint --fix-suggestionsapplies the first suggestion of every rule at once, and some bad fixes got in. They wrote a 300-line codemod instead.prefer-is-over-tag's fix addedimport { is } from "@evobug_com/safe-ts-std/enum"in files where the prelude already givesis(92 files). The import always used double quotes.prefer-q's fix wroteqinto files Vite loads without the transform (vite.config.tsand what it imports).qthrows 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)Project::check_file, failures followed into other files). The program resolves as forcheck.--writechanges the files, likemigrate. The report goes to stderr.--jsonprints the counts, every site, and each changed file's diff.--fixpicks one by the start of its title.Diagnostic.fixespipeline, so the other lane's cross-filestrict-truthinessfixes will show up without changes here.Imports added by fixes (
fixes.rs)ImportContextnow records the file's import style (quote, semicolon).ensure,ensure_type,local_forand the codemods'add_imports/unions.rsimports all write in that style.local_for, used byprefer-is-over-tagandprefer-q, didn't check the prelude. It now returns the prelude's name with no import, and only renames tosafeIswhen a localishides the prelude's.prefer-is-over-tagkeeps the tag in the quotes it was written with.Files that run untransformed (
Project::untransformed_by,untransformed.rs)vite.config.*,vitest.config.*andvitest.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, likeglobal_type_files.--configLoader native, the same exemption the existing config warning uses.needs-transform(error), modelled onprelude-in-page. It reportsq(...),parseJson<T>/validate<T>/fromFields<T>/arbitrary<T>, and prelude values used without an import.qfixes write the plain form:const x = make(); if (isErr(x)) return x;,mapErrforq(x, f),return make()forreturn q(make()).ImportContext.preludeis off, so fixes import the std names they use.must-use-resultoffersconst result = f(); if (isErr(result)) return resultinstead ofq(…).unhandled-failuredrops itsq(unsafe(…))option, and "Use the safe version" drops itsqform.prefer-qisn't reported, andprefer-map-err's help names onlymapErr.diagnostics_for: any fix whose result has moreq,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.mdwith checked examples (a newvite-configfence word intests/docs.rsanddocs-typecheck.mjs); theprefer-q,prefer-map-errandprefer-is-over-tagpages; README (layout, CLI, rule table, quick-fix table, prelude notes); docs index; sidebar; CHANGELOG; skill (references/tool-gaps.md,references/patterns-core.md: usesafe-ts fixone 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, andsafeIswhen a localishides it; untransformedqwritten out (plain,mapErr,return); prelude import fix;must-use-resultandunhandled-failurewithoutqin untransformed files.tests/fixtures/untransformed(a Vite config importingscripts/plugin.ts, and app code that isn't reached and still getsprefer-q).fixing.rsfor 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).vite.config.tsimporting a script that usesqand prelude names):fix prefer-is-over-taggives!is(light, 'Green')with no import.fix needs-transformaddsimport { err, isErr } from '...'and writesqout without semicolons.checkon the three example Vite configs reports noneeds-transform.Not covered, and why
safe-ts migrate throw-to-resultstill writesq(...)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.checkwarning 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.
8c607f0cb34115ae9b75