Skip to content

Rule Proposal: Prevent accessing props directly in the template #2095

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
nasvillanueva opened this issue Mar 1, 2023 · 7 comments
Closed

Comments

@nasvillanueva
Copy link

Please describe what the rule should do:
When using <script setup>, local variables declared with the same name as the props takes precedence over the props. To avoid potential issues where a locally declared variable has different values or type than the props, I think it would be nice to have a rule to enforce accessing props from a variable.

For instance, enforce accessing props from const props = defineProps(...), so in the template it will be {{ props.propsValue }}.

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:

WARN

<script setup lang="ts">
defineProps<{
  msg: string;
}>()

const msg = "override props";
</script>

<template>
  <div>{{ msg }}</div> <!-- vague reference? are you referencing the one from props or local? -->
</template>

WARN

<script lang="ts">
import { defineComponent, computed } from "vue";

export default defineComponent({
  props: {
    msg: String
  },
  setup: (props) => {
    const msg = computed(() => `Message: ${props.msg}`);
    
    return {
      msg
    };
  }
})
</script>

<template>
    <div>{{ msg }}</div> <!-- vague reference? are you referencing the one from props or local? -->
</template>

OK

<script setup lang="ts">
const props = defineProps<{
  msg: string;
}>()

const msg = "override props";
</script>

<template>
  <div>From local: {{ msg }}</div>
  <div>From props: {{ props.msg }}</div>
</template>

Additional context

I understand that there are cases where a component's <script setup> is basically calling defineProps with no local variable declaration, so maybe we should be able to allow that? But I'm not sure if it's feasible.

<script setup lang="ts">
defineProps<{
  msg: string;
}>()
</script>

<template>
  <div>{{ msg }}</div> <!-- Maybe Okay? -->
</template>

(Possibly) Related Issues

@FloEdelmann
Copy link
Member

FloEdelmann commented Mar 1, 2023

I think it would make more sense to add a rule similar to core ESLint's no-shadow rule that disallows variables with the same name as props, to avoid confusion right from the start.

Possible name for that rule: vue/no-prop-shadow. What do you think?

@nasvillanueva
Copy link
Author

@FloEdelmann Oh, that sounds even better. I was thinking about this in a different perspective so no-shadow didn't cross my mind, but a vue/no-prop-shadow would be great!

@FloEdelmann
Copy link
Member

Would you open a new issue for vue/no-prop-shadow then and close this one, please?

@FloEdelmann
Copy link
Member

Note also the similar rule: https://eslint.vuejs.org/rules/no-template-shadow.html

@nasvillanueva
Copy link
Author

Closing in favor of #2096

@M-jerez
Copy link

M-jerez commented Aug 8, 2024

Is there a rule or vue config enforce the use of props const in the template?

Think enforcing the user to use the props constant in templates is a less magic way of doing things. ie:

<script setup lang="ts">
const props = defineProps<{
  msg: string;
}>()
</script>

<template>
  <div>{{ props.msg }}</div> <!-- this will work -->
  <div>{{ msg }}</div> <!-- this will fail -->
</template>

@FloEdelmann
Copy link
Member

No, there is none. Please open a new issue for this request and link to this issue here for reference.

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

No branches or pull requests

3 participants