feat(git-hooks): enhance pre-commit hook with whitelist support for console statements
Add whitelist functionality to debug checker to allow intentional console statements in specific files: - Add WHITELIST_FILES configuration for platform services and utilities - Update pre-commit hook to skip console pattern checks for whitelisted files - Support regex patterns in whitelist for flexible file matching - Maintain security while allowing legitimate debug code in platform services This resolves the issue where the hook was blocking commits due to intentional console statements in whitelisted files like WebPlatformService and CapacitorPlatformService.
This commit is contained in:
@@ -61,6 +61,22 @@ SKIP_PATTERNS=(
|
||||
"\.yaml$" # YAML config files
|
||||
)
|
||||
|
||||
# Files that are whitelisted for console statements
|
||||
# These files may contain intentional console.log statements that are
|
||||
# properly whitelisted with eslint-disable-next-line no-console comments
|
||||
WHITELIST_FILES=(
|
||||
"src/services/platforms/WebPlatformService.ts" # Worker context logging
|
||||
"src/services/platforms/CapacitorPlatformService.ts" # Platform-specific logging
|
||||
"src/services/platforms/ElectronPlatformService.ts" # Electron-specific logging
|
||||
"src/services/QRScanner/.*" # QR Scanner services
|
||||
"src/utils/logger.ts" # Logger utility itself
|
||||
"src/utils/LogCollector.ts" # Log collection utilities
|
||||
"scripts/.*" # Build and utility scripts
|
||||
"test-.*/.*" # Test directories
|
||||
".*\.test\..*" # Test files
|
||||
".*\.spec\..*" # Spec files
|
||||
)
|
||||
|
||||
# Logging level (debug, info, warn, error)
|
||||
LOG_LEVEL="info"
|
||||
|
||||
|
||||
@@ -18,6 +18,11 @@ DEFAULT_DEBUG_PATTERNS=(
|
||||
"<!-- debug"
|
||||
"debug.*="
|
||||
)
|
||||
DEFAULT_WHITELIST_FILES=(
|
||||
"src/services/platforms/WebPlatformService.ts"
|
||||
"src/services/platforms/CapacitorPlatformService.ts"
|
||||
"src/services/platforms/ElectronPlatformService.ts"
|
||||
)
|
||||
|
||||
# Load configuration from file if it exists
|
||||
load_config() {
|
||||
@@ -27,6 +32,7 @@ load_config() {
|
||||
PROTECTED_BRANCHES=()
|
||||
DEBUG_PATTERNS=()
|
||||
SKIP_PATTERNS=()
|
||||
WHITELIST_FILES=()
|
||||
|
||||
# Read protected branches
|
||||
while IFS= read -r line; do
|
||||
@@ -70,6 +76,20 @@ load_config() {
|
||||
done
|
||||
fi
|
||||
done < "$CONFIG_FILE"
|
||||
|
||||
# Read whitelist files
|
||||
while IFS= read -r line; do
|
||||
if [[ "$line" =~ ^WHITELIST_FILES=\( ]]; then
|
||||
while IFS= read -r line; do
|
||||
if [[ "$line" =~ ^\)$ ]]; then
|
||||
break
|
||||
fi
|
||||
if [[ "$line" =~ \"([^\"]+)\" ]]; then
|
||||
WHITELIST_FILES+=("${BASH_REMATCH[1]}")
|
||||
fi
|
||||
done
|
||||
fi
|
||||
done < "$CONFIG_FILE"
|
||||
fi
|
||||
|
||||
# Use defaults if config loading failed
|
||||
@@ -85,6 +105,9 @@ load_config() {
|
||||
SKIP_PATTERNS=("${DEFAULT_SKIP_PATTERNS[@]}")
|
||||
fi
|
||||
|
||||
if [[ ${#WHITELIST_FILES[@]} -eq 0 ]]; then
|
||||
WHITELIST_FILES=("${DEFAULT_WHITELIST_FILES[@]}")
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
@@ -110,6 +133,17 @@ should_skip_file() {
|
||||
return 1
|
||||
}
|
||||
|
||||
# Check if file is whitelisted for console statements
|
||||
is_whitelisted_file() {
|
||||
local file="$1"
|
||||
for whitelisted in "${WHITELIST_FILES[@]}"; do
|
||||
if [[ "$file" =~ $whitelisted ]]; then
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
# Main execution
|
||||
main() {
|
||||
# Load configuration
|
||||
@@ -159,6 +193,11 @@ main() {
|
||||
|
||||
# Check for debug patterns in the file
|
||||
for pattern in "${DEBUG_PATTERNS[@]}"; do
|
||||
# Skip console pattern checks for whitelisted files
|
||||
if [[ "$pattern" == "console\." ]] && is_whitelisted_file "$file"; then
|
||||
continue
|
||||
fi
|
||||
|
||||
# For new files, check the file content directly
|
||||
# For modified files, check the staged diff
|
||||
if [[ -f "$file" ]]; then
|
||||
|
||||
Reference in New Issue
Block a user