Skip to content

Commit 6cd570e

Browse files
Update src/rules/infinite-reactive-loop.ts
Co-authored-by: Yosuke Ota <[email protected]>
1 parent 4d64ecf commit 6cd570e

File tree

1 file changed

+19
-32
lines changed

1 file changed

+19
-32
lines changed

src/rules/infinite-reactive-loop.ts

+19-32
Original file line numberDiff line numberDiff line change
@@ -228,43 +228,30 @@ function getDeclarationBody(
228228

229229
/** */
230230
function getFunctionDeclarationNode(
231+
context: RuleContext,
231232
functionCall: TSESTree.Identifier,
232233
): TSESTree.BlockStatement | TSESTree.Expression | null {
233-
let parent: AST.SvelteScriptElement | TSESTree.Node | undefined = functionCall
234-
let declaration: TSESTree.BlockStatement | TSESTree.Expression | null = null
235-
236-
while (parent) {
237-
if (declaration) return declaration
238-
parent = parent.parent as
239-
| AST.SvelteScriptElement
240-
| TSESTree.Node
241-
| undefined
242-
if (parent && parent.type === "BlockStatement") {
243-
traverseNodes(parent, {
244-
// eslint-disable-next-line no-loop-func -- ignore
245-
enterNode(node) {
246-
if (!declaration) {
247-
declaration = getDeclarationBody(node, functionCall.name)
248-
}
249-
},
250-
leaveNode() {
251-
/* noop */
252-
},
253-
})
254-
} else if (parent && parent.type === "SvelteScriptElement") {
255-
for (const node of parent.body) {
256-
if (declaration) break
257-
if (node.type === "VariableDeclaration") {
258-
for (const child of node.declarations) {
259-
declaration = getDeclarationBody(child, functionCall.name)
260-
if (declaration) break
261-
}
262-
}
234+
const variable = findVariable(context, functionCall)
235+
if (!variable) {
236+
return null
237+
}
238+
for (const def of variable.defs) {
239+
if (def.type === "FunctionName") {
240+
if (def.node.type === "FunctionDeclaration") {
241+
return def.node.body
242+
}
243+
}
244+
if (def.type === "Variable") {
245+
if (
246+
def.node.init &&
247+
(def.node.init.type === "FunctionExpression" ||
248+
def.node.init.type === "ArrowFunctionExpression")
249+
) {
250+
return def.node.init.body
263251
}
264252
}
265253
}
266-
267-
return declaration
254+
return null
268255
}
269256

270257
/**

0 commit comments

Comments
 (0)