eslint-plugin-vue icon indicating copy to clipboard operation
eslint-plugin-vue copied to clipboard

[Rule proposal] Warn when referencing props without `props.` prefix in functional component template

Open panstromek opened this issue 6 years ago • 5 comments

Please describe what the rule should do: When I use functional components with templates, I very often forget to reference props through props object (props.user instead of user). This leads to a lot of lost time in debugging until I find out because there is no warning or error thrown to indicate this issue.

What category should the rule belong to?

  • [ ] Enforces code style
  • [x] 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:

<template functional>
  <div>{{ username }}</div>
</template>
<script>
export default {
  functional: true,
  props: {
    username: String,
  }
}
</script>

should be

<div>{{ props.username }}</div>

Additional context

panstromek avatar Aug 09 '19 09:08 panstromek

I like this proposal.

@ota-meshi What do you think about this?

tyankatsu0105 avatar Jul 01 '20 14:07 tyankatsu0105

Hi @tyankatsu0105. I like this rule of finding mistakes.

However, the option of props is optional, and it may be difficult to determine that it is a prop.

https://vuejs.org/v2/guide/render-function.html#Functional-Components

e.g.

<template functional>
  <div>{{ username }}</div>
</template>
<script>
export default {
  functional: true,
  // props: {
  //  username: String,
  // }
}
</script>

Instead, it may be possible to report access other than the context properties (props, children, slots and more).

ota-meshi avatar Jul 02 '20 02:07 ota-meshi

@ota-meshi

Since Vue3, <template functional> style will not be supported. https://github.com/vuejs/rfcs/blob/master/active-rfcs/0007-functional-async-api-change.md

The rule that fulfills this issue's request looks like useless.

tyankatsu0105 avatar Jul 03 '20 14:07 tyankatsu0105

I know that, but it doesn't stop supporting Vue 2, so it useful for some users. However, I think this rule has a lower priority than the rules that are useful to both Vue 2 and 3 users.

ota-meshi avatar Jul 03 '20 14:07 ota-meshi

@ota-meshi Thanks for your quick response!!!

OK. I will work on this issue if I have time😉

tyankatsu0105 avatar Jul 03 '20 14:07 tyankatsu0105