-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathsvelte-lifecycle.ts
53 lines (51 loc) · 1.38 KB
/
svelte-lifecycle.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
45
46
47
48
49
50
51
52
53
import type { TSESTree } from "@typescript-eslint/types"
import { ReferenceTracker } from "@eslint-community/eslint-utils"
import type { RuleContext } from "../../types"
type LifeCycleName =
| "onMount"
| "beforeUpdate"
| "afterUpdate"
| "onDestroy"
| "tick"
/**
* Get usage of Svelte life cycle functions.
*/
export function* extractSvelteLifeCycleReferences(
context: RuleContext,
fuctionName: LifeCycleName[] = [
"onMount",
"beforeUpdate",
"afterUpdate",
"onDestroy",
"tick",
],
): 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,
onMount: {
[ReferenceTracker.CALL]: fuctionName.includes("onMount"),
},
beforeUpdate: {
[ReferenceTracker.CALL]: fuctionName.includes("beforeUpdate"),
},
afterUpdate: {
[ReferenceTracker.CALL]: fuctionName.includes("afterUpdate"),
},
onDestroy: {
[ReferenceTracker.CALL]: fuctionName.includes("onDestroy"),
},
tick: {
[ReferenceTracker.CALL]: fuctionName.includes("tick"),
},
},
})) {
yield {
node: node as TSESTree.CallExpression,
name: path[path.length - 1],
}
}
}