forked from sveltejs/eslint-plugin-svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-export-load-in-svelte-module-in-kit-pages.ts
54 lines (50 loc) · 1.64 KB
/
no-export-load-in-svelte-module-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
import type * as ESTree from "estree"
import { createRule } from "../utils"
import { isKitPageComponent, hasSvelteKit } from "./kit-helpers/kit-helpers"
export default createRule("no-export-load-in-svelte-module-in-kit-pages", {
meta: {
docs: {
description:
"Disallow exporting load functions in `*.svelte` module in Svelte Kit page components.",
category: "Possible Errors",
recommended: true,
},
schema: [],
messages: {
unexpected:
"Disallow exporting load functions in `*.svelte` module in Svelte Kit page components.",
},
type: "problem",
},
create(context) {
if (!hasSvelteKit || !isKitPageComponent(context)) return {}
let isModule = false
return {
// <script context="module">
[`Program > SvelteScriptElement > SvelteStartTag > SvelteAttribute[key.name="context"] > SvelteLiteral[value="module"]`]:
() => {
isModule = true
},
// <script>
[`Program > SvelteScriptElement > SvelteStartTag > SvelteAttribute[key.name="context"] > SvelteLiteral[value!="module"]`]:
() => {
isModule = false
},
// </script>
["SvelteEndTag"]: () => {
isModule = false
},
// export function load() {}
// export const load = () => {}
[`ExportNamedDeclaration :matches(FunctionDeclaration, VariableDeclaration > VariableDeclarator) > Identifier[name="load"]`]:
(node: ESTree.Identifier) => {
if (!isModule) return {}
return context.report({
node,
loc: node.loc!,
messageId: "unexpected",
})
},
}
},
})