@@ -228,43 +228,30 @@ function getDeclarationBody(
228
228
229
229
/** */
230
230
function getFunctionDeclarationNode (
231
+ context : RuleContext ,
231
232
functionCall : TSESTree . Identifier ,
232
233
) : 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
263
251
}
264
252
}
265
253
}
266
-
267
- return declaration
254
+ return null
268
255
}
269
256
270
257
/**
0 commit comments