Skip to content

Commit 972e900

Browse files
committed
refactor
1 parent bcdb3a9 commit 972e900

File tree

2 files changed

+30
-49
lines changed

2 files changed

+30
-49
lines changed

src/rules/infinite-reactive-loop.ts

+5-49
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,11 @@
11
import type { TSESTree } from "@typescript-eslint/types"
22
import type { AST } from "svelte-eslint-parser"
3-
import { ReferenceTracker } from "@eslint-community/eslint-utils"
43
import { createRule } from "../utils"
54
import type { RuleContext } from "../types"
65
import { findVariable } from "../utils/ast-utils"
76
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"
559

5610
/**
5711
* If `node` is inside of `maybeAncestorNode`, return true.
@@ -402,7 +356,9 @@ export default createRule("infinite-reactive-loop", {
402356
create(context) {
403357
return {
404358
["SvelteReactiveStatement"]: (ast: AST.SvelteReactiveStatement) => {
405-
const tickCallExpressions = extractTickReferences(context)
359+
const tickCallExpressions = Array.from(
360+
extractSvelteLifeCycleReferences(context, ["tick"]),
361+
)
406362
const taskReferences = extractTaskReferences(context)
407363
const reactiveVariableReferences =
408364
getReactiveVariableReferences(context)
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { TSESTree } from "@typescript-eslint/types"
2+
import { ReferenceTracker } from "@eslint-community/eslint-utils"
3+
import type { RuleContext } from "../../types"
4+
5+
/**
6+
* Get usage of `setTimeout`, `setInterval`, `queueMicrotask`
7+
*/
8+
export function extractTaskReferences(
9+
context: RuleContext,
10+
): { node: TSESTree.CallExpression; name: string }[] {
11+
const referenceTracker = new ReferenceTracker(
12+
context.getSourceCode().scopeManager.globalScope!,
13+
)
14+
const a = referenceTracker.iterateGlobalReferences({
15+
setTimeout: { [ReferenceTracker.CALL]: true },
16+
setInterval: { [ReferenceTracker.CALL]: true },
17+
queueMicrotask: { [ReferenceTracker.CALL]: true },
18+
})
19+
return Array.from(a).map(({ node, path }) => {
20+
return {
21+
node: node as TSESTree.CallExpression,
22+
name: path[path.length - 1],
23+
}
24+
})
25+
}

0 commit comments

Comments
 (0)