forked from sveltejs/eslint-plugin-svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalid-prop-names-in-kit-pages.ts
83 lines (77 loc) · 2.31 KB
/
valid-prop-names-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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import type { AST } from "svelte-eslint-parser"
import type { TSESTree } from "@typescript-eslint/types"
import { createRule } from "../utils"
import { isKitPageComponent } from "../utils/svelte-kit"
const EXPECTED_PROP_NAMES = ["data", "errors", "trailingSlash"]
export default createRule("valid-prop-names-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
},
"ExportNamedDeclaration > VariableDeclaration > VariableDeclarator": (
node: TSESTree.VariableDeclarator,
) => {
if (!isScript) return
// export let foo
if (node.id.type === "Identifier") {
if (!EXPECTED_PROP_NAMES.includes(node.id.name)) {
context.report({
node,
loc: node.loc,
messageId: "unexpected",
})
}
return
}
// export let { xxx, yyy } = zzz
if (node.id.type !== "ObjectPattern") return
for (const p of node.id.properties) {
if (
p.type === "Property" &&
p.value.type === "Identifier" &&
!EXPECTED_PROP_NAMES.includes(p.value.name)
) {
context.report({
node: p.value,
loc: p.value.loc,
messageId: "unexpected",
})
}
}
},
}
},
})