eslint-plugin-vue
eslint-plugin-vue copied to clipboard
Rule Proposal: Disallow v-if, v-else and v-for on non-template tags
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>
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
The main reason for this is consistency since sometimes it is required to put
v-iforv-foron 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.