Help us understand the problem. What is going on with this article?

VSCode + ESLint + Prettier + React + TypeScript (自分用メモ Fall, 2020)

はじめに

目標

前提

  • Node.jsYarnはインストール済みとします(Yarnを使います)

ESLint, Prettier, TypeScript, React のインストール

プロジェクト・フォルダの作成

bash
$ mkdir qiita && cd $_
$ yarn init -y
yarn init v1.22.10
warning The yes flag has been set. This will automatically answer yes to all questions, which may have security implications.
success Saved package.json
✨  Done in 0.04s.

ESLint と Prettier

bash
$ yarn add -D eslint prettier eslint-config-prettier

TypeScript 関連

bash
$ yarn add -D typescript @typescript-eslint/{parser,eslint-plugin}

React 関連

bash
$ yarn add react react-dom
$ yarn add -D @types/{react,react-dom}
$ yarn add -D eslint-plugin-{react,react-hooks}

tsconfig.json

tsconfig.json
{
  "compilerOptions": {
    "target": "ES2015",
    "module": "ES2015",
    "lib": ["DOM", "ES2015"],
    "jsx": "react",
    "strict": true,
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "baseUrl": ".",
    "paths": {
      "*": ["@types/*"]
    }
  }
}

.eslintrc.json

.eslintrc.json
{
  // 適用する環境
  "env": {
    "es6": true,
    "node": true,
    "browser": true,
    "commonjs": true
  },
  // パーサー
  "parser": "@typescript-eslint/parser",
  // jsx を使います
  "parserOptions": {
    "ecmaVersion": 2018,
    "ecmaFeatures": {
      "jsx": true
    },
    "sourceType": "module"
  },
  // React のバージョンは自動検出に
  "settings": {
    "react": {
      "version": "detect"
    }
  },
  "plugins": ["react-hooks", "react", "@typescript-eslint"],
  // 基本的にルールは recommended に従う
  // prettier 関連は配列の最後尾に書く
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:react/recommended",
    "plugin:react-hooks/recommended",
    "prettier",
    "prettier/react",
    "prettier/@typescript-eslint"
  ],
  "rules": {
    // TypeScirpt なので prop-types は要らない
    "react/prop-types": "off"
  },
  // .js ファイルをオーバーライド (webpack.conig.jsなど)
  "overrides": [
    {
      "files": ["*.js"],
      "rules": {
        "@typescript-eslint/no-var-requires": "off",
        "@typescript-eslint/explicit-function-return-type": "off"
      }
    }
  ]
}

注意点: extends では prettier 関連を配列の最後部に記述すること!

.prettierrc.json

prettierのオプションはこちら(少ない!)。

.prettierrc.json
{
  "singleQuote": true,
  "jsxBracketSameLine": true
}

VSCodeの設定

ESLint, Prettier 拡張をインストール

.vscode/settings.json

settings.json
{
  "editor.formatOnSave": true,    // <-- prettierで整形
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true  // <-- eslintでリント
  },
  // デフォルトフォーマッタをprettierに
  "editor.defaultFormatter": "esbenp.prettier-vscode"
}

editor.defaultFormatter がキモ?
editor.formatOnSave でPrettierの整形が効かない時は大抵これを忘れてます。

謝々

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away