Skip to content

refactor: reduce sonarjs cognitive complexity to ≤15#72

Merged
marcusolsson merged 8 commits into
mainfrom
reduce-complexity
Jun 3, 2026
Merged

refactor: reduce sonarjs cognitive complexity to ≤15#72
marcusolsson merged 8 commits into
mainfrom
reduce-complexity

Conversation

@marcusolsson

Copy link
Copy Markdown
Contributor

Summary

Reduces sonarjs cognitive complexity across the codebase from threshold 79 → 15. All functions now pass the stricter lint rule.

Changes

Extracted helpers from high-complexity functions:

File Function Before After Key Extracts
39 ≤15 , , ,
76 ≤15 , , , , , , , , , interface
/ 18/20 ≤15 , ,
action handler 79 ≤15 , , , , , , , , , , , , ,
58 ≤15 , , ,
21 ≤15 , , interface
25 ≤15 , , ,

Tests

  • Added 23 tests for
  • Added 16 tests for streaming + non-streaming paths
  • All existing tests preserved — 262 tests pass across 22 test files

Verification

RUN v4.1.8 /Users/marcus/berget-monorepo/cli

Test Files 22 passed (22)
Tests 262 passed (262)
Start at 09:26:05
Duration 1.04s (transform 845ms, setup 0ms, import 1.43s, tests 1.20s, environment 1ms)

- Extract validation into validateCreateOptions()
- Extract error mapping into mapCreateError() with switch statement
- Extract network error enhancement into enhanceNetworkError()
- Extract creation failed message into buildCreationFailedMessage()
- Add comprehensive tests for ApiKeyService.create()
- All 23 tests pass
- Extract handleStreamingResponse into focused helper methods:
  - logStreamingRequest, makeStreamingRequest
  - readStream, processLines
  - hasBalancedBraces, tryParseAndProcess
  - accumulateContent, logParseError, buildStreamResult
- Introduce StreamState interface to pass state between methods
- Add node:stream/web import for ReadableStreamDefaultReader type
- Add 16 tests covering streaming, non-streaming, and error paths
- Extract resolveToolConfigured() for tool installation retry logic
- Extract buildScopeOptions() for scope selection options
- Extract configureTool() for tool-specific configuration
- Extract buildNextSteps() for next steps message generation
- Reorder functions to satisfy perfectionist/sort-modules
- All 45 existing tests pass
- Extract resolveApiKey() for API key resolution logic
- Extract resolveApiKeyFromId() for API key ID → key rotation
- Extract resolveApiKeyForList() for list command key resolution
- Extract resolveInputMessage() for stdin + argument combining
- Extract verifyAuthentication() for auth check
- Extract buildCompletionOptions() for options construction
- Extract runSingleShotChat() for non-interactive chat
- Extract runInteractiveChat() with createAskQuestion() for interactive mode
- Extract runInteractiveStreaming() and runInteractiveNonStreaming()
- Extract handleStreamingCompletion() and handleNonStreamingCompletion()
- Extract displayResponse() and displayModels() for output formatting
- Reorder functions alphabetically per perfectionist/sort-modules
…xity to ≤15

- Extract logCompletionOptions() for debug logging
- Extract formatCompletionError() and formatListModelsError()
- All 16 tests pass
- Extract resolveDefaultApiKey() for default API key resolution logic
- Simplify early returns for env var and command-line API keys
- Extract classifyRefreshError() to determine error handling action
- Extract storeNewTokens() for token persistence logic
- Introduce RefreshErrorAction interface for structured error classification
- All 12 existing tests pass
- Extract handleUndecodableJwt() for JWT decode failure path
- Extract handleHasSeat() for subscription seat flow
- Extract handleNoSeat() for no-subscription API key flow
- Extract createAndSyncApiKey() for shared API key creation logic
- All 40 existing tests pass
@marcusolsson marcusolsson merged commit 611ec7c into main Jun 3, 2026
1 check passed
@marcusolsson marcusolsson deleted the reduce-complexity branch June 3, 2026 07:34
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