Skip to content

refactor(sonar): reduce visual pixel check cognitive complexity (typescript:S3776) - #226

Merged
setchy merged 10 commits into
mainfrom
fix/sonar/typescript-S3776
Oct 7, 2026
Merged

setchy merged 10 commits into
mainfrom
fix/sonar/typescript-S3776

Conversation

@setchy

@setchy setchy commented Oct 3, 2026 •

Copy link
Copy Markdown
Member

Summary

Rule typescript:S3776 reported the pixel-check logic in tests/visual/run.mts at 35 cognitive complexity points against the 15 allowed. The scan is broken out so no single function exceeds the threshold:

  • classifyPixel() — per-pixel colour classification
  • countPixels() — the nested scan loop (just the two loops; ~3 points)
  • accumulatePixel() — tallies one classified pixel into the running counts, extracted from the loop body
  • check() — combines the counts into the detection booleans

Behaviour is preserved: the accumulatePixel extraction was re-verified against the pre-extraction algorithm over 2000 randomized captures with zero mismatches.

Verification

  • pnpm lint (oxlint + oxfmt) — clean
  • pnpm typecheck — clean
  • pnpm test — 129/129 passing
  • Randomized equivalence check (2000 trials, before/after extraction) — 0 mismatches
  • SonarCloud PR analysis — 0 open issues, quality gate OK

Findings

SonarQube rule typescript:S3776 flags check() at 35 points vs the 15
allowed. The pixel scan and classification are extracted into classifyPixel
and countPixels helpers, leaving check() to only combine the counts into
the detection booleans. Behaviour is unchanged (verified against the
original algorithm over randomized captures).
@setchy setchy closed this Oct 3, 2026
@setchy setchy reopened this Oct 3, 2026
@setchy
setchy marked this pull request as ready for review October 4, 2026 03:33
@setchy
setchy requested a review from afonsojramos as a code owner October 4, 2026 03:33
setchy added 8 commits October 4, 2026 18:16
classifyPixel() returned a fresh {tray,saturated,white,black} object for
every pixel — ~786k per capture, rerun on each retry attempt — where the
original inlined arithmetic allocated nothing. Pack the flags into a number
instead; verified identical output over 2.6M sampled/boundary pixels.
1 << 0 tripped typescript:S7767 ("use Math.trunc instead of << 0").
Express the bit flags as binary literals instead; values are unchanged.
The PIXEL_* constants were declared after the top-level retry loop that
calls check(), so accumulatePixel() hit the temporal dead zone and threw
"Cannot access 'PIXEL_TRAY' before initialization", failing every Visual
check. Move them next to the other detection thresholds.
@sonarqubecloud

sonarqubecloud Bot commented Oct 7, 2026

Copy link
Copy Markdown

@setchy
setchy merged commit e24599f into main Oct 7, 2026
29 checks passed
@setchy
setchy deleted the fix/sonar/typescript-S3776 branch October 7, 2026 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant