72
58

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

JS(React)でコード整形 (ESlint+Prettier)

Last updated at Posted at 2019-02-01

いろいろ調べてて疲れたので、とりあえず結論だけまとめる

方針

色々あるけど、以下の方針が良さそう。

入れるものは以下

npm install --save-dev eslint prettier eslint-config-prettier eslint-plugin-prettier eslint-plugin-react husky lint-staged

設定

以下を既存の{}の中に追加

package.json
  "husky": {
    "hooks": {
      "pre-commit": "lint-staged"
    }
  },
  "lint-staged": {
    "*.{js,jsx}": ["eslint --fix", "git add"]
  },
.eslintrc.js
{
  "env": {
    "es6": true,
    "node": true
  },
  "parser": "babel-eslint",
  "plugins": [
    "react",
    "prettier"
  ],
  "parserOptions": {
    "version": 2018,
    "sourceType": "module",
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "extends": [
    "eslint:recommended",
    "plugin:react/recommended",
    "plugin:prettier/recommended",
    "prettier/react"
  ],
  "rules": {
    "prettier/prettier": "error"
  }
}

prettierの設定は.eslintrc.jsにかいたplugin:prettier/recommendedのおかげで.prettierrcも呼んでくれるようになっているので全てここでする。中身はここに書いてるのはあくまで参考なのでお好きに。

.prettierrc
{
  "printWidth": 120,
  "useTabs": false,
  "semi": true,
  "singleQuote": false,
  "trailingComma": "es5",
  "bracketSpacing": true,
  "jsxBracketSameLine": false
}

VS Codeの設定

{
  "javascript.format.enable": false,
  "eslint.autoFixOnSave": true
}

わかっているDeprecated情報

いくつかのソースを調べていた時に書き方が分かれていて混乱したので。どちらがいいかわかったものは書いておく

{
  "scripts": {
-   "precommit": "npm test",
-   "commitmsg": "commitlint -E GIT_PARAMS"
  },
+ "husky": {
+   "hooks": {
+     "pre-commit": "npm test",
+     "commit-msg": "commitlint -E HUSKY_GIT_PARAMS"
+   }
+ }
}
72
58
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
72
58

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?