Refactor (packages/opencode/src/cli/cmd/run/scrollback.shared.ts): Function with many returns - #177
Open
ladnerm wants to merge 2 commits into
Open
Refactor (packages/opencode/src/cli/cmd/run/scrollback.shared.ts): Function with many returns#177ladnerm wants to merge 2 commits into
ladnerm wants to merge 2 commits into
Conversation
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.
P1B: Starter Task: Refactoring PR
Use this pull request template to briefly answer the questions below in one to two sentences each.
Feel free to delete this text at the top after filling out the template.
1. Issue
#134
packages/opencode/src/cli/cmd/run/scrollback.shared.ts
This file contains shared helper functions used by the terminal scrollback renderer to decide how each streamed entry should be styled.
EntryLook function
Function with many returns (count = 9) in entryLook, originally reported at scrollback.shared.ts:21.
2. Refactoring
The original entryLook used nine sequential if blocks, each with its own early return. This made the function harder to scan at a glance and inflated Qlty's complexity/return-count metrics.
I extracted each if branch into a declarative {matches, result} rule object, collected them into an ordered entryLookRules array, and replaced the function body with a single .find() call plus one return statement.
The function now has one return path instead of nine, directly resolving Qlty's smell, and the conditions/results are data rather than control flow, making it easier to add, remove, or reorder rules without touching the function's logic.
3. Validation
I wrote 9 unit tests in scrollback.shared.test.ts, one per original branch, each constructing a StreamCommit that triggers exactly one rule and asserting entryLook returns the expected {fg, attrs} pair. All 9 tests pass, and I generated a coverage report confirming the refactored function and its rules are exercised by these tests.