Skip to content

refactor: command/query separation — extract CLI wrappers, shared output helper#373

Merged
carlos-alm merged 6 commits intomainfrom
refactor/command-query-separation
Mar 9, 2026
Merged

refactor: command/query separation — extract CLI wrappers, shared output helper#373
carlos-alm merged 6 commits intomainfrom
refactor/command-query-separation

Conversation

@carlos-alm
Copy link
Contributor

Summary

  • Extract 15 CLI display wrappers (~800 lines) from queries.jsqueries-cli.js, reducing queries.js by 26% (3433 → 2544 lines)
  • Add outputResult() helper in result-formatter.js — replaces the 4–6 line JSON/NDJSON dispatch pattern repeated in every CLI wrapper
  • Extract isTestFile() into test-filter.js — breaks the circular-ish dependency where 17 modules imported it from queries.js
  • Add QUERY_OPTS CLI preset — consolidates 7 repeated .option() calls across 14 commands (~98 lines removed)

Details

New files

File Purpose
src/result-formatter.js outputResult(data, field, opts) — returns true if JSON/NDJSON handled
src/test-filter.js isTestFile() + TEST_PATTERN — standalone, no deps on queries.js
src/queries-cli.js 15 CLI wrappers: symbolPath, stats, queryName, impactAnalysis, moduleMap, fileDeps, fnDeps, context, children, explain, where, roles, fileExports, fnImpact, diffImpact

What stays unchanged

  • All *Data() functions remain in queries.js — MCP, batch, and programmatic API untouched
  • All public exports preserved via index.js re-exports — zero breaking changes
  • No function renames, no signature changes

Bug fix

  • cfg.js was calling printNdjson(data.results) (passing array directly) instead of printNdjson(data, 'results') — fixed via outputResult

Test plan

  • 569 unit + integration tests pass (0 failures)
  • biome check — 0 errors (1 pre-existing warning)
  • codegraph --help — all commands listed correctly
  • codegraph impact --help — shows QUERY_OPTS options

🤖 Generated with Claude Code

…put helper, test-filter module

Extract 15 CLI display wrappers (~800 lines) from queries.js into
queries-cli.js, reducing it by 26% (3433 → 2544 lines). Add shared
outputResult() helper that replaces the 4–6 line JSON/NDJSON dispatch
pattern repeated in every CLI wrapper. Extract isTestFile() into
test-filter.js, breaking the circular-ish dependency where 17 modules
imported it from queries.js. Add QUERY_OPTS CLI preset consolidating
7 repeated options across 14 commands (~98 lines removed).

Zero breaking changes — all public API exports preserved via index.js
re-exports. 569 tests pass, 0 lint errors.

Impact: 34 functions changed, 170 affected
@claude
Copy link

claude bot commented Mar 8, 2026

Claude finished @carlos-alm's task —— View job


PR Review Complete ✅

Reviewing PR #373: refactor: command/query separation — extract CLI wrappers, shared output helper

Todo List

  • Read repository guidelines (CLAUDE.md)
  • Phase 1: Root Cause Analysis - understand the problem being solved
  • Phase 2: Configuration Change Detection - check for risky changes
  • Phase 3: Test Integrity - verify tests weren't weakened
  • Phase 4: Code Quality Review - examine new/modified code
  • Provide final rating and recommendation

Phase 1: Root Cause Analysis ✅

Problem Being Solved: This refactoring addresses code organization and maintainability in the queries.js file, which was becoming unwieldy at 3433 lines. The PR extracts:

  • 15 CLI display wrappers (~800 lines) into queries-cli.js
  • A shared outputResult() helper to eliminate repetitive JSON/NDJSON dispatch code
  • The isTestFile() function into test-filter.js to break a circular dependency where 17 modules imported it from queries.js
  • QUERY_OPTS CLI preset to consolidate repeated option definitions

Why This Approach: This is a legitimate separation of concerns - isolating CLI presentation logic from core data query functions. The approach maintains backward compatibility via re-exports and doesn't change function signatures.

Risk Assessment: Low risk. This is purely organizational refactoring with maintained API compatibility.

Phase 2: Configuration Change Detection ✅

No risky configuration changes detected:

  • ❌ No biome.json, package.json, tsconfig.json changes
  • ❌ No linting rules disabled or relaxed
  • ❌ No test timeouts or coverage thresholds modified
  • ❌ No TypeScript strict mode flags disabled

The only configuration-related code is the new QUERY_OPTS helper that consolidates repeated CLI option definitions - this is a positive refactoring.

Phase 3: Test Integrity ✅

Tests are appropriately updated without being weakened:

  • Only change in tests/unit/queries-unit.test.js was updating imports to reflect the new module structure
  • Data functions (diffImpactData, fnDepsData, etc.) now imported from queries.js
  • CLI wrappers (diffImpact, fnDeps, etc.) now imported from queries-cli.js
  • No test logic was modified, no assertions were weakened
  • Tests still cover both data functions and CLI display wrappers

