Back to Biomejs

noVueDuplicateKeys

src/content/docs/linter/rules/no-vue-duplicate-keys.mdx

latest9.7 KB
Original Source

import { Tabs, TabItem } from '@astrojs/starlight/components';

<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v2.2.5` - Diagnostic Category: [`lint/correctness/noVueDuplicateKeys`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule doesn't have a fix. - The default severity of this rule is [**error**](/reference/diagnostics#error). - This rule belongs to the following domains: - [`vue`](/linter/domains#vue) - Sources: - Same as [`vue/no-dupe-keys`](https://eslint.vuejs.org/rules/no-dupe-keys)

How to configure

json
{
	"linter": {
		"rules": {
			"correctness": {
				"noVueDuplicateKeys": "error"
			}
		}
	}
}

Description

Disallow duplicate keys in Vue component data, methods, computed properties, and other options.

This rule prevents the use of duplicate keys across different Vue component options such as props, data, computed, methods, and setup. Even if keys don't conflict in the script tag, they may cause issues in the template since Vue allows direct access to these keys.

Examples

Invalid

vue
<script>
export default {
    props: ['foo'],
    data() {
        return {
            foo: 'bar'
        };
    }
};
</script>
<pre class="language-text"><code class="language-text">code-block.vue:2:13 <a href="https://biomejs.dev/linter/rules/no-vue-duplicate-keys">lint/correctness/noVueDuplicateKeys</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Duplicate key </span><span style="color: Tomato;"><strong>foo</strong></span><span style="color: Tomato;"> found in Vue component.</span> <strong>1 │ </strong>export default &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>2 │ </strong> props: ['foo'], <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>3 │ </strong> data() &#123; <strong>4 │ </strong> return &#123; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Key </span><span style="color: lightgreen;"><strong>foo</strong></span><span style="color: lightgreen;"> is also defined here.</span> <strong>3 │ </strong> data() &#123; <strong>4 │ </strong> return &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>5 │ </strong> foo: 'bar' <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>6 │ </strong> &#125;; <strong>7 │ </strong> &#125; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Keys defined in different Vue component options (props, data, methods, computed) can conflict when accessed in the template. Rename the key to avoid conflicts.</span> </code></pre>
vue
<script>
export default {
    data() {
        return {
            message: 'hello'
        };
    },
    methods: {
        message() {
            console.log('duplicate key');
        }
    }
};
</script>
<pre class="language-text"><code class="language-text">code-block.vue:4:13 <a href="https://biomejs.dev/linter/rules/no-vue-duplicate-keys">lint/correctness/noVueDuplicateKeys</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Duplicate key </span><span style="color: Tomato;"><strong>message</strong></span><span style="color: Tomato;"> found in Vue component.</span> <strong>2 │ </strong> data() &#123; <strong>3 │ </strong> return &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>4 │ </strong> message: 'hello' <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>5 │ </strong> &#125;; <strong>6 │ </strong> &#125;, <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Key </span><span style="color: lightgreen;"><strong>message</strong></span><span style="color: lightgreen;"> is also defined here.</span> <strong>6 │ </strong> &#125;, <strong>7 │ </strong> methods: &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>8 │ </strong> message() &#123; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>9 │ </strong> console.log('duplicate key'); <strong>10 │ </strong> &#125; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Keys defined in different Vue component options (props, data, methods, computed) can conflict when accessed in the template. Rename the key to avoid conflicts.</span> </code></pre>
vue
<script>
export default {
    computed: {
        count() {
            return this.value * 2;
        }
    },
    methods: {
        count() {
            this.value++;
        }
    }
};
</script>
<pre class="language-text"><code class="language-text">code-block.vue:3:9 <a href="https://biomejs.dev/linter/rules/no-vue-duplicate-keys">lint/correctness/noVueDuplicateKeys</a> ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ <strong><span style="color: Tomato;">✖</span></strong> <span style="color: Tomato;">Duplicate key </span><span style="color: Tomato;"><strong>count</strong></span><span style="color: Tomato;"> found in Vue component.</span> <strong>1 │ </strong>export default &#123; <strong>2 │ </strong> computed: &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>3 │ </strong> count() &#123; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>4 │ </strong> return this.value &#42; 2; <strong>5 │ </strong> &#125; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Key </span><span style="color: lightgreen;"><strong>count</strong></span><span style="color: lightgreen;"> is also defined here.</span> <strong>6 │ </strong> &#125;, <strong>7 │ </strong> methods: &#123; <strong><span style="color: Tomato;">&gt;</span></strong> <strong>8 │ </strong> count() &#123; <strong> │ </strong> <strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong><strong><span style="color: Tomato;">^</span></strong> <strong>9 │ </strong> this.value++; <strong>10 │ </strong> &#125; <strong><span style="color: lightgreen;">ℹ</span></strong> <span style="color: lightgreen;">Keys defined in different Vue component options (props, data, methods, computed) can conflict when accessed in the template. Rename the key to avoid conflicts.</span> </code></pre>

Valid

vue
<script>
export default {
    props: ['foo'],
    data() {
        return {
            bar: 'baz'
        };
    },
    methods: {
        handleClick() {
            console.log('unique key');
        }
    }
};
</script>
vue
<script>
export default {
    computed: {
        displayMessage() {
            return this.message.toUpperCase();
        }
    },
    methods: {
        clearMessage() {
            this.message = '';
        }
    }
};
</script>
</TabItem> </Tabs>