Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESLint in VSCode not fixing on save

I am using ESLint in my Vue(Nuxt) project in VSCode. When I save I would like my ESLint to run automatically and fix all the warnings for me automatically.

This is my settings.json file in VSCode:

{
    "editor.codeActionsOnSave": {
        "source.fixAll.eslint": true
    },
    "vetur.format.defaultFormatter.js": "vscode-typescript",
    "vetur.validation.template": false,
    "vetur.completion.scaffoldSnippetSources": {},
    "vetur.completion.useScaffoldSnippets": false,
    "vetur.format.defaultFormatter.html": "none",
    "workbench.iconTheme": "material-icon-theme",
    "git.autofetch": true,
    "git.defaultCloneDirectory": "",
    "gitlens.views.repositories.files.layout": "list",
    "editor.tabSize": 2,
    "editor.detectIndentation": false,
}

And this is my .eslintrc.js file:

module.exports = {
  root: true,
  env: {
    browser: true,
    node: true,
  },
  extends: [
    "@nuxtjs",
    "plugin:nuxt/recommended",
    "../.eslintrc.js"
  ],
  rules: {
    //Add custom rules for the frontend here.
    //Rules that are common for shared, frontend and backend should go in the parent file
    "nuxt/no-cjs-in-config": "off",
  },
}

The linked ../.eslintrc.js file contains the following:

module.exports = {
  parserOptions: {
    parser: 'babel-eslint',
  },
  plugins: ['jest'],
  rules: {
    'prefer-const': 'off',
    'comma-dangle': ['error', 'always-multiline'],
    'prefer-template': 'error',
  },
  env: {
    'jest/globals': true
  }
}

Whenever I save the file the warnings just show up and will not automatically fix themselves. eslint warnings

EDIT: I've turned on verbose output and i'm seeing this in the output:

(node:6832) UnhandledPromiseRejectionWarning: Error: Failed to load plugin 'import' declared in 'frontend\.eslintrc.js » @nuxtjs/eslint-config': Cannot find module 'eslint-plugin-import'
Require stack:

I've then ran yarn add eslint-plugin-import and tried again, it still returns the same error.

like image 942
Dennis Avatar asked Jun 09 '20 07:06

Dennis


People also ask

How do I automatically fix ESLint errors on save?

If you have the ESLint extension installed you can use CTRL+SHIFT+P to open the Command Palette. Then search for ESLint: Fix all auto-fixable Problems and press ENTER (or RETURN ).

How to make ESLint work with VSCode?

For ESLint to work correctly on file same, you must change the VSCode preferences. Go to File > Preferences > Settings > Workplace and try to find: Editor: Code Actions On Save Code action kinds to be run on save. Edit in settings.json Then click settings.json.

How do I enable ESLint to fix a file on save?

Once the ESLint extension has installed you can use CTRL + SHIFT + P to open the Command Palette. Then search for “ESLint fix all auto-fixable Problems” and press enter. This command would enable eslint to fix the file on save. Show activity on this post.

Why is the ESLint fix code so expensive?

@Alonski the problem with the old fix code was that it didn't fix all fixable problems due to the nature how ESLint computes them. So I change the way which made it more complex and hence more expensive. Positive is that now all fixable problems will be addressed.

Does Visual Studio Code support linting?

Visual Studio code can support linting on every save. Your workflow may benefit from performing frequent lint checks to address small issues over time rather than addressing many large issues that may delay deploying code. In this tutorial, you will install ESLint, construct rules, and enable codeActionsOnSave in Visual Studio Code.


Video Answer


7 Answers

Get eslint plugin, add this code to your settings.json

 {
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
  "eslint.validate": ["javascript"]
 }

source

like image 61
Wojciechu Avatar answered Oct 19 '22 03:10

Wojciechu


I've managed to fix the issue. The problem was that there were multiple working directories in my solution, which all have their own eslint config. Putting the following line in the settings.json file of VSCode solved my issue:

"eslint.workingDirectories": [{ "mode": "auto" }]
like image 44
Dennis Avatar answered Oct 19 '22 01:10

Dennis


Launch VSCode, Command + Shift + P, type settings and hit enter, paste and save the following:

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

You're good to go!

like image 38
Shah Avatar answered Oct 19 '22 02:10

Shah


I tried those solutions and others, and it still didn't work. Actually it was just that ESLint's use had to be approved for use in VSCode. That is, I clicked on the ESLint item on the editor's bottom bar:

enter image description here

Which opened a popup asking me to approve ESLint. After approval autocorrect was running as expected.

like image 24
Eino Gourdin Avatar answered Oct 19 '22 02:10

Eino Gourdin


Install ESLint extension from the VSCode marketplace.

Once the ESLint extension has installed you may use CTRL + SHIFT + P to open the Command Palette. Search “ESLint fix all auto-fixable Problems” and press enter.

This command would enable eslint to fix the file on save.

like image 15
isAif Avatar answered Oct 19 '22 01:10

isAif


enter image description here

In the snap above as you can see that I am getting eslint errors and just to inform you all that despite saving the file, all auto-fixable problems are were not getting fixed by eslint/prettier setup.

So I tried pressing ctrl+shift+p and selecting prettier as default formatter and also tried doing eslint restart server but that didn't worked.

I noticed that vscode was giving me some notifications at the bottom right corner (bell icon). I clicked on that and some list of pop up came up stating that there are multiple formatters installed for the same extension file. Like for example in the below snap there is .jsx file(it had two formatters one was prettier and other was vscodes inbuilt formatter). I clicked on configure button and selected prettier as default and when I saved the file it worked!

enter image description here

If this doesn't works for you then I think this all worked for me because I had eslint npm packages installed in my project that works with prettier to enforce the prettier rules. (these packages are eslint-config-prettier and eslint-plugin-prettier)

like image 6
utkarsh-k Avatar answered Oct 19 '22 01:10

utkarsh-k


I ran into a similar problem-- ESLint was not properly formatting only certain, seemingly random, files on save. Running ESLint --fix would fix the formatting errors, but saving would not. Adding this line to our workspace settings.json fixed the problem:

"eslint.format.enable": true,

Making all our formatter settings look like this:

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

You can also go into the ESLint extension settings and check off the checkbox labeled ESLint > Format:Enable. Worked like a charm!

like image 2
Allie C Avatar answered Oct 19 '22 03:10

Allie C