forked from sveltejs/eslint-plugin-svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexperimental-require-strict-events.ts
53 lines (50 loc) · 1.51 KB
/
experimental-require-strict-events.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import type { AST } from "svelte-eslint-parser"
import { createRule } from "../utils"
import { findAttribute, getLangValue } from "../utils/ast-utils"
const EVENTS_TYPE_NAME = "$$Events"
export default createRule("experimental-require-strict-events", {
meta: {
docs: {
description: "require the strictEvents attribute on `<script>` tags",
category: "Experimental",
recommended: false,
},
schema: [],
messages: {
missingStrictEvents: `The component must have the strictEvents attribute on its <script> tag or it must define the $$Events interface.`,
},
type: "suggestion",
},
create(context) {
let isTs = false
let hasAttribute = false
let hasDeclaredEvents = false
let scriptNode: AST.SvelteScriptElement
return {
SvelteScriptElement(node) {
const lang = getLangValue(node)?.toLowerCase()
isTs = lang === "ts" || lang === "typescript"
hasAttribute = findAttribute(node, "strictEvents") !== null
scriptNode = node
},
TSInterfaceDeclaration(node) {
if (node.id.name === EVENTS_TYPE_NAME) {
hasDeclaredEvents = true
}
},
TSTypeAliasDeclaration(node) {
if (node.id.name === EVENTS_TYPE_NAME) {
hasDeclaredEvents = true
}
},
"Program:exit"() {
if (isTs && !hasAttribute && !hasDeclaredEvents) {
context.report({
node: scriptNode,
messageId: "missingStrictEvents",
})
}
},
}
},
})