Simplify completePatchContents in packages/session-ui/src/components/session-diff.ts - #182
Open
anish-jai wants to merge 1 commit into
Open
Simplify completePatchContents in packages/session-ui/src/components/session-diff.ts#182anish-jai wants to merge 1 commit into
anish-jai wants to merge 1 commit into
Conversation
Reduce the Qlty-reported complexity of completePatchContents (packages/session-ui/src/components/session-diff.ts:74, complexity 37, 7 returns) without changing behavior: - Replace the stateful line loop with a marker-to-side lookup table and a map/filter pass; a "\ No newline" marker is handled by looking one line ahead instead of mutating the previous entry. - Move the shared try/parsePatch into parseFirstPatch, which patchInput also used to duplicate. Add tests for the branches that were not exercised before: a single hunk that starts after line 1, a no-newline marker after a context line, and a blank line inside a hunk. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
anish-jai
force-pushed
the
refactor-session-diff
branch
from
September 8, 2026 03:06
7992a8f to
b84504e
Compare
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.
Refactors
completePatchContentsto remove the Qlty "Function with high complexity (count = 37)" smell with no behavior change.1. Issue
Link to the associated GitHub issue:
Closes #181
Full path to the refactored file:
packages/session-ui/src/components/session-diff.ts
What do you think this file does?
It normalizes the different diff shapes the session UI receives (snapshot, VCS, tool patch, legacy before/after) into one
ViewDifffor the review components. For unified patches it decides whether the patch covers the whole file (render full before/after text) or only part of it (keep a partial hunk view).What is the scope of your refactoring within that file?
completePatchContents(the target) andpatchInput, which duplicated the sametry { parsePatch(patch)[0] } catchblock; that parse now lives in a small sharedparseFirstPatchhelper. No exported function or signature changed.Which Qlty-reported issue did you address?
Function with high complexity (count = 37): completePatchContentsat line 74. Fixing it also clearedFunction with many returns (count = 7)and the file-levelHigh total complexity (count = 53); Qlty now reports nothing for this file.2. Refactoring
How did the specific issue you chose impact the codebase's maintainability?
The function mixed four validation rules, a
try/catch, and a stateful loop that remembered the previous line's marker and reached back to mutate earlier entries on a\ No newline at end of filemarker. Following the complete-vs-partial decision meant tracing seven exit points plus that hidden state, and the parse block was copy-pasted intopatchInput.What changes did you make to resolve the issue?
Replaced the stateful loop with a
patchSideslookup table (marker to side) and a singlemap/filterpass that looks one line ahead for the no-newline marker instead of mutating earlier entries. Moved the duplicated parse intoparseFirstPatch, used by both functions, and flattened the guards from seven exits to five.How do your changes improve maintainability? Did you consider alternatives?
Each rule is now one readable line, the data flow is a pure map/filter, and the marker semantics live in one table instead of four
ifbranches. I considered splitting into several single-use helpers to push the number lower, but the repo's AGENTS.md asks not to extract single-use helpers preemptively, so I only extracted the one piece that is genuinely reused.3. Validation
How did you validate that the change is correct?
session-diff.test.ts(10 tests) exercisescompletePatchContentsandpatchInputthrough the publicresolveFileDiff/normalizeAPI and still passes unchanged.bun test --coveragereports 100% functions and 100% lines forsession-diff.ts;bun run typecheckpasses;bun lintfindings are identical before and after (2 pre-existing errors in unrelated files, 0 in the touched files).packages/session-uiis already wired intobun turbo test, so these tests run in thetest / unitjob on this PR.Attach a screenshot of the test coverage showing the lines were executed by the tests.
cd packages/session-ui && bun test --coverage --coverage-dir=./coverage:session-diff.ts100% functions, 100% lines, 86 tests pass.Attach a screenshot showing the tests that cover the change passing during CI
All 3 checks green on the final commit
b84504e; thetest / unitjob runssession-diff.test.ts.Attach a screenshot of
qlty smells --no-snippets <full/path/to/file.ts>showing fewer reported issues after the changes.BEFORE, on
main:AFTER, on
refactor-session-diff, same command and file (nothing reported):Additional local evidence
cd packages/session-ui && bun test: 86 pass, 0 fail. The redparseLineTypelines are console output from the@pierre/diffsfallback parser during the blank-line test, not a failure.bunx oxlinton the two changed files: 0 warnings, 0 errors.bun lintrepo-wide: 698 warnings, 2 errors, identical on untouchedmain(inpackages/webandprompt-input/index.tsx, neither touched here).Linked issue #181 with bot assignment: