Skip to content

Rule proposal: no-array-index-key #3031

@Grawl

Description

@Grawl

Please describe what the rule should do:

Disallow using array item index as key in v-for iteration.

This rule should do the same as no-array-index-key in eslint-plugin-react

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

Code examples that this rule should warn about

Incorrect code:

<template>
  <ul>
    <!-- List is an array, item index cannot be a stable key -->
    <li v-for="(item, index) in list" :key="index"></li>
  </ul>
</template>

Correct code:

<template>
  <ul>
    <!-- List is an array, each item has unique stable key named id -->
    <li v-for="item in list" :key="item.id"></li>
  </ul>
</template>

Correct code:

<template>
  <ul>
    <!-- List is an object, each key is unique stable key -->
    <li v-for="(value, key) in object" :key="key"></li>
 </ul>

Additional context

This issue is a duplicate of #1746 from 2021. I hope now we can determine a difference between array and object in v-for at a linter level using TypeScript.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions