forked from sveltejs/eslint-plugin-svelte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno-store-async.ts
44 lines (42 loc) · 1.19 KB
/
no-store-async.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
import { createRule } from "../utils"
import type * as ESTree from "estree"
export default createRule("no-store-async", {
meta: {
docs: {
description:
"disallow using async/await inside svelte stores because it causes issues with the auto-unsubscribing features",
category: "Possible Errors",
recommended: true,
default: "error",
},
schema: [],
messages: {
unexpected: "Do not pass async functions to svelte stores.",
},
type: "problem",
},
create(context) {
return {
CallExpression(node: ESTree.CallExpression) {
if (node.callee.type !== "Identifier") return
const { name } = node.callee
if (name !== "writable" && name !== "readable" && name !== "derived")
return
const [, fn] = node.arguments
if (fn.type !== "ArrowFunctionExpression" || !fn.async) return
const start = fn.loc?.start ?? { line: 1, column: 0 }
context.report({
node: fn,
loc: {
start,
end: {
line: start.line,
column: start.column + 5,
},
},
messageId: "unexpected",
})
},
}
},
})