Skip to content

refactor: split util.js to reduce total and per-function complexity#310

Merged
albe merged 3 commits into
mainfrom
copilot/address-codeql-feedback-utils
May 10, 2026
Merged

refactor: split util.js to reduce total and per-function complexity#310
albe merged 3 commits into
mainfrom
copilot/address-codeql-feedback-utils

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented May 10, 2026

CodeQL flagged scanForFiles at cyclomatic complexity 24 and util.js total complexity at 75. Both stem from a single overloaded utility file and one deeply nested recursive function.

scanForFiles complexity (24 → ~3)

Extracted four module-level helpers so each unit of work is a flat, named function:

// Before: nested scan() + next() inside scanForFiles = complexity 24
function scanForFiles(directory, regexPattern, onEach, onDone) {
    function scan(dir, relativePrefix, isRoot, done) {
        fs.readdir(..., (err, entries) => {
            // classify, match, recurse — all inline
            function next() { ... }
            next();
        });
    }
    scan(directory, '', true, onDone);
}

// After: scanForFiles is a one-liner; logic lives in named helpers
function scanForFiles(directory, regexPattern, onEach, onDone) {
    scanDir(directory, '', true, regexPattern, onEach, onDone);
}
// visitMatchingPath / classifyEntries / scanSubdirs / scanDir — each ≤ complexity 4

Total util.js complexity

Relocated cohesive groups to more specific modules:

  • src/fsUtil.js (new) — ensureDirectory, scanForFiles, and their extracted helpers. scanForFilesSync was removed as it had no remaining callers after the async storage open change.
  • src/metadataUtil.js (existing) — buildMetadataHeader added here; it already owned the rest of the metadata serialization surface

All import sites across src/ updated accordingly; src/fsUtil.js added to package.json files.

@albe albe marked this pull request as ready for review May 10, 2026 11:27
@coveralls
Copy link
Copy Markdown

coveralls commented May 10, 2026

Coverage Status

coverage: 97.964% (+0.3%) from 97.684% — copilot/address-codeql-feedback-utils into main

Copy link
Copy Markdown
Owner

@albe albe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot address scanForFilesSync

Comment thread src/fsUtil.js Outdated
Copy link
Copy Markdown
Contributor Author

Copilot AI commented May 10, 2026

@copilot address scanForFilesSync

scanForFilesSync had no remaining callers after the async storage open change, so it's been removed entirely (db3eeb9).

Copilot AI requested a review from albe May 10, 2026 11:36
Copilot finished work on behalf of albe May 10, 2026 11:36
@albe albe merged commit 7ca2093 into main May 10, 2026
13 checks passed
@albe albe deleted the copilot/address-codeql-feedback-utils branch May 10, 2026 11:53
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.

3 participants