Skip to content

refactor(nxz): split parseMemlimitSize via extract-method (CC 17→2)#120

Merged
oorabona merged 1 commit into
masterfrom
refactor/parse-memlimit-cc
Apr 30, 2026
Merged

refactor(nxz): split parseMemlimitSize via extract-method (CC 17→2)#120
oorabona merged 1 commit into
masterfrom
refactor/parse-memlimit-cc

Conversation

@oorabona
Copy link
Copy Markdown
Owner

Summary

Pure refactor. parseMemlimitSize in packages/nxz/src/memlimit.ts had 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 normalization
  • isZeroSynonym: CC 2 — '0' or case-insensitive 'max' → true
  • parseSuffixed: CC 3 — generic regex+multipliers, returns null if no match (used by both IEC and SI branches, eliminates the prior duplication)
  • parsePlainBytes: CC 1 — bare integer fallback

Behavior preservation

29/29 vitest cases in packages/nxz/test/parse-memlimit.test.ts pass byte-identical. The exported parseMemlimitSize(s: string): bigint | undefined signature is unchanged. Edge cases verified : decimal rejection (1.5MiB throws), mixed-case suffix (256mib accepted), all-zero forms (0, 0MiB, max, MAX → undefined), whitespace tolerance (256 MiB accepted, 256MiB rejected), arbitrary-precision (99999999999999999999999999999 exact bigint).

Diff

1 file, +50 / -48 net.

Gates

  • pnpm install --frozen-lockfile: EXIT 0
  • pnpm --filter nxz-cli build: EXIT 0
  • pnpm type-check: EXIT 0
  • pnpm 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 skipped

Test plan

  • CI green
  • Biome check shows zero warnings repo-wide

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.
@oorabona oorabona merged commit 43c4d25 into master Apr 30, 2026
6 of 7 checks passed
@oorabona oorabona deleted the refactor/parse-memlimit-cc branch April 30, 2026 10:28
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant