Skip to content

Commit 104f8b2

Browse files
committed
feat: implement stores-no-async rule
1 parent 0543166 commit 104f8b2

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/rules/no-store-async.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { createRule } from "../utils"
2+
import type * as ESTree from "estree"
3+
4+
export default createRule("no-store-async", {
5+
meta: {
6+
docs: {
7+
description:
8+
"disallow using async/await inside svelte stores because it causes issues with the auto-unsubscribing features",
9+
category: "Possible Errors",
10+
recommended: true,
11+
default: "error",
12+
},
13+
schema: [],
14+
messages: {
15+
unexpected: "Do not pass async functions to svelte stores.",
16+
},
17+
type: "problem",
18+
},
19+
create(context) {
20+
return {
21+
CallExpression(node: ESTree.CallExpression) {
22+
if (node.callee.type !== "Identifier") return
23+
const { name } = node.callee
24+
if (name !== "writable" && name !== "readable" && name !== "derived")
25+
return
26+
const [, fn] = node.arguments
27+
if (fn.type !== "ArrowFunctionExpression" || !fn.async) return
28+
29+
const start = fn.loc?.start ?? { line: 1, column: 0 }
30+
context.report({
31+
node: fn,
32+
loc: {
33+
start,
34+
end: {
35+
line: start.line,
36+
column: start.column + 5,
37+
},
38+
},
39+
messageId: "unexpected",
40+
})
41+
},
42+
}
43+
},
44+
})

src/utils/rules.ts

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import noReactiveFunctions from "../rules/no-reactive-functions"
2121
import noReactiveLiterals from "../rules/no-reactive-literals"
2222
import noShorthandStylePropertyOverrides from "../rules/no-shorthand-style-property-overrides"
2323
import noSpacesAroundEqualSignsInAttribute from "../rules/no-spaces-around-equal-signs-in-attribute"
24+
import noStoreAsync from "../rules/no-store-async"
2425
import noTargetBlank from "../rules/no-target-blank"
2526
import noUnknownStyleDirectiveProperty from "../rules/no-unknown-style-directive-property"
2627
import noUnusedSvelteIgnore from "../rules/no-unused-svelte-ignore"
@@ -59,6 +60,7 @@ export const rules = [
5960
noReactiveLiterals,
6061
noShorthandStylePropertyOverrides,
6162
noSpacesAroundEqualSignsInAttribute,
63+
noStoreAsync,
6264
noTargetBlank,
6365
noUnknownStyleDirectiveProperty,
6466
noUnusedSvelteIgnore,

0 commit comments

Comments
 (0)