Ratchet unicorn/prefer-number-coercion to error#296
Merged
Conversation
`Number()` replaces `parseInt(x, 10)` / `parseFloat(x)` at the 13 sites
where the input is a bare numeric token — `\d`-anchored regex captures
(timer colon/unit groups, the `px`-stripped length, duration tokens, rgb
channels and alpha) and a `String(i)` dataset index — so the whole-string
coercion is exactly equivalent.
Two sites in hidden-text-strip.ts keep `parseFloat` behind a scoped
disable, because its leading-numeric parse is load-bearing:
- `hasNonzeroDuration` reads computed `transition`/`animation-duration`,
which carry a unit suffix (`"0.5s"`); `Number("0.5s")` is `NaN`, which
would make every non-zero duration read as zero and disable the check.
- the `opacity-0` test reads computed `opacity`, which is `""` when unset
(notably under jsdom); `Number("") === 0` would flag and redact every
such element, whereas `parseFloat("")` is `NaN`.
Full suite (2059 tests) green; `bun run check` clean.
Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of the #279 unicorn ratchet (follow-up to #295, now merged).
This is the "needs care — not semantically equivalent" candidate (
parseInt/parseFloat→Number()), so each of the 15 sites was verified individually rather than batch-autofixed.Converted to
Number()(13 sites — provably equivalent)All read a bare numeric token, so whole-string coercion matches the leading-numeric parse exactly:
countdown-timer-redact.ts(66/67/69/80) —\d-anchored colon/unit regex captures (\d{1,3},[0-5]\d,\d+).hidden-text-strip.ts:px-stripped capture(-?\d+(?:\.\d+)?).(\d*\.?\d+)(unit is a separate group).matrix()/matrix3d()components (browser-normalized unitless numbers;Numberhandles scientific notation identically).(\d+(?:\.\d+)?)/([\d.]+); theNumber.isNaNguard right below makesNumber()'s stricter rejection intentional.text-walk-shadow.property.test.ts—dataset.index, always set viaString(i).Kept
parseFloatbehind a scoped disable (2 sites — semantics load-bearing)In
hidden-text-strip.ts,parseFloat's "parse the leading number, ignore the rest" is the point:hasNonzeroDurationreads computedtransition-duration/animation-duration, which carry a unit suffix ("0.5s","150ms").Number("0.5s")isNaN→ every non-zero duration would read as zero and silently disable the hidden-text duration check.opacity-0check reads computedopacity, which comes back""when unset (notably under jsdom).Number("") === 0would flag and redact every such element;parseFloat("")isNaN, so only a genuine0matches.Each carries an inline comment +
eslint-disable-next-line … -- <reason>.Verification
bun run checkclean (biome + tsc + eslint, exit 0);unicorn/prefer-number-coercionnowerrorwith 0 violations.🤖 Generated with Claude Code