number-parse-nan, and std parseNumber/parseInteger #22

Merged
sionzee merged 1 commit from rules/number-parse-nan into main 2026-11-04 21:22:15 +01:00
Owner

What

New rule number-parse-nan (warn in recommended.json). It covers Number(x), parseInt(x), parseFloat(x), Number.parseInt/Number.parseFloat, and unary +x when x is declared as a string. Each of these gives NaN for text that isn't a number, and NaN goes on silently: NaN * 2 is NaN, every comparison is false, and JSON.stringify writes null.

It reports when the result is used before a check:

  • Uses: an arithmetic or comparison operand (bitwise operators excluded, since they turn NaN into 0), unary minus, return or an arrow's expression body, an object property value, an array element, an assignment to a member, or a copy into another variable.
  • Checks: Number.isNaN, Number.isFinite, Number.isInteger, Number.isSafeInteger, and the global isNaN/isFinite, on the variable holding the result.
  • Order: for a result stored in a variable (const n = Number(s), or n = ... later), the first use or check after it, in source order, decides. So if (!Number.isInteger(size) || size <= 0) is fine, as in examples/demo/src/settings.ts, and const d = size * 2; if (Number.isNaN(size)) ... is reported.
  • Handling: x || fallback and x | 0 count as handling. x ?? fallback and cond ? x : y are looked through.
  • Not a use: passing the value to a function (it may be the check), template strings, and member access.
  • Not reported: Number(x) where x is a literal or declared number/bigint/boolean, and a local function named parseInt.

New std module @evobug/safe-ts-std/number (packages/std/src/number.ts, exported in package.json):

  • parseNumber(text): Result<number, NumberError> accepts decimal notation (sign, digits, fraction, exponent) with surrounding whitespace allowed, and the result must be finite. Rejected are empty text (which Number reads as 0), trailing characters ("12px", which parseFloat reads as 12), hex, "Infinity", "1e999", "1_000", and similar.
  • parseInteger(text) accepts decimal digits only and requires a safe integer, matching what SafeInt does for JSON. "9007199254740993" fails rather than silently becoming ...992.
  • NumberError = { text, reason }, where reason is one of "empty" | "not a number" | "not finite" | "not an integer" | "not a safe integer". This follows the JsonError style of a plain object you can read.

Quick fix: Use parseNumber(…) for Number(x), parseFloat(x) and +x, and Use parseInteger(…) for parseInt(x) or parseInt(x, 10). Each fix adds the import. A different radix gets no fix. After the fix the value is a Result, so tsc points at the uses that still need isErr; the fix title and the rule page say so, the same way unhandled-failure's unsafe(…) fix does.

Implemented as its own module (crates/safe_ts_core/src/number_parse.rs); it uses the file's ImportContext for fix imports.

Examples that are now reported, rightly: both examples in docs/rules/keep-error-cause.md returned Number(JSON.parse(text)) as a port without a check, so "abc" gave NaN. They now check Number.isInteger(port). examples/demo, examples/vue, the docs Vue demo components and the playground presets report exactly what they did before (diffed per file and location). examples/demo/src/settings.ts checks first and is accepted.

Docs: rule page docs/rules/number-parse-nan.md with checked bad/good examples and a "can't see" section, a "Numbers from text" section in the std-helpers guide, the README rule, quick-fix and std tables, docs/README.md, the sidebar (Correctness group) and the oxlint plugin.

