Skip to content

chore: add pre-commit hook to auto-format and lint staged .ts files#21

Merged
t2tx merged 2 commits into
mainfrom
chore/lefthook-precommit
Jun 22, 2026
Merged

chore: add pre-commit hook to auto-format and lint staged .ts files#21
t2tx merged 2 commits into
mainfrom
chore/lefthook-precommit

Conversation

@t2tx

@t2tx t2tx commented Jun 21, 2026

Copy link
Copy Markdown
Owner

Summary

Adds a pre-commit lefthook job that runs prettier --write + eslint --fix on staged *.ts files and re-stages them.

Why

  • Commits are formatted/linted on the way in, so they're less likely to fail the pre-push verify (format:check / lint).
  • pre-push (pnpm verify) is unchanged.

Verification

  • lefthook dump parses the config; lefthook run pre-commit skips when no *.ts is staged (glob works).
  • No functional change — tooling only, so no version bump.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Chores
    • Added automated pre-commit checks for TypeScript code formatting and linting.

pre-commit runs prettier --write + eslint --fix on staged *.ts and re-stages
them, so commits are already formatted/linted and less likely to fail the
pre-push verify (format:check / lint). pre-push verify is unchanged.

No functional change — tooling only.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
@coderabbitai

coderabbitai Bot commented Jun 21, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 43f3a7b1-06ae-488c-8a11-19c922bc4386

📥 Commits

Reviewing files that changed from the base of the PR and between 357885e and d95beb2.

📒 Files selected for processing (1)
  • lefthook.yml
🚧 Files skipped from review as they are similar to previous changes (1)
  • lefthook.yml

Walkthrough

A pre-commit hook section is added to lefthook.yml with two jobs: format (runs pnpm prettier --write on staged *.ts files and re-stages them) and lint (runs pnpm eslint --fix on staged *.ts files and re-stages them). The existing pre-push configuration is unchanged.

Changes

Pre-commit Lefthook Configuration

Layer / File(s) Summary
Pre-commit format and lint jobs
lefthook.yml
Adds a pre-commit section with format and lint jobs that run pnpm prettier --write and pnpm eslint --fix respectively on staged *.ts files, followed by git add {staged_files} to persist auto-fixes.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: adding a pre-commit hook for auto-formatting and linting TypeScript files. It is concise, clear, and directly related to the primary purpose of the pull request.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch chore/lefthook-precommit

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

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 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 `@lefthook.yml`:
- Line 4: The glob patterns in the lefthook.yml file for both the format and
lint jobs use `*.ts` which only matches TypeScript files in the root directory
and does not match files in subdirectories like src/. Update both glob pattern
declarations (in the format job at line 4 and in the lint job at line 7) from
`*.ts` to `**/*.ts` to match TypeScript files at any depth in the directory
structure, ensuring that staged files in src/ subdirectories are properly
processed by the pre-commit hooks.
- Around line 1-9: The glob patterns in both the format and lint jobs under
pre-commit are too restrictive and only match TypeScript files in the repository
root. Update the glob pattern from "*.ts" to "**/*.ts" (or "src/**/*.ts" if you
want to be more specific) in both the format job and lint job to ensure that
TypeScript files in subdirectories like src/ are also matched and processed
during the pre-commit hook.
🪄 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 5386c64b-f1a9-4a66-98ac-a6ae4ae4e589

📥 Commits

Reviewing files that changed from the base of the PR and between 354ae1e and 357885e.

📒 Files selected for processing (1)
  • lefthook.yml

Comment thread lefthook.yml
Comment thread lefthook.yml Outdated
CodeRabbit flagged "*.ts" as not matching nested files. Empirically it does
match staged src/*.ts under lefthook 2.1.9 (glob is applied per file name),
so this is not a behavior fix — but **/*.ts states the intent explicitly and
is robust to future glob_matcher changes.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
@t2tx

t2tx commented Jun 22, 2026

Copy link
Copy Markdown
Owner Author

@coderabbitai Updated both globs to **/*.ts.

A note for the record: I tested the original *.ts against a staged src/format.ts under lefthook 2.1.9 and it did match (format + eslint ran on it) — lefthook applies the glob per staged file name, so root-only matching doesn't apply here. So this wasn't a behavior fix. That said, **/*.ts makes the "any depth" intent explicit and is robust to future glob_matcher changes, so I've adopted it.

@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown

Tip

For best results, initiate chat on the files or code changes.

@t2tx: Thanks for the update and the detailed clarification! Good to know that lefthook 2.1.9 applies globs against the bare filename rather than the full path, so *.ts was already matching src/format.ts correctly. The switch to **/*.ts still reads better as a statement of intent and guards against any future glob_matcher changes, so it's a net improvement even if it wasn't a functional fix.


✏️ Learnings added
Learnt from: t2tx
URL: https://github.com/t2tx/iris/pull/21

Timestamp: 2026-06-22T01:31:25.151Z
Learning: In lefthook 2.1.9, the `glob` filter in a job configuration is applied against each staged file's **basename** (not the full path). This means `*.ts` already matches files in subdirectories (e.g., `src/format.ts`), so root-only matching is not a concern. Using `**/*.ts` is still preferred for explicit "any depth" intent and robustness against future `glob_matcher` changes, but switching from `*.ts` to `**/*.ts` in a lefthook configuration is a clarity improvement, not a behavior fix.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

@t2tx t2tx merged commit 45f5391 into main Jun 22, 2026
5 checks passed
@t2tx t2tx deleted the chore/lefthook-precommit branch June 22, 2026 01:33
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