Refactor (packages/app/src/context/file/path.ts): Deeply nested control flow (level = 5) - #18
Open
RobbyTato wants to merge 3 commits into
Open
Conversation
…de path checking regex output.
…hieves 100% function coverage.
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.
1. Issue
Link to the associated GitHub issue:
#13
Full path to the refactored file:
packages/app/src/context/file/path.ts
What do you think this file does?
This file provides path-handling utilities used to normalize, encode, and decode file paths across different operating systems (Linux, macOS, Windows) and different path representations (e.g., file:// URLs, git-quoted paths with escape sequences). It exposes helpers like createPathHelpers, encodeFilePath, decodeFilePath, stripQueryAndHash, stripFileProtocol, and unquoteGitPath, which together let the app consistently resolve a file's path relative to a workspace root regardless of platform-specific separators or encoding quirks.
What is the scope of your refactoring within that file?
I refactored the unquoteGitPath function. Specifically, I replaced a deeply nested ternary chain (checking next against "n", "r", "t", "b", "f", "v", "\", and """ one branch at a time) with a flat lookup table. I also identified and removed a dead/unreachable if branch in the octal-escape handling logic, since the surrounding condition guarantees the regex always matches.
Which Qlty‑reported issue did you address?
Deeply nested control flow (level = 5) at line 58
2. Refactoring
How did the specific issue you chose impact the codebase’s maintainability?
The nested ternary chain required tracking eight levels of conditionals to perform a simple character mapping. This made the escape logic unnecessarily slower and inflated the function's complexity score. The dead/unreachable if branch affected code coverage reports so it made sense to remove it along with solving this issue.
What changes did you make to resolve the issue?
Replaced the ternary chain with a flat ESCAPE_MAP lookup object and a single ESCAPE_MAP[next] ?? next fallback. Also removed an unreachable if (!match) branch in the octal-escape logic, since the preceding condition guarantees the regex always matches.
How do your changes improve maintainability? Did you consider alternatives?
The lookup table removes all nesting for the escape mapping and is easier to extend (one line per new escape). Removing the dead branch further reduces complexity. I considered a switch statement, but it wouldn't reduce branching as much as a map.
3. Validation
How did you validate that the change is correct?
Added tests in path.test.ts under describe("unquoteGitPath", ...) covering all ESCAPE_MAP entries, octal escapes (1–3 digits, multiple in one string), unrecognized escape fallback, unquoted/malformed input, and trailing backslash. Ran bun test --coverage --coverage-dir=./coverage and confirmed all tests pass and the changed lines are covered.
Attach a screenshot of the test coverage showing the lines were executed by the tests.


Local linter and tests running:
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: