@@ -18,11 +18,12 @@ import { ESLintUtils } from '@typescript-eslint/utils';
18
18
// Walk the AST to find a node that matches the callback, up to maxDepth levels deep
19
19
// This is a depth-first search and could be improved
20
20
function walkFind (
21
- node : TSESTree . Node ,
21
+ node : TSESTree . Node | undefined | null ,
22
22
callback : ( node : TSESTree . Node ) => boolean ,
23
23
maxDepth = 10
24
24
) : TSESTree . Node | null {
25
25
if ( maxDepth <= 0 ) return null ;
26
+ if ( node && typeof node !== 'object' ) return null ;
26
27
if ( callback ( node ) ) return node ;
27
28
for ( const key in node ) {
28
29
if ( key === 'parent' ) continue ;
@@ -37,6 +38,7 @@ function walkFind(
37
38
if ( result ) return result ;
38
39
}
39
40
}
41
+ return null ;
40
42
}
41
43
42
44
function blockStatementIncludesSecurityCall ( block : TSESTree . BlockStatement ) : boolean {
@@ -208,14 +210,21 @@ export default ESLintUtils.RuleCreator(() => '')({
208
210
} else {
209
211
// This is a single function (load or endpoint), so find its body
210
212
// There should be a block statement (function body) within a couple levels
211
- blockStatements . push (
212
- walkFind (
213
- ( functionExport as TSESTree . VariableDeclaratorMaybeInit ) . init ||
214
- ( functionExport as TSESTree . FunctionDeclaration ) ,
215
- ( n ) => n . type === 'BlockStatement' ,
216
- 4
217
- ) as TSESTree . BlockStatement
218
- ) ;
213
+ const blockStatement = walkFind (
214
+ ( functionExport as TSESTree . VariableDeclaratorMaybeInit ) . init ||
215
+ ( functionExport as TSESTree . FunctionDeclaration ) ,
216
+ ( n ) => n . type === 'BlockStatement' ,
217
+ 4
218
+ ) as TSESTree . BlockStatement ;
219
+ if ( blockStatement ) blockStatements . push ( blockStatement ) ;
220
+ else {
221
+ context . report ( {
222
+ node : node . declaration ,
223
+ data : { type : functionExport . type } ,
224
+ messageId : 'unexpectedFunction'
225
+ } ) ;
226
+ return ;
227
+ }
219
228
}
220
229
blockStatements . forEach ( ( bs ) => {
221
230
if ( ! blockStatementIncludesSecurityCall ( bs ) ) {
0 commit comments