Fix lint hook: output additionalContext JSON so Claude sees errors in-context

Replaced the old inline command (which just printed raw text and exited
non-zero) with a small script at .claude/hooks/lint-on-edit.sh. When
oxlint finds errors, the script outputs JSON with hookSpecificOutput
.additionalContext, which explicitly injects the lint errors into
Claude's context so it fixes them immediately rather than missing a
background notification.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Chris Parsons 2026-03-24 21:50:36 -07:00
parent e62e9554c9
commit 286a9afbbe
2 changed files with 38 additions and 1 deletions

35
.claude/hooks/lint-on-edit.sh Executable file
View file

@ -0,0 +1,35 @@
#!/bin/bash
# Run oxlint on the edited file and inject errors back into Claude's context.
# Reads the PostToolUse JSON payload from stdin.
# Exits 0 silently on pass or non-TS file; outputs JSON additionalContext on failure.
set -euo pipefail
FILE=$(python3 -c "
import sys, json
d = json.load(sys.stdin)
print(d.get('tool_input', {}).get('file_path', ''))
")
# Only lint TypeScript files
case "$FILE" in
*.ts|*.tsx) ;;
*) exit 0 ;;
esac
cd /home/chris/Projects/brackt.com
OUTPUT=$(npm run lint:path -- "$FILE" 2>&1) && exit 0
# Lint failed — inject errors into Claude's context so it fixes them immediately
python3 -c "
import json, sys
out = sys.argv[1]
f = sys.argv[2]
print(json.dumps({
'hookSpecificOutput': {
'hookEventName': 'PostToolUse',
'additionalContext': 'Lint errors found in ' + f + ' — fix before continuing:\n' + out
}
}))
" "$OUTPUT" "$FILE"

View file

@ -6,7 +6,9 @@
"hooks": [
{
"type": "command",
"command": "python3 -c \"import sys,json; d=json.load(sys.stdin); f=d.get('tool_input',{}).get('file_path',''); print(f)\" | xargs -I{} sh -c 'case \"{}\" in *.ts|*.tsx) npm run lint:path -- \"{}\" 2>&1 ;; esac'"
"command": ".claude/hooks/lint-on-edit.sh",
"timeout": 30,
"statusMessage": "Linting..."
}
]
}