PR: Add Husky pre-commit hooks for automated linting and quality checks#105
PR: Add Husky pre-commit hooks for automated linting and quality checks#105ashleyshaw wants to merge 4 commits into
Conversation
…sting Set up Husky v9 with lint-staged to enforce code quality standards before commits and pushes. This ensures all code meets linting, formatting, and test requirements before being committed to the repository. Changes: - Install Husky (9.1.7), lint-staged (16.2.6), and markdownlint-cli as dev dependencies - Add prepare script to package.json for automatic Husky initialization - Configure lint-staged for JS/TS, JSON, Markdown, and YAML files - Update pre-commit hook to run lint-staged on staged files (removed deprecated husky.sh) - Add pre-push hook to run test suite before pushing - Document Git Hooks setup and usage in DEVELOPMENT.md This automation helps maintain consistent code quality across the repository and prevents broken code from being pushed to remote branches. Note: prettier.config.js needs to be converted to .cjs format to work with package.json "type": "module" - will be addressed in follow-up commit.
Renamed babel.config.js and prettier.config.js to .cjs extension to fix compatibility issues with package.json "type": "module" setting. These files use CommonJS syntax (require/module.exports) and must have .cjs extension when the package is configured as an ES module. This resolves test failures and linting errors that occurred when git hooks tried to run these tools.
|
Note Other AI code review bot(s) detectedCodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review. Note
|
| Cohort / File(s) | Summary |
|---|---|
Git hooks .husky/pre-commit, .husky/pre-push |
Updated pre-commit shebang and changed command to npx lint-staged; added pre-push hook that sources Husky and runs npm test |
Package configuration package.json |
Added devDependencies: husky, lint-staged, markdownlint-cli; added prepare script (husky install); added top-level lint-staged config for JS/TS, JSON, MD, YAML, and package.json |
Documentation DEVELOPMENT.md |
Added documentation describing Husky hooks, lint-staged rules, toolchain (ESLint, Prettier, markdownlint, npm-package-json-lint), and bypass instructions (--no-verify) |
Sequence Diagram(s)
sequenceDiagram
autonumber
participant Dev as Developer
participant Git as Git
participant Husky as Husky hooks
participant LintStaged as lint-staged
participant TestRunner as npm test
rect rgb(235,245,255)
Dev->>Git: git commit
Git->>Husky: invoke .husky/pre-commit
Husky->>LintStaged: run npx lint-staged
LintStaged-->>Husky: exit (0/1)
alt lint-staged success
Husky-->>Git: allow commit
else lint-staged failed
Husky-->>Git: block commit
end
end
rect rgb(245,255,235)
Dev->>Git: git push
Git->>Husky: invoke .husky/pre-push
Husky->>TestRunner: run npm test
TestRunner-->>Husky: exit (0/1)
alt tests pass
Husky-->>Git: allow push
else tests fail
Husky-->>Git: block push
end
end
Estimated code review effort
🎯 2 (Simple) | ⏱️ ~10 minutes
- Check
.husky/pre-commitshebang andnpxinvocation for expected environment/CI compatibility. - Verify
lint-stagedconfig entries andnpmPkgJsonLintcommand availability. - Ensure
preparescript and added devDependencies use intended versions and don't conflict with CI hooks.
Possibly related PRs
- PR: Scaffold CI/CD Workflows, Pre-commit Hooks, Schema Validation, and Metrics for Project Foundation #74 — also modifies Husky hooks and lint-related package.json configuration (related changes to
.husky/pre-commitand lint tooling).
Pre-merge checks and finishing touches
✅ Passed checks (3 passed)
| Check name | Status | Explanation |
|---|---|---|
| Title check | ✅ Passed | The title clearly and specifically describes the main change: adding Husky pre-commit hooks for automated linting and quality checks. |
| Description check | ✅ Passed | The description follows the required template with all major sections completed: linked issues, comprehensive changelog entries (Added/Changed), detailed risk assessment, thorough testing instructions with prerequisites, expected results and edge cases, and a completed checklist. |
| Docstring Coverage | ✅ Passed | No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. |
✨ Finishing touches
🧪 Generate unit tests (beta)
- Create PR with unit tests
- Post copyable unit tests in a comment
- Commit unit tests in branch
claude/setup-husky-linting-01XHEPbL36Mg4G5jrptrfSKm
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 @coderabbitai help to get the list of available commands and usage tips.
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (2)
.husky/pre-push (1)
1-2: Consider adding explicit exit code handling for clarity.The script will correctly block the push if
npm testfails (shell exits with the last command's exit code), but explicit error handling improves readability and maintenance.Apply this diff for explicit exit handling:
#!/usr/bin/env sh -npm test +npm test || exit 1package.json (1)
99-99: Consider adopting a deliberate version pinning strategy across devDependencies.The new dependencies use exact version pinning, which aligns with existing packages like
@actions/coreand@actions/github, but differs from most others like@babel/*. While the versions are current (9.1.7, 16.2.6, 0.45.0 are all latest), the file uses mixed pinning patterns.For consistency and maintainability, consider standardizing whether exact pinning or semver ranges (
^) should be used throughout devDependencies. If using exact pinning for development tool stability is intentional, document this strategy; otherwise, consider updating these and similar packages to^for automatic patch updates.
📜 Review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
package-lock.jsonis excluded by!**/package-lock.json
📒 Files selected for processing (4)
.husky/pre-commit(1 hunks).husky/pre-push(1 hunks)DEVELOPMENT.md(1 hunks)package.json(3 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/package.json
⚙️ CodeRabbit configuration file
**/package.json: Review package.json for WordPress block theme project:
- Ensure dependencies are focused on WordPress theme/plugin development.
- Check for security vulnerabilities and outdated packages.
- Ensure scripts are documented and have clear, descriptive names.
- Check for presence of postinstall, prepublish, and prepare scripts if needed.
- Validate repository, bugs, and homepage fields for open source projects.
- Validate semantic versioning and proper version pinning.
- Confirm devDependencies vs dependencies separation.
- Check for unnecessary or duplicate dependencies.
- Ensure scripts follow org standards (lint, test, build, format, env, etc.).
- Confirm presence of npm scripts for linting, formatting, testing, and CI.
- Validate package metadata (name, version, description, author, license).
Files:
package.json
🔇 Additional comments (4)
.husky/pre-commit (1)
1-2: LGTM!The shebang change to
/usr/bin/env shimproves portability, and usingnpx lint-stagedis the standard approach for running lint-staged with the configuration defined in package.json.DEVELOPMENT.md (1)
55-91: Excellent documentation!The Git Hooks section accurately describes the implementation, with file types and tools matching the lint-staged configuration in package.json. The structure is clear, and the warnings about bypassing hooks are appropriate.
package.json (2)
118-118: LGTM!The
preparescript is the standard approach for Husky v9, ensuring Git hooks are installed automatically afternpm install.
138-156: Well-configured lint-staged setup!The configuration comprehensively covers all relevant file types with appropriate tooling. The order of operations (fix, then format) is logical, and the specific package.json linting with npmPkgJsonLint is a good practice.
There was a problem hiding this comment.
Pull Request Overview
This PR sets up Husky to automate code quality checks through Git hooks, ensuring linting, formatting, and testing standards are enforced before commits and pushes.
Key Changes:
- Added Husky and lint-staged as development dependencies to automate pre-commit quality checks
- Configured lint-staged to run appropriate linters and formatters for different file types
- Updated documentation to explain the Git hooks workflow and their purpose
Reviewed Changes
Copilot reviewed 4 out of 7 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| package.json | Added Husky, lint-staged, and markdownlint-cli dependencies; configured lint-staged rules for various file types; added prepare script for Husky installation |
| DEVELOPMENT.md | Added comprehensive documentation explaining the Git hooks setup, pre-commit and pre-push behaviors, and bypass instructions |
| .husky/pre-commit | Updated to run lint-staged instead of the previous check script |
| .husky/pre-push | Created new pre-push hook to run tests before pushing |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Ash Shaw <ashley@lightspeedwp.agency>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Ash Shaw <ashley@lightspeedwp.agency>
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
.husky/pre-push (1)
3-3: Test script verified; echo statement remains optional.The verification confirms that the
testscript is properly defined inpackage.jsonasnpm run test:js, so the pre-push hook will execute tests correctly without risk of silent failures. Adding the echo statement for user feedback remains a valid optional improvement for UX, but the current implementation is functionally sound.
📜 Review details
Configuration used: Path: .coderabbit.yml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
.husky/pre-commit(1 hunks).husky/pre-push(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- .husky/pre-commit
🔇 Additional comments (1)
.husky/pre-push (1)
1-2: LGTM! Past review comment has been addressed.The shebang and Husky sourcing are now correctly in place. The past review comment about missing the Husky shell script source line has been resolved.
name: "Pull Request"
about: "General changes, refactors, and maintenance"
title: "PR: Add Husky pre-commit hooks for automated linting and quality checks"
labels: ["status:needs-review"]
General Pull Request
Linked issues
Closes #
Changelog
Added
Changed
Fixed
Removed
Risk Assessment
Risk Level: Low
Potential Impact:
Mitigation Steps:
npm install(oryarn install) to enable hooks.How to Test
Prerequisites
npm installoryarn install(enables Husky hooks)Test Steps
Expected Results
Edge Cases to Verify
Checklist (Global DoD / PR)
References
Summary by CodeRabbit
New Features
Documentation
Chores