Skip to content

refactor: remove dead code and deduplicate shared utilities#8

Merged
woai3c merged 7 commits into
mainfrom
refactor/deduplicate-dead-code
May 6, 2026
Merged

refactor: remove dead code and deduplicate shared utilities#8
woai3c merged 7 commits into
mainfrom
refactor/deduplicate-dead-code

Conversation

@woai3c
Copy link
Copy Markdown
Owner

@woai3c woai3c commented May 6, 2026

Dead code removed:

  • src/config/index.ts (CliOptions + resolveCliConfig, never imported)
  • Dead fallback loop in getToolInputPreview (all tool types covered above)

Duplication eliminated:

  • normalizeLineEndings: was duplicated in stdout-writer.ts and tool-display.ts, now lives once in src/ui/utils.ts
  • Shell persistence commands: switch(shell) blocks in printNoApiKeyMessage and printNoWebSearchKeyHint were identical — extracted into formatPersistCommand() in src/shell.ts alongside detectShell()
  • applyFg/applyColor: render-diff.ts had its own copy of syntax-highlight's applyColor; now imports the exported version
  • formatDuration/formatTaskDuration: merged into one configurable function in utils.ts with precision and compact options
  • Boolean arg parsing: parseBooleanArg() replaces the same on/off/true/false/enable chains in handleThinkingToggle and handlePlanToggle

New files:

  • src/shell.ts: detectShell() + formatPersistCommand()
  • src/ui/utils.ts: normalizeLineEndings(), parseBooleanArg(), formatDuration(), getToolLabel(), getToolInputPreview(), getToolResultSummary(), formatReadGroupSummary(), isCollapsibleReadOnlyTool()

tool-display.ts is now a thin re-export barrel for backward compatibility.

Net: -470 lines, tsc clean, 33/33 tests pass.

woai3c and others added 7 commits May 6, 2026 14:54
Dead code removed:
- src/config/index.ts (CliOptions + resolveCliConfig, never imported)
- Dead fallback loop in getToolInputPreview (all tool types covered above)

Duplication eliminated:
- normalizeLineEndings: was duplicated in stdout-writer.ts and tool-display.ts,
  now lives once in src/ui/utils.ts
- Shell persistence commands: switch(shell) blocks in printNoApiKeyMessage and
  printNoWebSearchKeyHint were identical — extracted into formatPersistCommand()
  in src/shell.ts alongside detectShell()
- applyFg/applyColor: render-diff.ts had its own copy of syntax-highlight's
  applyColor; now imports the exported version
- formatDuration/formatTaskDuration: merged into one configurable function in
  utils.ts with precision and compact options
- Boolean arg parsing: parseBooleanArg() replaces the same on/off/true/false/enable
  chains in handleThinkingToggle and handlePlanToggle

New files:
- src/shell.ts: detectShell() + formatPersistCommand()
- src/ui/utils.ts: normalizeLineEndings(), parseBooleanArg(), formatDuration(),
  getToolLabel(), getToolInputPreview(), getToolResultSummary(),
  formatReadGroupSummary(), isCollapsibleReadOnlyTool()

tool-display.ts is now a thin re-export barrel for backward compatibility.

Net: -470 lines, tsc clean, 33/33 tests pass.
The unified formatDuration defaults to precision=1, but the task summary
line previously used Math.floor(ms/1000) — `30s`, not `30.0s`. Pass
precision: 0 explicitly so the merged helper matches the old output.
Without a `prepare` script, `pnpm install` never invokes the husky CLI,
so `core.hooksPath` stays unset and `.husky/pre-commit` (lint-staged)
plus `.husky/commit-msg` (commitlint) are silently dead. Adding the
script means a fresh checkout + install enables both hooks.
The previous PR moved every helper out of tool-display.ts into utils.ts
and left the file as a re-export shim "for backward compatibility."
There are no external consumers of @x-code-cli/cli, so the only effect
was two import paths into the same set of functions and a guaranteed
drift point. Migrate the two remaining importers (use-agent, ChatInput)
to utils directly and delete the barrel.
Six focused cleanups, all behavior-preserving (typecheck + 356 tests
unchanged):

- Drop unused export RIGHT_MARGIN_SAFETY from terminal-glyphs (no
  importers anywhere).
- Replace ChatInput's private formatTokens with utils.formatTokenCount
  (bodies were identical; promote the latter to an export).
- Hoist RESULT_INDENT into utils so render-diff and stdout-writer share
  one definition instead of two const declarations carrying the same
  6-space literal.
- Collapse addInfoMessage/addUserMessage in use-agent onto a shared
  addMessage(role, content) core; keep the named wrappers so callers
  still document intent.
- Replace four near-identical keyword-classification branches in
  syntax-highlight's paint() (python/go/rust/shell) with a
  KEYWORD_RULES table + single dispatch.
- Merge EXT_TO_LANG and FENCE_LANG_TO_LANG into one LANG_LOOKUP table
  keyed by both file extensions and fence-language aliases; both
  detect* functions now read the same source.

Net: -54 lines, no API surface change for external consumers.
- app.tsx: remove unused React import (jsx: react-jsx — no runtime
  reference needed).
- ChatInput.tsx: remove cursorAnchor; the variable was assigned at two
  call sites but never read. The accompanying comment claimed it was
  kept for "future revival of the visible cursor" — speculative future-
  proofing per project convention. Stale comments referring to it have
  been updated.
- sub-agents/runner.ts: drop the unused `aborted` local (return values
  use `aborted: true|false` literals directly), prefix the unused
  onToolCall callback param to match the existing onToolProgress
  pattern, and remove the unused `toolName` parameter from the private
  previewInput helper.

Found by `tsc --noEmit --noUnusedLocals --noUnusedParameters`. ESLint's
no-unused-vars is configured at warn-level so these slipped past CI.
@woai3c woai3c merged commit 28f451d into main May 6, 2026
5 checks passed
@woai3c woai3c deleted the refactor/deduplicate-dead-code branch May 6, 2026 08:30
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