Improve console.log detection in pre-commit hook#2064
Conversation
Enhance console log check to report line numbers.
📝 WalkthroughWalkthroughThe pre-commit hook ChangesConsole Log Detection Enhancement
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Updates the check-console-log hook to scan modified JS/TS files for console.log usage at commit time and emit warnings with file + line numbers.
Changes:
- Added logic to enumerate modified
js/jsx/ts/tsxfiles and scan their contents forconsole.log. - Added warning output listing matching line numbers and a follow-up reminder message.
| process.stdin.on('end', () => { | ||
| try { | ||
| if (!isGitRepo()) { | ||
| process.stdout.write(data); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| const files = getGitModifiedFiles(['\\.tsx?$', '\\.jsx?$']) | ||
| .filter(f => fs.existsSync(f)) | ||
| .filter(f => !EXCLUDED_PATTERNS.some(pattern => pattern.test(f))); | ||
|
|
||
| let hasConsole = false; | ||
|
|
||
| for (const file of files) { | ||
| const content = readFile(file); | ||
| if (content) { | ||
| const lines = content.split('\n'); | ||
| const matchedLines = []; | ||
|
|
||
| lines.forEach((line, index) => { | ||
| if (line.includes('console.log')) { | ||
| matchedLines.push(index + 1); | ||
| } | ||
| }); | ||
|
|
||
| if (matchedLines.length > 0) { | ||
| log(`[Hook] WARNING: console.log found in ${file} on line(s): ${matchedLines.join(', ')}`); | ||
| hasConsole = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (hasConsole) { | ||
| log('[Hook] Remove console.log statements before committing'); | ||
| } | ||
| } catch (err) { | ||
| log(`[Hook] check-console-log error: ${err.message}`); | ||
| } | ||
|
|
||
| // Always output the original data | ||
| process.stdout.write(data); | ||
| process.exit(0); | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| process.stdin.on('end', () => { |
| lines.forEach((line, index) => { | ||
| if (line.includes('console.log')) { | ||
| matchedLines.push(index + 1); | ||
| } | ||
| }); |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@scripts/hooks/check-console-log.js`:
- Around line 83-84: There is a stray/extra closing brace and a duplicated
process.stdin.on('end', ...) handler causing a SyntaxError and duplicate logic;
open scripts/hooks/check-console-log.js, remove the second/duplicated
process.stdin.on('end', ...) block (the stale handler) and fix the unmatched
closing brace(s) so the remaining process.stdin.on('end', ...) callback and
surrounding closure/parens are balanced (ensure process.stdin.on('end', ...) and
any surrounding process.stdin.resume()/setEncoding calls remain intact and only
appear once).
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: ee73b36d-28c4-45c0-b199-da829315edb7
📒 Files selected for processing (1)
scripts/hooks/check-console-log.js
| } | ||
| }); |
There was a problem hiding this comment.
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
FILE="scripts/hooks/check-console-log.js"
echo "end-handler registrations:"
rg -n "process\\.stdin\\.on\\('end'" "$FILE"
echo
echo "context around parse-error region:"
nl -ba "$FILE" | sed -n '76,100p'Repository: affaan-m/ECC
Length of output: 251
🏁 Script executed:
#!/bin/bash
set -euo pipefail
FILE="scripts/hooks/check-console-log.js"
echo "end-handler registrations:"
rg -n "process\.stdin\.on\('end'" "$FILE"
echo
echo "node syntax check:"
node --check "$FILE"
echo
echo "context around 76-100:"
awk 'NR>=76 && NR<=100 {print NR ":" $0}' "$FILE"Repository: affaan-m/ECC
Length of output: 426
Fix the syntax error and remove the duplicated stdin end handler in scripts/hooks/check-console-log.js.
scripts/hooks/check-console-log.jsfails to parse:SyntaxError: Unexpected token '}'at line 83.- The hook registers
process.stdin.on('end', ...)twice: at line 40 and line 86 (stale/duplicated logic).
🧰 Tools
🪛 Biome (2.4.15)
[error] 83-84: Expected a statement but instead found '}
})'.
(parse)
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@scripts/hooks/check-console-log.js` around lines 83 - 84, There is a
stray/extra closing brace and a duplicated process.stdin.on('end', ...) handler
causing a SyntaxError and duplicate logic; open
scripts/hooks/check-console-log.js, remove the second/duplicated
process.stdin.on('end', ...) block (the stale handler) and fix the unmatched
closing brace(s) so the remaining process.stdin.on('end', ...) callback and
surrounding closure/parens are balanced (ensure process.stdin.on('end', ...) and
any surrounding process.stdin.resume()/setEncoding calls remain intact and only
appear once).
There was a problem hiding this comment.
2 issues found across 1 file
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="scripts/hooks/check-console-log.js">
<violation number="1" location="scripts/hooks/check-console-log.js:40">
P1: Duplicate `process.stdin.on('end')` handler left in file, creating unreachable dead code that will never execute and risks misleading future edits</violation>
<violation number="2" location="scripts/hooks/check-console-log.js:45">
P0: Remove the stray closing brace/paren pair; they make the hook file syntactically invalid.</violation>
</file>
Reply with feedback, questions, or to request a fix.
Re-trigger cubic
| if (!isGitRepo()) { | ||
| process.stdout.write(data); | ||
| process.exit(0); | ||
| } |
There was a problem hiding this comment.
P0: Remove the stray closing brace/paren pair; they make the hook file syntactically invalid.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At scripts/hooks/check-console-log.js, line 45:
<comment>Remove the stray closing brace/paren pair; they make the hook file syntactically invalid.</comment>
<file context>
@@ -37,6 +37,52 @@ process.stdin.on('data', chunk => {
+ if (!isGitRepo()) {
+ process.stdout.write(data);
+ process.exit(0);
+ }
+
+ const files = getGitModifiedFiles(['\\.tsx?$', '\\.jsx?$'])
</file context>
| @@ -37,6 +37,52 @@ process.stdin.on('data', chunk => { | |||
| } | |||
There was a problem hiding this comment.
P1: Duplicate process.stdin.on('end') handler left in file, creating unreachable dead code that will never execute and risks misleading future edits
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At scripts/hooks/check-console-log.js, line 40:
<comment>Duplicate `process.stdin.on('end')` handler left in file, creating unreachable dead code that will never execute and risks misleading future edits</comment>
<file context>
@@ -37,6 +37,52 @@ process.stdin.on('data', chunk => {
}
});
+process.stdin.on('end', () => {
+ try {
+ if (!isGitRepo()) {
</file context>
Enhance console log check to report line numbers.
What Changed
Why This Change
Testing Done
node tests/run-all.js)Type of Change
fix:Bug fixfeat:New featurerefactor:Code refactoringdocs:Documentationtest:Testschore:Maintenance/toolingci:CI/CD changesSecurity & Quality Checklist
Documentation
Summary by CodeRabbit
No end-user visible changes in this release.
Summary by cubic
Pre-commit hook now reports exact line numbers for any console.log in staged JS/TS files, making cleanups faster. It warns with file and line numbers but does not block commits.
console.login.js,.jsx,.ts, and.tsxfiles.Written for commit f2fbe65. Summary will update on new commits. Review in cubic