Skip to content
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

feat: added the no-useless-children-snippet rule #922

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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/loud-zoos-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-svelte': minor
---

feat: added the no-useless-children-snippet rule
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ These rules relate to better ways of doing things to help you avoid problems:
| [svelte/no-svelte-internal](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-svelte-internal/) | svelte/internal will be removed in Svelte 6. | |
| [svelte/no-unused-class-name](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-unused-class-name/) | disallow the use of a class in the template without a corresponding style | |
| [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-children-snippet](https://sveltejs.github.io/eslint-plugin-svelte/rules/no-useless-children-snippet/) | disallow explicit children snippet where it's not needed | |
| [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 | |
Expand Down
1 change: 1 addition & 0 deletions docs/rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ These rules relate to better ways of doing things to help you avoid problems:
| [svelte/no-svelte-internal](./rules/no-svelte-internal.md) | svelte/internal will be removed in Svelte 6. | |
| [svelte/no-unused-class-name](./rules/no-unused-class-name.md) | disallow the use of a class in the template without a corresponding style | |
| [svelte/no-unused-svelte-ignore](./rules/no-unused-svelte-ignore.md) | disallow unused svelte-ignore comments | :star: |
| [svelte/no-useless-children-snippet](./rules/no-useless-children-snippet.md) | disallow explicit children snippet where it's not needed | |
| [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 | |
Expand Down
61 changes: 61 additions & 0 deletions docs/rules/no-useless-children-snippet.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
---
pageClass: 'rule-details'
sidebarDepth: 0
title: 'svelte/no-useless-children-snippet'
description: "disallow explicit children snippet where it's not needed"
---

# svelte/no-useless-children-snippet

> disallow explicit children snippet where it's not needed

- :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

Any content inside component tags that is not a snippet declaration implicitly becomes part of the children snippet. Thus, declaring the children snippet explicitly is only necessary when the snippet has parameters.

<ESLintCodeBlock>

<!--eslint-skip-->

```svelte
<script>
/* eslint svelte/no-useless-children-snippet: "error" */

import { Foo } from './Foo.svelte';
</script>

<!-- ✓ GOOD -->
<Foo>
{#snippet bar()}
Hello
{/snippet}
</Foo>

<Foo>
{#snippet children(val)}
Hello {val}
{/snippet}
</Foo>

<Foo>Hello</Foo>

<!-- ✗ BAD -->
<Foo>
{#snippet children()}
Hello
{/snippet}
</Foo>
```

</ESLintCodeBlock>

## :wrench: Options

Nothing.

## :mag: Implementation

- [Rule source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/src/rules/no-useless-children-snippet.ts)
- [Test source](https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/tests/src/rules/no-useless-children-snippet.ts)
5 changes: 5 additions & 0 deletions packages/eslint-plugin-svelte/src/rule-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,11 @@ export interface RuleOptions {
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-unused-svelte-ignore/
*/
'svelte/no-unused-svelte-ignore'?: Linter.RuleEntry<[]>
/**
* disallow explicit children snippet where it's not needed
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-useless-children-snippet/
*/
'svelte/no-useless-children-snippet'?: Linter.RuleEntry<[]>
/**
* disallow unnecessary mustache interpolations
* @see https://sveltejs.github.io/eslint-plugin-svelte/rules/no-useless-mustaches/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { createRule } from '../utils';

export default createRule('no-useless-children-snippet', {
meta: {
docs: {
description: "disallow explicit children snippet where it's not needed",
category: 'Best Practices',
recommended: false
},
schema: [],
messages: {
uselessSnippet: "Found an unnecessary children snippet."

Check failure on line 12 in packages/eslint-plugin-svelte/src/rules/no-useless-children-snippet.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `"Found·an·unnecessary·children·snippet."` with `'Found·an·unnecessary·children·snippet.'`
},
type: 'suggestion'
},
create(context) {
return {
SvelteSnippetBlock(node) {
if (node.id.name === "children" && node.params.length === 0) {

Check failure on line 19 in packages/eslint-plugin-svelte/src/rules/no-useless-children-snippet.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `"children"` with `'children'`
context.report({node, messageId: "uselessSnippet"})

Check failure on line 20 in packages/eslint-plugin-svelte/src/rules/no-useless-children-snippet.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `node,·messageId:·"uselessSnippet"})` with `·node,·messageId:·'uselessSnippet'·});`
}
}
};
}
});
2 changes: 2 additions & 0 deletions packages/eslint-plugin-svelte/src/utils/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import noTrailingSpaces from '../rules/no-trailing-spaces';
import noUnknownStyleDirectiveProperty from '../rules/no-unknown-style-directive-property';
import noUnusedClassName from '../rules/no-unused-class-name';
import noUnusedSvelteIgnore from '../rules/no-unused-svelte-ignore';
import noUselessChildrenSnippet from '../rules/no-useless-children-snippet';
import noUselessMustaches from '../rules/no-useless-mustaches';
import preferClassDirective from '../rules/prefer-class-directive';
import preferDestructuredStoreProps from '../rules/prefer-destructured-store-props';
Expand Down Expand Up @@ -116,6 +117,7 @@ export const rules = [
noUnknownStyleDirectiveProperty,
noUnusedClassName,
noUnusedSvelteIgnore,
noUselessChildrenSnippet,
noUselessMustaches,
preferClassDirective,
preferDestructuredStoreProps,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- message: Found an unnecessary children snippet.
line: 2
column: 3
suggestions: null
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Foo>
{#snippet children()}
Hello
{/snippet}
</Foo>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Foo>
Hello
</Foo>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Foo>
{#snippet bar()}
Hello
{/snippet}
</Foo>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Foo>
{#snippet children(val)}
Hello {val}
{/snippet}
</Foo>
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { RuleTester } from '../../utils/eslint-compat';
import rule from '../../../src/rules/no-useless-children-snippet';
import { loadTestCases } from '../../utils/utils';

const tester = new RuleTester({
languageOptions: {
ecmaVersion: 2020,
sourceType: 'module'
}
});

tester.run('no-useless-children-snippet', rule as any, loadTestCases('no-useless-children-snippet'));
Loading