最近使いまわしている設定ファイルの内容を自分用に記録。

パッケージインストール

今回の構成に必要なパッケージをインストールするには以下を実行。

npm i -D \
  typescript \
  prettier \
  eslint \
  @typescript-eslint/parser \
  @typescript-eslint/eslint-plugin \
  eslint-config-airbnb \
  eslint-config-airbnb-typescript \
  eslint-config-next \
  eslint-config-prettier \
  eslint-plugin-import \
  eslint-plugin-jsx-a11y \
  eslint-plugin-react \
  eslint-plugin-react-hooks

Next.js 関連

next.config.js

module.exports = {
  reactStrictMode: true,
  trailingSlash: true,
};

TypeScript 関連

tsconfig.json

{
  "compilerOptions": {
    "baseUrl": ".",
    "target": "esnext",
    "module": "esnext",
    "jsx": "preserve",
    "lib": ["dom", "dom.iterable", "esnext"],
    "moduleResolution": "node",
    "noEmit": true,
    "allowJs": true,
    "sourceMap": true,
    "removeComments": true,
    "skipLibCheck": true,
    "allowSyntheticDefaultImports": true,
    "forceConsistentCasingInFileNames": true,
    "esModuleInterop": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noImplicitReturns": true,
    "noImplicitOverride": true,
    "incremental": false
  },
  "exclude": ["node_modules", ".next", "out"],
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"]
}

参考:設定値の一覧

tsconfig.eslint.json

{
  "extends": "./tsconfig.json",
  "include": ["**/*.js", "**/*.jsx", "**/*.ts", "**/*.tsx"]
}

ESLint 関連

.eslintrc.json

{
  "root": true,
  "env": {
    "browser": true,
    "es2021": true
  },
  "settings": {
    "react": {
      "version": "detect"
    }
  },
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "project": ["./tsconfig.eslint.json"],
    "sourceType": "module",
    "ecmaVersion": "latest",
    "ecmaFeatures": { "jsx": true }
  },
  "plugins": ["@typescript-eslint", "import", "react", "react-hooks", "jsx-a11y"],
  "extends": [
    "airbnb",
    "airbnb-typescript",
    "airbnb/hooks",
    "eslint:recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:@typescript-eslint/recommended-requiring-type-checking",
    "plugin:react/recommended",
    "plugin:react-hooks/recommended",
    "plugin:react/jsx-runtime",
    "plugin:jsx-a11y/recommended",
    "plugin:import/recommended",
    "plugin:import/typescript",
    "next",
    "next/core-web-vitals",
    "prettier"
  ],
  "rules": {
    "@next/next/no-img-element": "off",
    "react/no-unescaped-entities": "off",
    "react/jsx-props-no-spreading": "off",
    "react/require-default-props": "off",
    "react/display-name": "off",
    "@typescript-eslint/ban-ts-comment": "off",
    "@typescript-eslint/no-use-before-define": "off",
    "@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
    "@typescript-eslint/consistent-type-assertions": [
      "error",
      { "assertionStyle": "as", "objectLiteralTypeAssertions": "never" }
    ],
    "@typescript-eslint/consistent-type-definitions": ["error", "type"],
    "@typescript-eslint/consistent-type-imports": "error",
    "import/prefer-default-export": "off",
    "import/exports-last": "error",
    "import/order": [
      "error",
      {
        "groups": [
          "builtin",
          "external",
          "internal",
          "parent",
          "sibling",
          "index",
          "object",
          "type"
        ],
        "alphabetize": { "order": "asc" },
        "newlines-between": "never"
      }
    ],
    "sort-imports": ["error", { "ignoreDeclarationSort": true, "ignoreMemberSort": false }],
    "arrow-body-style": "off",
    "no-alert": "off"
  }
}

参考:プリセットの内容

.eslintignore

node_modules/
.next/
out/

参考:書き方の例

Prettier 関連

.prettierrc.json

{
  "printWidth": 100,
  "singleQuote": true,
  "trailingComma": "all"
}

参考:設定値の一覧

.prettierignore

node_modules/
.next/
out/

参考:書き方の例

package.json 関連

package.json

scripts のフィールドにリント実行用のコマンドを追加

{
  "scripts": {
    "lint:tsc": "tsc --noEmit",
    "lint:eslint": "eslint src --ext .js,.jsx,.ts,.tsx",
    "fix:eslint": "eslint src --ext .js,.jsx,.ts,.tsx --fix",
    "lint:prettier": "prettier --check src",
    "fix:prettier": "prettier --write src",
    "lint:all": "npm run lint:tsc && npm run lint:eslint && npm run lint:prettier"
  }
}