|
| 1 | +--- |
| 2 | +pageClass: "rule-details" |
| 3 | +sidebarDepth: 0 |
| 4 | +title: "svelte/valid-each-key" |
| 5 | +description: "enforce keys to use variables defined in the `{#each}` block" |
| 6 | +--- |
| 7 | + |
| 8 | +# svelte/valid-each-key |
| 9 | + |
| 10 | +> enforce keys to use variables defined in the `{#each}` block |
| 11 | +
|
| 12 | +- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge> |
| 13 | + |
| 14 | +## :book: Rule Details |
| 15 | + |
| 16 | +This rule reports that `{#each}` block keys does not use the variables which are defined by the `{#each}` block. |
| 17 | + |
| 18 | +<ESLintCodeBlock> |
| 19 | + |
| 20 | +<!--eslint-skip--> |
| 21 | + |
| 22 | +```svelte |
| 23 | +<script> |
| 24 | + /* eslint svelte/valid-each-key: "error" */ |
| 25 | +
|
| 26 | + let things = [ |
| 27 | + { id: 1, name: "apple" }, |
| 28 | + { id: 2, name: "banana" }, |
| 29 | + { id: 3, name: "carrot" }, |
| 30 | + { id: 4, name: "doughnut" }, |
| 31 | + { id: 5, name: "egg" }, |
| 32 | + ] |
| 33 | + let foo = 42 |
| 34 | +</script> |
| 35 | +
|
| 36 | +<!-- ✓ GOOD --> |
| 37 | +{#each things as thing (thing.id)} |
| 38 | + <Thing name={thing.name} /> |
| 39 | +{/each} |
| 40 | +
|
| 41 | +<!-- ✗ BAD --> |
| 42 | +{#each things as thing (foo)} |
| 43 | + <Thing name={thing.name} /> |
| 44 | +{/each} |
| 45 | +``` |
| 46 | + |
| 47 | +</ESLintCodeBlock> |
| 48 | + |
| 49 | +## :wrench: Options |
| 50 | + |
| 51 | +Nothing. |
| 52 | + |
| 53 | +## :couple: Related Rules |
| 54 | + |
| 55 | +- [svelte/require-each-key](./require-each-key.md) |
| 56 | + |
| 57 | +## :books: Further Reading |
| 58 | + |
| 59 | +- [Svelte - Tutorial > 4. Logic / Keyed each blocks](https://svelte.dev/tutorial/keyed-each-blocks) |
| 60 | + |
| 61 | +## :mag: Implementation |
| 62 | + |
| 63 | +- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/src/rules/valid-each-key.ts) |
| 64 | +- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/tests/src/rules/valid-each-key.ts) |
0 commit comments