Skip to content

feat: add husky pre-commit hooks for type checking and linting#84

Open
kushbosamiya wants to merge 1 commit intoStabilityNexus:mainfrom
kushbosamiya:feat/husky-precommit-hooks
Open

feat: add husky pre-commit hooks for type checking and linting#84
kushbosamiya wants to merge 1 commit intoStabilityNexus:mainfrom
kushbosamiya:feat/husky-precommit-hooks

Conversation

@kushbosamiya
Copy link
Copy Markdown

@kushbosamiya kushbosamiya commented Mar 19, 2026

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 if npm run typecheck or lint-staged fails.
  • package.json:
    • Scripts: Added "prepare": "husky" to ensure hooks are automatically installed when contributors run npm install.
    • Dependencies: Added husky (v9.1.7) and lint-staged (v16.4.0).
    • Configuration: Defined lint-staged rules:
      • *.{js,jsx,ts,tsx}: Runs eslint --fix and prettier --write.
      • *.{json,md,css}: Runs prettier --write.
  • Safety: Husky hooks are configured to run tsc --noEmit globally to ensure cross-module type consistency, while lint-staged keeps formatting fast by only targeting changed files.

3. Verification

  • Pre-commit Validation: Verified the lifecycle locally. The commit process now automatically triggers the following success path:
    Running Pre-commit Hooks...
    > fate@0.1.0 typecheck
    > tsc --noEmit
    ✔ Backed up original state in git stash
    ✔ Running tasks for staged files...
    ✔ Applying modifications from tasks...
    ✔ Cleaning up temporary files...
    All checks passed!
  • Error Prevention: Manually injected a type error in src/app/page.tsx; confirmed that the pre-commit hook successfully aborted the commit with an informative error message.
  • Build Integrity: Confirmed that npm run typecheck passes without warnings, ensuring zero regressions on the main build path.

Summary by CodeRabbit

  • Chores
    • Established automated pre-commit checks to validate code type safety and quality before commits are made.
    • Configured automatic code linting and formatting for JavaScript, TypeScript, JSON, Markdown, and CSS files to maintain consistent code standards across the project.
    • Added development tooling infrastructure to support enhanced code quality and maintainability processes.

- 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
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 19, 2026

📝 Walkthrough

Walkthrough

Added pre-commit hooks and staging tools to automatically run linting and type checking before commits. Introduced .husky/pre-commit hook script and configured package.json with husky, lint-staged, and prettier dependencies and scripts to catch code quality issues locally.

Changes

Cohort / File(s) Summary
Pre-commit Hook Setup
.husky/pre-commit
New shell script that executes npm run typecheck and npx lint-staged with error handling, ensuring type and lint checks pass before commits.
Package Configuration
package.json
Added prepare and typecheck npm scripts; introduced lint-staged configuration to run eslint with auto-fix and prettier formatting on staged JS/TS/TSX/JSON/MD/CSS files; added husky, lint-staged, and prettier as dev dependencies.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 Hops of joy, I must confess,
Pre-commit hooks catch the mess,
Before you push, lint runs with care,
Type-check whispers "all is fair,"
Cleaner code hops everywhere!

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: adding Husky pre-commit hooks for type checking and linting, which aligns with the pull request's primary objective.
Linked Issues check ✅ Passed All coding objectives from issue #81 are met: Husky and lint-staged added, prepare script configured, .husky/pre-commit hook created, and lint-staged configured for ESLint and Prettier on relevant file types.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing Husky pre-commit hooks as specified in issue #81; no unrelated modifications are present in the changeset.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 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

📥 Commits

Reviewing files that changed from the base of the PR and between 3377a06 and 65fbbae.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (2)
  • .husky/pre-commit
  • package.json

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.

chore: Add Husky pre-commit hooks for lint and type-check

1 participant