Refactor (packages/llm/src/schema/options.ts): Function with high complexity - #63
Open
leoliu012 wants to merge 4 commits into
Open
Refactor (packages/llm/src/schema/options.ts): Function with high complexity#63leoliu012 wants to merge 4 commits into
leoliu012 wants to merge 4 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
Link to the associated GitHub issue:
Issue 62
Full path to the refactored file:
packages/llm/src/schema/options.tsWhat do you think this file does?
This file defines schema-related options and helper functions for the LLM package.
mergeJsonRecordscombines JSON-like option records while preserving nested values and ignoring undefined values.What is the scope of your refactoring within that file?
I refactored
mergeJsonRecordsand extracted the nested value-merging and record-merging logic intomergeJsonRecordValueandmergeJsonRecordInto.Which Qlty‑reported issue did you address?
Qlty reported
Function with high complexity (count = 21): mergeJsonRecordsat line 6.2. Refactoring
How did the specific issue you chose impact the codebase’s maintainability?
mergeJsonRecordshandled filtering, special cases, iteration, nested-record detection, and recursive merging in one function, which made the control flow harder to understand and modify safely.What changes did you make to resolve the issue?
I extracted the logic for merging individual values into
mergeJsonRecordValueand the logic for merging one record intomergeJsonRecordInto, leavingmergeJsonRecordsresponsible for the overall merge flow.How do your changes improve maintainability? Did you consider alternatives?
The refactoring separates the merge behavior into smaller functions with clearer responsibilities while preserving the existing API and behavior. I considered rewriting the merge using a different iteration pattern, but chose extraction because it reduced complexity with a smaller and less risky behavioral change.
3. Validation
How did you validate that the change is correct?
I ran the tests covering
mergeJsonRecords, including nested recursive merges, undefined handling, and single-record behavior, and then ran the package/repository test and lint commands.Here are the tests added:
Screenshots showing the tests that cover the change passing during CI

qlty smells --no-snippets packages/llm/src/schema/options.tsscreenshot before the fixqlty smells --no-snippets packages/llm/src/schema/options.tsscreenshot after the fixBun test all passed

Bun lint runs

It reports pre-existing linkt/type errors in unrelated files. None are introduced by this refactoring.