Skip to content

PR: Add Husky pre-commit hooks for automated linting and quality checks#105

Open
ashleyshaw wants to merge 4 commits into
developfrom
claude/setup-husky-linting-01XHEPbL36Mg4G5jrptrfSKm
Open

PR: Add Husky pre-commit hooks for automated linting and quality checks#105
ashleyshaw wants to merge 4 commits into
developfrom
claude/setup-husky-linting-01XHEPbL36Mg4G5jrptrfSKm

Conversation

@ashleyshaw
Copy link
Copy Markdown
Member

@ashleyshaw ashleyshaw commented Nov 18, 2025


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

This repository enforces changelog, release, and label automation for all PRs and issues.
See the organisation-wide Automation Governance & Release Strategy for contributor rules.

Linked issues

Closes #

Changelog

Added

  • Integrated Husky to run automated code quality checks (linting and formatting) before commits and pushes.
  • Added Git hooks to ensure consistent standards across all contributions.
  • Updated documentation for local development setup and Husky workflow integration.

Changed

  • Developer workflow now enforces linting and formatting pre-commit via Husky.
  • Updated contribution and setup documentation to include Husky requirements.

Fixed

Removed


Risk Assessment

Risk Level: Low

Potential Impact:

  • Minimal risk as changes are limited to development tooling and do not affect runtime code.
  • Potential for initial commit errors if contributors' environments are not set up with Husky prerequisites.

Mitigation Steps:

  • Provided clear documentation for Husky setup.
  • Developers are instructed to run npm install (or yarn install) to enable hooks.
  • CI checks remain in place for additional coverage.

How to Test

Prerequisites

  • Node.js and npm/yarn installed
  • Fresh install of dependencies: npm install or yarn install (enables Husky hooks)

Test Steps

  1. Create a commit with a linting error
    • Modify a file in a way that would violate lint rules.
    • Attempt to commit the change; the commit should be blocked, and lint errors displayed.
  2. Create a passing commit
    • Fix lint errors.
    • Stage files and commit again; the commit should succeed.
  3. Try a push (if pre-push hooks are configured)
    • Make a push to remote; additional checks (if set) should run as part of the push process.

Expected Results

  • Commits are only accepted if code passes linting and formatting checks.
  • Contributors receive clear feedback on what failed during a blocked commit.
  • Workflow ensures a consistent codebase and reduces reviewer workload.

Edge Cases to Verify

  • Developer skips Husky install—does linting still run in CI?
  • Lint errors only block commits, not uncommitted work.
  • Windows, Mac, and Linux compatibility.
  • Git GUIs and CLI both respect hooks.

Checklist (Global DoD / PR)

  • All AC met and demonstrated
  • Tests added/updated (unit/E2E as appropriate)
  • A11y considerations addressed where relevant
  • Docs/readme/changelog updated (if user-facing)
  • Security/perf impact reviewed where relevant
  • Code/design reviews approved
  • CI green; linked issues closed; release notes prepared (if shipping)
  • Risk assessment completed above
  • Testing instructions provided above

References


Summary by CodeRabbit

  • New Features

    • Automated code formatting and linting checks now run before commits
    • Tests automatically execute before pushing to prevent faulty code from being pushed
  • Documentation

    • Added comprehensive guide for Git hooks workflow, including bypass instructions
  • Chores

    • Added development dependencies for enhanced code quality automation

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

coderabbitai Bot commented Nov 18, 2025

Note

Other AI code review bot(s) detected

CodeRabbit 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

.coderabbit.yml has unrecognized properties

CodeRabbit is using all valid settings from your configuration. Unrecognized properties (listed below) have been ignored and may indicate typos or deprecated fields that can be removed.

⚠️ Parsing warnings (1)
Validation error: Unrecognized key(s) in object: 'auto_labels', 'auto_assign', 'auto_review'
⚙️ Configuration instructions
  • Please see the configuration documentation for more information.
  • You can also validate your configuration using the online YAML validator.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
📝 Walkthrough

Walkthrough

Adds Husky git hooks and lint-staged integration: pre-commit now runs lint-staged, a new pre-push hook runs npm test, package.json gains husky/lint-staged/markdownlint-cli and a prepare script, and DEVELOPMENT.md documents the workflows and tooling.

Changes

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
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

  • Check .husky/pre-commit shebang and npx invocation for expected environment/CI compatibility.
  • Verify lint-staged config entries and npmPkgJsonLint command availability.
  • Ensure prepare script and added devDependencies use intended versions and don't conflict with CI hooks.

Possibly related PRs

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.

❤️ 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.

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 test fails (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 1
package.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/core and @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

📥 Commits

Reviewing files that changed from the base of the PR and between c422cb5 and cabe30d.

⛔ Files ignored due to path filters (1)
  • package-lock.json is 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 sh improves portability, and using npx lint-staged is 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 prepare script is the standard approach for Husky v9, ensuring Git hooks are installed automatically after npm 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.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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.

Comment thread .husky/pre-push
Comment thread .husky/pre-commit
@ashleyshaw ashleyshaw changed the title Set up Husky for automated linting and quality checks PR: Add Husky pre-commit hooks for automated linting and quality checks Nov 18, 2025
ashleyshaw and others added 2 commits November 18, 2025 12:29
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>
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.

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 test script is properly defined in package.json as npm 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

📥 Commits

Reviewing files that changed from the base of the PR and between cabe30d and 023cf8d.

📒 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.

@ashleyshaw ashleyshaw closed this May 19, 2026
@ashleyshaw ashleyshaw reopened this May 19, 2026
@ashleyshaw ashleyshaw closed this May 20, 2026
@ashleyshaw ashleyshaw reopened this May 20, 2026
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.

3 participants