Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rule Proposal: Disallow v-if, v-else and v-for on non-template tags #1747

Open
BenAlbin opened this issue Dec 23, 2021 · 2 comments
Open

Rule Proposal: Disallow v-if, v-else and v-for on non-template tags #1747

BenAlbin opened this issue Dec 23, 2021 · 2 comments

Comments

@BenAlbin
Copy link

BenAlbin commented Dec 23, 2021

Please describe what the rule should do:
This rule would enforce putting v-if, v-else-if, v-else, v-for directives on a <template> tag and disallow them on html elements or custom components. The main reason for this is consistency since sometimes it is required to put v-if or v-for on a <template> tag rather than on the element. It also allows for better and more clear code collapsing in editors, since the component that we want to control with the directive may also have a number of attributes.

What category should the rule belong to?

[x] Enforces code style (layout)
[ ] Warns about a potential error (problem)
[ ] Suggests an alternate way of doing something (suggestion)
[ ] Other (please specify:)

Provide 2-3 code examples that this rule should warn about:

// BAD

<div v-if="loading">
  ...
</div>
<div v-else>
  ...
</div>

<MyItem v-for="item in items" :key="item.id" />



// GOOD

<template v-if="Loading">
  <div>
    ...
  </div>
</template>
<template v-else>
  <div>
    ...
  </div>
</template>


<template v-for="item in items" :key="item.id">
  <MyItem />
</template>
@BenAlbin BenAlbin changed the title Disallow v-if, v-else and v-for on non-template tags Rule Proposal: Disallow v-if, v-else and v-for on non-template tags Dec 23, 2021
@ota-meshi
Copy link
Member

Thank you for the rule proposal.
I think that some users prefer the opposite, so it would be better if we could offer various options by referring to the curly rule.

https://eslint.org/docs/rules/curly

@kleinfreund
Copy link

kleinfreund commented Aug 28, 2022

The main reason for this is consistency since sometimes it is required to put v-if or v-for on a <template> tag rather than on the element.

When is this actually the case in Vue 3?

For example, vue/no-use-v-if-with-v-for currently requires v-if to be moved to a wrapper element.

A rational for this being problematic can be found in https://v3-migration.vuejs.org/breaking-changes/v-if-v-for.html#migration-strategy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants