Skip to content

feat: commit時にstageされたファイルのみ自動でformatとlintを実行するように#5047

Open
renkonmaster wants to merge 10 commits intomasterfrom
feat/add-precommit-hook
Open

feat: commit時にstageされたファイルのみ自動でformatとlintを実行するように#5047
renkonmaster wants to merge 10 commits intomasterfrom
feat/add-precommit-hook

Conversation

@renkonmaster
Copy link
Contributor

@renkonmaster renkonmaster commented Jan 21, 2026

概要

commit時に自動でformatとlintを実行するようにした
対象となるファイルはstageされたファイルのみ

なぜこの PR を入れたいのか

close: #4784

動作確認の手順

image

pre-commit の末尾に exit 1 を書き、commitしたときのLog

PR を出す前の確認事項

  • (機能の追加なら)追加することの合意がチームで取れている
    • 取れていない場合はチェックを外して PR にすれば OK
  • 動作確認ができている
  • 自分で一度コードを眺めて自分的に問題はなさそう

備考

@yas-ako とペアプロしました

Summary by CodeRabbit

  • Chores
    • 開発ワークフロー改善: コミット時に自動でフォーマットとリンティングを実行する仕組み(Husky + lint-staged)を導入し、コミットを品質ルールに従わせます。
  • Documentation
    • 開発ドキュメントを更新し、コミット時の自動整形・リンティングの設定と導入手順を追加しました。

@github-actions
Copy link

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements automatic formatting and linting on commit for staged files only, using lint-staged and husky. The goal is to ensure all commits are properly formatted and linted, eliminating the need for manual "lint & format" commits.

Changes:

  • Added lint-staged configuration to run eslint and prettier checks on staged files
  • Added husky prepare script to enable git hooks
  • Created pre-commit hook to execute lint-staged

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated 3 comments.

File Description
package.json Added "prepare" script for husky, added lint-staged dependency, and configured lint-staged rules for TypeScript/JavaScript and formatting
package-lock.json Added lint-staged and its dependencies (commander, listr2, micromatch, etc.)
.husky/pre-commit Created git pre-commit hook that executes npx lint-staged

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov
Copy link

codecov bot commented Jan 21, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 62.60%. Comparing base (249e7dc) to head (15473e6).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5047      +/-   ##
==========================================
- Coverage   62.77%   62.60%   -0.17%     
==========================================
  Files         108      108              
  Lines        3097     3097              
  Branches      630      630              
==========================================
- Hits         1944     1939       -5     
- Misses       1040     1048       +8     
+ Partials      113      110       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 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.

Copy link
Contributor

@uni-kakurenbo uni-kakurenbo left a comment

Choose a reason for hiding this comment

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

概ね良いかと思います.ありがとう

数件コメントしたので直してもらえると嬉しい


must
使い方を development.md とかに文書として書き置いてもらえると助かります.

@renkonmaster renkonmaster self-assigned this Jan 24, 2026
@coderabbitai
Copy link

coderabbitai bot commented Mar 2, 2026

📝 Walkthrough

Walkthrough

Husky と lint-staged を導入し、.husky/pre-commit フックでコミット時に npx lint-staged を実行してステージされたファイルへ ESLint と Prettier のチェック/自動修正を行う仕組みを追加した。

Changes

Cohort / File(s) Summary
Pre-commit フック
/.husky/pre-commit
新規追加。コミット時に npx lint-staged を実行する Husky pre-commit フックを追加。失敗時はコミットを中止。
lint-staged 設定
lint-staged.config.js
新規追加。*.{ts,js,jsx,tsx,vue} に対して ESLint(--fix, --cache, --max-warnings=0)を実行、その他のファイル群に対して Prettier を実行するマッピングを定義。
パッケージ設定
package.json
huskylint-staged を依存に追加およびスクリプト箇所に準備用スクリプトを追記(Husky 初期化関連)。
ドキュメント
docs/development.md
開発ドキュメントを更新し、コミット時の自動フォーマット/lint の設定と利用手順を追記。

Sequence Diagram(s)

sequenceDiagram
  participant Dev as 開発者
  participant Git as Git (commit)
  participant Husky as Husky (.husky/pre-commit)
  participant LintStaged as lint-staged
  participant Tools as ESLint/Prettier

  Dev->>Git: git commit
  Git->>Husky: run pre-commit hook
  Husky->>LintStaged: npx lint-staged
  LintStaged->>Tools: run ESLint/Prettier on staged files
  Tools-->>LintStaged: success / fail
  alt success
    LintStaged-->>Husky: exit 0
    Husky-->>Git: allow commit
  else fail
    LintStaged-->>Husky: non-zero exit
    Husky-->>Git: abort commit
  end
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The PR title clearly describes the main change: adding automatic formatting and linting for staged files at commit time, which matches the primary objective of the changeset.
Description check ✅ Passed The description follows the required template with all key sections completed: overview, reason, verification steps with screenshot, and confirmation checklist items marked.
Linked Issues check ✅ Passed The PR successfully implements the requirements from issue #4784: Husky pre-commit hook added, lint-staged configured with eslint and prettier for appropriate file types, and automatic format/lint on staged files at commit time.
Out of Scope Changes check ✅ Passed All changes are within scope: Husky setup, lint-staged configuration, package.json updates, and development documentation. No unrelated modifications detected.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/add-precommit-hook

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.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@package.json`:
- Line 29: The package.json "prepare" script is invalid because it uses
"husky:prepare"; update the "prepare" script value to "husky" so Husky v9 runs
correctly during npm install—locate the "prepare" key in package.json, replace
"husky:prepare" with "husky", and ensure no other scripts reference the old
"husky:prepare" token.

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 249e7dc and de9a059.

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json
📒 Files selected for processing (3)
  • .husky/pre-commit
  • lint-staged.config.js
  • package.json

@renkonmaster
Copy link
Contributor Author

ながらく放置していました 🙇‍♂️
再レビューお願いします

Copy link
Contributor

@uni-kakurenbo uni-kakurenbo left a comment

Choose a reason for hiding this comment

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

ありがとうございます。

一か所だけ気になったのでコメントしました


このリポジトリでは [husky](https://typicode.github.io/husky/) と [lint-staged](https://github.com/okonet/lint-staged) を利用して、コミット時にステージされたファイルへ自動でフォーマット(prettier)とlint(eslint --fix)が実行されます。

特別な操作は不要で、通常通りコミットするだけで自動的に修正・整形が行われます。
Copy link
Contributor

Choose a reason for hiding this comment

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

prepare:husky しなくても動くんだっけ

Copy link
Contributor Author

Choose a reason for hiding this comment

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

prepare:husky は必要でした.
prepare:husky は各自で実行する感じですか?それとも postinstall などに含めて、npm install 時に実行するようにしたほうがいいですか?

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@docs/development.md`:
- Line 19: 行のコマンド例に付いているシェルプロンプト記号を削除してください — マークダウンの該当行の "$ npm run
husky:prepare" をプレフィックスなしの "npm run husky:prepare" に置き換え、MD014
警告が出ないようコマンド例をプロンプト無し表記に統一してください。

ℹ️ Review info

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between de9a059 and 4cc01c2.

📒 Files selected for processing (2)
  • docs/development.md
  • package.json

@renkonmaster
Copy link
Contributor Author

一度 prepare:husky 実行する必要があることを development.md に加えました

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.

lint や format のチェックを pre-commit hook で行いたい

4 participants