Skip to content

Rule proposal: warn about missing required props #1829

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

Closed
Preston-Landers opened this issue Mar 25, 2022 · 1 comment
Closed

Rule proposal: warn about missing required props #1829

Preston-Landers opened this issue Mar 25, 2022 · 1 comment

Comments

@Preston-Landers
Copy link

Preston-Landers commented Mar 25, 2022

Please describe what the rule should do:

Enforce (or warn) when a required prop is not given to a Vue component.

What category should the rule belong to?

[ ] Enforces code style (layout)
[X] 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:

// CurrentMood.vue
<script>
export default {
  name: "CurrentMood",
  props: {
    emotion: {
      type: String,
      required: true,
    },
  },
}
</script>

// GOOD example
<CurrentMood emotion="happy" />

// BAD example - missing required prop
<CurrentMood />

Additional context

Am I missing something obvious or is there no way to detect a case when you fail to give a required prop? PyCharm's Vue plugin is able to detect this via the HTML inspection "missing required attribute".

It would be great if eslint could detect this too. I did a ticket search and couldn't find any similar requests. Please forgive me if this has already been discussed and rejected.

On a related note, I'm also looking for a way to require in eslint that a certain list of HTML elements has certain required attributes within a Vue file. E.g. require "id" attributes on <button>, <input>, etc., as well as on certain Vue components.

@FloEdelmann
Copy link
Member

I'm afraid this is out of scope for this ESLint plugin (or maybe even for ESLint in general), as there is no way to look at the source of other files. Right now, it would only be possible to check components that are defined in the same file; and according to the style guide, this would only be recursive components then.

require in eslint that a certain list of HTML elements has certain required attributes

That could be a good rule for this plugin, but the configuration would need to be specified manually. Please open a new issue for that rule suggestion.

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

2 participants