@@ -303,14 +303,7 @@ function componentRule(rule, context) {
303
303
return calledOnReact ;
304
304
} ,
305
305
306
- /**
307
- * Check if the node is returning JSX
308
- *
309
- * @param {ASTNode } ASTnode The AST node being checked
310
- * @param {Boolean } strict If true, in a ternary condition the node must return JSX in both cases
311
- * @returns {Boolean } True if the node is returning JSX, false if not
312
- */
313
- isReturningJSX : function ( ASTnode , strict ) {
306
+ getReturnPropertyAndNode ( ASTnode ) {
314
307
let property ;
315
308
let node = ASTnode ;
316
309
switch ( node . type ) {
@@ -321,19 +314,34 @@ function componentRule(rule, context) {
321
314
property = 'body' ;
322
315
if ( node [ property ] && node [ property ] . type === 'BlockStatement' ) {
323
316
node = utils . findReturnStatement ( node ) ;
324
- if ( ! node ) {
325
- return false ;
326
- }
327
317
property = 'argument' ;
328
318
}
329
319
break ;
330
320
default :
331
321
node = utils . findReturnStatement ( node ) ;
332
- if ( ! node ) {
333
- return false ;
334
- }
335
322
property = 'argument' ;
336
323
}
324
+ return {
325
+ node : node ,
326
+ property : property
327
+ } ;
328
+ } ,
329
+
330
+ /**
331
+ * Check if the node is returning JSX
332
+ *
333
+ * @param {ASTNode } ASTnode The AST node being checked
334
+ * @param {Boolean } strict If true, in a ternary condition the node must return JSX in both cases
335
+ * @returns {Boolean } True if the node is returning JSX, false if not
336
+ */
337
+ isReturningJSX : function ( ASTnode , strict ) {
338
+ const nodeAndProperty = utils . getReturnPropertyAndNode ( ASTnode ) ;
339
+ const node = nodeAndProperty . node ;
340
+ const property = nodeAndProperty . property ;
341
+
342
+ if ( ! node ) {
343
+ return false ;
344
+ }
337
345
338
346
const returnsConditionalJSXConsequent =
339
347
node [ property ] &&
@@ -363,6 +371,35 @@ function componentRule(rule, context) {
363
371
) ;
364
372
} ,
365
373
374
+ /**
375
+ * Check if the node is returning null
376
+ *
377
+ * @param {ASTNode } ASTnode The AST node being checked
378
+ * @returns {Boolean } True if the node is returning null, false if not
379
+ */
380
+ isReturningNull ( ASTnode ) {
381
+ const nodeAndProperty = utils . getReturnPropertyAndNode ( ASTnode ) ;
382
+ const property = nodeAndProperty . property ;
383
+ const node = nodeAndProperty . node ;
384
+
385
+ if ( ! node ) {
386
+ return false ;
387
+ }
388
+
389
+ return node [ property ] && node [ property ] . value === null ;
390
+ } ,
391
+
392
+ /**
393
+ * Check if the node is returning JSX or null
394
+ *
395
+ * @param {ASTNode } ASTnode The AST node being checked
396
+ * @param {Boolean } strict If true, in a ternary condition the node must return JSX in both cases
397
+ * @returns {Boolean } True if the node is returning JSX or null, false if not
398
+ */
399
+ isReturningJSXOrNull ( ASTNode , strict ) {
400
+ return utils . isReturningJSX ( ASTNode , strict ) || utils . isReturningNull ( ASTNode ) ;
401
+ } ,
402
+
366
403
/**
367
404
* Find a return statment in the current node
368
405
*
@@ -437,7 +474,7 @@ function componentRule(rule, context) {
437
474
return null ;
438
475
}
439
476
// Return the node if it is a function that is not a class method and is not inside a JSX Element
440
- if ( isFunction && ! isMethod && ! isJSXExpressionContainer && utils . isReturningJSX ( node ) ) {
477
+ if ( isFunction && ! isMethod && ! isJSXExpressionContainer && utils . isReturningJSXOrNull ( node ) ) {
441
478
return node ;
442
479
}
443
480
scope = scope . upper ;
0 commit comments