forked from sveltejs/eslint-plugin-svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-not-data-props-in-kit-pages.ts
62 lines (58 loc) · 1.76 KB
/
no-not-data-props-in-kit-pages.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
54
55
56
57
58
59
60
61
62
import type { AST } from "svelte-eslint-parser"
import type * as ESTree from "estree"
import { createRule } from "../utils"
import { isKitPageComponent } from "../utils/svelte-kit"
const EXPECTED_PROP_NAMES = ["data", "errors"]
export default createRule("no-not-data-props-in-kit-pages", {
meta: {
docs: {
description:
"disallow props other than data or errors in Svelte Kit page components.",
category: "Possible Errors",
// TODO Switch to recommended in the major version.
recommended: false,
},
schema: [],
messages: {
unexpected:
"disallow props other than data or errors in Svelte Kit page components.",
},
type: "problem",
},
create(context) {
if (!isKitPageComponent(context)) return {}
let isScript = false
return {
// <script>
[`Program > SvelteScriptElement > SvelteStartTag`]: (
node: AST.SvelteStartTag,
) => {
// except for <script context="module">
isScript = !node.attributes.some(
(a) =>
a.type === "SvelteAttribute" &&
a.key.name === "context" &&
a.value.some(
(v) => v.type === "SvelteLiteral" && v.value === "module",
),
)
},
// </script>
"Program > SvelteScriptElement:exit": () => {
isScript = false
},
// export let xxx
[`ExportNamedDeclaration > VariableDeclaration > VariableDeclarator > Identifier`]:
(node: ESTree.Identifier) => {
if (!isScript) return {}
const { name } = node
if (EXPECTED_PROP_NAMES.includes(name)) return {}
return context.report({
node,
loc: node.loc!,
messageId: "unexpected",
})
},
}
},
})