VSCode codeActionsOnSave ignore specific file

Viewed 2412

I'm using organizeImports on save, but there are some instances where the order matters and this causes an issue. I can't find anywhere if there's a way to simply ignore a page, either through comments within the page (ideally) or within the config settings.

Perhaps there's an extension that provides this functionality if not baked in. In any case, really appreciate any help tracking down a solution for this.

2 Answers

Couldn't find how to do it properly but here is a solution that might solve your end needs if you use prettier.

The way I handled organizeImports selectively for files was as follow.

1 - Make sure your default formatter is prettier (as explained below) https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode

2 - In your settings set formatOnSave to true but organizeImports to false such as below.

{
  "editor.codeActionsOnSave": {
    "source.organizeImports": false
  },
  "editor.formatOnSave": true
}

3 - Install https://github.com/simonhaenisch/prettier-plugin-organize-imports

This is a prettier plugin that allows organizing import as part of the prettier formatting and has an option to disable organize import for files (i.e. // organize-imports-ignore )

If you're using ESLint (which I would highly recommend), you can use sort-imports or import/order (via eslint-plugin-import) to sort your import statements across the entire project, then ignore the rule in specific files/regions with special comments, like this:

/* eslint-disable import/order */

import * from "abcdefg";
import "cool-module";
// etc...

VSCode has a great ESLint plugin, which in combination with

"editor.codeActionsOnSave": {
    "source.fixAll.eslint": true,
},

will auto format and fix your code whenever the file is saved.

Related