-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
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", | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
tests/fixtures/rules/require-each-key/invalid/each-block-without-key01-errors.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
21 changes: 21 additions & 0 deletions
21
tests/fixtures/rules/require-each-key/invalid/each-block-without-key01-input.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
21 changes: 21 additions & 0 deletions
21
tests/fixtures/rules/require-each-key/valid/keyed-each-block01-input.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay!