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 suggestion: vue/no-use-v-else-with-v-for #1939

Closed
FloEdelmann opened this issue Jul 25, 2022 · 13 comments · Fixed by #2224
Closed

Rule suggestion: vue/no-use-v-else-with-v-for #1939

FloEdelmann opened this issue Jul 25, 2022 · 13 comments · Fixed by #2224

Comments

@FloEdelmann
Copy link
Member

FloEdelmann commented Jul 25, 2022

What rule do you want to change?
vue/no-use-v-if-with-v-for New rule vue/no-use-v-else-with-v-for

Does this change cause the rule to produce more or fewer warnings?
More

How will the change be implemented? (New option, new default behavior, etc.)?
Not sure, but probably default behavior.

Please provide some example code that this change will affect:

<!-- GOOD, this is caught be the vue/no-use-v-if-with-v-for rule already -->
<div v-if="foo" v-for="x in xs">{{ x }}</div>
<!-- BAD -->
<div v-if="foo">foo</div>
<div v-else v-for="x in xs">{{ x }}</div>

<!-- GOOD -->
<div v-if="foo">foo</div>
<template v-else>
  <div v-for="x in xs">{{ x }}</div>
</template>
<!-- BAD -->
<div v-if="foo">foo</div>
<div v-else-if="bar" v-for="x in xs">{{ x }}</div>

<!-- GOOD -->
<div v-if="foo">foo</div>
<template v-else-if="bar">
  <div v-for="x in xs">{{ x }}</div>
</template>

What does the rule currently do for this code?
Only report the first case (v-if+v-for).

What will the rule do after it's changed?
Also warn about the other cases (v-else+v-for and v-else-if+v-for), and maybe even autofix to add a wrapper <template> tag.

@ota-meshi
Copy link
Member

Thank you for your suggestion.
I think it would be great if could check them out with a plugin 👍. The sample code you provided is very hard to read.

But I don't think it needs to be included in vue/no-use-v-if-with-v-for. Avoiding the combination of v-if and v-for is intended to avoid unintended behavior, but avoiding the combination of v-else and v-for improves readability. I think the purpose is a little different. What do you think?

@FloEdelmann

This comment was marked as off-topic.

@ota-meshi

This comment was marked as off-topic.

@ota-meshi

This comment was marked as off-topic.

@FloEdelmann

This comment was marked as off-topic.

@ota-meshi

This comment was marked as off-topic.

@ota-meshi

This comment was marked as off-topic.

@FloEdelmann

This comment was marked as off-topic.

@ota-meshi

This comment was marked as off-topic.

@ota-meshi

This comment was marked as off-topic.

@ota-meshi
Copy link
Member

ota-meshi commented Jul 27, 2022

@FloEdelmann FloEdelmann changed the title Also warn about v-else-if and v-else in vue/no-use-v-if-with-v-for Rule suggestion: vue/no-use-v-else-with-v-for Aug 27, 2022
@FloEdelmann
Copy link
Member Author

@kleinfreund Could you elaborate why you are against this rule? (I'm interpreting your 👎 reaction on the issue that way.)

@kleinfreund
Copy link

@FloEdelmann Because to me, the following is not ambiguous:

<div v-if="foo">foo</div>
<div v-else v-for="x in xs">{{ x }}</div>

I suppose the intent here is to avoid ambiguity like this:

<div v-if="foo">foo</div>
<div v-else-if="x === thing" v-for="x in xs">{{ x }}</div>

Now that’s perhaps something that’s worth it to make less ambiguous via stricter linter rules in which I don’t feel strongly about the introduction of the proposed rule.

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

Successfully merging a pull request may close this issue.

3 participants