refactor(nxz): split parseMemlimitSize via extract-method (CC 17→2)#120
Merged
Conversation
Drop cognitive complexity from 17 to 2 by extracting three private helpers (isZeroSynonym, parseSuffixed, parsePlainBytes) and hoisting IEC/SI multiplier maps + regex literals to module scope. The main function becomes a thin coordinator: zero-synonym short-circuit, nullish-coalescing chain across the three parsers, single error throw, single zero-result normalization. 29/29 vitest cases in parse-memlimit.test.ts still pass byte-identical; behavior is preserved across decimal-rejection, mixed-case suffix, all-zero forms, whitespace handling, and arbitrary-precision values beyond UINT64_MAX.
2 tasks
oorabona
added a commit
that referenced
this pull request
Apr 30, 2026
…#121) The PR #120 extract-method refactor introduced three defensive paths in parseSuffixed that vitest's v8 instrumentation flagged as unreachable: the '?? ""' fallbacks on regex capture groups (unreachable when exec returns non-null) and the 'mult === undefined' guard (unreachable because the regex constrains the suffix to keys present in the multiplier map). - Replace 'match[1] ?? ""' and 'match[2] ?? ""' with 'as string' type assertions: compile-time only, no runtime branch for v8 to instrument, and not flagged by biome's noNonNullAssertion rule (unlike non-null '!' which would re-introduce the lint warning we resolved in PR #117 fix-round 1). - Wrap the noUncheckedIndexedAccess-required mult-undefined guard with /* v8 ignore start ... stop */: TypeScript strict mode demands the check but the regex+map alignment makes it structurally unreachable. Project convention is start/stop pairs (never 'next'). Coverage: lines 95.2 -> 100, branches 83.3 -> 100 on memlimit.ts. 707 tests still pass byte-identical.
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.
Summary
Pure refactor.
parseMemlimitSizeinpackages/nxz/src/memlimit.tshad cognitive complexity 17 (biome cap 15). Three private helpers extracted, multiplier maps + regex literals hoisted to module scope.Result
parseMemlimitSize: CC 17 → 2 — coordinator with zero-synonym short-circuit, nullish-coalescing chain, one error throw, one result normalizationisZeroSynonym: CC 2 —'0'or case-insensitive'max'→ trueparseSuffixed: CC 3 — generic regex+multipliers, returnsnullif no match (used by both IEC and SI branches, eliminates the prior duplication)parsePlainBytes: CC 1 — bare integer fallbackBehavior preservation
29/29 vitest cases in
packages/nxz/test/parse-memlimit.test.tspass byte-identical. The exportedparseMemlimitSize(s: string): bigint | undefinedsignature is unchanged. Edge cases verified : decimal rejection (1.5MiBthrows), mixed-case suffix (256mibaccepted), all-zero forms (0,0MiB,max,MAX→ undefined), whitespace tolerance (256 MiBaccepted,256MiBrejected), arbitrary-precision (99999999999999999999999999999exact bigint).Diff
1 file, +50 / -48 net.
Gates
pnpm install --frozen-lockfile: EXIT 0pnpm --filter nxz-cli build: EXIT 0pnpm type-check: EXIT 0pnpm exec biome check .: EXIT 0, 0 warnings, 0 errors (the prior CC=17 warning is the last one, repo is now biome-clean)pnpm test: 707 pass / 0 fail / 3 skippedTest plan