number-parse-nan, and std parseNumber/parseInteger #22
Loading…
Reference in a new issue
No description provided.
Delete branch "rules/number-parse-nan"
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?
What
New rule
number-parse-nan(warn inrecommended.json). It coversNumber(x),parseInt(x),parseFloat(x),Number.parseInt/Number.parseFloat, and unary+xwhenxis declared as astring. Each of these givesNaNfor text that isn't a number, andNaNgoes on silently:NaN * 2isNaN, every comparison is false, andJSON.stringifywritesnull.It reports when the result is used before a check:
NaNinto 0), unary minus,returnor an arrow's expression body, an object property value, an array element, an assignment to a member, or a copy into another variable.Number.isNaN,Number.isFinite,Number.isInteger,Number.isSafeInteger, and the globalisNaN/isFinite, on the variable holding the result.const n = Number(s), orn = ...later), the first use or check after it, in source order, decides. Soif (!Number.isInteger(size) || size <= 0)is fine, as inexamples/demo/src/settings.ts, andconst d = size * 2; if (Number.isNaN(size)) ...is reported.x || fallbackandx | 0count as handling.x ?? fallbackandcond ? x : yare looked through.Number(x)wherexis a literal or declarednumber/bigint/boolean, and a local function namedparseInt.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 (whichNumberreads as 0), trailing characters ("12px", whichparseFloatreads as 12), hex,"Infinity","1e999","1_000", and similar.parseInteger(text)accepts decimal digits only and requires a safe integer, matching whatSafeIntdoes for JSON."9007199254740993"fails rather than silently becoming...992.NumberError = { text, reason }, wherereasonis one of"empty" | "not a number" | "not finite" | "not an integer" | "not a safe integer". This follows theJsonErrorstyle of a plain object you can read.Quick fix: Use parseNumber(…) for
Number(x),parseFloat(x)and+x, and Use parseInteger(…) forparseInt(x)orparseInt(x, 10). Each fix adds the import. A different radix gets no fix. After the fix the value is aResult, so tsc points at the uses that still needisErr; the fix title and the rule page say so, the same wayunhandled-failure'sunsafe(…)fix does.Implemented as its own module (
crates/safe_ts_core/src/number_parse.rs); it uses the file'sImportContextfor fix imports.Examples that are now reported, rightly: both examples in
docs/rules/keep-error-cause.mdreturnedNumber(JSON.parse(text))as a port without a check, so"abc"gaveNaN. They now checkNumber.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.tschecks first and is accepted.Docs: rule page
docs/rules/number-parse-nan.mdwith 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
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:NumberbecomesparseNumberwith the import added,parseInt(x, 10)becomesparseInteger(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.mainat1fe2d1d(head6ffb562), which includes #5, #11, #12 and #6. What had to change:analyze.rsrunsmegamorphic,fetch_signal, thennumber_parse;number-parse-nangoes afterfetch-signalinRULES, the oxlint plugin andrecommended.json) and intests/fixes.rs, where main'simport-attributesandfetch-signaltests and this PR's test are all kept.std/numbermodule is wired into the npm build (#12). It was already in std'sexports, soscripts/build-packages.mjscompiles it todist/number.js+.d.tswith the rest of std.scripts/verify-packages.mjsnow importsparseNumber/parseIntegerfrom the published@evobug/safe-ts-std/numberand checks their results.number-parse-nandoesn't fire on main's new docs (dev checks, stable-shape, no-accumulating-copy, the catches/promises fixtures).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 checkonexamples/demo(22 problems, the demo's intended ones) andexamples/vue(none): nonumber-parse-nanreports.Left out / uncertain
+xis only judged whenxis declaredstring.+input.valueand+someCall()are not, since+dateand+boolare common and fine.Math.max(0, Number(s))is not reported). The prelude doesn't includeparseNumber/parseInteger; adding them there is a separate decision.docs/README.mdrule 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
5df9bea18dto6ffb562cf9