Skip to content

Extend vue/no-dupe-keys to support <script setup> #2096

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 · 8 comments · Fixed by #2185 or #2189
Closed

Extend vue/no-dupe-keys to support <script setup> #2096

nasvillanueva opened this issue Mar 1, 2023 · 8 comments · Fixed by #2185 or #2189

Comments

@nasvillanueva
Copy link

nasvillanueva commented Mar 1, 2023

Please describe what the rule should do:
Similar to ESLint's no-shadow rule, it would be great to have something similar, like vue/no-prop-shadow where it would warn on local variable declaration that has the same name as a prop, which can lead to issues when referencing it in the template because props can be directly accessed from the template.

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"; // WARN: there's already a prop named `msg`
</script>

<template>
  <div>{{ msg }}</div> <!-- vague reference. Is this from props or local variable? -->
</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, // WARN: there's already a prop named `msg`
    };
  }
})
</script>

<template>
    <div>{{ msg }}</div> <!-- vague reference. Is this from props or local variable? -->
</template>

Additional context

More context from the original issue:

@nasvillanueva nasvillanueva changed the title Rule Proposal: vue/no-props-shadow Rule Proposal: vue/no-prop-shadow Mar 1, 2023
@ItMaga
Copy link
Contributor

ItMaga commented Mar 9, 2023

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, // WARN: there's already a prop named `msg`
    };
  }
})
</script>

<template>
    <div>{{ msg }}</div> <!-- vague reference. Is this from props or local variable? -->
</template>

@nasvillanueva In this part, the rule vue/no-dupe-keys resolve this case.

Maybe it would be better to extend vue/no-dupe-keys to support <script setup> instead of the new rule implementation?

@FloEdelmann
Copy link
Member

I also think that vue/no-dupe-keys should be extended to support <script setup> instead of adding a new rule.

@FloEdelmann FloEdelmann changed the title Rule Proposal: vue/no-prop-shadow Extend vue/no-dupe-keys to support <script setup> May 16, 2023
@zigomir
Copy link

zigomir commented May 23, 2023

I'm also seeing problems for reactive props destructuring https://blog.vuejs.org/posts/vue-3-3#reactive-props-destructure with [v9.14.0](https://github.com/vuejs/eslint-plugin-vue/releases/tag/v9.14.0)

@FloEdelmann
Copy link
Member

Reactive props destructure is not supported in eslint-plugin-vue, since it is an experimental Vue feature.

@zigomir
Copy link

zigomir commented May 23, 2023

I see, thanks!

@Bernankez
Copy link

Hi, I have the same problem with this comment, but not using reactive props destructure, just like this:

const props = defineProps<{
    componentKey?: string
    fullData: UnfAble<HomeRootObject>
}>()

const { componentKey, fullData } = toRefs(props) // Duplicated key 'componentKey'.

Should this case be considered?

@4350pChris
Copy link

Hi, I have the same problem with this comment, but not using reactive props destructure, just like this:

const props = defineProps<{
    componentKey?: string
    fullData: UnfAble<HomeRootObject>
}>()

const { componentKey, fullData } = toRefs(props) // Duplicated key 'componentKey'.

Should this case be considered?

Same. Imho this is legit usage of duplicate keys and should not be considered for this rule. I had to deactivate this particular rule for now, as my whole project uses toRefs without reassigning to different names, causing hundreds of linting errors.

@FloEdelmann
Copy link
Member

Indeed, this seems like a bug. A fix is already being worked on by @ota-meshi in #2189.

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