Phase 4: Code Quality Review ✅

Excellent refactoring quality:

✅ Strengths:

  1. Clean separation: Data functions stay in queries.js, CLI display logic moves to queries-cli.js
  2. Genuine bug fix: Fixed cfg.js calling printNdjson(data.results) (array directly) instead of printNdjson(data, 'results') (proper field parameter)
  3. DRY principle: outputResult() helper eliminates 4-6 line JSON/NDJSON dispatch pattern repeated across ~15 functions
  4. Dependency cleanup: Breaking circular dependency by extracting isTestFile() to dedicated module
  5. CLI consolidation: QUERY_OPTS removes ~98 lines of duplicated option definitions across 14 commands
  6. API compatibility: All exports preserved via index.js re-exports - zero breaking changes
  7. Consistent patterns: All CLI wrappers follow the same pattern: call *Data function → outputResult() → format output

✅ Code Quality:

  • result-formatter.js: Simple, focused, well-documented helper (21 lines)
  • test-filter.js: Minimal, single-purpose module (7 lines)
  • queries-cli.js: Well-organized with clear section separators and consistent formatting
  • Import statements properly updated across all affected files

✅ No Issues Found:

  • No performance regressions
  • No security concerns
  • No breaking changes
  • No configuration relaxations

Final Assessment

## Root Cause Analysis
**Problem Being Solved**: Code organization - queries.js had grown to 3433 lines mixing data logic with CLI presentation
**Why This Approach**: Clean separation of concerns with maintained API compatibility via re-exports
**Risk Assessment**: Very low - purely organizational refactoring with no functional changes

## Critical Concerns
None. This is a well-executed refactoring that improves maintainability.

## Final Recommendation
- Rating: ⭐⭐⭐⭐☆ (4/5)
- Action: APPROVE
- Reasoning: Excellent separation of concerns, genuine bug fix, DRY improvements, zero breaking changes, comprehensive test coverage maintained. Only missing the 5th star due to being "just" a refactoring rather than new functionality.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Mar 8, 2026

Greptile Summary

This PR is a well-structured refactor that separates CLI display concerns from pure data-query logic: 15 CLI wrappers are extracted from queries.js into queries-cli.js, a shared outputResult() helper eliminates ~6-line JSON/NDJSON dispatch blocks repeated across every module, isTestFile() moves to its own dependency-free module, and a QUERY_OPTS preset removes ~98 lines of duplicated Commander option declarations. Backward compatibility is generally preserved through re-exports, and the cfg.js NDJSON bug fix (printNdjson(data.results)outputResult(data, 'results', opts)) is a genuine improvement.

Two issues require attention before merging:

  • diff-impact lost its --json flag — this command was not migrated to QUERY_OPTS and its manual -j, --json option was deleted without replacement. opts.json in the action handler will always be undefined, so codegraph diff-impact --json silently produces text output. Users must now use --format json instead, which is an undocumented breaking CLI change.
  • dataflow / dataflowImpact NDJSON format changed — the old inline NDJSON loop printed only result objects; the new printNdjson path additionally emits a {"_meta": ...} line when data._pagination is present, silently changing the wire format for any pipeline that parses this output.

