Repository navigation
refactor(sonar): reduce visual pixel check cognitive complexity (typescript:S3776) - #226
Merged
Merged
Conversation
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
marked this pull request as ready for review
October 4, 2026 03:33
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.
|
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
Rule
typescript:S3776reported the pixel-check logic intests/visual/run.mtsat 35 cognitive complexity points against the 15 allowed. The scan is broken out so no single function exceeds the threshold:classifyPixel()— per-pixel colour classificationcountPixels()— the nested scan loop (just the two loops; ~3 points)accumulatePixel()— tallies one classified pixel into the running counts, extracted from the loop bodycheck()— combines the counts into the detection booleansBehaviour is preserved: the
accumulatePixelextraction was re-verified against the pre-extraction algorithm over 2000 randomized captures with zero mismatches.Verification
pnpm lint(oxlint + oxfmt) — cleanpnpm typecheck— cleanpnpm test— 129/129 passingFindings