6
votes

I am using VScode for Vue development using Prettier and Eslint.

Currently, Prettier is formatting my code like this:

enter image description here

What I'd like is to force the following

enter image description here

If I manually change it to my wanted format it won't mark it as incorrect, but it also doesn't do this format by default.

Is there a way to accomplish this by default?

My relevant VScode Settings.json

"prettier.disableLanguages": ["json"],
  "[scss, css]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "vetur.validation.template": false,
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": true
  },
  "[vue]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "editor.formatOnSave": true,
  "html.format.wrapAttributes": "force-aligned",
  "sync.gist": "30b867ce7d7d1360ee7bad0cf5599fc3",
  "sync.autoDownload": true,
  "sync.autoUpload": true,
  "sync.forceUpload": false,
  "sync.removeExtensions": false,
  "sync.quietSync": true,
  "[javascript]": {
    "editor.defaultFormatter": "vscode.typescript-language-features"
  },

My .prettierrc settings

{
  "trailingComma": "none",
  "tabWidth": 2,
  "semi": false,
  "singleQuote": true
}

My eslintrc.js settings

extends: [
    'plugin:vue/recommended',
    'eslint:recommended',
    'prettier/vue',
    'plugin:prettier/recommended'
  ],
  plugins: ['vue', 'prettier'],
2
This happens to angular as well... - Ever
I have the same problem with a similar configuration. Have you found any solution yet? - JSONaLeo
I gave up on it at the time, but recently came across this. Perhaps it will be of use: stackoverflow.com/questions/61715509/… - Tim Titus

2 Answers

1
votes

Adding this to the .prettierrc or .prettierrc.json file seems to fix this kind of issue:

"htmlWhitespaceSensitivity": "ignore"

Yours would look like this:

{
  "trailingComma": "none",
  "tabWidth": 2,
  "semi": false,
  "singleQuote": true,
  "htmlWhitespaceSensitivity": "ignore"
}

To understand better if this is a good solution for you, you can read about this topic: Whitespace-sensitive formatting

-1
votes

Need to modify the printwidth option in the prettier, but you need to aware of the below thing

There are some edge cases, such as really long string literals, regexps, comments and variable names, which cannot be broken across lines (without using code transforms which Prettier doesn’t do). Or if you nest your code 50 levels deep your lines are of course going to be mostly indentation :) - prettier

https://i.stack.imgur.com/GjBGG.png