Tests

  • New fixture fixtures/number_parse/main.ts: 10 reported cases (direct, arithmetic, stored, compared, checked too late, +text, reassigned, ??, arrow body) and 8 accepted ones.
  • tests/fixes.rs: Number becomes parseNumber with the import added, parseInt(x, 10) becomes parseInteger(x), and radix 16 gets no fix.
  • packages/std/test/number.test.ts: 4 tests over accepted forms, every rejection reason, and safe-integer limits.
  • Rebased onto main at 1fe2d1d (head 6ffb562), which includes #5, #11, #12 and #6. What had to change:
    • The conflicts were only in the rule lists (analyze.rs runs megamorphic, fetch_signal, then number_parse; number-parse-nan goes after fetch-signal in RULES, the oxlint plugin and recommended.json) and in tests/fixes.rs, where main's import-attributes and fetch-signal tests and this PR's test are all kept.
    • The new std/number module is wired into the npm build (#12). It was already in std's exports, so scripts/build-packages.mjs compiles it to dist/number.js + .d.ts with the rest of std. scripts/verify-packages.mjs now imports parseNumber/parseInteger from the published @evobug/safe-ts-std/number and checks their results.
    • Nothing else: main's new rules don't fire on this PR's fixture or doc examples, and number-parse-nan doesn't fire on main's new docs (dev checks, stable-shape, no-accumulating-copy, the catches/promises fixtures).
  • After the rebase:
    • cargo nextest run --workspace: 105 passed, 0 failed.
    • npm run test:std: 134 passed, 0 failed.
    • npm run test:vite, npm run typecheck, npm run test:docs: passed.
    • node scripts/verify-packages.mjs: all package checks passed (Linux x64 addon only).
    • safe-ts check on examples/demo (22 problems, the demo's intended ones) and examples/vue (none): no number-parse-nan reports.

Left out / uncertain

  • The rule goes by source order, not paths: a check in one branch counts for code after it too. I chose this to keep false positives low; it is stated on the page.
  • +x is only judged when x is declared string. +input.value and +someCall() are not, since +date and +bool are common and fine.
  • Values passed to functions aren't followed (Math.max(0, Number(s)) is not reported). The prelude doesn't include parseNumber/parseInteger; adding them there is a separate decision.
  • The README and docs/README.md rule tables, the rule lists and the sidebar are also touched by #14 (bounded-concurrency), so whichever merges second has trivial conflicts in those rows.

🤖 Generated with Claude Code

## What **New rule `number-parse-nan`** (warn in `recommended.json`). It covers `Number(x)`, `parseInt(x)`, `parseFloat(x)`, `Number.parseInt`/`Number.parseFloat`, and unary `+x` when `x` is declared as a `string`. Each of these gives `NaN` for text that isn't a number, and `NaN` goes on silently: `NaN * 2` is `NaN`, every comparison is false, and `JSON.stringify` writes `null`. It reports when the result is **used** before a check: - **Uses:** an arithmetic or comparison operand (bitwise operators excluded, since they turn `NaN` into 0), unary minus, `return` or an arrow's expression body, an object property value, an array element, an assignment to a member, or a copy into another variable. - **Checks:** `Number.isNaN`, `Number.isFinite`, `Number.isInteger`, `Number.isSafeInteger`, and the global `isNaN`/`isFinite`, on the variable holding the result. - **Order:** for a result stored in a variable (`const n = Number(s)`, or `n = ...` later), the first use or check after it, in source order, decides. So `if (!Number.isInteger(size) || size <= 0)` is fine, as in `examples/demo/src/settings.ts`, and `const d = size * 2; if (Number.isNaN(size)) ...` is reported. - **Handling:** `x || fallback` and `x | 0` count as handling. `x ?? fallback` and `cond ? x : y` are looked through. - **Not a use:** passing the value to a function (it may be the check), template strings, and member access. - **Not reported:** `Number(x)` where `x` is a literal or declared `number`/`bigint`/`boolean`, and a local function named `parseInt`. **New std module `@evobug/safe-ts-std/number`** (`packages/std/src/number.ts`, exported in package.json): - `parseNumber(text): Result<number, NumberError>` accepts decimal notation (sign, digits, fraction, exponent) with surrounding whitespace allowed, and the result must be finite. Rejected are empty text (which `Number` reads as 0), trailing characters (`"12px"`, which `parseFloat` reads as 12), hex, `"Infinity"`, `"1e999"`, `"1_000"`, and similar. - `parseInteger(text)` accepts decimal digits only and requires a safe integer, matching what `SafeInt` does for JSON. `"9007199254740993"` fails rather than silently becoming `...992`. - `NumberError = { text, reason }`, where `reason` is one of `"empty" | "not a number" | "not finite" | "not an integer" | "not a safe integer"`. This follows the `JsonError` style of a plain object you can read. **Quick fix:** *Use parseNumber(…)* for `Number(x)`, `parseFloat(x)` and `+x`, and *Use parseInteger(…)* for `parseInt(x)` or `parseInt(x, 10)`. Each fix adds the import. A different radix gets no fix. After the fix the value is a `Result`, so tsc points at the uses that still need `isErr`; the fix title and the rule page say so, the same way `unhandled-failure`'s `unsafe(…)` fix does. Implemented as its own module (`crates/safe_ts_core/src/number_parse.rs`); it uses the file's `ImportContext` for fix imports. **Examples that are now reported, rightly:** both examples in `docs/rules/keep-error-cause.md` returned `Number(JSON.parse(text))` as a port without a check, so `"abc"` gave `NaN`. They now check `Number.isInteger(port)`. `examples/demo`, `examples/vue`, the docs Vue demo components and the playground presets report exactly what they did before (diffed per file and location). `examples/demo/src/settings.ts` checks first and is accepted. Docs: rule page `docs/rules/number-parse-nan.md` with checked bad/good examples and a "can't see" section, a "Numbers from text" section in the std-helpers guide, the README rule, quick-fix and std tables, `docs/README.md`, the sidebar (Correctness group) and the oxlint plugin. ## Tests - New fixture `fixtures/number_parse/main.ts`: 10 reported cases (direct, arithmetic, stored, compared, checked too late, `+text`, reassigned, `??`, arrow body) and 8 accepted ones. - `tests/fixes.rs`: `Number` becomes `parseNumber` with the import added, `parseInt(x, 10)` becomes `parseInteger(x)`, and radix 16 gets no fix. - `packages/std/test/number.test.ts`: 4 tests over accepted forms, every rejection reason, and safe-integer limits. - Rebased onto `main` at 1fe2d1d (head 6ffb562), which includes #5, #11, #12 and #6. What had to change: - The conflicts were only in the rule lists (`analyze.rs` runs `megamorphic`, `fetch_signal`, then `number_parse`; `number-parse-nan` goes after `fetch-signal` in `RULES`, the oxlint plugin and `recommended.json`) and in `tests/fixes.rs`, where main's `import-attributes` and `fetch-signal` tests and this PR's test are all kept. - The new `std/number` module is wired into the npm build (#12). It was already in std's `exports`, so `scripts/build-packages.mjs` compiles it to `dist/number.js` + `.d.ts` with the rest of std. `scripts/verify-packages.mjs` now imports `parseNumber`/`parseInteger` from the published `@evobug/safe-ts-std/number` and checks their results. - Nothing else: main's new rules don't fire on this PR's fixture or doc examples, and `number-parse-nan` doesn't fire on main's new docs (dev checks, stable-shape, no-accumulating-copy, the catches/promises fixtures). - After the rebase: - `cargo nextest run --workspace`: 105 passed, 0 failed. - `npm run test:std`: 134 passed, 0 failed. - `npm run test:vite`, `npm run typecheck`, `npm run test:docs`: passed. - `node scripts/verify-packages.mjs`: all package checks passed (Linux x64 addon only). - `safe-ts check` on `examples/demo` (22 problems, the demo's intended ones) and `examples/vue` (none): no `number-parse-nan` reports. ## Left out / uncertain - The rule goes by source order, not paths: a check in one branch counts for code after it too. I chose this to keep false positives low; it is stated on the page. - `+x` is only judged when `x` is declared `string`. `+input.value` and `+someCall()` are not, since `+date` and `+bool` are common and fine. - Values passed to functions aren't followed (`Math.max(0, Number(s))` is not reported). The prelude doesn't include `parseNumber`/`parseInteger`; adding them there is a separate decision. - The README and `docs/README.md` rule tables, the rule lists and the sidebar are also touched by #14 (`bounded-concurrency`), so whichever merges second has trivial conflicts in those rows. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
sionzee force-pushed rules/number-parse-nan from 5df9bea18d to 6ffb562cf9
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m36s
2026-11-03 23:41:11 +01:00
Compare
sionzee deleted branch rules/number-parse-nan 2026-11-04 21:22:15 +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!22
No description provided.