Skip to content

Commit ffc0bce

Browse files
committed
add tests and fix bug
1 parent 6ff0994 commit ffc0bce

File tree

8 files changed

+55
-3
lines changed

8 files changed

+55
-3
lines changed

docs/rules/valid-context-access.md

+12
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,23 @@ This rule reports where context API is called except during component initializa
4545
update()
4646
setContext("answer", 42)
4747
})
48+
49+
const update2 = async () => {
50+
await Promise.resolve()
51+
setContext("answer", 42)
52+
}
53+
54+
;(async () => {
55+
await Promise.resolve()
56+
setContext("answer", 42)
57+
})()
4858
</script>
4959
```
5060

5161
</ESLintCodeBlock>
5262

63+
- :warning: This rule only inspects Svelte files, not JS / TS files.
64+
5365
## :wrench: Options
5466

5567
Nothing.

src/rules/valid-context-access.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export default createRule("valid-context-access", {
107107
belongingFunction:
108108
| TSESTree.FunctionDeclaration
109109
| TSESTree.VariableDeclaration
110+
| TSESTree.ArrowFunctionExpression
110111
node: TSESTree.Node
111112
}[] = []
112113

@@ -207,9 +208,11 @@ export default createRule("valid-context-access", {
207208
AwaitExpression(node) {
208209
let parent: TSESTree.Node | undefined = node.parent
209210
while (parent) {
210-
if (parent.type === "FunctionDeclaration") {
211-
awaitExpressions.push({ belongingFunction: parent, node })
212-
} else if (parent.type === "VariableDeclaration") {
211+
if (
212+
parent.type === "FunctionDeclaration" ||
213+
parent.type === "VariableDeclaration" ||
214+
parent.type === "ArrowFunctionExpression"
215+
) {
213216
awaitExpressions.push({ belongingFunction: parent, node })
214217
}
215218
parent = parent.parent
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- message: Do not call setContext except during component initialization.
2+
line: 5
3+
column: 5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<script>
2+
import { setContext } from "svelte"
3+
;(async () => {
4+
await Promise.resolve()
5+
setContext("answer", 42)
6+
})()
7+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- message: Do not call setContext except during component initialization.
2+
line: 4
3+
column: 5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<script>
2+
import { setContext, onMount } from "svelte"
3+
const doSomething = () => {
4+
setContext("answer", 42)
5+
}
6+
7+
onMount(() => {
8+
doSomething()
9+
})
10+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- message: Do not call setContext except during component initialization.
2+
line: 4
3+
column: 5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
import { setContext, onMount } from "svelte"
3+
const doSomething = () => {
4+
setContext("answer", 42)
5+
}
6+
7+
;(async () => {
8+
await Promise.resolve()
9+
doSomething()
10+
})()
11+
</script>

0 commit comments

Comments
 (0)