Loading...
Alerts when code complexity exceeds thresholds in real-time
{
"hookConfig": {
"hooks": {
"notification": {
"script": "./.claude/hooks/code-complexity-alert-monitor.sh"
}
}
},
"scriptContent": "#!/usr/bin/env bash\n\n# Read the tool input from stdin\nINPUT=$(cat)\nTOOL_NAME=$(echo \"$INPUT\" | jq -r '.tool_name')\nFILE_PATH=$(echo \"$INPUT\" | jq -r '.tool_input.file_path // .tool_input.path // \"\"')\n\nif [ -z \"$FILE_PATH\" ]; then\n exit 0\nfi\n\n# Check if it's a supported file type\nif [[ \"$FILE_PATH\" == *.js ]] || [[ \"$FILE_PATH\" == *.jsx ]] || [[ \"$FILE_PATH\" == *.ts ]] || [[ \"$FILE_PATH\" == *.tsx ]] || [[ \"$FILE_PATH\" == *.py ]]; then\n echo \"🔍 Analyzing code complexity for $FILE_PATH...\" >&2\n \n # Check file exists\n if [ ! -f \"$FILE_PATH\" ]; then\n echo \"📁 File does not exist yet, skipping complexity analysis\" >&2\n exit 0\n fi\n \n # Line count analysis\n LINES=$(wc -l < \"$FILE_PATH\" 2>/dev/null || echo \"0\")\n if [ \"$LINES\" -gt 500 ]; then\n echo \"⚠️ COMPLEXITY: File exceeds 500 lines ($LINES lines) - consider splitting into smaller modules\" >&2\n elif [ \"$LINES\" -gt 300 ]; then\n echo \"📊 INFO: File is getting large ($LINES lines) - monitor for complexity\" >&2\n fi\n \n # Function/method count analysis\n FUNCTION_COUNT=$(grep -E '(function|def |const.*=>|class |async |export function)' \"$FILE_PATH\" 2>/dev/null | wc -l || echo \"0\")\n if [ \"$FUNCTION_COUNT\" -gt 20 ]; then\n echo \"⚠️ COMPLEXITY: Too many functions/methods ($FUNCTION_COUNT) - consider modularization\" >&2\n elif [ \"$FUNCTION_COUNT\" -gt 15 ]; then\n echo \"📊 INFO: High function count ($FUNCTION_COUNT) - good candidate for refactoring\" >&2\n fi\n \n # Nesting level analysis (rough estimate)\n OPEN_BRACES=$(grep -o '{' \"$FILE_PATH\" 2>/dev/null | wc -l || echo \"0\")\n CLOSE_BRACES=$(grep -o '}' \"$FILE_PATH\" 2>/dev/null | wc -l || echo \"0\")\n \n if [ \"$OPEN_BRACES\" -gt 50 ]; then\n echo \"⚠️ COMPLEXITY: High nesting detected ($OPEN_BRACES braces) - consider flattening logic\" >&2\n fi\n \n # Python-specific checks\n if [[ \"$FILE_PATH\" == *.py ]]; then\n INDENT_DEPTH=$(grep -E '^[ ]{12,}' \"$FILE_PATH\" 2>/dev/null | wc -l || echo \"0\")\n if [ \"$INDENT_DEPTH\" -gt 5 ]; then\n echo \"⚠️ COMPLEXITY: Deep indentation detected in Python code - consider function extraction\" >&2\n fi\n fi\n \n echo \"✅ Complexity analysis completed for $FILE_PATH\" >&2\nelse\n echo \"File $FILE_PATH is not a supported type for complexity analysis\" >&2\nfi\n\nexit 0"
}.claude/hooks/~/.claude/hooks/{
"hooks": {
"notification": {
"script": "./.claude/hooks/code-complexity-alert-monitor.sh"
}
}
}#!/usr/bin/env bash
# Read the tool input from stdin
INPUT=$(cat)
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name')
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')
if [ -z "$FILE_PATH" ]; then
exit 0
fi
# Check if it's a supported file type
if [[ "$FILE_PATH" == *.js ]] || [[ "$FILE_PATH" == *.jsx ]] || [[ "$FILE_PATH" == *.ts ]] || [[ "$FILE_PATH" == *.tsx ]] || [[ "$FILE_PATH" == *.py ]]; then
echo "🔍 Analyzing code complexity for $FILE_PATH..." >&2
# Check file exists
if [ ! -f "$FILE_PATH" ]; then
echo "📁 File does not exist yet, skipping complexity analysis" >&2
exit 0
fi
# Line count analysis
LINES=$(wc -l < "$FILE_PATH" 2>/dev/null || echo "0")
if [ "$LINES" -gt 500 ]; then
echo "⚠️ COMPLEXITY: File exceeds 500 lines ($LINES lines) - consider splitting into smaller modules" >&2
elif [ "$LINES" -gt 300 ]; then
echo "📊 INFO: File is getting large ($LINES lines) - monitor for complexity" >&2
fi
# Function/method count analysis
FUNCTION_COUNT=$(grep -E '(function|def |const.*=>|class |async |export function)' "$FILE_PATH" 2>/dev/null | wc -l || echo "0")
if [ "$FUNCTION_COUNT" -gt 20 ]; then
echo "⚠️ COMPLEXITY: Too many functions/methods ($FUNCTION_COUNT) - consider modularization" >&2
elif [ "$FUNCTION_COUNT" -gt 15 ]; then
echo "📊 INFO: High function count ($FUNCTION_COUNT) - good candidate for refactoring" >&2
fi
# Nesting level analysis (rough estimate)
OPEN_BRACES=$(grep -o '{' "$FILE_PATH" 2>/dev/null | wc -l || echo "0")
CLOSE_BRACES=$(grep -o '}' "$FILE_PATH" 2>/dev/null | wc -l || echo "0")
if [ "$OPEN_BRACES" -gt 50 ]; then
echo "⚠️ COMPLEXITY: High nesting detected ($OPEN_BRACES braces) - consider flattening logic" >&2
fi
# Python-specific checks
if [[ "$FILE_PATH" == *.py ]]; then
INDENT_DEPTH=$(grep -E '^[ ]{12,}' "$FILE_PATH" 2>/dev/null | wc -l || echo "0")
if [ "$INDENT_DEPTH" -gt 5 ]; then
echo "⚠️ COMPLEXITY: Deep indentation detected in Python code - consider function extraction" >&2
fi
fi
echo "✅ Complexity analysis completed for $FILE_PATH" >&2
else
echo "File $FILE_PATH is not a supported type for complexity analysis" >&2
fi
exit 0Notification hook not running on file write operations
Verify hook is registered under hooks.notification in .claude/config.json. Check script has executable permissions. Ensure jq is installed for JSON parsing from stdin input.
Complexity alerts not showing for TypeScript files
Check file extension matches supported types: .js, .jsx, .ts, .tsx, .py. Verify FILE_PATH extraction from tool_input using jq processes correctly. Test with echo command to debug input parsing.
False positives for brace count in JSX files
Hook counts all braces including JSX elements. Adjust thresholds in script for JSX-heavy files. Consider implementing AST-based complexity analysis instead of regex pattern matching.
File does not exist error during notification processing
Hook runs after tool execution but file may not be written yet. Add delay with sleep 1 before analysis. Check tool_name matches write or edit operations. Verify path resolution is absolute.
Python indentation warnings incorrect for valid code
Hook detects 12+ space indentation as deep nesting. Adjust threshold for projects using different indent styles. Use grep -E pattern to match your preferred indentation depth level.
Loading reviews...
Join our community of Claude power users. No spam, unsubscribe anytime.
Automated accessibility testing and compliance checking for web applications following WCAG guidelines
Automatically generates or updates API documentation when endpoint files are modified
Automatically formats code files after Claude writes or edits them using Prettier, Black, or other formatters