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: v-for-delimiter-style #1228

Closed
1 of 4 tasks
FloEdelmann opened this issue Jul 1, 2020 · 3 comments · Fixed by #1267
Closed
1 of 4 tasks

Rule proposal: v-for-delimiter-style #1228

FloEdelmann opened this issue Jul 1, 2020 · 3 comments · Fixed by #1267

Comments

@FloEdelmann
Copy link
Member

FloEdelmann commented Jul 1, 2020

Please describe what the rule should do:
You can use both v-for="x in xs" and v-for="x of xs". There should be a rule to enforce one of the styles (probably configurable).

What category should the rule belong to?

  • Enforces code style
  • Warns about a potential error
  • Suggests an alternate way of doing something
  • Other (please specify:)

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

<!-- "vue/v-for-delimiter-style": ["warn", "in"] -->
<div v-for="x in xs" /><!-- GOOD -->
<div v-for="x of xs" /><!-- BAD -->

<!-- "vue/v-for-delimiter-style": ["warn", "of"] -->
<div v-for="x of xs" /><!-- GOOD -->
<div v-for="x in xs" /><!-- BAD -->

Additional context
See https://vuejs.org/v2/guide/list.html#Mapping-an-Array-to-Elements-with-v-for:

You can also use of as the delimiter instead of in, so that it is closer to JavaScript’s syntax for iterators.

@ota-meshi
Copy link
Member

Thank you for your proposal!

Sounds good to me.

However, looking at the rules of eslint, it seems that the meaning is a little different from the rules that have consistent as a prefix.
I think names like vue/v-for-delimiter or vue/v-for-delimiter-style are good. What do you think?

@FloEdelmann
Copy link
Member Author

FloEdelmann commented Jul 1, 2020

Indeed. I'd go with vue/v-for-delimiter-style then. I've updated the issue title and description.

@FloEdelmann FloEdelmann changed the title Rule proposal: consistent-v-for-delimiter Rule proposal: v-for-delimiter-style Jul 1, 2020
@ota-meshi
Copy link
Member

If the PR to the parser is unacceptable, you can get it from the token. (it's difficult to read...)

const tokenStore =
    context.parserServices.getTemplateBodyTokenStore &&
    context.parserServices.getTemplateBodyTokenStore()

const token = /** @type {Token} */ (tokenStore.getTokenAfter(
  node.left.length ? node.left[node.left.length-1] : tokenStore.getFirstToken(node),
  token => token.type !== 'Punctuator' || token.value !== ')'
))

token.value // "in" or "of"

The following sources are helpful.

const inToken = /** @type {Token} */ (tokenStore.getTokenAfter(

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.

2 participants