P1B: Refactor (packages/app/src/components/dialog-custom-provider-form.ts): Function with many returns: validateCustomProvider - #85
Open
MichaelYeh507 wants to merge 3 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
1. Issue
Link to the associated GitHub issue:
Closes #79 — #79
Full path to the refactored file:
packages/app/src/components/dialog-custom-provider-form.tsWhat do you think this file does?
This file checks the "add custom provider" form before it gets saved. It cleans up what the user typed, figures out which fields are missing or wrong, and if everything looks good it builds the settings object the app stores for that provider.
What is the scope of your refactoring within that file?
I changed the body of the
validateCustomProviderfunction and added three small helper functions above it (uniqueError,validateModels,validateHeaders) that now hold the model-row and header-row checks. Nothing else in the file changed.Which Qlty-reported issue did you address?
"Function with many returns (count = 7): validateCustomProvider" at line 51. Fixing it also removed the "Function with high complexity (count = 31)" warning on the same function.
2. Refactoring
How did the specific issue you chose impact the codebase's maintainability?
The function had seven different places where it could return a value, and most of them were hidden inside nested conditionals and small inline functions. That made it hard to read the checks in order and easy to break the duplicate-detection logic when adding a new rule.
What changes did you make to resolve the issue?
I pulled the model-row and header-row checks into their own functions, and put the shared "is this blank or a duplicate?" check into one helper. The main function now just calls those helpers and keeps its original two returns at the end.
How do your changes improve maintainability? Did you consider alternatives?
Each helper does one job and can be read and tested on its own, and the duplicate check lives in one place instead of being copied twice. I considered just flattening the conditionals in place, but that would have left all the returns inside the main function and kept the copied logic.
3. Validation
How did you validate that the change is correct?
The two tests that already existed for this function pass unchanged after the refactor, which shows the output shape and error messages are the same as before. I added nine more tests that cover each field-level error (provider ID, name, base URL), the required and duplicate checks in the row helpers, and the success case with a plain API key. All 11 pass locally, and the coverage report shows 100% of the lines in the file were run. These tests run automatically in CI because the
packages/apptest script runs every test undersrc/. Lint reports no warnings or errors on the two files I changed.Lint on the two changed files:

Full packages/app unit suite:

Attach a screenshot of the test coverage showing the lines were executed by the tests.

Attach a screenshot showing the tests that cover the change passing during CI


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

After:
