feat: add husky pre-commit hooks for type checking and linting#84
feat: add husky pre-commit hooks for type checking and linting#84kushbosamiya wants to merge 1 commit intoStabilityNexus:mainfrom
Conversation
- Integrate Husky (v9.1.7) and lint-staged (v16.4.0) to enforce code quality. - Configure .husky/pre-commit to execute 'npm run typecheck' and 'npx lint-staged'. - Add 'prepare' script to package.json to facilitate automatic Husky setup on install. - Define lint-staged rules in package.json for JS/TS (ESLint, Prettier) and JSON/CSS/MD (Prettier). Closes StabilityNexus#81
📝 WalkthroughWalkthroughAdded pre-commit hooks and staging tools to automatically run linting and type checking before commits. Introduced Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
.husky/pre-commit (1)
6-8: Reorder hooks to type-check after auto-fixes.Line 6 runs typecheck before lint-staged. If lint-staged auto-fixes staged files, those changes are never type-checked. Swap the order so lint-staged runs first, then typecheck validates the final content.
Suggested fix
-npm run typecheck - -npx lint-staged +npx lint-staged + +npm run typecheck🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In @.husky/pre-commit around lines 6 - 8, The pre-commit hook runs npm run typecheck before npx lint-staged, which means auto-fixes from lint-staged won't be type-checked; swap the order so npx lint-staged runs first, then npm run typecheck, i.e. update the .husky/pre-commit hook to invoke the lint-staged command before the typecheck command (look for the lines with "npx lint-staged" and "npm run typecheck" and reverse their order).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In @.husky/pre-commit:
- Around line 6-8: The pre-commit hook runs npm run typecheck before npx
lint-staged, which means auto-fixes from lint-staged won't be type-checked; swap
the order so npx lint-staged runs first, then npm run typecheck, i.e. update the
.husky/pre-commit hook to invoke the lint-staged command before the typecheck
command (look for the lines with "npx lint-staged" and "npm run typecheck" and
reverse their order).
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 1aae506b-3221-4480-bd46-d54c67f27009
⛔ Files ignored due to path filters (1)
package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (2)
.husky/pre-commitpackage.json
1. Context
Closes #81
Problem:
The repository currently relies solely on GitHub Actions to detect type mismatches and linting errors post-push. This leads to avoidable CI failures, wasted runner minutes, and delayed feedback loops for both contributors and reviewers (as evidenced by recent build failures caught by mentors in the Discord thread).
Solution:
Implemented a local pre-commit strategy using Husky and lint-staged to enforce code quality gates before commits are finalized.
2. Implementation Details
.husky/pre-commit: Created a shell script to orchestrate the pre-commit flow. It terminates the commit ifnpm run typecheckorlint-stagedfails.package.json:"prepare": "husky"to ensure hooks are automatically installed when contributors runnpm install.husky(v9.1.7) andlint-staged(v16.4.0).lint-stagedrules:*.{js,jsx,ts,tsx}: Runseslint --fixandprettier --write.*.{json,md,css}: Runsprettier --write.tsc --noEmitglobally to ensure cross-module type consistency, whilelint-stagedkeeps formatting fast by only targeting changed files.3. Verification
src/app/page.tsx; confirmed that the pre-commit hook successfully aborted the commit with an informative error message.npm run typecheckpasses without warnings, ensuring zero regressions on the main build path.Summary by CodeRabbit