Skip to content

Refactor (packages/opencode/src/cli/cmd/run/scrollback.shared.ts): Function with many returns - #177

Open
ladnerm wants to merge 2 commits into
CMU-313:mainfrom
ladnerm:feature
Open

Refactor (packages/opencode/src/cli/cmd/run/scrollback.shared.ts): Function with many returns#177
ladnerm wants to merge 2 commits into
CMU-313:mainfrom
ladnerm:feature

Conversation

@ladnerm

@ladnerm ladnerm commented Sep 8, 2026

Copy link
Copy Markdown

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.

Screenshot 2026-09-07 at 10 32 09 PM Screenshot 2026-09-07 at 10 31 55 PM Screenshot 2026-09-07 at 10 46 07 PM

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