This content originally appeared on DEV Community and was authored by Damir Drempetić
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
}
fixAll is not necessary for this case but could be useful too.
fixAll property turns on Auto Fix for all providers including ESLint, docs: https://github.com/microsoft/vscode-docs/blob/efa17ec71d755cf14c4722c7d05d10c538cb0e97/release-notes/v1_41.md#eslint
? 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? ? ?
This content originally appeared on DEV Community and was authored by Damir Drempetić
Damir Drempetić | Sciencx (2021-05-07T15:03:55+00:00) Easily sort imports and remove unused declarations on each save in VS Code. Retrieved from https://www.scien.cx/2021/05/07/easily-sort-imports-and-remove-unused-declarations-on-each-save-in-vs-code/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.