Nightly Code Complexity Reduction
The caller provides pre-computed context:
- Package manager (
npm,yarn, orbun) - Current thresholds (cognitiveComplexity, maxLinesPerFunction from eslint.thresholds.json)
- Proposed thresholds (each metric decreased toward target minimums)
- Metrics being reduced (which metrics are above target)
Instructions
- Update eslint.thresholds.json with the proposed new threshold values (do NOT change the maxLines threshold)
- Run the project's lint script with the provided package manager (e.g.,
npm run lint,yarn lint, orbun run lint) to find functions that violate the new stricter thresholds - Before editing, check each violating file's total line count (
wc -l). If a file is within 20 lines of itsmax-linesESLint limit (typically 300), extract helpers into a separate companion file (e.g.,fooHelpers.ts) instead of adding them to the same file. Extracting functions into the same file adds net lines and can create new max-lines violations. - Fix violations one file at a time. Read only the specific function that violates — do not pre-read all files upfront. Fix it, then move to the next.
- For cognitive complexity violations: use early returns, extract helper functions, replace conditionals with lookup tables
- For max-lines-per-function violations: split large functions, extract helper functions, separate concerns
- After each file edit, run the project's formatter (e.g.,
bun run formatornpx prettier --write <file>) to ensure line counts reflect the final formatted state before moving on - Re-run the lint script with the provided package manager to verify all violations are resolved (both the target metric AND max-lines)
- Run the TypeScript compiler to catch type errors early:
npx tsc --noEmit 2>&1 | head -30. If there are type errors, fix them now — do NOT wait until the commit step. Pre-commit hooks run type checking, and discovering errors at commit time wastes turns. - Run the project's test script with the provided package manager (e.g.,
npm run test,yarn test, orbun run test) to verify no tests are broken by the refactoring - Commit all changes (refactored code + updated eslint.thresholds.json) with conventional commit messages
- Create a PR with
gh pr createwith a title like "refactor: reduce code complexity: [metrics being reduced]" summarizing the changes