Skip to content

Commit 1eab26b

Browse files
committed
chore: refactor
1 parent 6cd570e commit 1eab26b

File tree

1 file changed

+24
-38
lines changed

1 file changed

+24
-38
lines changed

src/rules/infinite-reactive-loop.ts

+24-38
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { AST } from "svelte-eslint-parser"
33
import { ReferenceTracker } from "eslint-utils"
44
import { createRule } from "../utils"
55
import type { RuleContext } from "../types"
6-
import { getScope } from "../utils/ast-utils"
6+
import { findVariable } from "../utils/ast-utils"
77
import { traverseNodes } from "svelte-eslint-parser"
88

99
/**
@@ -74,40 +74,15 @@ function isFunctionCall(node: TSESTree.Node): boolean {
7474
return parent.callee.type === "Identifier" && parent.callee.name === node.name
7575
}
7676

77-
/**
78-
* Return true if `node` is a variable.
79-
*
80-
* e.g. foo.bar
81-
* If node is `foo`, return true.
82-
* If node is `bar`, return false.
83-
*
84-
* e.g. let baz = 1
85-
* If node is `baz`, return true.
86-
*/
87-
function isVariableNode(node: TSESTree.Identifier): boolean {
88-
const { parent } = node
89-
if (parent?.type !== "MemberExpression") return true
90-
if (
91-
parent.type === "MemberExpression" &&
92-
parent.object.type !== "Identifier"
93-
) {
94-
return false
95-
}
96-
97-
return parent.object.type !== "Identifier"
98-
? false
99-
: parent.object.name === node.name
100-
}
101-
10277
/**
10378
* Return true if `node` is a reactive variable.
10479
*/
10580
function isReactiveVariableNode(
106-
context: RuleContext,
81+
reactiveVariableReferences: TSESTree.Identifier[],
10782
node: TSESTree.Node,
10883
): node is TSESTree.Identifier {
10984
if (node.type !== "Identifier") return false
110-
return getAllReactiveVariableReferences(context).includes(node)
85+
return reactiveVariableReferences.includes(node)
11186
}
11287

11388
/**
@@ -146,11 +121,10 @@ function isPromiseThenOrCatchBody(node: TSESTree.Node): boolean {
146121
return ["then", "catch"].includes(property.name)
147122
}
148123

149-
150124
/**
151125
* Get all reactive variable reference.
152126
*/
153-
function getAllReactiveVariableReferences(context: RuleContext) {
127+
function getReactiveVariableReferences(context: RuleContext) {
154128
const scopeManager = context.getSourceCode().scopeManager
155129
// Find the top-level (module or global) scope.
156130
// Any variable defined at the top-level (module scope or global scope) can be made reactive.
@@ -181,18 +155,18 @@ function getAllReactiveVariableReferences(context: RuleContext) {
181155
* Get all tracked reactive variables.
182156
*/
183157
function getTrackedVariableNodes(
184-
context: RuleContext,
158+
reactiveVariableReferences: TSESTree.Identifier[],
185159
ast: AST.SvelteReactiveStatement,
186160
) {
187-
const reactiveVariableNodes: TSESTree.Identifier[] = []
188-
for (const identifier of getAllReactiveVariableReferences(context)) {
161+
const reactiveVariableNodes: Set<TSESTree.Identifier> = new Set()
162+
for (const identifier of reactiveVariableReferences) {
189163
if (
190164
// If the identifier is within the reactive statement range,
191165
// it is used within the reactive statement.
192166
ast.range[0] <= identifier.range[0] &&
193167
identifier.range[1] <= ast.range[1]
194168
) {
195-
reactiveVariableNodes.push(identifier)
169+
reactiveVariableNodes.add(identifier)
196170
}
197171
}
198172
return reactiveVariableNodes
@@ -293,6 +267,7 @@ function doLint(
293267
name: string
294268
}[],
295269
reactiveVariableNames: string[],
270+
reactiveVariableReferences: TSESTree.Identifier[],
296271
pIsSameTask: boolean,
297272
) {
298273
let isSameMicroTask = pIsSameTask
@@ -325,7 +300,10 @@ function doLint(
325300

326301
if (node.type === "Identifier" && isFunctionCall(node)) {
327302
// traverse used functions body
328-
const functionDeclarationNode = getFunctionDeclarationNode(node)
303+
const functionDeclarationNode = getFunctionDeclarationNode(
304+
context,
305+
node,
306+
)
329307
if (functionDeclarationNode) {
330308
doLint(
331309
context,
@@ -334,14 +312,15 @@ function doLint(
334312
tickCallExpressions,
335313
taskReferences,
336314
reactiveVariableNames,
315+
reactiveVariableReferences,
337316
isSameMicroTask,
338317
)
339318
}
340319
}
341320

342321
if (!isSameMicroTask) {
343322
if (
344-
isReactiveVariableNode(context, node) &&
323+
isReactiveVariableNode(reactiveVariableReferences, node) &&
345324
reactiveVariableNames.includes(node.name) &&
346325
isNodeForAssign(node)
347326
) {
@@ -425,14 +404,21 @@ export default createRule("infinite-reactive-loop", {
425404
["SvelteReactiveStatement"]: (ast: AST.SvelteReactiveStatement) => {
426405
const tickCallExpressions = extractTickReferences(context)
427406
const taskReferences = extractTaskReferences(context)
428-
const trackedVariableNodes = getTrackedVariableNodes(context, ast)
407+
const reactiveVariableReferences =
408+
getReactiveVariableReferences(context)
409+
const trackedVariableNodes = getTrackedVariableNodes(
410+
reactiveVariableReferences,
411+
ast,
412+
)
413+
429414
doLint(
430415
context,
431416
ast.body,
432417
[],
433418
tickCallExpressions,
434419
taskReferences,
435-
trackedVariableNodes.map((node) => node.name),
420+
Array.from(trackedVariableNodes).map((node) => node.name),
421+
reactiveVariableReferences,
436422
true,
437423
)
438424
},

0 commit comments

Comments
 (0)