CORRECT001 · Keyed each block
Severity: warning · Category: correctness
What it checks
Section titled “What it checks”Flags an {#each} block with no key. Checked by static (CLI) analysis of every .svelte component under src/. Two shapes are ignored:
- A constant inline array literal (
{#each [1, 2, 3] as n}) — it has a fixed length and never reorders, so a key cannot help. - An itemless each (
{#each { length: 8 }, i}, the “render N times” pattern) — there is no item identity to key on; the only possible key is the index itself, which is a no-op.
Why it matters
Section titled “Why it matters”Without a key, when the list reorders or items are inserted/removed, Svelte adds/removes nodes at the end and rewrites the data of the DOM nodes in between — so element state (focus, inputs, transitions) sticks to positions instead of items, and extra work is done. A key lets Svelte insert, move, and delete the right nodes instead.
How to fix
Section titled “How to fix”{#each items as item (item.id)} <li>{item.name}</li>{/each}