ESLintで不使用のimport自動削除

その他

前提

  • vscode使用
  • vscodeのESLintプラグイン使用

インストール

npm i -D eslint

続いて

npm init @eslint/config

質問に答えていく

Need to install the following packages:
  @eslint/create-config
Ok to proceed? (y) y
✔ How would you like to use ESLint? · problems
✔ What type of modules does your project use? · esm
✔ Which framework does your project use? · react
✔ Does your project use TypeScript? · No / Yes
✔ Where does your code run? · browser
✔ What format do you want your config file to be in? · JSON
The config that you've selected requires the following dependencies:


eslint-plugin-react@latest @typescript-eslint/eslint-plugin@latest @typescript-eslint/parser@latest
✔ Would you like to install them now? · No / Yes
✔ Which package manager do you want to use? · npm

unused-importsのインストール

npm i -D eslint-plugin-unused-imports

.eslintrc.jsonに設定を追加。

  "plugins": [
    "unused-imports"
   ],
   "rules": {
     "unused-imports/no-unused-imports": "warn"
   },

これで、lint走る度に不使用のimportが自動で削除されます。

importを触る手間が省けるので、個人的にはおすすめの設定です!