Idiomatic rules (opt-in): prefer-option, no-swallowed-error; undefined-to-option codemod #48

Merged
sionzee merged 2 commits from evobug/idiomatic-option into main 2026-11-16 17:31:58 +01:00
Owner

From the TwitchRTS diff review. The migration wrote GorePose | undefined where the framework's spelling is Option<GorePose>: about 200 | undefined against 828 Option<…> in their branch. Our own no-null quick fix produced those.

Opt-in idiomatic layer. "safe-ts": { "idiomatic": true } in package.json turns it on. The project pass drops these rules' diagnostics unless the setting is on, so check, Vite, the Node loader, the LSP and the oxlint plugin all follow one switch. The oxlint preset lists the rules, but they report nothing until the setting is on.

  • prefer-option: T | undefined is written Option<T>.
    • The quick fix adds the import (none with the prelude).
    • No fix is offered in a file that has its own Option type.
    • Skipped: unions with null (no-null's job), void/any/unknown/never, the argument of Option<…>, and .d.ts files.
    • Error in the oxlint preset.
  • no-swallowed-error: a failure turned into undefined.
    • Reported: if (isErr(r)) return undefined (also isErr(a) || isErr(b)), isErr(r) ? undefined : r (and the negated form), catch { return undefined; }, .catch(() => undefined).
    • Not reported: a branch that does something with the error first.
    • Warning in the oxlint preset.

Fixes and codemod

  • safe-ts migrate undefined-to-option: applies prefer-option's fix everywhere, with notes for files that have their own Option type.
  • no-null's quick fix on T | null now writes Option<T>, as the null-to-undefined codemod already did.
  • q-needs-result-return now accepts std's JsonResult<T>.

Docs turn the setting on, so every example follows the rules:

  • 13 examples were converted with the new codemod.
  • The JSON guide's examples now return the parse error (JsonResult<T> with q) instead of undefined.
  • The SQLite example from #46 now returns JsonResult<Option<Account>>.
  • New pages: rule pages for both rules, an "Idiomatic rules" section in the adoption guide, the codemod section, the README table and the sidebar.

Verification

  • Rust: 183 tests passed. New fixtures idiomatic and idiomatic_off, plus codemod cases basic, clash and imported.
  • std tests: 242 passed.
  • Docs: 425 examples checked, 348 compile under tsc; the docs site builds.
  • npm run build, typecheck and typecheck:emit pass.
  • Clippy reports nothing in the new code.

🤖 Generated with Claude Code

From the TwitchRTS diff review. The migration wrote `GorePose | undefined` where the framework's spelling is `Option<GorePose>`: about 200 `| undefined` against 828 `Option<…>` in their branch. Our own `no-null` quick fix produced those. **Opt-in idiomatic layer.** `"safe-ts": { "idiomatic": true }` in package.json turns it on. The project pass drops these rules' diagnostics unless the setting is on, so `check`, Vite, the Node loader, the LSP and the oxlint plugin all follow one switch. The oxlint preset lists the rules, but they report nothing until the setting is on. - **`prefer-option`:** `T | undefined` is written `Option<T>`. - The quick fix adds the import (none with the prelude). - No fix is offered in a file that has its own `Option` type. - Skipped: unions with `null` (`no-null`'s job), `void`/`any`/`unknown`/`never`, the argument of `Option<…>`, and `.d.ts` files. - Error in the oxlint preset. - **`no-swallowed-error`:** a failure turned into `undefined`. - Reported: `if (isErr(r)) return undefined` (also `isErr(a) || isErr(b)`), `isErr(r) ? undefined : r` (and the negated form), `catch { return undefined; }`, `.catch(() => undefined)`. - Not reported: a branch that does something with the error first. - Warning in the oxlint preset. **Fixes and codemod** - **`safe-ts migrate undefined-to-option`:** applies `prefer-option`'s fix everywhere, with notes for files that have their own `Option` type. - **`no-null`'s quick fix** on `T | null` now writes `Option<T>`, as the `null-to-undefined` codemod already did. - **`q-needs-result-return`** now accepts std's `JsonResult<T>`. **Docs** turn the setting on, so every example follows the rules: - 13 examples were converted with the new codemod. - The JSON guide's examples now return the parse error (`JsonResult<T>` with `q`) instead of `undefined`. - The SQLite example from #46 now returns `JsonResult<Option<Account>>`. - New pages: rule pages for both rules, an "Idiomatic rules" section in the adoption guide, the codemod section, the README table and the sidebar. **Verification** - Rust: 183 tests passed. New fixtures `idiomatic` and `idiomatic_off`, plus codemod cases basic, clash and imported. - std tests: 242 passed. - Docs: 425 examples checked, 348 compile under tsc; the docs site builds. - `npm run build`, typecheck and typecheck:emit pass. - Clippy reports nothing in the new code. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Idiomatic rules (opt-in): prefer-option, no-swallowed-error; undefined-to-option codemod
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m40s
4facd70a6c
`"safe-ts": { "idiomatic": true }` in package.json turns on a second layer of
rules that hold code to the framework's own idioms. The project pass drops
their diagnostics unless the setting is on, so check, Vite, the loader, the
LSP and the oxlint plugin all follow it.

- prefer-option: `T | undefined` is written `Option<T>`. Quick fix adds the
  import (none with the prelude); no fix where the file has its own `Option`.
- no-swallowed-error: `if (isErr(r)) return undefined`, `isErr(r) ? undefined : r`,
  `catch { return undefined; }`, `.catch(() => undefined)`: return the Result.
- `safe-ts migrate undefined-to-option` applies prefer-option's fix everywhere.
- no-null's quick fix on `T | null` writes `Option<T>`, as the codemod does.
- q-needs-result-return accepts std's `JsonResult<T>`.

The docs turn the setting on: examples write `Option<T>`, and the JSON guide's
examples return the parse error instead of `undefined`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merge remote-tracking branch 'origin/main' into up-48
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m50s
43e84b5280
sionzee deleted branch evobug/idiomatic-option 2026-11-16 17:31:58 +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!48
No description provided.