バージョン
「Reactの勉強」で使用している主なソフトウェアのバージョンは以下の通り。
- node: 15.5.0
- react-create-app: 4.0.1
- react: 17.0.1
- react-dom: 17.0.1
- typescript: 4.1.3
- eslint: 7.17.0
- prettier: 2.2.1
設定
Prettierをインストールする
以下のコマンドを実行してPrettierをインストールする。
$ npm install -D prettier $ npm install -D eslint-plugin-prettier $ npm install -D eslint-config-prettier
typesync
を実行する。
$ node_modules/typesync/bin/typesync
ESLintとPrettierが衝突していないかの確認
本には以下のコマンドで確認できると書いてあったが、公式ドキュメントによると、eslint-config-prettierのバージョン7.0.0移行ではこのやり方ではないらしい。(私の環境のバージョンは7.1.0)
$ npx eslint --print-config .eslintrc.js | npx eslint-config-prettier-check
バージョン7.0.0以降では、任意の対象ファイルに対して以下のコマンドを実行するようである。
$ npx eslint-config-prettier path/to/main.js
※ 原理的には全ての対象ファイルに対して上記のコマンドを実行しないと、衝突していないことを100%担保したとはいえないが、フォーマットルールが共通なら、1ファイルサンプリングして実行してOKなら良いのでは?とのことのようである。
In theory you need to run the tool for every single file in your project to be 100% sure that there are no conflicting rules, because ESLint supports having different rules for different files. But usually you’ll have about the same rules for all files, so it is good enough to run the command on one file. But if you use multiple configuration files or overrides, you can provide several files check:
試しに以下の2ファイルに対して実行してみたが、衝突はなかった。
$ npx eslint-config-prettier index.tsx App.tsx No rules that are unnecessary or conflict with Prettier were found.