Search code examples
vue.jsvisual-studio-codeeslintprettiereslint-config-vue

Weird Vue linter behavior: "123 {{ v }} 123" within H2 multi-line tag caused error


I just setup my development environment, with the newly installed linter & prettier for Vue.js & NUXTJS I've checked my configuration with

npx eslint --print-config ./pages/index.vue | npx eslint-config-prettier-check

and

No rules that are unnecessary or conflict with Prettier were found.

When I write something like the following and the eslint pop-ups and say, hey, the prettier don't want you to do that, does anyone know why? I don't want to just make the linter ignore in this case since this kind of style seems to be very usual in my codes. Any help would be appreciated!

Error:

why

The codes:

  <h2 class="subtitle">
    123 {{ env }} 123
  </h2>
  <h2 class="subtitle">
    {{ env }}
  </h2>
  <h1 class="title">
    reborn
  </h1>

Also my .eslintrc.js is showed below:

module.exports = {
  root: true,
  env: {
    browser: true,
    node: true
  },
  parserOptions: {
    parser: 'babel-eslint'
  },
  plugins: ['prettier', 'vue'],
  rules: {
    'vue/component-name-in-template-casing': ['error', 'PascalCase'],
    'no-console': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    'no-debuger': process.env.NODE_ENV === 'production' ? 'error' : 'off',
    'vue/max-attributes-per-line': 'off',
    'nuxt/no-cjs-in-config': 'off'
  },
  globals: {
    $nuxt: true
  },
  extends: [
    '@nuxtjs',
    'plugin:prettier/recommended',
    'plugin:nuxt/recommended',
    'plugin:vue/recommended',
    'prettier',
    'prettier/babel',
    'prettier/vue',
    'prettier/unicorn'
  ]
}

Solution

  • The linter is complaining about your return characters, notice the return character at the beginning and just after the second 123.

    I believe it is saying you should put them all on the same line, ie

    <h2 class="subtitle">123 {{ env }} 123</h2>
    

    Of course, I wouldn't be surprised if there was a conflicting rule on the auto formatter that says not to put it on one line. You will need to decide which you rather. Personally, I would find and turn off that spacing rule because I prefer the formatting you have already.