Skip to content

Add precommit hook for comments#12922

Draft
AustinMroz wants to merge 1 commit into
mainfrom
austin/comment-concerns
Draft

Add precommit hook for comments#12922
AustinMroz wants to merge 1 commit into
mainfrom
austin/comment-concerns

Conversation

@AustinMroz

Copy link
Copy Markdown
Collaborator

An early, aggressive POC: Forbids commits being created that contain both code changes and new comments.

I'm not terribly fond of the implementation. It's rather brittle and particularly vulnerable to false positives. It might be better to loosen the restrictions to only

  • multiline comments
  • in-dom comments

Which should also cut the implementation complexity by ~90%.

@github-actions

github-actions Bot commented Jun 17, 2026

Copy link
Copy Markdown

🎭 Playwright: ✅ 1465 passed, 0 failed · 3 flaky

📊 Browser Reports
  • chromium: View Report (✅ 1444 / ❌ 0 / ⚠️ 3 / ⏭️ 5)
  • chromium-2x: View Report (✅ 2 / ❌ 0 / ⚠️ 0 / ⏭️ 0)
  • chromium-0.5x: View Report (✅ 1 / ❌ 0 / ⚠️ 0 / ⏭️ 0)
  • mobile-chrome: View Report (✅ 18 / ❌ 0 / ⚠️ 0 / ⏭️ 0)

@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

A new pre-commit script (scripts/check-comment-commits.ts) is added that parses the staged git diff, classifies each added/removed line as comment or code using a stateful lexer, and exits with code 1 if a commit mixes new comment additions with any code changes. The Husky pre-commit hook is updated to invoke this check with set -e for fail-fast behavior. A Vitest suite covers all classification and enforcement scenarios.

Changes

Comment-only commit enforcement

Layer / File(s) Summary
Comment syntax contracts and file-type detection
scripts/check-comment-commits.ts, scripts/check-comment-commits.test.ts
CommentSyntax interface and commentSyntaxForFile map .ts/.js, .vue, and .css extensions to their comment capabilities; tests verify Vue HTML comment enablement, CSS line-comment disabling, and null for unsupported extensions.
Stateful line scanner
scripts/check-comment-commits.ts, scripts/check-comment-commits.test.ts
ScanState, freshScanState, LineScan, and scanLine implement a lexer that tracks block comment, Vue HTML comment, and template literal state across lines, classifying each as comment-only, code-only, or mixed. Tests cover full-line/trailing inline comments, comment markers inside strings and regex, and multi-line block comment state propagation.
Diff parsing, violation detection, and CLI entry point
scripts/check-comment-commits.ts, scripts/check-comment-commits.test.ts
analyzeStagedDiff parses unified diff hunk headers to align new-file line numbers, maintains independent scan states for +/- lines, and sets violation when comment additions coexist with any code change. getStagedDiff, reportViolation, main, and invokedDirectly form the CLI layer. Tests cover mixed/pure comment/pure code commits, cross-file mixing, removed-code-as-code-change, non-code file ignoring, and hunk-header line number mapping.
Pre-commit hook wiring
.husky/pre-commit
Adds set -e and appends pnpm exec tsx scripts/check-comment-commits.ts to the hook's command sequence.

Sequence Diagram(s)

sequenceDiagram
  participant Git as Git commit
  participant Hook as .husky/pre-commit
  participant LintStaged as lint-staged
  participant CheckComments as check-comment-commits.ts
  participant GitDiff as git diff --cached

  Git->>Hook: trigger pre-commit
  Hook->>LintStaged: pnpm exec lint-staged
  LintStaged-->>Hook: pass
  Hook->>CheckComments: pnpm exec tsx
  CheckComments->>GitDiff: run git diff --cached
  GitDiff-->>CheckComments: staged diff text
  CheckComments->>CheckComments: analyzeStagedDiff (scanLine per hunk)
  alt comment-only or code-only commit
    CheckComments-->>Hook: exit 0
    Hook-->>Git: commit proceeds
  else mixed comment + code changes
    CheckComments-->>Hook: stderr report + exit 1
    Hook-->>Git: commit blocked
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐇 Hop hop, no sneaky notes in code piles,
The rabbit checks each diff for comment guiles.
set -e keeps the hook from going astray,
Mix code and comments? Not on my watch today!
Clean commits only — the warren approves. 🥕

