-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added the experimental-require-slot-types rule (#368)
Co-authored-by: ota-meshi <[email protected]>
- Loading branch information
1 parent
5c41254
commit fcb5e31
Showing
13 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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: added the `svelte/experimental-require-slot-types` rule |
This file contains 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 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 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,113 @@ | ||
--- | ||
pageClass: "rule-details" | ||
sidebarDepth: 0 | ||
title: "svelte/experimental-require-slot-types" | ||
description: "require slot type declaration using the `$$Slots` interface" | ||
--- | ||
|
||
# svelte/experimental-require-slot-types | ||
|
||
> require slot type declaration using the `$$Slots` interface | ||
- :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 enforces the presence of the `$$Slots` interface if any slots are present in the component. This interface declares all of the used slots and their props and enables typechecking both in the component itself as well as all components that include it. | ||
The `$$Slots` interface is experimental and is documented in [svelte RFC #38](https://github.com/dummdidumm/rfcs/blob/ts-typedefs-within-svelte-components/text/ts-typing-props-slots-events.md#typing-slots). | ||
|
||
<ESLintCodeBlock> | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<!-- ✓ GOOD --> | ||
<script> | ||
/* eslint svelte/experimental-require-slot-types: "error" */ | ||
</script> | ||
<b>No slots here!</b> | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
<ESLintCodeBlock> | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<!-- ✓ GOOD --> | ||
<script> | ||
/* eslint svelte/experimental-require-slot-types: "error" */ | ||
interface $$Slots { | ||
default: Record<string, never>; | ||
} | ||
</script> | ||
<slot /> | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
<ESLintCodeBlock> | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<!-- ✓ GOOD --> | ||
<script lang="ts"> | ||
/* eslint svelte/experimental-require-slot-types: "error" */ | ||
interface $$Slots { | ||
default: { prop: boolean; }; | ||
} | ||
</script> | ||
<slot prop={true} /> | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
<ESLintCodeBlock> | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<!-- ✓ GOOD --> | ||
<script lang="ts"> | ||
/* eslint svelte/experimental-require-slot-types: "error" */ | ||
interface $$Slots { | ||
named: Record<string, never>; | ||
} | ||
</script> | ||
<slot name = "named" /> | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
<ESLintCodeBlock> | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<!-- ✗ BAD --> | ||
<script> | ||
/* eslint svelte/experimental-require-slot-types: "error" */ | ||
</script> | ||
<slot /> | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/experimental-require-slot-types.ts) | ||
- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/experimental-require-slot-types.ts) |
This file contains 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,50 @@ | ||
import { createRule } from "../utils" | ||
import { getLangValue } from "../utils/ast-utils" | ||
|
||
export default createRule("experimental-require-slot-types", { | ||
meta: { | ||
docs: { | ||
description: | ||
"require slot type declaration using the `$$Slots` interface", | ||
category: "Experimental", | ||
recommended: false, | ||
}, | ||
schema: [], | ||
messages: { | ||
missingSlotsInterface: `The component must define the $$Slots interface.`, | ||
}, | ||
type: "suggestion", | ||
}, | ||
create(context) { | ||
let isTs = false | ||
let hasSlot = false | ||
let hasInterface = false | ||
return { | ||
SvelteScriptElement(node) { | ||
const lang = getLangValue(node)?.toLowerCase() | ||
isTs = lang === "ts" || lang === "typescript" | ||
}, | ||
SvelteElement(node) { | ||
if (node.name.type === "SvelteName" && node.name.name === "slot") { | ||
hasSlot = true | ||
} | ||
}, | ||
TSInterfaceDeclaration(node) { | ||
if (node.id.name === "$$Slots") { | ||
hasInterface = true | ||
} | ||
}, | ||
"Program:exit"() { | ||
if (isTs && hasSlot && !hasInterface) { | ||
context.report({ | ||
loc: { | ||
line: 1, | ||
column: 1, | ||
}, | ||
messageId: "missingSlotsInterface", | ||
}) | ||
} | ||
}, | ||
} | ||
}, | ||
}) |
This file contains 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/experimental-require-slot-types/invalid/no-slot-types01-errors.yaml
This file contains 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: The component must define the $$Slots interface. | ||
line: 1 | ||
column: 2 | ||
suggestions: null |
4 changes: 4 additions & 0 deletions
4
tests/fixtures/rules/experimental-require-slot-types/invalid/no-slot-types01-input.svelte
This file contains 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 @@ | ||
<script lang="ts"> | ||
</script> | ||
|
||
<slot /> |
7 changes: 7 additions & 0 deletions
7
tests/fixtures/rules/experimental-require-slot-types/valid/has-slot-types01-input.svelte
This file contains 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,7 @@ | ||
<script lang="ts"> | ||
interface $$Slots { | ||
defalt: Record<string, never> | ||
} | ||
</script> | ||
|
||
<slot /> |
7 changes: 7 additions & 0 deletions
7
tests/fixtures/rules/experimental-require-slot-types/valid/named-slot01-input.svelte
This file contains 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,7 @@ | ||
<script lang="ts"> | ||
interface $$Slots { | ||
named: Record<string, never> | ||
} | ||
</script> | ||
|
||
<slot name="named" /> |
4 changes: 4 additions & 0 deletions
4
tests/fixtures/rules/experimental-require-slot-types/valid/no-slots01-input.svelte
This file contains 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 @@ | ||
<script lang="ts"> | ||
</script> | ||
|
||
content |
4 changes: 4 additions & 0 deletions
4
tests/fixtures/rules/experimental-require-slot-types/valid/no-typescript01-input.svelte
This file contains 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 @@ | ||
<script> | ||
</script> | ||
|
||
<slot /> |
This file contains 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,16 @@ | ||
import { RuleTester } from "eslint" | ||
import rule from "../../../src/rules/experimental-require-slot-types" | ||
import { loadTestCases } from "../../utils/utils" | ||
|
||
const tester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: "module", | ||
}, | ||
}) | ||
|
||
tester.run( | ||
"experimental-require-slot-types", | ||
rule as any, | ||
loadTestCases("experimental-require-slot-types"), | ||
) |