lint-staged で Git のステージングに上がったファイルのみを対象にする、なぜか .eslintignore が効かず .eslintrc.js の ignorePatterns は効く
pre-commit で lint / format するファイルの対象がアプリケーションの全ファイル(./src)になっている
- これは lint-staged の設定で叩く
yarn lint:fixのコマンドの引数に指定されているから
{
"scripts": {
"lint": "eslint --cache --ext .js,.ts,.tsx --ignore-path .gitignore --fix ./src",
"format": "prettier --check --ignore-path .gitignore --write \"**/*.+(js|json|ts|tsx)\"",
"lint:fix": "yarn lint & yarn format"
},
"lint-staged": {
"*.{js,ts,tsx}": "yarn lint:fix"
}
}
- ↑こんな感じだった
npm scripts で eslint の引数にファイルパスを渡さない
./srcという指定が原因で全ファイル対象になっていた- npm scripts で
./srcの指定を外す or lint-staged 専用の npm scripts を作る- 専用の npm scripts を作ってもよかったが内容がほぼ一緒なのでどうなの?という気もする
- が、ローカルの pre-commit だけでなく CI / GitHub Actions で走らせることも考えると別々にしたほうがよさそう
lint-staged.config.js の設定
- lint-staged.config.jsでlint-stagedの設定したときのメモ
- ここに書いたのと基本同じ
- JavaScript で設定ファイルを書くことで filenames を引数に取って使えたりするので、ここで Git のステージングに上がったファイルだけを対象にするようにできる
parserOptions.project のエラー
0:0 error Parsing error: "parserOptions.project" has been set for @typescript-eslint/parser- lint-staged.config.js をコミットしようとすると上記エラーが出る
.eslintrc.jsのコミットでも上記エラーが出る
- package.json の
"eslintIgnore"で設定していてもダメだった(lint-staged.config.js を作成する前は効いてた) .eslintignoreに移してもダメだった
Ignoring Code - ESLint - Pluggable JavaScript Linter
- 色々やって
.eslintrc.jsの ignorePatterns に ".eslintrc.js" と "lint-staged.config.js" を指定したらいけた - ただ公式ドキュメントには
Patterns defined in .eslintignore take precedence over the ignorePatterns property of config files.
DeepL で翻訳すると
.eslintignoreで定義されたパターンは、設定ファイルのignorePatternsプロパティより優先されます。
とあるが……?
結果原因はよくわからなかった
- とりあえず ignorePatterns に指定したらしてほしい動作をしてくれたのでこれにした
- が、なぜ
.eslintignoreでダメだったのかはよく分かっていない