Skip to content

Commit 088358a

Browse files
committed
chore: minor refactor
1 parent 2f1d89a commit 088358a

File tree

8 files changed

+25
-37
lines changed

8 files changed

+25
-37
lines changed

.changeset/witty-donuts-jog.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"eslint-plugin-svelte": minor
33
---
44

5-
Added the require-event-dispatcher-types rule
5+
feat: added the `svelte/require-event-dispatcher-types` rule

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ These rules relate to better ways of doing things to help you avoid problems:
336336
| [svelte/no-unused-svelte-ignore](https://ota-meshi.github.io/eslint-plugin-svelte/rules/no-unused-svelte-ignore/) | disallow unused svelte-ignore comments | :star: |
337337
| [svelte/no-useless-mustaches](https://ota-meshi.github.io/eslint-plugin-svelte/rules/no-useless-mustaches/) | disallow unnecessary mustache interpolations | :wrench: |
338338
| [svelte/prefer-destructured-store-props](https://ota-meshi.github.io/eslint-plugin-svelte/rules/prefer-destructured-store-props/) | destructure values from object stores for better change tracking & fewer redraws | :bulb: |
339-
| [svelte/require-event-dispatcher-types](https://ota-meshi.github.io/eslint-plugin-svelte/rules/require-event-dispatcher-types/) | require type parameters for createEventDispatcher | |
339+
| [svelte/require-event-dispatcher-types](https://ota-meshi.github.io/eslint-plugin-svelte/rules/require-event-dispatcher-types/) | require type parameters for `createEventDispatcher` | |
340340
| [svelte/require-optimized-style-attribute](https://ota-meshi.github.io/eslint-plugin-svelte/rules/require-optimized-style-attribute/) | require style attributes that can be optimized | |
341341
| [svelte/require-stores-init](https://ota-meshi.github.io/eslint-plugin-svelte/rules/require-stores-init/) | require initial value in store | |
342342

docs/rules.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ These rules relate to better ways of doing things to help you avoid problems:
5555
| [svelte/no-unused-svelte-ignore](./rules/no-unused-svelte-ignore.md) | disallow unused svelte-ignore comments | :star: |
5656
| [svelte/no-useless-mustaches](./rules/no-useless-mustaches.md) | disallow unnecessary mustache interpolations | :wrench: |
5757
| [svelte/prefer-destructured-store-props](./rules/prefer-destructured-store-props.md) | destructure values from object stores for better change tracking & fewer redraws | :bulb: |
58-
| [svelte/require-event-dispatcher-types](./rules/require-event-dispatcher-types.md) | require type parameters for createEventDispatcher | |
58+
| [svelte/require-event-dispatcher-types](./rules/require-event-dispatcher-types.md) | require type parameters for `createEventDispatcher` | |
5959
| [svelte/require-optimized-style-attribute](./rules/require-optimized-style-attribute.md) | require style attributes that can be optimized | |
6060
| [svelte/require-stores-init](./rules/require-stores-init.md) | require initial value in store | |
6161

docs/rules/require-event-dispatcher-types.md

+6-8
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@
22
pageClass: "rule-details"
33
sidebarDepth: 0
44
title: "svelte/require-event-dispatcher-types"
5-
description: "require type parameters for createEventDispatcher"
5+
description: "require type parameters for `createEventDispatcher`"
66
---
77

88
# svelte/require-event-dispatcher-types
99

10-
> require type parameters for createEventDispatcher
10+
> require type parameters for `createEventDispatcher`
11+
12+
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> **_This rule has not been released yet._** </badge>
1113

1214
## :book: Rule Details
1315

14-
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).
16+
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).
1517

16-
<ESLintCodeBlock language="javascript">
18+
<ESLintCodeBlock>
1719

1820
<!--eslint-skip-->
1921

@@ -40,10 +42,6 @@ This rule is aimed to enforce type parameters when calling `createEventDispatche
4042

4143
Nothing.
4244

43-
## :rocket: Version
44-
45-
This rule was introduced in eslint-plugin-svelte v2.16.0
46-
4745
## :mag: Implementation
4846

4947
- [Rule source](https://github.com/ota-meshi/eslint-plugin-svelte/blob/main/src/rules/require-event-dispatcher-types.ts)

src/rules/reference-helpers/svelte-createEventDispatcher.ts

-20
This file was deleted.

src/rules/require-event-dispatcher-types.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
1+
import { ReferenceTracker } from "eslint-utils"
12
import { createRule } from "../utils"
23
import { getLangValue } from "../utils/ast-utils"
3-
import { extractCreateEventDispatcherReferences } from "./reference-helpers/svelte-createEventDispatcher"
4+
import type { TSESTree } from "@typescript-eslint/types"
45

56
export default createRule("require-event-dispatcher-types", {
67
meta: {
78
docs: {
8-
description: "require type parameters for createEventDispatcher",
9+
description: "require type parameters for `createEventDispatcher`",
910
category: "Best Practices",
1011
recommended: false,
1112
},
1213
schema: [],
1314
messages: {
14-
missingTypeParameter: `Type parameters missing for the createEventDispatcher function call.`,
15+
missingTypeParameter: `Type parameters missing for the \`createEventDispatcher\` function call.`,
1516
},
1617
type: "suggestion",
1718
},
@@ -28,7 +29,16 @@ export default createRule("require-event-dispatcher-types", {
2829
if (!isTs) {
2930
return
3031
}
31-
for (const node of extractCreateEventDispatcherReferences(context)) {
32+
const referenceTracker = new ReferenceTracker(context.getScope())
33+
for (const { node: n } of referenceTracker.iterateEsmReferences({
34+
svelte: {
35+
[ReferenceTracker.ESM]: true,
36+
createEventDispatcher: {
37+
[ReferenceTracker.CALL]: true,
38+
},
39+
},
40+
})) {
41+
const node = n as TSESTree.CallExpression
3242
if (node.typeParameters === undefined) {
3343
context.report({ node, messageId: "missingTypeParameter" })
3444
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
- message: Type parameters missing for the createEventDispatcher function call.
1+
- message: Type parameters missing for the `createEventDispatcher` function call.
22
line: 4
33
column: 20
44
suggestions: null
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
- message: Type parameters missing for the createEventDispatcher function call.
1+
- message: Type parameters missing for the `createEventDispatcher` function call.
22
line: 4
33
column: 20
44
suggestions: null

0 commit comments

Comments
 (0)