各種プラグインのインストール
yarnの場合
yarn add -D prettier eslint typescript-eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-plugin-prettier eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-import
npmの場合
npm install --save-dev prettier eslint typescript-eslint @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-plugin-prettier eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-import
設定ファイルを記述する
root/.eslintrc.json
{
"extends": [
"next",
"next/core-web-vitals",
"eslint:recommended",
"plugin:prettier/recommended",
"plugin:react/recommended",
"plugin:react-hooks/recommended",
"plugin:@typescript-eslint/recommended",
"plugin:import/recommended",
"plugin:import/typescript"
],
"rules": {
"react/react-in-jsx-scope": "off",
"import/order": [2, { "alphabetize": { "order": "asc" } }],
"prettier/prettier": [
"error",
{
"trailingComma": "all",
"endOfLine": "lf",
"semi": false,
"singleQuote": true,
"printWidth": 80,
"tabWidth": 2
}
]
}
}
root/package.json
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint --dir src",
"format": "next lint --fix --dir src"
},
使い方
% npm run lint // リント実施
% npm run format // リントエラーを自動フォーマット