Skip to content

set up claude hook for file formatting with Prettier#3256

Open
shouples wants to merge 3 commits intomainfrom
djs/claude-format-hook
Open

set up claude hook for file formatting with Prettier#3256
shouples wants to merge 3 commits intomainfrom
djs/claude-format-hook

Conversation

@shouples
Copy link
Contributor

@shouples shouples commented Feb 4, 2026

Same as https://code.claude.com/docs/en/hooks-guide#auto-format-code-after-edits but with some extra guarding in case a user doesn't have jq installed or the prettier command fails. This should reduce the number of times Claude needs to iterate to check diagnostics/linting between edits.

Docs: https://code.claude.com/docs/en/hooks

This also sets up .prettierignore so we don't need to keep track of exclusion patterns in multiple locations.

@shouples shouples marked this pull request as ready for review February 6, 2026 20:42
@shouples shouples requested a review from a team as a code owner February 6, 2026 20:42
Copilot AI review requested due to automatic review settings February 6, 2026 20:42
"hooks": [
{
"type": "command",
"command": "command -v jq >/dev/null 2>&1 && jq -r '.tool_input.file_path' | xargs npx prettier --write --ignore-unknown 2>/dev/null || true"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note this doesn't use gulp format because we don't want to format the entire project every time a single file is edited/created

Copy link

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

Sets up auto-formatting after Claude edits by wiring Prettier into Claude hooks and aligning formatting behavior with .prettierignore.

Changes:

  • Simplifies format() globbing and makes the internal Prettier formatter respect .prettierignore
  • Adds a repo-level .prettierignore for generated/build artifacts
  • Adds a Claude PostToolUse hook to run Prettier on edited/written files

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
Gulpfile.js Updates formatting pipeline + Prettier invocation to consult .prettierignore
.prettierignore Centralizes ignore patterns for generated code and build output
.claude/settings.json Adds a hook to run Prettier automatically after Edit/Write tool use

"hooks": [
{
"type": "command",
"command": "command -v jq >/dev/null 2>&1 && jq -r '.tool_input.file_path' | xargs npx prettier --write --ignore-unknown 2>/dev/null || true"
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

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

Piping the file path through xargs without NUL delimiting/quoting can break on paths with spaces/newlines and can allow argument injection. Prefer capturing the value and invoking Prettier with proper shell quoting (and -- before the path) rather than xargs.

Suggested change
"command": "command -v jq >/dev/null 2>&1 && jq -r '.tool_input.file_path' | xargs npx prettier --write --ignore-unknown 2>/dev/null || true"
"command": "command -v jq >/dev/null 2>&1 && file_path=\"$(jq -r '.tool_input.file_path' | head -n1)\" && [ -n \"$file_path\" ] && npx prettier --write --ignore-unknown -- \"$file_path\" 2>/dev/null || true"

Copilot uses AI. Check for mistakes.
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.

1 participant