Easily sort imports and remove unused declarations on each save in VS Code

Damir Drempetić - May 7 '21 - - Dev Community

All this time I was using VSCode formatting on save which triggers the selected default formatter (Prettier).

Also I was organizing and sorting my imports manually all the time. Or in other words, I didn’t. So foolish…

Because it couldn’t be more simpler to enable it without installation of any additional ESLint or Prettier plugins. Of course, I’m assuming you already have ESLint and Prettier VS Code extensions because they are a must have for any JavaScript project.

Just add the following to your VS Code settings.json:

 ...
 "editor.formatOnSave": true,
 "editor.codeActionsOnSave": {
   "source.fixAll": true,
   "source.organizeImports": true,
   "source.sortMembers": true
 }
Enter fullscreen mode Exit fullscreen mode

fixAll is not necessary for this case but could be useful too.

fixAll property turns on Auto Fix for all providers including ESLint, according to VS Code Docs, Release Notes November 2019 (version 1.41)

🚀 Voilà, while having all these enabled in your settings, VS Code will automatically on each file save:

  • run code formatting with default formatter
  • sort imports
  • remove all unused declarations
  • run ESLint auto fix

It's always nice when you don't need to take care of these things manually, right? 😎 🤖

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player