Repository navigation
Refactor: (/opencode/packages/ui/src/v2/components/tooltip-v2.tsx): Function with too many returns - #124
Open
rbhagra wants to merge 10 commits into
Open
Refactor: (/opencode/packages/ui/src/v2/components/tooltip-v2.tsx): Function with too many returns#124rbhagra wants to merge 10 commits into
rbhagra wants to merge 10 commits into
Conversation
Co-authored-by: Cursor <cursoragent@cursor.com>
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
#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.


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


Screenshots of bun lint / bun test passing

Lint:
Original branch:
Refactor branch:

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

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:



After: