Skip to content

Refactor: (/opencode/packages/ui/src/v2/components/tooltip-v2.tsx): Function with too many returns - #124

Open
rbhagra wants to merge 10 commits into
CMU-313:mainfrom
rbhagra:return-refactoring
Open

rbhagra wants to merge 10 commits into
CMU-313:mainfrom
rbhagra:return-refactoring

Conversation

@rbhagra

@rbhagra rbhagra commented Sep 7, 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

#92 (comment)

Full path to the refactored file:
opencode/packages/ui/src/v2/components/tooltip-v2.tsx

What do you think this file does?
I believe this file manages the tooltip that is launched by Kobalte. It shows a value when someone hovers over a specific trigger / button.

What is the scope of your refactoring within that file?
The ToolTipv2() function was refactored, and the sub areas of inside, drop, sync, createEffect, onOpenChange, and onKeyDownCapture were impacted.

Which Qlty‑reported issue did you address?
Function with too many returns (8): TooltipV2

2. Refactoring

How did the specific issue you chose impact the codebase’s maintainability?
This issue impacted the codebases maintainability by making the control flow hard to decipher; aditionally, it made it so any future changes of the control flow guards would likely cause an erroneous state change, as returns would occur before the proper "reset" code (JustClickedTrigger).

What changes did you make to resolve the issue?
To fix this issue, I refactored early return blocks into set conditions that trigger the actions that triggered the returns, and combined multiple previous returns into the one condition. This allowed less total returns and an interpretable names to see what triggered the return. Note - I wrote these conditions in a sepereate helper - tooltip-v2-behavior.ts to allow for easy testing without loading Kobalte. The original file imports this helper.

How do your changes improve maintainability? Did you consider alternatives?

The changes improve maintainability by allowing future changes in tooltip behavior to be implemented in a more easy manner by finding the relevant condition , rather than having to work through multiple return statements. An alternative I considered was creating a structured if/else chain, but that would not productively reduce the number of return paths.

3. Validation

How did you validate that the change is correct?
I validated that the change is correct by writing new tests in a new file (tooltip-vs.test.ts) that cover hit refactored helpers, as no tests previously existed. I also verified that the changes pass CI in order to validate that creating a new helper file did not introduce errors in other parts of the code. Since this function was responsible for reporting on/changing the tooltip state, I validated that the decisions and state changes it reports are accurate.

Attach a screenshot of the test coverage showing the lines were executed by the tests.
Screenshot 2026-09-07 at 3 29 47 PM
Screenshot 2026-09-07 at 3 30 25 PM

Note: These tests successfully cover the return behavior of tooltip-v2.tsx as the return logic was moved to tooltip-v2-behavior and re imported to tooltip-v2.tsx. We run tests in tooltip-v2-behavior for ease of testing, and establish that the extracted decisions (which are provided by the returns) do not change. Nothing else was changed, so the rest of the tooltip code will function as normal, and was not explicitly tested.

Attach a screenshot showing the tests that cover the change passing during CI
Screenshot 2026-09-07 at 3 34 28 PM
Screenshot 2026-09-07 at 3 34 47 PM

Screenshots of bun lint / bun test passing
Lint:
Original branch:
Screenshot 2026-09-07 at 4 22 54 PM

Refactor branch:
Screenshot 2026-09-07 at 4 22 05 PM

With Lint, no new errors/warnings are introduced by my changes.

Test:
Screenshot 2026-09-07 at 4 24 09 PM

In the packages/ui directory, all tests pass.

Attach a screenshot of qlty smells --no-snippets <full/path/to/file.ts> showing fewer reported issues after the changes.

Before:
Screenshot 2026-09-07 at 3 51 20 PM
After:
Screenshot 2026-09-07 at 4 15 18 PM
Screenshot 2026-09-07 at 3 45 22 PM

@rbhagra rbhagra changed the title Refactor: (/workspaces/opencode/packages/ui/src/v2/components/tooltip-v2.tsx): Function with too many returns Refactor: (/opencode/packages/ui/src/v2/components/tooltip-v2.tsx): Function with too many returns Sep 7, 2026
Co-authored-by: Cursor <cursoragent@cursor.com>
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