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, 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? 😎 🤖
Top comments (9)
So I think this post is wrong in a bunch of ways. First,
organizeImports
seems to also runsortMembers
. Second, none of these settings have anything to do with prettier or eslint. Also, ESLint's import sorting disagrees with VSCode's, so if you do turn that on you're going to have problems.thx for the tip! Still, when I do that my code doesnt format on Auto Save. I gotta save manually for it to work, sigh. Any ideas? I program on C# and F#
Hi @laxedo17! I'm glad if post was useful to you. Did it work on manual save with C# and F#?
Which preference do you use for Auto Save (
afterDelay
,onFocusChange
,onWindowChange
)?I don't use Auto Save because I like to have full control, but tested it now and it didn't work only when I choose afterDelay.
Please check this issue on GitHub: Allow autoSave and formatOnSave. There is whole discussion about it and a possible workaround which could work for you.
Let me know whether it works. :)
okayyyy. Final test made, and I chose to use onFocusChange instead of afterDelay.
The issue is that when formatting in real time, you can go crazy and delete, move, switch to certain expressions 'cos the autoformat in real time moves your cursor or displaces words to tidy everything up.
cheers m8
Cool, I'm glad you quickly found a solution for your way of working. :)
tested already. Works like a charm! Only thing I'd need is to configure it for all files (.cs .fs), and I found this easy way following the post you linked to! (changed the .cs or .fs whatever, to simply a .* )
thanks!!!! I use afterDelay, also no change with others. Gotta test the workaround. Many many thanks
In order to remove C# usings from vs code solution you can follow github.com/OmniSharp/omnisharp-vsc... link also
{
"editor.formatOnPaste": true,
"source.organizeImports": true,
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true,
"source.fixAll.format": true,
"source.fixAll": true,
"source.organizeImports": "explicit"
}
}
I use above config in settings.json for Next Js but it is not work
can you help me?