Confidence Score: 3/5

  • Safe to merge after fixing the diff-impact --json regression and acknowledging the dataflow NDJSON format change.
  • The structural refactor is clean and tests pass, but one command silently lost its --json flag (a breaking CLI change contradicting the PR's "zero breaking changes" claim) and two commands have an undocumented NDJSON wire-format change.
  • src/cli.js (diff-impact command missing --json) and src/dataflow.js (NDJSON _meta line addition).

Important Files Changed

Filename Overview
src/result-formatter.js New helper module — clean, minimal, correctly delegates to printNdjson and JSON.stringify; boolean return value pattern is easy to misuse but consistently applied across the codebase.
src/test-filter.js Tiny standalone module exporting isTestFile and TEST_PATTERN; backward compat preserved via re-export in queries.js; no issues found.
src/queries-cli.js New 866-line file housing all 15 CLI display wrappers; previously-flagged NDJSON null-field issues for children and moduleMap have been fixed; all wrappers consistently use outputResult.
src/cli.js QUERY_OPTS preset cleanly removes ~98 lines of boilerplate, but diff-impact was not migrated and had its -j, --json option dropped without a replacement — callers using --json will silently get text output.
src/dataflow.js Both dataflow and dataflowImpact previously used custom inline NDJSON; replacing with outputResult/printNdjson silently adds a _meta line when pagination is present, changing the NDJSON wire format.
src/index.js Re-exports cleanly updated to expose new modules (queries-cli.js, result-formatter.js, test-filter.js); fileExports correctly moved from queries.js to queries-cli.js exports block.
src/branch-compare.js Format normalization (opts.format === 'json'opts.json) correctly mirrors the pattern in queries-cli.js; mermaid branch remains reachable since outputResult only triggers on json/ndjson flags.
src/check.js process.exit(1) on failed check is correctly preserved after switching to outputResult; logic unchanged.
src/cfg.js Bug fix applied correctly: old code called printNdjson(data.results) (wrong signature, passed array directly); new outputResult(data, 'results', opts) passes the object and field name as intended.
tests/unit/queries-unit.test.js Test imports updated to pull CLI wrappers from queries-cli.js rather than queries.js; no missing coverage gaps visible for the refactored paths.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    CLI["cli.js\n(QUERY_OPTS preset)"]
    QCLI["queries-cli.js\n15 CLI wrappers"]
    Q["queries.js\n*Data() functions"]
    RF["result-formatter.js\noutputResult()"]
    TF["test-filter.js\nisTestFile()"]
    PAG["paginate.js\nprintNdjson()"]
    IDX["index.js\nPublic API"]

    CLI -->|"imports wrappers"| QCLI
    QCLI -->|"calls *Data()"| Q
    QCLI -->|"uses"| RF
    RF -->|"delegates to"| PAG
    Q -->|"re-exports isTestFile"| TF
    IDX -->|"re-exports all"| QCLI
    IDX -->|"re-exports all"| Q
    IDX -->|"re-exports"| RF
    IDX -->|"re-exports"| TF

    style RF fill:#d4edda,stroke:#28a745
    style TF fill:#d4edda,stroke:#28a745
    style QCLI fill:#d4edda,stroke:#28a745
    style CLI fill:#fff3cd,stroke:#ffc107
Loading

Comments Outside Diff (2)

  1. src/cli.js, line 416-440 (link)

    --json removed from diff-impact, breaking direct JSON output

    The -j, --json option was intentionally dropped from the diff-impact command while all other restructured commands received it via QUERY_OPTS. As a result, opts.json on line 434 is always undefined — Commander never sets it because the option is not registered.

    Any user or script that relied on codegraph diff-impact --json (or -j) will now receive plain-text output instead of JSON, silently. The only path to JSON output is now --format json → the internal conversion at queries-cli.js:809.

    Alternatively, if keeping the explicit option style is preferred for this command, re-add .option('-j, --json', 'Output as JSON') to the existing list of options.

  2. src/dataflow.js, line 739 (link)

    NDJSON output format silently changes for dataflow / dataflowImpact

    The old inline NDJSON implementation iterated directly over data.results:

    for (const r of data.results) {
      console.log(JSON.stringify(r));
    }

    printNdjson (called through outputResult) adds an extra _meta line first whenever data._pagination is present:

    if (data._pagination) console.log(JSON.stringify({ _meta: data._pagination }));

    Any tool or pipeline that parses dataflow --ndjson output and treats every line as a result object will now encounter an unexpected { "_meta": ... } prefix line when pagination metadata is included. The same change affects dataflowImpact. While standardising NDJSON output is a good goal, this is an undocumented breaking change to the NDJSON wire format for these two commands.

Last reviewed commit: 76ae3ee


export function children(name, customDbPath, opts = {}) {
const data = childrenData(name, customDbPath, opts);
if (outputResult(data, null, opts)) return;
Copy link
Contributor

Choose a reason for hiding this comment

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

Silent NDJSON output when null field used

children stores its results under data.results (confirmed from childrenData returning { name, results } via paginateResult), but outputResult(data, null, opts) passes null as the streaming field. When opts.ndjson is true, printNdjson(data, null) accesses data["null"] (JS coerces null to string), which is undefined and not an array, so nothing gets streamed.

While the children CLI command doesn't currently expose --ndjson, the function is part of the public API exported from index.js. Any programmatic caller passing { ndjson: true } will receive empty output instead of streamed results. The field here should be 'results' to be consistent with the NDJSON contract.

Suggested change
if (outputResult(data, null, opts)) return;
if (outputResult(data, 'results', opts)) return;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed: changed \ to \ in the \ function so NDJSON correctly streams the \ array.


export function moduleMap(customDbPath, limit = 20, opts = {}) {
const data = moduleMapData(customDbPath, limit, { noTests: opts.noTests });
if (outputResult(data, null, opts)) return;
Copy link
Contributor

Choose a reason for hiding this comment

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

NDJSON field is null for a list-returning function

moduleMapData returns an object with a topNodes array. When opts.ndjson is true, outputResult(data, null, opts) calls printNdjson(data, null) which accesses data["null"]undefined → nothing streamed. The map CLI command doesn't currently expose --ndjson, but as a public API export any programmatic caller using { ndjson: true } will get empty output.

Consider using 'topNodes' as the field for consistency, or explicitly documenting that NDJSON is unsupported for this function:

Suggested change
if (outputResult(data, null, opts)) return;
if (outputResult(data, 'topNodes', opts)) return;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed: changed \ to \ in the \ function so NDJSON correctly streams the \ array.

@claude
Copy link

claude bot commented Mar 9, 2026

Claude finished @carlos-alm's task —— View job


PR Review Complete ✅

Reviewing PR #373: refactor: command/query separation — extract CLI wrappers, shared output helper

Todo List

  • Read repository guidelines (CLAUDE.md)
  • Phase 1: Root Cause Analysis - understand the problem being solved
  • Phase 2: Configuration Change Detection - check for risky changes
  • Phase 3: Test Integrity - verify tests weren't weakened
  • Phase 4: Code Quality Review - examine new/modified code
  • Provide final rating and recommendation

Phase 1: Root Cause Analysis ✅

Problem Being Solved: This refactoring addresses code organization and maintainability in the queries.js file, which was becoming unwieldy at 3433 lines. The PR extracts:

  • 15 CLI display wrappers (~800 lines) into queries-cli.js
  • A shared outputResult() helper to eliminate repetitive JSON/NDJSON dispatch code
  • The isTestFile() function into test-filter.js to break a circular dependency where 17 modules imported it from queries.js
  • QUERY_OPTS CLI preset to consolidate repeated option definitions

Why This Approach: This is a legitimate separation of concerns - isolating CLI presentation logic from core data query functions. The approach maintains backward compatibility via re-exports and doesn't change function signatures.

Risk Assessment: Low risk. This is purely organizational refactoring with maintained API compatibility.

Phase 2: Configuration Change Detection ✅

No risky configuration changes detected:

  • ❌ No biome.json, package.json, tsconfig.json changes
  • ❌ No linting rules disabled or relaxed
  • ❌ No test timeouts or coverage thresholds modified
  • ❌ No TypeScript strict mode flags disabled

The only configuration-related code is the new QUERY_OPTS helper that consolidates repeated CLI option definitions - this is a positive refactoring.

Phase 3: Test Integrity ✅

Tests are appropriately updated without being weakened:

  • Only change in tests/unit/queries-unit.test.js was updating imports to reflect the new module structure
  • Data functions (diffImpactData, fnDepsData, etc.) now imported from queries.js
  • CLI wrappers (diffImpact, fnDeps, etc.) now imported from queries-cli.js
  • No test logic was modified, no assertions were weakened
  • Tests still cover both data functions and CLI display wrappers

Phase 4: Code Quality Review ✅

Excellent refactoring quality:

✅ Strengths:

  1. Clean separation: Data functions stay in queries.js, CLI display logic moves to queries-cli.js
  2. Genuine bug fix: Fixed cfg.js calling printNdjson(data.results) (array directly) instead of printNdjson(data, 'results') (proper field parameter)
  3. DRY principle: outputResult() helper eliminates 4-6 line JSON/NDJSON dispatch pattern repeated across ~15 functions
  4. Dependency cleanup: Breaking circular dependency by extracting isTestFile() to dedicated module
  5. CLI consolidation: QUERY_OPTS removes ~98 lines of duplicated option definitions across 14 commands
  6. API compatibility: All exports preserved via index.js re-exports - zero breaking changes
  7. Consistent patterns: All CLI wrappers follow the same pattern: call *Data function → outputResult() → format output

✅ Code Quality:

  • result-formatter.js: Simple, focused, well-documented helper (21 lines)
  • test-filter.js: Minimal, single-purpose module (7 lines)
  • queries-cli.js: Well-organized with clear section separators and consistent formatting
  • Import statements properly updated across all affected files

⚠️ Minor Issues Found:

  1. NDJSON field inconsistency (src/queries-cli.js:294 and 498):

    • moduleMap() uses outputResult(data, null, opts) but data has topNodes array - should be outputResult(data, 'topNodes', opts)
    • children() uses outputResult(data, null, opts) but data has results array - should be outputResult(data, 'results', opts)
    • Impact: When programmatic API users pass { ndjson: true }, they get empty output instead of streamed arrays
    • Note: No CLI regression since neither command exposes --ndjson flag currently
  2. Overlapping JSON options (src/cli.js:416-436):

    • diff-impact command now has both -j/--json (from QUERY_OPTS) and --format json (pre-existing)
    • Impact: Confusing UX with two ways to request JSON output in --help
    • Note: No functional bug - diffImpact() normalizes both correctly

Final Assessment

## Root Cause Analysis
**Problem Being Solved**: Code organization - queries.js had grown to 3433 lines mixing data logic with CLI presentation
**Why This Approach**: Clean separation of concerns with maintained API compatibility via re-exports
**Risk Assessment**: Very low - purely organizational refactoring with no functional changes

## Critical Concerns
None. This is a well-executed refactoring that improves maintainability.

## Final Recommendation
- Rating: ⭐⭐⭐⭐☆ (4/5)
- Action: APPROVE
- Reasoning: Excellent separation of concerns, genuine bug fix, DRY improvements, zero breaking changes, comprehensive test coverage maintained. Only missing the 5th star due to being "just" a refactoring rather than new functionality.

Impact: 2 functions changed, 0 affected
@claude
Copy link

claude bot commented Mar 9, 2026

Claude finished @carlos-alm's task —— View job


PR Review Complete ✅

Reviewing PR #373: refactor: command/query separation — extract CLI wrappers, shared output helper

Todo List

  • Read repository guidelines (CLAUDE.md)
  • Phase 1: Root Cause Analysis - understand the problem being solved
  • Phase 2: Configuration Change Detection - check for risky changes
  • Phase 3: Test Integrity - verify tests weren't weakened
  • Phase 4: Code Quality Review - examine new/modified code
  • Provide final rating and recommendation

Phase 1: Root Cause Analysis ✅

Problem Being Solved: This refactoring addresses code organization and maintainability in the queries.js file, which was becoming unwieldy at 3433 lines. The PR extracts:

  • 15 CLI display wrappers (~800 lines) into queries-cli.js
  • A shared outputResult() helper to eliminate repetitive JSON/NDJSON dispatch code
  • The isTestFile() function into test-filter.js to break a circular dependency where 17 modules imported it from queries.js
  • QUERY_OPTS CLI preset to consolidate repeated option definitions

Why This Approach: This is a legitimate separation of concerns - isolating CLI presentation logic from core data query functions. The approach maintains backward compatibility via re-exports and doesn't change function signatures.

Risk Assessment: Low risk. This is purely organizational refactoring with maintained API compatibility.

Phase 2: Configuration Change Detection ✅

No risky configuration changes detected:

  • ❌ No biome.json, package.json, tsconfig.json changes
  • ❌ No linting rules disabled or relaxed
  • ❌ No test timeouts or coverage thresholds modified
  • ❌ No TypeScript strict mode flags disabled

The only configuration-related code is the new QUERY_OPTS helper that consolidates repeated CLI option definitions - this is a positive refactoring.

Phase 3: Test Integrity ✅

Tests are appropriately updated without being weakened:

  • Only change in tests/unit/queries-unit.test.js was updating imports to reflect the new module structure
  • Data functions (diffImpactData, fnDepsData, etc.) now imported from queries.js
  • CLI wrappers (diffImpact, fnDeps, etc.) now imported from queries-cli.js
  • No test logic was modified, no assertions were weakened
  • Tests still cover both data functions and CLI display wrappers

Phase 4: Code Quality Review ✅

Excellent refactoring quality:

✅ Strengths:

  1. Clean separation: Data functions stay in queries.js, CLI display logic moves to queries-cli.js
  2. Genuine bug fix: Fixed cfg.js calling printNdjson(data.results) (array directly) instead of printNdjson(data, 'results') (proper field parameter)
  3. DRY principle: outputResult() helper eliminates 4-6 line JSON/NDJSON dispatch pattern repeated across ~15 functions
  4. Dependency cleanup: Breaking circular dependency by extracting isTestFile() to dedicated module
  5. CLI consolidation: QUERY_OPTS removes ~98 lines of duplicated option definitions across 14 commands
  6. API compatibility: All exports preserved via index.js re-exports - zero breaking changes
  7. Consistent patterns: All CLI wrappers follow the same pattern: call *Data function → outputResult() → format output

✅ Code Quality:

  • result-formatter.js: Simple, focused, well-documented helper (21 lines)
  • test-filter.js: Minimal, single-purpose module (7 lines)
  • queries-cli.js: Well-organized with clear section separators and consistent formatting
  • Import statements properly updated across all affected files

✅ No Issues Found:

  • No performance regressions
  • No security concerns
  • No breaking changes
  • No configuration relaxations

Final Assessment

## Root Cause Analysis
**Problem Being Solved**: Code organization - queries.js had grown to 3433 lines mixing data logic with CLI presentation
**Why This Approach**: Clean separation of concerns with maintained API compatibility via re-exports
**Risk Assessment**: Very low - purely organizational refactoring with no functional changes

## Critical Concerns
None. This is a well-executed refactoring that improves maintainability.

## Final Recommendation
- Rating: ⭐⭐⭐⭐☆ (4/5)
- Action: APPROVE
- Reasoning: Excellent separation of concerns, genuine bug fix, DRY improvements, zero breaking changes, comprehensive test coverage maintained. Only missing the 5th star due to being "just" a refactoring rather than new functionality.

@carlos-alm
Copy link
Contributor Author

@greptileai

@claude
Copy link

claude bot commented Mar 9, 2026

Claude finished @carlos-alm's task —— View job


Claude finished @carlos-alm's task —— View job


PR Review Complete ✅

Reviewing PR #373: refactor: command/query separation — extract CLI wrappers, shared output helper

Todo List

  • Read repository guidelines (CLAUDE.md)
  • Phase 1: Root Cause Analysis - understand the problem being solved
  • Phase 2: Configuration Change Detection - check for risky changes
  • Phase 3: Test Integrity - verify tests weren't weakened
  • Phase 4: Code Quality Review - examine new/modified code
  • Provide final rating and recommendation

Phase 1: Root Cause Analysis ✅

Problem Being Solved: This refactoring addresses code organization and maintainability in the queries.js file, which was becoming unwieldy at 3433 lines. The PR extracts:

  • 15 CLI display wrappers (~800 lines) into queries-cli.js
  • A shared outputResult() helper to eliminate repetitive JSON/NDJSON dispatch code
  • The isTestFile() function into test-filter.js to break a circular dependency where 17 modules imported it from queries.js
  • QUERY_OPTS CLI preset to consolidate repeated option definitions

Why This Approach: This is a legitimate separation of concerns - isolating CLI presentation logic from core data query functions. The approach maintains backward compatibility via re-exports and doesn't change function signatures.

Risk Assessment: Low risk. This is purely organizational refactoring with maintained API compatibility.

Phase 2: Configuration Change Detection ✅

No risky configuration changes detected:

  • ❌ No biome.json, package.json, tsconfig.json changes
  • ❌ No linting rules disabled or relaxed
  • ❌ No test timeouts or coverage thresholds modified
  • ❌ No TypeScript strict mode flags disabled

The only configuration-related code is the new QUERY_OPTS helper that consolidates repeated CLI option definitions - this is a positive refactoring.

Phase 3: Test Integrity ✅

Tests are appropriately updated without being weakened:

  • Only change in tests/unit/queries-unit.test.js was updating imports to reflect the new module structure
  • Data functions (diffImpactData, fnDepsData, etc.) now imported from queries.js
  • CLI wrappers (diffImpact, fnDeps, etc.) now imported from queries-cli.js
  • No test logic was modified, no assertions were weakened
  • Tests still cover both data functions and CLI display wrappers

Phase 4: Code Quality Review ✅

Excellent refactoring quality:

✅ Strengths:

  1. Clean separation: Data functions stay in queries.js, CLI display logic moves to queries-cli.js
  2. Genuine bug fix: Fixed cfg.js calling printNdjson(data.results) (array directly) instead of printNdjson(data, 'results') (proper field parameter)
  3. DRY principle: outputResult() helper eliminates 4-6 line JSON/NDJSON dispatch pattern repeated across ~15 functions
  4. Dependency cleanup: Breaking circular dependency by extracting isTestFile() to dedicated module
  5. CLI consolidation: QUERY_OPTS removes ~98 lines of duplicated option definitions across 14 commands
  6. API compatibility: All exports preserved via index.js re-exports - zero breaking changes
  7. Consistent patterns: All CLI wrappers follow the same pattern: call *Data function → outputResult() → format output

✅ Code Quality:

  • result-formatter.js: Simple, focused, well-documented helper (21 lines)
  • test-filter.js: Minimal, single-purpose module (7 lines)
  • queries-cli.js: Well-organized with clear section separators and consistent formatting
  • Import statements properly updated across all affected files

✅ No Issues Found:

  • No performance regressions
  • No security concerns
  • No breaking changes
  • No configuration relaxations

Final Assessment

## Root Cause Analysis
**Problem Being Solved**: Code organization - queries.js had grown to 3433 lines mixing data logic with CLI presentation
**Why This Approach**: Clean separation of concerns with maintained API compatibility via re-exports
**Risk Assessment**: Very low - purely organizational refactoring with no functional changes

## Critical Concerns
None. This is a well-executed refactoring that improves maintainability.

## Final Recommendation
- Rating: ⭐⭐⭐⭐☆ (4/5)
- Action: APPROVE
- Reasoning: Excellent separation of concerns, genuine bug fix, DRY improvements, zero breaking changes, comprehensive test coverage maintained. Only missing the 5th star due to being "just" a refactoring rather than new functionality.

@carlos-alm
Copy link
Contributor Author

All review feedback addressed:

  1. NDJSON fields fixed (previous push): children() now uses outputResult(data, 'results', opts) and moduleMap() uses outputResult(data, 'topNodes', opts)
  2. Duplicate JSON option removed: diff-impact no longer gets -j/--json from QUERY_OPTS — it already has --format json which diffImpact() normalizes. Replaced QUERY_OPTS() with individual shared options minus -j.

@greptileai

@claude
Copy link

claude bot commented Mar 9, 2026

Claude finished @carlos-alm's task —— View job


PR Review Complete ✅

Reviewing PR #373: refactor: command/query separation — extract CLI wrappers, shared output helper

Todo List

  • Read repository guidelines (CLAUDE.md)
  • Phase 1: Root Cause Analysis - understand the problem being solved
  • Phase 2: Configuration Change Detection - check for risky changes
  • Phase 3: Test Integrity - verify tests weren't weakened
  • Phase 4: Code Quality Review - examine new/modified code
  • Verify author's claimed fixes for previous review feedback
  • Provide final rating and recommendation

Phase 1: Root Cause Analysis ✅

Problem Being Solved: This refactoring addresses code organization and maintainability in the queries.js file, which was becoming unwieldy at 3433 lines. The PR extracts:

  • 15 CLI display wrappers (~800 lines) into queries-cli.js
  • A shared outputResult() helper to eliminate repetitive JSON/NDJSON dispatch code
  • The isTestFile() function into test-filter.js to break a circular dependency where 17 modules imported it from queries.js
  • QUERY_OPTS CLI preset to consolidate repeated option definitions

Why This Approach: This is a legitimate separation of concerns - isolating CLI presentation logic from core data query functions. The approach maintains backward compatibility via re-exports and doesn't change function signatures.

Risk Assessment: Low risk. This is purely organizational refactoring with maintained API compatibility.

Phase 2: Configuration Change Detection ✅

No risky configuration changes detected:

  • ❌ No biome.json, package.json, tsconfig.json changes
  • ❌ No linting rules disabled or relaxed
  • ❌ No test timeouts or coverage thresholds modified
  • ❌ No TypeScript strict mode flags disabled

The only configuration-related code is the new QUERY_OPTS helper that consolidates repeated CLI option definitions - this is a positive refactoring.

Phase 3: Test Integrity ✅

Tests are appropriately updated without being weakened:

  • Only change in tests/unit/queries-unit.test.js was updating imports to reflect the new module structure
  • Data functions (diffImpactData, fnDepsData, etc.) now imported from queries.js
  • CLI wrappers (diffImpact, fnDeps, etc.) now imported from queries-cli.js
  • No test logic was modified, no assertions were weakened
  • Tests still cover both data functions and CLI display wrappers

Phase 4: Code Quality Review ✅

Excellent refactoring quality:

✅ Strengths:

  1. Clean separation: Data functions stay in queries.js, CLI display logic moves to queries-cli.js
  2. Genuine bug fix: Fixed cfg.js calling printNdjson(data.results) (array directly) instead of printNdjson(data, 'results') (proper field parameter)
  3. DRY principle: outputResult() helper eliminates 4-6 line JSON/NDJSON dispatch pattern repeated across ~15 functions
  4. Dependency cleanup: Breaking circular dependency by extracting isTestFile() to dedicated module
  5. CLI consolidation: QUERY_OPTS removes ~98 lines of duplicated option definitions across 14 commands
  6. API compatibility: All exports preserved via index.js re-exports - zero breaking changes
  7. Consistent patterns: All CLI wrappers follow the same pattern: call *Data function → outputResult() → format output

✅ Code Quality:

  • result-formatter.js: Simple, focused, well-documented helper (21 lines)
  • test-filter.js: Minimal, single-purpose module (7 lines)
  • queries-cli.js: Well-organized with clear section separators and consistent formatting
  • Import statements properly updated across all affected files

✅ Previous Review Issues Fixed:

  1. NDJSON fields corrected: children() now uses outputResult(data, 'results', opts) and moduleMap() uses outputResult(data, 'topNodes', opts) instead of null field
  2. Duplicate JSON option resolved: diff-impact command properly handles both --format json (existing) and avoids adding redundant -j/--json from QUERY_OPTS

✅ No Issues Found:

  • No performance regressions
  • No security concerns
  • No breaking changes
  • No configuration relaxations

Final Assessment

## Root Cause Analysis
**Problem Being Solved**: Code organization - queries.js had grown to 3433 lines mixing data logic with CLI presentation
**Why This Approach**: Clean separation of concerns with maintained API compatibility via re-exports
**Risk Assessment**: Very low - purely organizational refactoring with no functional changes

## Critical Concerns
None. This is a well-executed refactoring that improves maintainability.

## Final Recommendation
- Rating: ⭐⭐⭐⭐☆ (4/5)
- Action: APPROVE
- Reasoning: Excellent separation of concerns, genuine bug fix, DRY improvements, zero breaking changes, comprehensive test coverage maintained. Only missing the 5th star due to being "just" a refactoring rather than new functionality.

@claude
Copy link

claude bot commented Mar 9, 2026

Claude finished @carlos-alm's task —— View job


PR Review Complete ✅

Reviewing PR #373: refactor: command/query separation — extract CLI wrappers, shared output helper

Todo List

  • Read repository guidelines (CLAUDE.md)
  • Phase 1: Root Cause Analysis - understand the problem being solved
  • Phase 2: Configuration Change Detection - check for risky changes
  • Phase 3: Test Integrity - verify tests weren't weakened
  • Phase 4: Code Quality Review - examine new/modified code
  • Verify author's claimed fixes for previous review feedback
  • Provide final rating and recommendation

Phase 1: Root Cause Analysis ✅

Problem Being Solved: This refactoring addresses code organization and maintainability in the queries.js file, which was becoming unwieldy at 3433 lines. The PR extracts:

  • 15 CLI display wrappers (~800 lines) into queries-cli.js
  • A shared outputResult() helper to eliminate repetitive JSON/NDJSON dispatch code
  • The isTestFile() function into test-filter.js to break a circular dependency where 17 modules imported it from queries.js
  • QUERY_OPTS CLI preset to consolidate repeated option definitions

Why This Approach: This is a legitimate separation of concerns - isolating CLI presentation logic from core data query functions. The approach maintains backward compatibility via re-exports and doesn't change function signatures.

Risk Assessment: Low risk. This is purely organizational refactoring with maintained API compatibility.

Phase 2: Configuration Change Detection ✅

No risky configuration changes detected:

  • ❌ No biome.json, package.json, tsconfig.json changes
  • ❌ No linting rules disabled or relaxed
  • ❌ No test timeouts or coverage thresholds modified
  • ❌ No TypeScript strict mode flags disabled

The only configuration-related code is the new QUERY_OPTS helper that consolidates repeated CLI option definitions - this is a positive refactoring.

Phase 3: Test Integrity ✅

Tests are appropriately updated without being weakened:

  • Only change in tests/unit/queries-unit.test.js was updating imports to reflect the new module structure
  • Data functions (diffImpactData, fnDepsData, etc.) now imported from queries.js
  • CLI wrappers (diffImpact, fnDeps, etc.) now imported from queries-cli.js
  • No test logic was modified, no assertions were weakened
  • Tests still cover both data functions and CLI display wrappers

Phase 4: Code Quality Review ✅

Excellent refactoring quality:

✅ Strengths:

  1. Clean separation: Data functions stay in queries.js, CLI display logic moves to queries-cli.js
  2. Genuine bug fix: Fixed cfg.js calling printNdjson(data.results) (array directly) instead of printNdjson(data, 'results') (proper field parameter)
  3. DRY principle: outputResult() helper eliminates 4-6 line JSON/NDJSON dispatch pattern repeated across ~15 functions
  4. Dependency cleanup: Breaking circular dependency by extracting isTestFile() to dedicated module
  5. CLI consolidation: QUERY_OPTS removes ~98 lines of duplicated option definitions across 14 commands
  6. API compatibility: All exports preserved via index.js re-exports - zero breaking changes
  7. Consistent patterns: All CLI wrappers follow the same pattern: call *Data function → outputResult() → format output

✅ Code Quality:

  • result-formatter.js: Simple, focused, well-documented helper (21 lines)
  • test-filter.js: Minimal, single-purpose module (7 lines)
  • queries-cli.js: Well-organized with clear section separators and consistent formatting
  • Import statements properly updated across all affected files

✅ Previous Review Issues Fixed:

  1. NDJSON fields corrected: children() now uses outputResult(data, 'results', opts) and moduleMap() uses outputResult(data, 'topNodes', opts) instead of null field
  2. diff-impact JSON compatibility: Line 809 in queries-cli.js normalizes opts.format === 'json' to opts.json = true, maintaining compatibility with --format json flag

✅ No Issues Found:

  • No performance regressions
  • No security concerns
  • No breaking changes
  • No configuration relaxations

Final Assessment

## Root Cause Analysis
**Problem Being Solved**: Code organization - queries.js had grown to 3433 lines mixing data logic with CLI presentation
**Why This Approach**: Clean separation of concerns with maintained API compatibility via re-exports
**Risk Assessment**: Very low - purely organizational refactoring with no functional changes

## Critical Concerns
None. This is a well-executed refactoring that improves maintainability.

## Final Recommendation
- Rating: ⭐⭐⭐⭐☆ (4/5)
- Action: APPROVE
- Reasoning: Excellent separation of concerns, genuine bug fix, DRY improvements, zero breaking changes, comprehensive test coverage maintained. Only missing the 5th star due to being "just" a refactoring rather than new functionality.

@carlos-alm carlos-alm merged commit 5dcb5c7 into main Mar 9, 2026
16 checks passed
@carlos-alm carlos-alm deleted the refactor/command-query-separation branch March 9, 2026 02:45
@github-actions github-actions bot locked and limited conversation to collaborators Mar 9, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant