|
1 | 1 | import type { TSESTree } from "@typescript-eslint/types"
|
2 | 2 | import type { AST } from "svelte-eslint-parser"
|
3 |
| -import { ReferenceTracker } from "@eslint-community/eslint-utils" |
4 | 3 | import { createRule } from "../utils"
|
5 | 4 | import type { RuleContext } from "../types"
|
6 | 5 | import { findVariable } from "../utils/ast-utils"
|
7 | 6 | import { traverseNodes } from "svelte-eslint-parser"
|
8 |
| - |
9 |
| -/** |
10 |
| - * Get usage of `tick` |
11 |
| - */ |
12 |
| -function extractTickReferences( |
13 |
| - context: RuleContext, |
14 |
| -): { node: TSESTree.CallExpression; name: string }[] { |
15 |
| - const referenceTracker = new ReferenceTracker( |
16 |
| - context.getSourceCode().scopeManager.globalScope!, |
17 |
| - ) |
18 |
| - const a = referenceTracker.iterateEsmReferences({ |
19 |
| - svelte: { |
20 |
| - [ReferenceTracker.ESM]: true, |
21 |
| - tick: { |
22 |
| - [ReferenceTracker.CALL]: true, |
23 |
| - }, |
24 |
| - }, |
25 |
| - }) |
26 |
| - return Array.from(a).map(({ node, path }) => { |
27 |
| - return { |
28 |
| - node: node as TSESTree.CallExpression, |
29 |
| - name: path[path.length - 1], |
30 |
| - } |
31 |
| - }) |
32 |
| -} |
33 |
| - |
34 |
| -/** |
35 |
| - * Get usage of `setTimeout`, `setInterval`, `queueMicrotask` |
36 |
| - */ |
37 |
| -function extractTaskReferences( |
38 |
| - context: RuleContext, |
39 |
| -): { node: TSESTree.CallExpression; name: string }[] { |
40 |
| - const referenceTracker = new ReferenceTracker( |
41 |
| - context.getSourceCode().scopeManager.globalScope!, |
42 |
| - ) |
43 |
| - const a = referenceTracker.iterateGlobalReferences({ |
44 |
| - setTimeout: { [ReferenceTracker.CALL]: true }, |
45 |
| - setInterval: { [ReferenceTracker.CALL]: true }, |
46 |
| - queueMicrotask: { [ReferenceTracker.CALL]: true }, |
47 |
| - }) |
48 |
| - return Array.from(a).map(({ node, path }) => { |
49 |
| - return { |
50 |
| - node: node as TSESTree.CallExpression, |
51 |
| - name: path[path.length - 1], |
52 |
| - } |
53 |
| - }) |
54 |
| -} |
| 7 | +import { extractSvelteLifeCycleReferences } from "./reference-helpers/svelte-lifecycle" |
| 8 | +import { extractTaskReferences } from "./reference-helpers/microtask" |
55 | 9 |
|
56 | 10 | /**
|
57 | 11 | * If `node` is inside of `maybeAncestorNode`, return true.
|
@@ -402,7 +356,9 @@ export default createRule("infinite-reactive-loop", {
|
402 | 356 | create(context) {
|
403 | 357 | return {
|
404 | 358 | ["SvelteReactiveStatement"]: (ast: AST.SvelteReactiveStatement) => {
|
405 |
| - const tickCallExpressions = extractTickReferences(context) |
| 359 | + const tickCallExpressions = Array.from( |
| 360 | + extractSvelteLifeCycleReferences(context, ["tick"]), |
| 361 | + ) |
406 | 362 | const taskReferences = extractTaskReferences(context)
|
407 | 363 | const reactiveVariableReferences =
|
408 | 364 | getReactiveVariableReferences(context)
|
|
0 commit comments