🚥 Pre-merge checks | ✅ 5 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description lacks required template sections: no Summary, Changes, Review Focus, or proper structure. The description is incomplete and does not follow the repository's template. Fill in all required template sections: provide a one-sentence summary explaining what changed and why, document the specific changes (What and any Breaking changes), add Dependencies if applicable, and include Review Focus with design decisions or edge cases requiring attention.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Add precommit hook for comments' clearly and concisely describes the main change—adding a pre-commit hook to enforce comment-related restrictions.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
End-To-End Regression Coverage For Fixes ✅ Passed PR is not a bug fix (commit subject: "Add precommit hook for comments", PR describes as "POC"; no bug-fix language). Changes are in .husky/ and scripts/, not src/ or packages/. E2E test requirement...
Adr Compliance For Entity/Litegraph Changes ✅ Passed ADR compliance check does not apply—no changes to src/lib/litegraph/, src/ecs/, or graph entity files.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch austin/comment-concerns

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@codecov

codecov Bot commented Jun 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

@@             Coverage Diff             @@
##             main   #12922       +/-   ##
===========================================
- Coverage   76.63%   62.15%   -14.49%     
===========================================
  Files        1568     1458      -110     
  Lines      106246    75063    -31183     
  Branches    32353    21146    -11207     
===========================================
- Hits        81424    46652    -34772     
- Misses      23945    28068     +4123     
+ Partials      877      343      -534     
Flag Coverage Δ
e2e ?
unit 62.15% <ø> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.
see 1176 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
scripts/check-comment-commits.ts (1)

53-168: 💤 Low value

Regex literal parsing is not handled, but acceptable for this use case.

The scanner doesn't parse regex literals (/pattern/), so constructs like const re = /foo\/bar/ would be parsed character-by-character rather than as a single token. However, since the scanner only looks for // and /* comment markers, this doesn't cause false positives in practice unless a regex contains those exact sequences. Given the PR acknowledges this is a "brittle" POC, this limitation is reasonable.

The backslash handling at lines 108-112 (outside strings) primarily handles line continuations and incidentally helps with some regex escapes, though it's not semantically correct for all contexts.

🤖 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/check-comment-commits.ts` around lines 53 - 168, The scanLine
function does not handle regex literal parsing, which means constructs like
regex patterns with escaped forward slashes could theoretically cause issues if
they contain comment markers. While the reviewer acknowledges this is acceptable
for the current POC use case since the scanner only looks for // and /*
patterns, consider adding a code comment above the scanLine function or near the
backslash handling logic (around the character-by-character iteration) to
explicitly document this known limitation and explain why it's acceptable for
this scanner's purpose. This will help future maintainers understand the
intentional design decision.
🤖 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.

Nitpick comments:
In `@scripts/check-comment-commits.ts`:
- Around line 53-168: The scanLine function does not handle regex literal
parsing, which means constructs like regex patterns with escaped forward slashes
could theoretically cause issues if they contain comment markers. While the
reviewer acknowledges this is acceptable for the current POC use case since the
scanner only looks for // and /* patterns, consider adding a code comment above
the scanLine function or near the backslash handling logic (around the
character-by-character iteration) to explicitly document this known limitation
and explain why it's acceptable for this scanner's purpose. This will help
future maintainers understand the intentional design decision.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 83d1f0b6-76b7-44f0-8f94-050d8c4a3147

📥 Commits

Reviewing files that changed from the base of the PR and between 6850d22 and 3d261e0.

📒 Files selected for processing (3)
  • .husky/pre-commit
  • scripts/check-comment-commits.test.ts
  • scripts/check-comment-commits.ts

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.

1 participant