Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between codeActionsOnSave and formatOnSave in VS Code

Tags:

Newer versions of VS code include a new codeActionsOnSave options. It is recommanded to setup Eslint for example. However I don't understand the difference with previously existing formatOnSave option, nor their relationship with the defaultFormater option, plugins like Prettier and ESLint, and VS Code default behavior.

"Fix" seems more complete, but disabling "format" will not always format my code on save. I also tend to have conflicts between Eslint and Prettier formatting...

Basically it is unclear to me how to set a proper VS Code formatting configuration, that respects project settings (TypeScript, Eslint, Prettier etc. that can be activated/deactivated depending on the project), due to a lack of understanding about the VS Code settings.

like image 396
Eric Burel Avatar asked Mar 17 '20 08:03

Eric Burel


People also ask

What is codeActionsOnSave?

Run Code Actions on save# The new editor.codeActionsOnSave setting lets you configure a set of Code Actions that are run when a file is saved.

What is the difference between Visual Studio Code?

Before getting deeper into the specifics, the main distinction between these editors is that Visual Studio is a full-fledged Integrated Development Environment (IDE), while Visual Studio Code is a rich text editor.

Is VSCodium better than VS Code?

Brief: VSCodium is a clone of Microsoft's popular Visual Studio Code editor. It's identical to VS Code with the single biggest difference that unlike VS Code, VSCodium doesn't track your usage data. Microsoft's Visual Studio Code is an excellent editor not only for web developers but also for other programmers.


1 Answers

I'm trying to get my VS Code to run ESLint and Prettier together properly as well.

Then VSCode 1.44 (March 2020) would highlight another difference between codeActionsOnSave and formatOnSave:

Explicit ordering for editor.codeActionsOnSave

You can now set editor.codeActionsOnSave to an array of code actions to execute in order. You can use this to guarantee that a specific code action is always run before or after another one that may conflict with it

The following editor.codeActionsOnSave will always run Organize Imports followed by Fix All once organize imports finishes:

"editor.codeActionsOnSave": [     "source.organizeImports",     "source.fixAll" ] 

But the main difference between codeActionsOnSave and formatOnSave remains that:

  • the latter (formatOnSave) only formats code,
  • while the former (codeActionsOnSave) can run one or several commands on the code, commands which might not be related to formatting.
like image 95
VonC Avatar answered Sep 22 '22 04:09

VonC