Skip to content

chore: add prettier auto-format hook for Claude Code#1037

Open
dqnykamp wants to merge 1 commit intoDoenet:mainfrom
dqnykamp:chore/auto-format-hook
Open

chore: add prettier auto-format hook for Claude Code#1037
dqnykamp wants to merge 1 commit intoDoenet:mainfrom
dqnykamp:chore/auto-format-hook

Conversation

@dqnykamp
Copy link
Copy Markdown
Member

@dqnykamp dqnykamp commented May 1, 2026

Summary

  • Adds .claude/settings.json with a PostToolUse hook that runs prettier --write --ignore-unknown on files after Write/Edit/MultiEdit/NotebookEdit operations.
  • Enforces automatic formatting rather than relying on the agent to read and follow the "format with prettier before committing" instruction in CLAUDE.md — which was missed in refactor(worker-javascript): extract Phase 1 helpers from Core.js #1036.
  • Hook respects .prettierignore (auto-discovered by prettier) and only formats files inside the project directory.

Test plan

  • After merging, contributors using Claude Code on this repo should open /hooks once (or restart Claude Code) so the new settings take effect.
  • Verify by editing a .ts/.js file with formatting violations and confirming it gets auto-formatted on save.
  • Confirm .md and other ignored paths are skipped (already covered by .prettierignore).

🤖 Generated with Claude Code

Adds .claude/settings.json with a PostToolUse hook that runs prettier on
files after Write/Edit/MultiEdit/NotebookEdit operations. Ensures code is
formatted automatically rather than relying on the agent to follow the
"format with prettier before committing" instruction in CLAUDE.md (which
was missed in PR Doenet#1036).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 1, 2026

⚠️ No Changeset found

Latest commit: 930919d

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Copy Markdown

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

Adds a Claude Code hook configuration to automatically run Prettier after file write/edit operations, aiming to keep agent-driven edits consistently formatted in this repo.

Changes:

  • Add .claude/settings.json defining a PostToolUse hook for Write|Edit|MultiEdit|NotebookEdit.
  • Hook runs npx --no-install prettier --write --ignore-unknown on the edited file path (when it appears to be under $PWD).

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread .claude/settings.json
Comment on lines +1 to +3
{
"hooks": {
"PostToolUse": [
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

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

The PR description mentions relying on an instruction in CLAUDE.md, but there is no CLAUDE.md in this repository (the existing guidance appears to be in AGENTS.md). Please update the description to reference the correct file so future contributors can find the formatting requirement.

Copilot uses AI. Check for mistakes.
Comment thread .claude/settings.json
"hooks": [
{
"type": "command",
"command": "jq -r '.tool_response.filePath // .tool_input.file_path' | { read -r f; case \"$f\" in \"$PWD\"/*) npx --no-install prettier --write --ignore-unknown \"$f\" ;; esac; } 2>/dev/null || true",
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

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

The stderr redirection (2>/dev/null) is applied to the { ... } block but not to the jq command. If jq is missing or errors, it can still print noise to stderr even though the hook ultimately succeeds due to || true. Consider redirecting jq stderr as well (or checking for jq before running) so hook output stays clean.

Suggested change
"command": "jq -r '.tool_response.filePath // .tool_input.file_path' | { read -r f; case \"$f\" in \"$PWD\"/*) npx --no-install prettier --write --ignore-unknown \"$f\" ;; esac; } 2>/dev/null || true",
"command": "jq -r '.tool_response.filePath // .tool_input.file_path' 2>/dev/null | { read -r f; case \"$f\" in \"$PWD\"/*) npx --no-install prettier --write --ignore-unknown \"$f\" ;; esac; } 2>/dev/null || true",

Copilot uses AI. Check for mistakes.
Comment thread .claude/settings.json
"hooks": [
{
"type": "command",
"command": "jq -r '.tool_response.filePath // .tool_input.file_path' | { read -r f; case \"$f\" in \"$PWD\"/*) npx --no-install prettier --write --ignore-unknown \"$f\" ;; esac; } 2>/dev/null || true",
Copy link

Copilot AI May 1, 2026

Choose a reason for hiding this comment

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

The "$PWD" prefix check can be bypassed by paths containing .. segments (e.g., "$PWD/../outside") which still match the case pattern but resolve outside the repo. To enforce “only format files inside the project directory”, normalize with realpath (or an equivalent) before comparing, and only run Prettier when the normalized path is within the normalized repo root.

Suggested change
"command": "jq -r '.tool_response.filePath // .tool_input.file_path' | { read -r f; case \"$f\" in \"$PWD\"/*) npx --no-install prettier --write --ignore-unknown \"$f\" ;; esac; } 2>/dev/null || true",
"command": "jq -r '.tool_response.filePath // .tool_input.file_path' | { read -r f; repo_root=$(realpath \"$PWD\") && target=$(realpath \"$f\") && case \"$target\" in \"$repo_root\"|\"$repo_root\"/*) npx --no-install prettier --write --ignore-unknown \"$target\" ;; esac; } 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.

2 participants