Skip to content

feat: add svelte/require-each-key rule #473

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

Merged
merged 2 commits into from
May 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/gentle-doors-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-svelte": minor
---

feat: add `svelte/require-each-key` rule
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ These rules relate to better ways of doing things to help you avoid problems:
| [svelte/no-unused-svelte-ignore](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-unused-svelte-ignore/) | disallow unused svelte-ignore comments | :star: |
| [svelte/no-useless-mustaches](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-useless-mustaches/) | disallow unnecessary mustache interpolations | :wrench: |
| [svelte/prefer-destructured-store-props](https://sveltejs.github.io/eslint-plugin-svelte/rules/prefer-destructured-store-props/) | destructure values from object stores for better change tracking & fewer redraws | :bulb: |
| [svelte/require-each-key](https://sveltejs.github.io/eslint-plugin-svelte/rules/require-each-key/) | require keyed `{#each}` block | |
| [svelte/require-event-dispatcher-types](https://sveltejs.github.io/eslint-plugin-svelte/rules/require-event-dispatcher-types/) | require type parameters for `createEventDispatcher` | |
| [svelte/require-optimized-style-attribute](https://sveltejs.github.io/eslint-plugin-svelte/rules/require-optimized-style-attribute/) | require style attributes that can be optimized | |
| [svelte/require-stores-init](https://sveltejs.github.io/eslint-plugin-svelte/rules/require-stores-init/) | require initial value in store | |
Expand Down
1 change: 1 addition & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ These rules relate to better ways of doing things to help you avoid problems:
| [svelte/no-unused-svelte-ignore](./rules/no-unused-svelte-ignore.md) | disallow unused svelte-ignore comments | :star: |
| [svelte/no-useless-mustaches](./rules/no-useless-mustaches.md) | disallow unnecessary mustache interpolations | :wrench: |
| [svelte/prefer-destructured-store-props](./rules/prefer-destructured-store-props.md) | destructure values from object stores for better change tracking & fewer redraws | :bulb: |
| [svelte/require-each-key](./rules/require-each-key.md) | require keyed `{#each}` block | |
| [svelte/require-event-dispatcher-types](./rules/require-event-dispatcher-types.md) | require type parameters for `createEventDispatcher` | |
| [svelte/require-optimized-style-attribute](./rules/require-optimized-style-attribute.md) | require style attributes that can be optimized | |
| [svelte/require-stores-init](./rules/require-stores-init.md) | require initial value in store | |
Expand Down
51 changes: 51 additions & 0 deletions docs/rules/require-each-key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
---
pageClass: "rule-details"
sidebarDepth: 0
title: "svelte/require-each-key"
description: "require keyed `{#each}` block"
---

# svelte/require-each-key

> require keyed `{#each}` block

- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge>

## :book: Rule Details

This rule reports `{#each}` block without key

<ESLintCodeBlock>

<!--eslint-skip-->

```svelte
<script>
/* eslint svelte/require-each-key: "error" */
</script>

<!-- ✓ GOOD -->
{#each things as thing (thing.id)}
<Thing name={thing.name} />
{/each}

<!-- ✗ BAD -->
{#each things as thing}
<Thing name={thing.name} />
{/each}
```

</ESLintCodeBlock>

## :wrench: Options

Nothing.

## :books: Further Reading

- [Svelte - Tutorial > 4. Logic / Keyed each blocks](https://svelte.dev/tutorial/keyed-each-blocks)

## :mag: Implementation

- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/src/rules/require-each-key.ts)
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/tests/src/rules/require-each-key.ts)
27 changes: 27 additions & 0 deletions src/rules/require-each-key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import type { AST } from "svelte-eslint-parser"
import { createRule } from "../utils"

export default createRule("require-each-key", {
meta: {
docs: {
description: "require keyed `{#each}` block",
category: "Best Practices",
recommended: false,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can toggle to true in major version up?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. I personally like to enforce the key, but vue/require-v-for-key is unpopular even in eslint-plugin-vue, so I think it's better not to include it in the preset. (However, the Vue style guide recommends using keys.)
I think it will be easier to introduce eslint if the presets are not too strict.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay!

},
schema: [],
messages: { expectedKey: "Each block should have a key" },
type: "suggestion",
},
create(context) {
return {
SvelteEachBlock(node: AST.SvelteEachBlock) {
if (node.key == null) {
context.report({
node,
messageId: "expectedKey",
})
}
},
}
},
})
2 changes: 2 additions & 0 deletions src/utils/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import noUselessMustaches from "../rules/no-useless-mustaches"
import preferClassDirective from "../rules/prefer-class-directive"
import preferDestructuredStoreProps from "../rules/prefer-destructured-store-props"
import preferStyleDirective from "../rules/prefer-style-directive"
import requireEachKey from "../rules/require-each-key"
import requireEventDispatcherTypes from "../rules/require-event-dispatcher-types"
import requireOptimizedStyleAttribute from "../rules/require-optimized-style-attribute"
import requireStoreCallbacksUseSetParam from "../rules/require-store-callbacks-use-set-param"
Expand Down Expand Up @@ -102,6 +103,7 @@ export const rules = [
preferClassDirective,
preferDestructuredStoreProps,
preferStyleDirective,
requireEachKey,
requireEventDispatcherTypes,
requireOptimizedStyleAttribute,
requireStoreCallbacksUseSetParam,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- message: Each block should have a key
line: 19
column: 1
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script>
import Thing from "./Thing.svelte"

let things = [
{ id: 1, name: "apple" },
{ id: 2, name: "banana" },
{ id: 3, name: "carrot" },
{ id: 4, name: "doughnut" },
{ id: 5, name: "egg" },
]

function handleClick() {
things = things.slice(1)
}
</script>

<button on:click={handleClick}> Remove first thing </button>

{#each things as thing}
<Thing name={thing.name} />
{/each}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<script>
import Thing from "./Thing.svelte"

let things = [
{ id: 1, name: "apple" },
{ id: 2, name: "banana" },
{ id: 3, name: "carrot" },
{ id: 4, name: "doughnut" },
{ id: 5, name: "egg" },
]

function handleClick() {
things = things.slice(1)
}
</script>

<button on:click={handleClick}> Remove first thing </button>

{#each things as thing (thing.id)}
<Thing name={thing.name} />
{/each}
12 changes: 12 additions & 0 deletions tests/src/rules/require-each-key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { RuleTester } from "eslint"
import rule from "../../../src/rules/require-each-key"
import { loadTestCases } from "../../utils/utils"

const tester = new RuleTester({
parserOptions: {
ecmaVersion: 2020,
sourceType: "module",
},
})

tester.run("require-each-key", rule as any, loadTestCases("require-each-key"))