refactor: reduce sonarjs cognitive complexity to ≤15#72
Merged
Conversation
- 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
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
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:
Tests
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)