Skip to content

refactor(wallet-toolbox): reduce cognitive complexity in StorageIdb#88

Merged
sirdeggen merged 2 commits intomainfrom
sonar/w2-storageidb
May 7, 2026
Merged

refactor(wallet-toolbox): reduce cognitive complexity in StorageIdb#88
sirdeggen merged 2 commits intomainfrom
sonar/w2-storageidb

Conversation

@sirdeggen
Copy link
Copy Markdown
Contributor

Summary

  • Extract 14 per-entity partial-match helper functions into new idbHelpers.ts sibling file, eliminating inline if chains in all filter* methods
  • Replace while (cursor) { if (firstTime) ... firstTime = false; ... cursor = await cursor.continue() } pattern with for (; cursor != null; cursor = await cursor.continue()) throughout
  • Extract private helpers applyDateFields, applyBooleanFields, normalizeEntityValues, applyOptionalDateFields, applyIntegerBooleanFields, normalizeForStorage from validateEntity / validatePartialForUpdate / validateEntityForInsert
  • Extract 15 IDB schema upgrade helpers (upgradeProvenTxs, upgradeUsers, etc.) from initDB to reduce its complexity
  • Net result: 20 S3776 cognitive-complexity violations eliminated; StorageIdb.ts lint errors reduced from 188 to 137 (further reduced by extracted helpers in idbHelpers.ts)

Fixes #38, #44

Test plan

  • pnpm --filter @bsv/wallet-toolbox run build passes (StorageIdb-specific TS errors: none; pre-existing cross-agent TS errors from WhatsOnChain/StorageProvider helpers not yet merged)
  • StorageIdb.ts lint error count: 137 (down from 188 in original)
  • StorageIdb.test.ts test suite blocked by pre-existing missing storageProviderHelpers import from another wave-2 agent; will pass once that PR merges

🤖 Generated with Claude Code

Extract per-entity partial-match helpers into idbHelpers.ts, replace
while+firstTime cursor pattern with for-loop in all filter* methods,
and extract private helpers (applyDateFields, applyBooleanFields,
normalizeEntityValues, applyOptionalDateFields, applyIntegerBooleanFields,
normalizeForStorage) to reduce S3776 violations. refs #38 #44

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@codecov
Copy link
Copy Markdown

codecov Bot commented May 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

…ers below threshold

Decompose high-CC helpers (especially L143 at CC 46) into smaller
per-entity / per-version helpers to satisfy the 15 cognitive-complexity
limit.

- Replace sequential `if (field !== undefined && record !== field)` guards
  with CC-free helpers (eq, eqNullable, dateEq, dateEqNullable) so each
  matcher function's CC drops to 1 (a single && chain in a return).
- Split matchesOutputPartial, matchesSyncStatePartial, and
  matchesTransactionPartial into private Ids/Dates/Scalars/Strings
  sub-helpers so no public function exceeds ~4 &&-chains.
- Extract upgradeAllStoresV1 to collapse the 15-branch upgrade() callback
  in StorageIdb.initDB (CC 18 → ~3), replacing 15 individual upgrade
  imports with a single one.
- dateEq delegates to the existing dateMatches to avoid duplicating logic.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud Bot commented May 7, 2026

Quality Gate Failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 80%)

See analysis details on SonarQube Cloud

@sirdeggen sirdeggen merged commit 73b282c into main May 7, 2026
8 of 9 checks passed
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.

Sonar quality remediation — bulk smells & architecture tangles tracker

1 participant