Fewer false positives: JSON.stringify, Number(), || defaults decided by declared types; no-redos, bounded-concurrency, URL and RegExp #67

Merged
sionzee merged 2 commits from evobug/fb3-false-positives into main 2026-11-16 23:38:53 +01:00
Owner

Feedback round 3, area false positives (the "Rules" section; #4 of the top ten).

What changed, per item

  1. JSON.stringify of plain data — done. New shape.rs records, from syntax, what a value is: a binding's declared type, a member of another value, what it was destructured from, the item a callback or for…of gets, a built-in method's result (slice, map.get, map.size, Object.fromEntries, Object.entries), Object.freeze(...), a function's or method's declared return type, and values after typeof x === "string" or if (isErr(r)) return. The whole-program pass evaluates it with the validator generator's type model (json_value.rs): aliases, interfaces, imports and re-exports, Record, mapped and conditional types, Option, and T["key"] looked up without resolving the rest of T. Every case in the entry passes now, including the destructuring cases that disagreed (now consistent, by type).
    • Still reported, and the message says why: bigint, unknown, JsonValue ("can hold a cycle"), a type that refers to itself, a toJSON field.
    • Replacer function: kept reported, with the reason (it runs for every value and may throw or return what can't be written). Documented.
    • Getters: a getter in an object literal is reported; a class getter read by this.x uses its declared type (it runs before JSON.stringify does).
    • Newly reported: const xs = [] (TypeScript lets its type grow with whatever is pushed).
    • Not done: page.evaluate(...)-style inferred types. SAFE-TS has no type inference for return values without a declared type.
  2. number-parse-nan — done. Number(...) of booleans (!!x, comparisons, in, a field declared boolean, also in another file's type), of numbers, and of capture groups of a matched regex that are only digits ((\d+), [0-9]{2}, -?\d+; top-level groups only, from exec, non-g match, matchAll). parseInt(true) is NaN, so booleans are exempt for Number only; this fixes an old exemption that covered parseInt too.
  3. prefer-nullish-default — done, by declared return type. Decision: f(x) || 1 with f declared (…): number is not reported. A number can't be undefined, so ?? would never apply and the quick fix would silently drop the default. Replacing 0 is the point here, which is the rule's existing exemption for Math.hypot(...) || 1. This is the opposite of the brief's first reading; the feedback entry (0 means "no run") supports it. Declared Option<number>, it is still reported. Works across files.
  4. no-redos — done. Negated classes are real sets now, no longer "any character". (?:[^<>"]|"[^"]*")* and the attribute tag regex pass; the tag regex is polynomial at worst, and only exponential cases are reported. Existing fixtures plus ([^a]+)+b, (?:[^x]|y)+$ stay reported.
  5. bounded-concurrency — done. Object.entries/keys/values of an Object.freeze({…}) or {…} as const table without spreads (or of an inline literal) has a fixed length, so it isn't reported. A Record<string, …> table still is.
  6. new URL / fileURLToPath — done. new URL("src", import.meta.url) (any path-like literal or const: no scheme, no // host, no whitespace or \) is accepted. So are fileURLToPath(new URL(<such a path>, import.meta.url)) and fileURLToPath(import.meta.url). Encoded %2F/%5C are refused, since fileURLToPath throws on them. The docs give import.meta.dirname / import.meta.filename as the alternative for helpers that take a parameter.
  7. new RegExp inconsistency — explained and fixed. Cause: inside a test callback nothing is reported (the runner catches the throw), so the "similar template in reproduction.test.ts" was quiet. In a helper, any template was reported, even number-only ones. Now a template made of escaped text (escapeRegExp), constant text and numbers (also numbers typed in other files) is accepted. Each pattern is parsed with worst-case stand-ins, so `x{${n}}` with u is still reported. Unescaped text is reported with the reason.

RTSS (evobug/adopt-safe-ts-architecture), before → after

  • unsafe(() => JSON.stringify(…)): 37 sites. With the wrapper removed, all 37 were reported before. After: 24. So 13 wrappers become unnecessary. Of the 24: 16 unknown, 1 JsonValue, 2 replacers (all with the reason in the message), and 5 untyped values (data: object, path.join(…) results, callback results, a spread of loop items). Items named in the feedback that are fixed: Object.fromEntries of an aliased Map (control-groups, vite-plugins), the cheat action spread, this.options… member reads in fake-server, DeepPartial<ServerConfig> overrides (draft.ts), Record aliases.
  • fileURLToPath(new URL(…, import.meta.url)): the 3 helpers (here ×2, besideThis) take a parameter and still need unsafe. Their 7 call sites written inline (fileURLToPath(new URL('src', import.meta.url))): before, each was reported; after, 0 are.
  • new RegExp: the one non-test site (assetTag) interpolates unescaped parameters and is still reported, rightly. Test-body ones were never reported.
  • bounded-concurrency (SAMPLE_URLS, an Object.freeze table), no-redos (R3 regexes), number-parse-nan (Number(!!a.job), Number(a > 0), Number(b.shows), Number(m[1])): the RTSS code was already rewritten. The same shapes in the new typed_values fixture are reported before and pass after.
  • No other RTSS diagnostic changed (61 before, the same 61 after). Check time unchanged (~0.7 s vs ~0.64 s for 550 files).
  • New fixture typed_values: 59 reports with main's checker → 18 now. Each of the 18 is expected.

Docs changed

docs/rules/unhandled-failure.md (JSON.stringify by type, why it is still reported, new RegExp templates, module URLs, fileURLToPath, import.meta.dirname), docs/rules/number-parse-nan.md, docs/rules/prefer-nullish-default.md, docs/rules/no-redos.md, docs/rules/bounded-concurrency.md, docs/guide/libraries.md (built-ins that can't fail), docs/guide/json.md (jsonNull with JSON.stringify), CHANGELOG.md (Unreleased → Fixed).

Verification

All pass: npm run build, cargo nextest run --workspace (212/212, including docs examples and the new typed_values fixture), cargo clippy -q --workspace --all-targets (no warnings), npm run test:std (285), npm run typecheck, npm run typecheck:emit, node scripts/docs-typecheck.mjs and npm run test:docs (386 examples), npm run test:vite, npm run bench:check, node scripts/verify-packages.mjs. New unit tests in redos.rs (negated classes, digit groups).

🤖 Generated with Claude Code

Feedback round 3, area **false positives** (the "Rules" section; #4 of the top ten). ## What changed, per item 1. **`JSON.stringify` of plain data — done.** New `shape.rs` records, from syntax, what a value is: a binding's declared type, a member of another value, what it was destructured from, the item a callback or `for…of` gets, a built-in method's result (`slice`, `map.get`, `map.size`, `Object.fromEntries`, `Object.entries`), `Object.freeze(...)`, a function's or method's declared return type, and values after `typeof x === "string"` or `if (isErr(r)) return`. The whole-program pass evaluates it with the validator generator's type model (`json_value.rs`): aliases, interfaces, imports and re-exports, `Record`, mapped and conditional types, `Option`, and `T["key"]` looked up without resolving the rest of `T`. Every case in the entry passes now, including the destructuring cases that disagreed (now consistent, by type). - Still reported, and the message says why: `bigint`, `unknown`, `JsonValue` ("can hold a cycle"), a type that refers to itself, a `toJSON` field. - **Replacer function: kept reported**, with the reason (it runs for every value and may throw or return what can't be written). Documented. - Getters: a getter in an object literal is reported; a class getter read by `this.x` uses its declared type (it runs before `JSON.stringify` does). - Newly reported: `const xs = []` (TypeScript lets its type grow with whatever is pushed). - Not done: `page.evaluate(...)`-style inferred types. SAFE-TS has no type inference for return values without a declared type. 2. **`number-parse-nan` — done.** `Number(...)` of booleans (`!!x`, comparisons, `in`, a field declared `boolean`, also in another file's type), of numbers, and of capture groups of a matched regex that are only digits (`(\d+)`, `[0-9]{2}`, `-?\d+`; top-level groups only, from `exec`, non-`g` `match`, `matchAll`). `parseInt(true)` is `NaN`, so booleans are exempt for `Number` only; this fixes an old exemption that covered `parseInt` too. 3. **`prefer-nullish-default` — done, by declared return type.** Decision: `f(x) || 1` with `f` declared `(…): number` is **not** reported. A `number` can't be `undefined`, so `??` would never apply and the quick fix would silently drop the default. Replacing `0` is the point here, which is the rule's existing exemption for `Math.hypot(...) || 1`. This is the opposite of the brief's first reading; the feedback entry (0 means "no run") supports it. Declared `Option<number>`, it is still reported. Works across files. 4. **`no-redos` — done.** Negated classes are real sets now, no longer "any character". `(?:[^<>"]|"[^"]*")*` and the attribute tag regex pass; the tag regex is polynomial at worst, and only exponential cases are reported. Existing fixtures plus `([^a]+)+b`, `(?:[^x]|y)+$` stay reported. 5. **`bounded-concurrency` — done.** `Object.entries/keys/values` of an `Object.freeze({…})` or `{…} as const` table without spreads (or of an inline literal) has a fixed length, so it isn't reported. A `Record<string, …>` table still is. 6. **`new URL` / `fileURLToPath` — done.** `new URL("src", import.meta.url)` (any path-like literal or `const`: no scheme, no `//` host, no whitespace or `\`) is accepted. So are `fileURLToPath(new URL(<such a path>, import.meta.url))` and `fileURLToPath(import.meta.url)`. Encoded `%2F`/`%5C` are refused, since `fileURLToPath` throws on them. The docs give `import.meta.dirname` / `import.meta.filename` as the alternative for helpers that take a parameter. 7. **`new RegExp` inconsistency — explained and fixed.** Cause: inside a test callback nothing is reported (the runner catches the throw), so the "similar template in reproduction.test.ts" was quiet. In a helper, any template was reported, even number-only ones. Now a template made of escaped text (`escapeRegExp`), constant text and numbers (also numbers typed in other files) is accepted. Each pattern is parsed with worst-case stand-ins, so `` `x{${n}}` `` with `u` is still reported. Unescaped text is reported with the reason. ## RTSS (`evobug/adopt-safe-ts-architecture`), before → after - `unsafe(() => JSON.stringify(…))`: 37 sites. With the wrapper removed, all 37 were reported before. After: 24. So **13 wrappers become unnecessary**. Of the 24: 16 `unknown`, 1 `JsonValue`, 2 replacers (all with the reason in the message), and 5 untyped values (`data: object`, `path.join(…)` results, callback results, a spread of loop items). Items named in the feedback that are fixed: `Object.fromEntries` of an aliased `Map` (control-groups, vite-plugins), the cheat action spread, `this.options…` member reads in fake-server, `DeepPartial<ServerConfig>` overrides (draft.ts), `Record` aliases. - `fileURLToPath(new URL(…, import.meta.url))`: the 3 helpers (`here` ×2, `besideThis`) take a parameter and still need `unsafe`. Their 7 call sites written inline (`fileURLToPath(new URL('src', import.meta.url))`): before, each was reported; after, 0 are. - `new RegExp`: the one non-test site (`assetTag`) interpolates unescaped parameters and is still reported, rightly. Test-body ones were never reported. - `bounded-concurrency` (`SAMPLE_URLS`, an `Object.freeze` table), `no-redos` (R3 regexes), `number-parse-nan` (`Number(!!a.job)`, `Number(a > 0)`, `Number(b.shows)`, `Number(m[1])`): the RTSS code was already rewritten. The same shapes in the new `typed_values` fixture are reported before and pass after. - No other RTSS diagnostic changed (61 before, the same 61 after). Check time unchanged (~0.7 s vs ~0.64 s for 550 files). - New fixture `typed_values`: 59 reports with main's checker → 18 now. Each of the 18 is expected. ## Docs changed `docs/rules/unhandled-failure.md` (`JSON.stringify` by type, why it is still reported, `new RegExp` templates, module URLs, `fileURLToPath`, `import.meta.dirname`), `docs/rules/number-parse-nan.md`, `docs/rules/prefer-nullish-default.md`, `docs/rules/no-redos.md`, `docs/rules/bounded-concurrency.md`, `docs/guide/libraries.md` (built-ins that can't fail), `docs/guide/json.md` (`jsonNull` with `JSON.stringify`), `CHANGELOG.md` (Unreleased → Fixed). ## Verification All pass: `npm run build`, `cargo nextest run --workspace` (212/212, including docs examples and the new `typed_values` fixture), `cargo clippy -q --workspace --all-targets` (no warnings), `npm run test:std` (285), `npm run typecheck`, `npm run typecheck:emit`, `node scripts/docs-typecheck.mjs` and `npm run test:docs` (386 examples), `npm run test:vite`, `npm run bench:check`, `node scripts/verify-packages.mjs`. New unit tests in `redos.rs` (negated classes, digit groups). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Fewer false positives: values decided by their declared types, across files
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m16s
8acb4e0d58
JSON.stringify, number-parse-nan and prefer-nullish-default now ask what an
expression is (shape.rs: declared types, members, destructuring, callback
items, this fields, typeof/isErr narrowing), and the whole-program pass
answers with the validator generator's type model (json_value.rs), so types
from other files, aliases, Record, mapped and indexed types count.
JSON.stringify messages say why a value is still refused (bigint, unknown,
JsonValue, recursive type, toJSON, replacer).

Also: no-redos compares negated classes; bounded-concurrency accepts frozen
or `as const` tables; new URL / fileURLToPath of a path against
import.meta.url and new RegExp of escaped/constant/number templates are not
unhandled-failure.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Merge remote-tracking branch 'origin/main' into up-67
All checks were successful
Benchmarks / bench (pull_request) Successful in 2m23s
d5ee04eebc
sionzee deleted branch evobug/fb3-false-positives 2026-11-16 23:38:53 +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!67
No description provided.