ESLint
Linter for JavaScript and TypeScript
計測
TIMING=1 npm run lint
plugin と extends
extends はルールを extend する
on/off を切り替えたり
Configuration Files - ESLint - Pluggable JavaScript Linter
plugin はいろいろできる?
Plugins - ESLint - Pluggable JavaScript Linter
off, warn, error をなんと数字でかけてしまうという仕様でいつもわからなくなるときに読むやつ
Getting Started with ESLint - ESLint - Pluggable JavaScript Linter
Ignoring Code - ESLint - Pluggable JavaScript Linter
node_modules と dotfiles dotdirectories はデフォルトで ignore される
monorepo の場合、一元管理するかディレクトリごとにするか
it looks in the current working directory to find an .eslintignore file
いろいろな状況があるとは思うが、root の eslint 使っている場合はそれ以下のディレクトリに ignore file があっても認識されない
ignore file をわけるのであれば、それぞれのディレクトリで eslint を実行する必要がある
読んでいないが share config も関係あるかも
Share Configurations - ESLint - Pluggable JavaScript Linter
サブディレクトリに root: true が設定された .eslintrc があると、実際のプロジェクトルートで実行する eslint が効かない
disable
https://eslint.org/docs/user-guide/configuring/rules#using-configuration-comments-1
eslint コマンドには file か directory を指定する必要がある
config file にはどこのを対象にするかのディレクティブはないと思われる
Getting Started with ESLint - ESLint - Pluggable JavaScript linter
設定は書く必要がある
面倒なので、extends に eslint:recommended とかできる
内容
List of available rules - ESLint - Pluggable JavaScript linter
他にも airbnb が出している extend がある
airbnb recommendation
https://eslint.org/docs/rules/no-restricted-syntax
for in for of 非推奨
forEach 使えばよし
TypeScript で使う場合はプラグイン
Linting your TypeScript Codebase | TypeScript ESLint
下記設定もおそらくこのプラグインで場合によって必要になるもの
code:eslint.rc
parserOptions: {
project: './tsconfig.eslint.json',
}
type-aware linting する場合は、含めるファイルを指定する必要がある
Linting with Type Information | TypeScript ESLint
tsconfigRootDir
環境によっては parseOptions に指定した ts 用のファイルを見つけられない場合がある
e.g. vscode プラグイン
working directory からの相対パスと評価されるため
解決するには tsconfigRootDir: __dirname を追加
https://stackoverflow.com/questions/64933543/parsing-error-cannot-read-file-tsconfig-json-eslint
__dirname は node.js の環境変数
vscode の場合 eslint plugin の設定でも解決できる
props spreading について
Notes for use: Filter out unnecessary props when possible.
https://airbnb.io/javascript/react/#props
不要な props は取り除くと良い
Rule を確認するには
eslint-config-airbnbのルール内容を確認する - dackdive's blog
React Native 向けに facebook が作っている config が存在する
react-native/packages/eslint-config-react-native-community at 5449148482271d60c87f6e5fa4483e69d4410320 · facebook/react-native
Prettier と組み合わせる場合は公式のドキュメントを参照する
Integrating with Linters · Prettier
git hooks で実行
lint-staged を使う場合
Pre-commit Hook · Prettier
Husky - Git hooks
code:shell
npm install --save-dev lint-staged
npx husky-init && npm install