Skip to content

Rule Proposal: vue/no-implicit-props-access #2172

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

Open
1 of 4 tasks
Tracked by #5488
dumptyd opened this issue May 14, 2023 · 3 comments
Open
1 of 4 tasks
Tracked by #5488

Rule Proposal: vue/no-implicit-props-access #2172

dumptyd opened this issue May 14, 2023 · 3 comments

Comments

@dumptyd
Copy link

dumptyd commented May 14, 2023

Please describe what the rule should do:

Props can be referenced in the template using propName or props.propName. Former is convenient; latter is more explicit, provides a clear delineation between local state and props and is consistent with how props are accessed in script. Former could also feel counter-intuitive if you're using Typescript (but that's subjective).

The rule, when enabled, should flag any occurrence of implicit prop access in the template.

What category should the rule belong to?

  • 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:

  <template>
    <div>
      <!-- bad -->
      <div v-if="myProp">
        myProp: true
      </div>

      <!-- bad -->
      <div>
        myProp: {{ myProp }}
      </div>

      <!-- good -->
      <div v-if="props.myProp">
        myProp: true
      </div>

      <!-- good -->
      <div>
        myProp: {{ props.myProp }}
      </div>
    </div>
  </template>
  <script lang="ts">
    export interface Props {
      myProp: boolean;
    }
    const props = defineProps<Props>();
  </script>

Additional context

I tried to look for existing rules and proposals but couldn't find anything on this subject. If this is already possible, please let me know. Thank you.

@FloEdelmann
Copy link
Member

Seems like a good rule to me. Note that it sounds similar to #938, maybe both rules can be combined into one?

@dumptyd
Copy link
Author

dumptyd commented May 17, 2023

It's been a while since I've used the Options API, but I believe it would require some special considerations in general.

IIRC, the ways to access the props in Options API are propName, $props.propName and props.propName (for functional components).

For functional components it's pretty straightforward, the logic can be incorporated in this rule based on what was proposed in #938.

For other components, going by the OP proposition, the rule should only allow $props.propName but I'm not sure if that's a good idea because:

  1. This would have to be implemented for prop accesses in script too for consistency.
  2. Since $-prefixed variables are considered low level, maybe having them all over your codebase would not be ideal?
  3. While this would help with delineating props and local state, it doesn't do much in terms of reducing magic (where's $props coming from?).

@TheAlexLichter
Copy link
Contributor

I'd go even a step further and customize the option to enforce props. as prefix or disallow it. That way, people can choose if they want the overhead but have clear indication of a prop in the template or the simplicity instead.

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

3 participants