Skip to content

chore: fix open SonarCloud maintainability issues#5

Merged
vasylenko merged 2 commits into
mainfrom
chore/sonar-maintainability-cleanup
May 15, 2026
Merged

chore: fix open SonarCloud maintainability issues#5
vasylenko merged 2 commits into
mainfrom
chore/sonar-maintainability-cleanup

Conversation

@vasylenko
Copy link
Copy Markdown
Owner

Summary

  • Resolves 4 open MAINTAINABILITY issues reported by SonarCloud (vasylenko_markfetch, OPEN+CONFIRMED filter) without changing any public behavior.
  • src/cli.ts: the -o save path branch is flipped from if (savedTo !== undefined) to if (savedTo === undefined) so the early branch handles the stdout case. Callers see identical output — markdown to stdout when no save path, Saved N bytes to <path> confirmation otherwise. Removes typescript:S7735.
  • src/sandbox.tsbuildAllowedRoots: the default-path case now returns early instead of wrapping the validation loop in an if (raw != null && raw !== "") block. The per-entry validation loop runs at one less nesting level. Public signature and the three error messages (every entry must be an absolute path, not a directory, could not resolve) are byte-for-byte identical, so existing regex assertions in tests/sandbox.test.ts still match. Cognitive complexity drops from 17 to ~9.
  • src/sandbox.tscheckPath: the while/try/catch ancestor-walk loop is extracted into a private walkToExtantAncestor helper. The main body is now a linear pipeline: walk → realpath → fold → contain-check → format error. Also removes a redundant if (rel === "") return ok inside the per-root loop — the subsequent !rel.startsWith("..") && !isAbsolute(rel) already accepts empty rel. The CheckResult discriminated union and the function signature are unchanged. Cognitive complexity drops from 20 to under the 15 threshold.
  • src/sandbox.ts — the nested template literal in the final error formatter is hoisted into a const rootsList = roots.map((r) => \'${r}'`).join(", ")above the return. Removestypescript:S4624`.
  • src/sandbox.tsreattached is simplified from trailing.length === 0 ? x : join(x, ...trailing) to join(resolvedAncestor, ...walked.trailing) since path.join(x) with no extra segments returns x unchanged.

Why

SonarCloud flags these four issues on every scan against main and they block a clean maintainability rating for the project. The refactor is mechanical — branch flips, one extracted helper, one hoisted expression — and preserves both the public API and every existing test assertion. npm test reports 63 passed / 2 skipped (the 2 are the existing win32-only case-fold tests) / 0 failed, and npm run build exits 0. LOC delta is +59 / −55 = +4 net, almost all of it the walkToExtantAncestor helper signature.

SonarCloud filter URL: https://sonarcloud.io/project/issues?impactSoftwareQualities=MAINTAINABILITY&issueStatuses=OPEN%2CCONFIRMED&id=vasylenko_markfetch

The branch is two commits — initial refactor and a followup that trimmed two helper extractions that turned out to be net cosmetic. Squash-merge is fine if a single commit on main is preferred.

vasylenko added 2 commits May 15, 2026 02:12
- src/cli.ts: flip savedTo branch to drop negated condition (S7735)
- src/sandbox.ts: extract resolveAllowedRoot helper + early-return
  default path to drop buildAllowedRoots cognitive complexity (S3776)
- src/sandbox.ts: extract walkToExtantAncestor + isContainedIn
  helpers to drop checkPath cognitive complexity (S3776)
- src/sandbox.ts: hoist roots list into rootsList const to remove
  nested template literal in the not-contained error (S4624)

Public exports (buildAllowedRoots, checkPath, CheckResult) keep
identical signatures and error-message wording. All existing tests
pass; tsc build succeeds.
Followup to the Sonar cleanup commit: re-inline two helpers whose
extraction was net cosmetic, drop a redundant ternary, and remove
two comments that didn't pull weight. Public API and behavior
unchanged; Sonar rules still satisfied.

Changes:
- Inline resolveAllowedRoot back into buildAllowedRoots: the early-
  return for the default path alone drops S3776 complexity (17 to
  ~9 by Sonar's counting), so the helper was redundant.
- Inline isContainedIn back into checkPath's for-loop: with the
  redundant 'rel === ""' check gone, the body is two lines —
  inlining doesn't push checkPath above the 15-point threshold
  (still ~6 points with walkToExtantAncestor still extracted).
- Collapse 'reattached' ternary to join(resolvedAncestor,
  ...walked.trailing). path.join(x) returns x when no extra
  segments are passed, so the trailing.length === 0 guard was
  dead code.
- Drop the 'Sequential (not Promise.all)' meta-comment (explains
  an alternative not taken).
- Drop the walkToExtantAncestor header comment (function name +
  return type already document it).

Combined PR diff vs main: +59 / -55 = +4 LOC net.
@sonarqubecloud
Copy link
Copy Markdown

@vasylenko vasylenko merged commit bab7251 into main May 15, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant