Skip to content

[vue/valid-template-root] Incorrect detection of root element when using v-if/v-else directives #986

@ThibaultVlacich

Description

@ThibaultVlacich

Tell us about your environment

  • ESLint version: 6.6.0
  • eslint-plugin-vue version: 6.0.0
  • Node version: 13.0.1

Please show your full configuration:

module.exports = {
  root: true,
  env: {
    browser: true,
    node: true
  },
  parserOptions: {
    parser: 'babel-eslint'
  },
  extends: [
    '@nuxtjs',
    'prettier',
    'prettier/vue',
    'plugin:prettier/recommended',
    'plugin:nuxt/recommended'
  ],
  plugins: [
    'prettier'
  ],
  // add your custom rules here
  rules: {
    'vue/no-v-html': 'off'
  }
}

What did you do?

<template>
  <div
    v-if="status === 'LOADING'"
    class="d-flex justify-content-center loading"
  >
    Loading comments
    <span class="one">.</span>
    <span class="two">.</span>
    <span class="three">.</span>
  </div>

  <div v-else-if="status === 'LOADED'">
    <h4 class="comments-head">
      <font-awesome-icon :icon="['far', 'comments']" class="mx-2" />
      {{ `${commentsCount} comment${commentsCount > 1 ? 's' : ''}` }}
    </h4>

    <div v-for="comment in comments" :key="comment.id" class="comment">
      <div class="meta">
        <span class="author">{{ comment.author.username }}</span>
        on
        <time :datetime="comment.publishedAt" class="date">
          {{ comment.publishedAt | formatDate }}
        </time>
      </div>

      <div v-html="comment.content" class="content"></div>
    </div>
    <div v-if="comments.length === 0" class="no-comments text-center py-3">
      There are no comments.
    </div>

    <b-pagination
      v-if="comments.length > 0"
      v-model="page"
      :total-rows="commentsCount"
      :per-page="perPage"
      size="sm"
      align="center"
    ></b-pagination>

    <div v-if="$auth.loggedIn" class="add-comment">
      <b-form
        v-if="['WAITING', 'SENDING'].includes(newCommentStatus)"
        @submit.prevent.stop="addComment"
      >
        <h4 class="comments-head">
          Add a comment
        </h4>

        <classic-editor
          id="new-comment-body"
          v-model="newCommentContent"
          :config="{ toolbar: ['bold', 'italic'], readOnly: true }"
          :disabled="newCommentStatus === 'SENDING'"
        ></classic-editor>

        <div class="mt-3 d-flex justify-content-end">
          <b-button
            :disabled="
              newCommentStatus === 'SENDING' || newCommentContent.length === 0
            "
            variant="primary"
            type="submit"
          >
            <font-awesome-icon
              v-if="newCommentStatus === 'SENDING'"
              icon="spinner"
              spin
              class="mr-2"
            />
            Send
          </b-button>
        </div>
      </b-form>

      <b-alert :show="newCommentStatus === 'SENT'" variant="success">
        Your comment has been successfully added.
      </b-alert>
      <b-alert :show="newCommentStatus === 'ERROR'" variant="danger">
        {{ newCommentErrorMessage }}
      </b-alert>
    </div>
  </div>
</template>

What did you expect to happen?

This code does not trigger an error

What actually happened?

The vue/valid-template-root rule throws an error. This exact same code was working on ESLint 5 / eslint-plugin-nuxt 5.2.3, and I'm seeing nothing in the changelog that could explain why it's not working anymore.

components\poll\Comments.vue
12:3 error The template root requires exactly one element vue/valid-template-root

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions