Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get correct autoformat on save in Visual Studio Code with ESLint and Prettier

in Visual Studio Code with ESLint and Prettier when working on .vue files, it seems I can't get vue/max-attributes-per-line to auto-fix correctly.

For example, with vue/max-attributes-per-line set to 'off', and I try to add line breaks manually it corrects it to always have every element on no more than one line, no matter if it is 81, 120, 200, or more characters wide. How can I figure out what is forcing my markup elements onto exactly one line?

I am using ESLint version 5.1.0 and Visual Studio Code (without the Prettier Extension), with Prettier 1.14.2.

Here's the example in a .vue file-- I cannot make this go on multiple lines no matter what I do, when 'vue/max-attributes-per-line': 'off'. Every time I save, it forces the long line of markup to be all on one line.

<template>   <font-awesome-icon v-if="statusOptions.icon" :icon="statusOptions.icon" :spin="statusOptions.isIconSpin" :class="['saving-indicator', 'pl-1', 'pt-1', statusOptions.iconClasses]" /> </template> 

If I set 'vue/max-attributes-per-line': 2, it formats like so, with one line break(which is quite wrong as well).

<font-awesome-icon   v-if="statusOptions.icon"    :icon="statusOptions.icon"   :spin="statusOptions.isIconSpin"   :class="['saving-indicator', 'pl-1', 'pt-1', statusOptions.iconClasses]" /> 

If I try to reformat it manually, it just reverts to the above when I save.

Additionally, it seems to reformat twice when I hit Ctrl+S: first it reformats to put it all on one line, then a half-second later the formatting above results. Any ideas? What is causing this weirdness--are there multiple reformatters running? How do I figure out what the first one is to disable it?

VS Code workspace settings:

{   "editor.formatOnType": false,   "editor.formatOnPaste": false,   "editor.formatOnSave": true,   "[javascript]": {     "editor.tabSize": 2   },   "[vue]": {     "editor.tabSize": 2   },   "[csharp]": {     "editor.tabSize": 4   },   "javascript.format.insertSpaceAfterFunctionKeywordForAnonymousFunctions": true,   "javascript.referencesCodeLens.enabled": true,   "vetur.validation.script": false,   "vetur.validation.template": false,   "eslint.autoFixOnSave": true,   "eslint.alwaysShowStatus": true,   "eslint.options": {     "extensions": [       ".html",       ".js",       ".vue",       ".jsx"     ]   },   "eslint.validate": [     {       "language": "html",       "autoFix": true     },     {       "language": "vue",       "autoFix": true     },     "vue-html",     {       "language": "javascript",       "autoFix": true     },     {       "language": "javascriptreact",       "autoFix": true     }   ],   "editor.rulers": [     80,     100   ] } 

.eslintrc.js:

module.exports = {   parserOptions: {     parser: 'babel-eslint'   },   env: {     browser: true,     node: true,     jest: true   },   globals: {     expect: true   },   extends: [     'prettier',     'plugin:vue/recommended',        // /base, /essential, /strongly-recommended, /recommended     'plugin:prettier/recommended',   // turns off all ESLINT rules that are unnecessary due to Prettier or might conflict with Prettier.      'eslint:recommended'   ],   plugins: ['vue', 'prettier'],   rules: {     'vue/max-attributes-per-line': 'off',     'prettier/prettier': [            // customizing prettier rules (not many of them are customizable)       'error',       {         singleQuote: true,         semi: false,         tabWidth: 2       },     ],     'no-console': 'off'   } } 

Without changing any settings, ESLint --fix does indeed format properly--breaking all my .vue template elements into many lines properly. So any ideas how I whip VS Code into shape? The above settings didn't help, but I am at a loss how as to even know what is interfering. Any ideas?

To emphasize, when I save in VS Code, a long HTML element will collapse to one line then break to two lines a half-second later, all from one save operation. I'm expecting it instead to break it up into many lines. It would be okay if it took several saves, but instead the first save shows this behavior as well as every subsequent save.

like image 705
Patrick Szalapski Avatar asked Aug 30 '18 17:08

Patrick Szalapski


People also ask

How do I fix ESLint errors in VSCode 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 ).

Why is VSCode not formatting on save?

To automatically format the file on save, In Visual Studio Code, press Control + Shift + P or Command + Shift + P (Mac) to open the command palette and type setting and then select Preferences: Open User Settings option. Search for format on save setting and check the checkbox.

How do I autoformat code in Visual Studio Code?

The code formatting is available in Visual Studio Code through the following shortcuts: On Windows Shift + Alt + F. On Mac Shift + Option + F. On Linux Ctrl + Shift + I.


1 Answers

Short answer: I needed: "editor.formatOnSave": false, "javascript.format.enable": false.

I finally found the magical combination of settings, thanks to this thread from Wes Bos on Twitter. I was right in my suspicion that there seem to be multiple conflicting formatters. Though I'm not sure what they actually are, I was able to turn off all but eslint as follows:

In the VS Code settings, I need:

"editor.formatOnSave": false, "javascript.format.enable": false, "eslint.autoFixOnSave": true, "eslint.alwaysShowStatus": true, "eslint.options": {   "extensions": [ ".html", ".js", ".vue", ".jsx" ] }, "eslint.validate": [   { "language": "html", "autoFix": true },   { "language": "vue", "autoFix": true },   { "language": "javascript", "autoFix": true },   { "language": "javascriptreact", "autoFix": true } ] 

In .eslintrc.js, then I can use the settings in my original post and then also change 'vue/max-attributes-per-line' as desired. Then VS Code's ESLint plugin will format code one step at a time on every save, much as kenjiru wrote. One last snag: HMR won't pick up these changes, so rebuild from scratch.

like image 101
Patrick Szalapski Avatar answered Oct 04 '22 06:10

Patrick Szalapski