-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathsvelte-context.ts
42 lines (40 loc) · 1.27 KB
/
svelte-context.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
import type { TSESTree } from "@typescript-eslint/types"
import { ReferenceTracker } from "@eslint-community/eslint-utils"
import type { RuleContext } from "../../types"
type ContextName = "setContext" | "getContext" | "hasContext" | "getAllContexts"
/** Extract svelte's context API references */
export function* extractContextReferences(
context: RuleContext,
contextNames: ContextName[] = [
"setContext",
"getContext",
"hasContext",
"getAllContexts",
],
): Generator<{ node: TSESTree.CallExpression; name: string }, void> {
const referenceTracker = new ReferenceTracker(
context.getSourceCode().scopeManager.globalScope!,
)
for (const { node, path } of referenceTracker.iterateEsmReferences({
svelte: {
[ReferenceTracker.ESM]: true,
setContext: {
[ReferenceTracker.CALL]: contextNames.includes("setContext"),
},
getContext: {
[ReferenceTracker.CALL]: contextNames.includes("getContext"),
},
hasContext: {
[ReferenceTracker.CALL]: contextNames.includes("hasContext"),
},
getAllContexts: {
[ReferenceTracker.CALL]: contextNames.includes("getAllContexts"),
},
},
})) {
yield {
node: node as TSESTree.CallExpression,
name: path[path.length - 1],
}
}
}