LoginSignup
4
0

More than 3 years have passed since last update.

ESLintでautofixに対応していないルールをautofixする

Posted at

やりたいこと

ESLintでは、autofixに対応しているルールと、していないルールがあります。
たとえば
no-varはautofixされますが、
no-unused-varsはautofixされません。

勝手に修正されると困るものはautofixされない仕様なのでしょうが、
それでも「no-unused-varsを一括で修正したいなー」みたいなケースはあります。

eslint-plugin-autofixで解決

https://www.npmjs.com/package/eslint-plugin-autofix
これを使うと、標準ではautofixできないルールをautofixしてくれちゃいます!

pluginsの設定を入れて、
"autofix/ルール名"で列挙すればOK!

.eslintrc
{
  "plugins": ["autofix"],
  "rules": {
    "autofix/no-unused-vars": "error",
    "autofix/no-plusplus": "error"
  }
}
before
var hoge = 1
var fuga = 2
hoge++
console.log(hoge)

after
var hoge = 1

hoge+=1
console.log(hoge)

いい感じ!

補足

全部のルールに対応しているわけではないっぽくて、
試したらeqeqeqなんかはダメでしたねー。
ドキュメント読んでも、未対応ルールは良く分らなかった。
まずは試してみるのが良いと思います。

常に有効にしてしまうとコードが壊れることもありそうなので、
「ここぞ」というタイミングで使うのが良いのかなーと思います。

ではまた~。

4
0
1

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
4
0