Skip to content

Commit d7d0449

Browse files
committed
fix function argument
1 parent 72dffcc commit d7d0449

File tree

7 files changed

+88
-7
lines changed

7 files changed

+88
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { TSESTree } from "@typescript-eslint/types"
2+
import { ReferenceTracker } from "@eslint-community/eslint-utils"
3+
import type { RuleContext } from "../../types"
4+
5+
type LifeCycleName =
6+
| "onMount"
7+
| "beforeUpdate"
8+
| "afterUpdate"
9+
| "onDestroy"
10+
| "tick"
11+
12+
/**
13+
* Get usage of Svelte life cycle functions.
14+
*/
15+
export function* extractSvelteLifeCycleReferences(
16+
context: RuleContext,
17+
fuctionName: LifeCycleName[] = [
18+
"onMount",
19+
"beforeUpdate",
20+
"afterUpdate",
21+
"onDestroy",
22+
"tick",
23+
],
24+
): Generator<{ node: TSESTree.CallExpression; name: string }, void> {
25+
const referenceTracker = new ReferenceTracker(
26+
context.getSourceCode().scopeManager.globalScope!,
27+
)
28+
for (const { node, path } of referenceTracker.iterateEsmReferences({
29+
svelte: {
30+
[ReferenceTracker.ESM]: true,
31+
onMount: {
32+
[ReferenceTracker.CALL]: fuctionName.includes("onMount"),
33+
},
34+
beforeUpdate: {
35+
[ReferenceTracker.CALL]: fuctionName.includes("beforeUpdate"),
36+
},
37+
afterUpdate: {
38+
[ReferenceTracker.CALL]: fuctionName.includes("afterUpdate"),
39+
},
40+
onDestroy: {
41+
[ReferenceTracker.CALL]: fuctionName.includes("onDestroy"),
42+
},
43+
tick: {
44+
[ReferenceTracker.CALL]: fuctionName.includes("tick"),
45+
},
46+
},
47+
})) {
48+
yield {
49+
node: node as TSESTree.CallExpression,
50+
name: path[path.length - 1],
51+
}
52+
}
53+
}

src/rules/valid-context-access.ts

+13-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createRule } from "../utils"
22
import { extractContextReferences } from "./reference-helpers/svelte-context"
3+
import { extractSvelteLifeCycleReferences } from "./reference-helpers/svelte-lifecycle"
34
import type { TSESTree } from "@typescript-eslint/types"
45

56
export default createRule("valid-context-access", {
@@ -23,8 +24,12 @@ export default createRule("valid-context-access", {
2324
return {}
2425
}
2526

26-
// Extract <script> blocks that is not module=context.
2727
const sourceCode = context.getSourceCode()
28+
const lifeCycleReferences = Array.from(
29+
extractSvelteLifeCycleReferences(context),
30+
).map((r) => r.node)
31+
32+
// Extract <script> blocks that is not module=context.
2833
const scriptNotModuleElements = sourceCode.ast.body.filter((b) => {
2934
if (b.type !== "SvelteScriptElement") return false
3035
const isModule = b.startTag.attributes.some((a) => {
@@ -86,6 +91,8 @@ export default createRule("valid-context-access", {
8691
return false
8792
}
8893

94+
const awaitExpressions: TSESTree.AwaitExpression[] = []
95+
8996
/** Let's lint! */
9097
function doLint(
9198
visitedCallExpressions: TSESTree.CallExpression[],
@@ -129,20 +136,23 @@ export default createRule("valid-context-access", {
129136
} else if (parent?.type === "ExpressionStatement") {
130137
if (parent.expression.type !== "CallExpression") {
131138
report(contextCallExpression)
132-
} else if (parent.expression.callee.type === "Identifier") {
139+
} else if (lifeCycleReferences.includes(parent.expression)) {
133140
report(contextCallExpression)
134141
}
135142
}
136143
}
137144
}
138145

139146
return {
140-
Program() {
147+
"Program:exit"() {
141148
for (const { node } of extractContextReferences(context)) {
142149
const visitedCallExpressions: TSESTree.CallExpression[] = []
143150
doLint(visitedCallExpressions, node, node)
144151
}
145152
},
153+
AwaitExpression(node) {
154+
awaitExpressions.push(node)
155+
},
146156
}
147157
},
148158
})
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
- message: Do not call setContext except during component initialization.
2-
line: 3
3-
column: 3
2+
line: 6
3+
column: 5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
<script context="module">
1+
<script>
22
import { setContext } from "svelte"
3-
setContext("answer", 42)
3+
4+
const something = async () => {
5+
await Promise.resolve()
6+
setContext("answer", 42)
7+
}
8+
something()
49
</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: 3
3+
column: 3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<script context="module">
2+
import { setContext } from "svelte"
3+
setContext("answer", 42)
4+
</script>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
import { setContext, onMount } from "svelte"
3+
4+
const something = (fn) => fn()
5+
something(() => setContext("answer", 42))
6+
</script>

0 commit comments

Comments
 (0)