Skip to content

feat: add svelte/valid-each-key rule #475

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/shy-moles-deliver.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-svelte": minor
---

feat: add `svelte/valid-each-key` rule
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ These rules relate to better ways of doing things to help you avoid problems:
| [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 | |
| [svelte/valid-each-key](https://sveltejs.github.io/eslint-plugin-svelte/rules/valid-each-key/) | enforce keys to use variables defined in the `{#each}` block | |

## Stylistic Issues

Expand Down
1 change: 1 addition & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ These rules relate to better ways of doing things to help you avoid problems:
| [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 | |
| [svelte/valid-each-key](./rules/valid-each-key.md) | enforce keys to use variables defined in the `{#each}` block | |

## Stylistic Issues

Expand Down
4 changes: 4 additions & 0 deletions docs/rules/require-each-key.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ This rule reports `{#each}` block without key

Nothing.

## :couple: Related Rules

- [svelte/valid-each-key](./valid-each-key.md)

## :books: Further Reading

- [Svelte - Tutorial > 4. Logic / Keyed each blocks](https://svelte.dev/tutorial/keyed-each-blocks)
Expand Down
64 changes: 64 additions & 0 deletions docs/rules/valid-each-key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
---
pageClass: "rule-details"
sidebarDepth: 0
title: "svelte/valid-each-key"
description: "enforce keys to use variables defined in the `{#each}` block"
---

# svelte/valid-each-key

> enforce keys to use variables defined in the `{#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 that `{#each}` block keys does not use the variables which are defined by the `{#each}` block.

<ESLintCodeBlock>

<!--eslint-skip-->

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

let things = [
{ id: 1, name: "apple" },
{ id: 2, name: "banana" },
{ id: 3, name: "carrot" },
{ id: 4, name: "doughnut" },
{ id: 5, name: "egg" },
]
let foo = 42
</script>

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

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

</ESLintCodeBlock>

## :wrench: Options

Nothing.

## :couple: Related Rules

- [svelte/require-each-key](./require-each-key.md)

## :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/valid-each-key.ts)
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/tests/src/rules/valid-each-key.ts)
59 changes: 59 additions & 0 deletions src/rules/valid-each-key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import type { AST } from "svelte-eslint-parser"
import { createRule } from "../utils"
import { getScope } from "../utils/ast-utils"

export default createRule("valid-each-key", {
meta: {
docs: {
description:
"enforce keys to use variables defined in the `{#each}` block",
category: "Best Practices",
// TODO Switch to recommended in the major version.
recommended: false,
},
schema: [],
messages: {
keyUseEachVars:
"Expected key to use the variables which are defined by the `{#each}` block.",
},
type: "suggestion",
},
create(context) {
return {
SvelteEachBlock(node: AST.SvelteEachBlock) {
if (node.key == null) {
return
}
const scope = getScope(context, node.key)
for (const variable of scope.variables) {
if (
!variable.defs.some(
(def) =>
(node.context.range[0] <= def.name.range[0] &&
def.name.range[1] <= node.context.range[1]) ||
(node.index &&
node.index.range[0] <= def.name.range[0] &&
def.name.range[1] <= node.index.range[1]),
)
) {
// It's not an iteration variable.
continue
}
for (const reference of variable.references) {
if (
node.key.range[0] <= reference.identifier.range[0] &&
reference.identifier.range[1] <= node.key.range[1]
) {
// A variable is used in the key.
return
}
}
}
context.report({
node: node.key,
messageId: "keyUseEachVars",
})
},
}
},
})
2 changes: 2 additions & 0 deletions src/utils/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import sortAttributes from "../rules/sort-attributes"
import spacedHtmlComment from "../rules/spaced-html-comment"
import system from "../rules/system"
import validCompile from "../rules/valid-compile"
import validEachKey from "../rules/valid-each-key"
import validPropNamesInKitPages from "../rules/valid-prop-names-in-kit-pages"

export const rules = [
Expand Down Expand Up @@ -115,5 +116,6 @@ export const rules = [
spacedHtmlComment,
system,
validCompile,
validEachKey,
validPropNamesInKitPages,
] as RuleModule[]
1 change: 1 addition & 0 deletions tests/fixtures/rules/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ module.exports = {
"one-var": "off",
"func-style": "off",
"no-console": "off",
"no-use-before-define": "off",
"node/no-unsupported-features/es-syntax": "off",

"@typescript-eslint/await-thenable": "off",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- message: Expected key to use the variables which are defined by the `{#each}` block.
line: 11
column: 25
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script>
let things = [
{ id: 1, name: "apple" },
{ id: 2, name: "banana" },
{ id: 3, name: "carrot" },
{ id: 4, name: "doughnut" },
{ id: 5, name: "egg" },
]
</script>

{#each things as thing (key)}
{@const key = thing.id}
{thing.name}
{/each}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- message: Expected key to use the variables which are defined by the `{#each}` block.
line: 12
column: 25
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script>
let things = [
{ id: 1, name: "apple" },
{ id: 2, name: "banana" },
{ id: 3, name: "carrot" },
{ id: 4, name: "doughnut" },
{ id: 5, name: "egg" },
]
const foo = "key"
</script>

{#each things as thing (foo)}
{thing.name}
{/each}
16 changes: 16 additions & 0 deletions tests/fixtures/rules/valid-each-key/valid/call-key01-input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script>
let things = [
{ id: 1, name: "apple" },
{ id: 2, name: "banana" },
{ id: 3, name: "carrot" },
{ id: 4, name: "doughnut" },
{ id: 5, name: "egg" },
]
function fn(thing) {
return thing.id
}
</script>

{#each things as thing (fn(thing))}
{thing.name}
{/each}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
let things = [
{ id: 1, name: "apple" },
{ id: 2, name: "banana" },
{ id: 3, name: "carrot" },
{ id: 4, name: "doughnut" },
{ id: 5, name: "egg" },
]
</script>

{#each things as { id, name } (id)}
{name}
{/each}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
let things = [
{ id: 1, name: "apple" },
{ id: 2, name: "banana" },
{ id: 3, name: "carrot" },
{ id: 4, name: "doughnut" },
{ id: 5, name: "egg" },
]
</script>

{#each things as thing (`thing_id=${thing.id}`)}
{thing.name}
{/each}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<script>
let things = [
{ id: 1, name: "apple" },
{ id: 2, name: "banana" },
{ id: 3, name: "carrot" },
{ id: 4, name: "doughnut" },
{ id: 5, name: "egg" },
]
const foo = "thing_id="
</script>

{#each things as thing (foo + thing.id)}
{thing.name}
{/each}
13 changes: 13 additions & 0 deletions tests/fixtures/rules/valid-each-key/valid/index-key01-input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
let things = [
{ id: 1, name: "apple" },
{ id: 2, name: "banana" },
{ id: 3, name: "carrot" },
{ id: 4, name: "doughnut" },
{ id: 5, name: "egg" },
]
</script>

{#each things as thing, index (index)}
{thing.name}
{/each}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script>
let things = [
{ id: 1, name: "apple" },
{ id: 2, name: "banana" },
{ id: 3, name: "carrot" },
{ id: 4, name: "doughnut" },
{ id: 5, name: "egg" },
]
</script>

{#each things as thing (thing.id)}
{thing.name}
{/each}
12 changes: 12 additions & 0 deletions tests/src/rules/valid-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/valid-each-key"
import { loadTestCases } from "../../utils/utils"

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

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