-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the
require-event-dispatcher-types
rule (#354)
Co-authored-by: Yosuke Ota <otameshiyo23@gmail.com>
- Loading branch information
1 parent
3464f23
commit 2f1d89a
Showing
15 changed files
with
170 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 | ||
--- | ||
|
||
Added the require-event-dispatcher-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,50 @@ | ||
--- | ||
pageClass: "rule-details" | ||
sidebarDepth: 0 | ||
title: "svelte/require-event-dispatcher-types" | ||
description: "require type parameters for createEventDispatcher" | ||
--- | ||
|
||
# svelte/require-event-dispatcher-types | ||
|
||
> require type parameters for createEventDispatcher | ||
## :book: Rule Details | ||
|
||
This rule is aimed to enforce type parameters when calling `createEventDispatcher`. Adding types makes all `dispatch` calls as well as all event listeners typechecked. For more information, see the [svelte docs](https://github.com/sveltejs/language-tools/blob/master/docs/preprocessors/typescript.md#typing-component-events). | ||
|
||
<ESLintCodeBlock language="javascript"> | ||
|
||
<!--eslint-skip--> | ||
|
||
```svelte | ||
<script lang="ts"> | ||
/* eslint svelte/require-event-dispatcher-types: "error" */ | ||
import { createEventDispatcher } from "svelte" | ||
/* ✓ GOOD */ | ||
const dispatch1 = createEventDispatcher<{ one: never; two: number }>() | ||
const dispatch2 = createEventDispatcher<Record<string, never>>() | ||
const dispatch3 = createEventDispatcher<any>() | ||
const dispatch4 = createEventDispatcher<unknown>() | ||
/* ✗ BAD */ | ||
const dispatch5 = createEventDispatcher() | ||
</script> | ||
``` | ||
|
||
</ESLintCodeBlock> | ||
|
||
## :wrench: Options | ||
|
||
Nothing. | ||
|
||
## :rocket: Version | ||
|
||
This rule was introduced in eslint-plugin-svelte v2.16.0 | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/require-event-dispatcher-types.ts) | ||
- [Test source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/tests/src/rules/require-event-dispatcher-types.ts) |
20 changes: 20 additions & 0 deletions
20
src/rules/reference-helpers/svelte-createEventDispatcher.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,20 @@ | ||
import type { TSESTree } from "@typescript-eslint/types" | ||
import { ReferenceTracker } from "eslint-utils" | ||
import type { RuleContext } from "../../types" | ||
|
||
/** Extract 'svelte createEventDispatcher' references */ | ||
export function* extractCreateEventDispatcherReferences( | ||
context: RuleContext, | ||
): Generator<TSESTree.CallExpression, void> { | ||
const referenceTracker = new ReferenceTracker(context.getScope()) | ||
for (const { node } of referenceTracker.iterateEsmReferences({ | ||
svelte: { | ||
[ReferenceTracker.ESM]: true, | ||
createEventDispatcher: { | ||
[ReferenceTracker.CALL]: true, | ||
}, | ||
}, | ||
})) { | ||
yield node as TSESTree.CallExpression | ||
} | ||
} |
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,39 @@ | ||
import { createRule } from "../utils" | ||
import { getLangValue } from "../utils/ast-utils" | ||
import { extractCreateEventDispatcherReferences } from "./reference-helpers/svelte-createEventDispatcher" | ||
|
||
export default createRule("require-event-dispatcher-types", { | ||
meta: { | ||
docs: { | ||
description: "require type parameters for createEventDispatcher", | ||
category: "Best Practices", | ||
recommended: false, | ||
}, | ||
schema: [], | ||
messages: { | ||
missingTypeParameter: `Type parameters missing for the createEventDispatcher function call.`, | ||
}, | ||
type: "suggestion", | ||
}, | ||
create(context) { | ||
let isTs = false | ||
return { | ||
SvelteScriptElement(node) { | ||
const lang = getLangValue(node)?.toLowerCase() | ||
if (lang === "ts" || lang === "typescript") { | ||
isTs = true | ||
} | ||
}, | ||
"Program:exit"() { | ||
if (!isTs) { | ||
return | ||
} | ||
for (const node of extractCreateEventDispatcherReferences(context)) { | ||
if (node.typeParameters === undefined) { | ||
context.report({ node, messageId: "missingTypeParameter" }) | ||
} | ||
} | ||
}, | ||
} | ||
}, | ||
}) |
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/require-event-dispatcher-types/invalid/import-alias01-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: Type parameters missing for the createEventDispatcher function call. | ||
line: 4 | ||
column: 20 | ||
suggestions: null |
5 changes: 5 additions & 0 deletions
5
tests/fixtures/rules/require-event-dispatcher-types/invalid/import-alias01-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,5 @@ | ||
<script lang="ts"> | ||
import { createEventDispatcher as ced } from "svelte" | ||
const dispatch = ced() | ||
</script> |
4 changes: 4 additions & 0 deletions
4
tests/fixtures/rules/require-event-dispatcher-types/invalid/no-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: Type parameters missing for the createEventDispatcher function call. | ||
line: 4 | ||
column: 20 | ||
suggestions: null |
5 changes: 5 additions & 0 deletions
5
tests/fixtures/rules/require-event-dispatcher-types/invalid/no-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,5 @@ | ||
<script lang="ts"> | ||
import { createEventDispatcher } from "svelte" | ||
const dispatch = createEventDispatcher() | ||
</script> |
8 changes: 8 additions & 0 deletions
8
tests/fixtures/rules/require-event-dispatcher-types/valid/has-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,8 @@ | ||
<script lang="ts"> | ||
import { createEventDispatcher } from "svelte" | ||
const dispatch1 = createEventDispatcher<{ one: never; two: number }>() | ||
const dispatch2 = createEventDispatcher<Record<string, never>>() | ||
const dispatch3 = createEventDispatcher<any>() | ||
const dispatch4 = createEventDispatcher<unknown>() | ||
</script> |
5 changes: 5 additions & 0 deletions
5
tests/fixtures/rules/require-event-dispatcher-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,5 @@ | ||
<script> | ||
import { createEventDispatcher } from "svelte" | ||
const dispatch = createEventDispatcher() | ||
</script> |
5 changes: 5 additions & 0 deletions
5
.../fixtures/rules/require-event-dispatcher-types/valid/non-svelte-dispatcher01-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,5 @@ | ||
<script lang="ts"> | ||
import { createEventDispatcher } from "./unknown" | ||
const dispatch = createEventDispatcher() | ||
</script> |
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/require-event-dispatcher-types" | ||
import { loadTestCases } from "../../utils/utils" | ||
|
||
const tester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2020, | ||
sourceType: "module", | ||
}, | ||
}) | ||
|
||
tester.run( | ||
"require-event-dispatcher-types", | ||
rule as any, | ||
loadTestCases("require-event-dispatcher-types"), | ||
) |