question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Volar and Prettier have fightin' words!

See original GitHub issue

In vscode, Volar formats an HTML tag with many attributes in as few lines as possible. Look at the <img> tag on line 3:

<template>
  <header>
    <img alt="Vue logo" class="logo" src="@/assets/logo.svg" width="125" height="125" data-option1="something"
      data-option2="something" data-option3="something" data-option4="something" />

    <div class="wrapper">
      <HelloWorld msg="You did it!" />

      <nav>
        <RouterLink to="/">Home</RouterLink>
        <RouterLink to="/about">About</RouterLink>
      </nav>
    </div>
  </header>

  <RouterView />
</template>

Prettier formats it like the following into multiple lines. As before, look at the <img> tag on line 3:

<template>
  <header>
    <img
      alt="Vue logo"
      class="logo"
      src="@/assets/logo.svg"
      width="125"
      height="125"
      data-option1="something"
      data-option2="something"
      data-option3="something"
      data-option4="something"
    />

    <div class="wrapper">
      <HelloWorld msg="You did it!" />

      <nav>
        <RouterLink to="/">Home</RouterLink>
        <RouterLink to="/about">About</RouterLink>
      </nav>
    </div>
  </header>

  <RouterView />
</template>

I am following the Vue recommendation to use the Volar extension, and so far it seems to be pretty good, except for the hiccup mentioned above.

Is there a way have the prettier setting used in this situation without disabling all of Volar? Or is this a Volar philosophy to minimize the number of lines?

In case it helps, here is my .eslint.js file:

/* eslint-env node */
require('@rushstack/eslint-patch/modern-module-resolution');

module.exports = {
  root: true,
  extends: [
    'plugin:vue/vue3-essential',
    'eslint:recommended',
    '@vue/eslint-config-typescript/recommended',
    '@vue/eslint-config-prettier',
  ],
  env: {
    'vue/setup-compiler-macros': true,
  },
  rules: {
    quotes: ['error', 'single', { avoidEscape: true }],
  },
};

In my vscode settings, I have:

  "[vue]": {
    "editor.defaultFormatter": "johnsoncodehk.volar"
  },

Finally, here is a screenshot of how it looks, in case that’s helpful:

Screen Shot 2022-04-18 at 4 12 41 PM

Note that I tried playing around with the vue/max-attributes-per-line setting, but that went off the rails very quickly and I immediately ran away from it.

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:13 (2 by maintainers)

github_iconTop GitHub Comments

14reactions
movycommented, Jun 7, 2022

Just tried switching from Vetur and stumbled upon this. Wrapping attributes is essential for readability and easy editing, and it is amongst strongly recommended Vue guidelines: https://vuejs.org/style-guide/rules-strongly-recommended.html#multi-attribute-elements

Cleary it does not work out of the box and Volar unwraps the attributes for no apparent reason. I saw a number of similar issues with no clear / working solution, how come it’s still unsolved?

7reactions
JessRascalcommented, Jul 19, 2022

For me, the key to solving this was changing @vue/eslint-config-prettier to prettier in the extends of the .eslintrc.* file.

Note: This is as per the note in the Installation section of the documentation regarding v8+).

/* eslint-env node */
require('@rushstack/eslint-patch/modern-module-resolution');

module.exports = {
  root: true,
  extends: [
    'plugin:vue/vue3-essential',
    'eslint:recommended',
    '@vue/eslint-config-typescript/recommended',
    'prettier', // this used to be @vue/eslint-config-prettier
  ],
};

They stopped fighting after I changed this.

I also made sure my default formatter in VS Code was set to Prettier - Code Formatter (right-click in a file > select Format Document With... > select Configure Default Formatter... > select Prettier - Code Formatter).

I hope this helps someone with the annoying default setup 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Having a tough time setting up EsLint/Prettier to work with Volar
I'm having a tough time understanding how VSCode with the Volar extension, Eslint and Prettier all work together. I followed this article to...
Read more >
How to resolve Vitesse formatting issue? - vue.js
json , I already have a vue section with Volar as defaultFormatter :( It feels like that vitesse uses ESLINT to format, not...
Read more >
Auto Formatting Files in VS Code (Volar/Built-in or Prettier) - A...
In this lesson, I'll show you to get auto formatting on save setup with both built in formatters and Prettier. Links. Prettier: Official...
Read more >
Options - Prettier
Prettier ships with a handful of format options. To learn more about Prettier's stance on options – see the Option Philosophy. If you...
Read more >
Quick VSCode setup for Vue.js with Prettier and ESLint
Prettier formats the JS code in a nice opinionated way. However this is not fully aligned with ... You have 2 free member-only...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found