7
7
8
8
const astUtil = require ( '../util/ast' ) ;
9
9
const docsUrl = require ( '../util/docsUrl' ) ;
10
+ const isFirstLetterCapitalized = require ( '../util/isFirstLetterCapitalized' ) ;
10
11
const report = require ( '../util/report' ) ;
11
12
const variableUtil = require ( '../util/variable' ) ;
12
13
@@ -54,32 +55,34 @@ module.exports = {
54
55
return value ;
55
56
}
56
57
57
- if ( returnNode . type === 'ConditionalExpression' ) {
58
- const possibleReturnValue = [ getReturnValue ( returnNode . consequent ) , getReturnValue ( returnNode . alternate ) ] ;
59
- const returnsUndefined = possibleReturnValue . some ( ( val ) => val === undefined ) ;
60
- if ( returnsUndefined ) return undefined ;
61
- return possibleReturnValue ;
62
- }
63
-
64
- if ( returnNode . type === 'CallExpression' ) {
65
- const calleeName = returnNode . callee . name ;
66
- const calleeNode = variableUtil . variablesInScope ( context ) . find ( ( item ) => item . name === calleeName ) ;
67
- const calleeDefinitionNode = calleeNode && calleeNode . defs && calleeNode . defs [ 0 ] && calleeNode . defs [ 0 ] . node ;
68
- const calleeReturnStatement = astUtil . findReturnStatement ( calleeDefinitionNode ) ;
69
- const calleeReturnNode = ( calleeReturnStatement && calleeReturnStatement . argument )
70
- || ( calleeDefinitionNode . init && calleeDefinitionNode . init . body ) ;
71
- return getReturnValue ( calleeReturnNode ) ;
72
- }
73
-
74
- if ( returnNode . type === 'ArrayExpression' ) {
75
- return returnNode . elements ;
76
- }
77
-
78
- if ( returnNode . type === 'JSXElement' ) {
79
- return returnNode ;
58
+ switch ( returnNode . type ) {
59
+ case 'LogicalExpression' : {
60
+ return getReturnValue ( returnNode . right ) ;
61
+ }
62
+ case 'ConditionalExpression' : {
63
+ const possibleReturnValue = [ getReturnValue ( returnNode . consequent ) , getReturnValue ( returnNode . alternate ) ] ;
64
+ const returnsUndefined = possibleReturnValue . some ( ( val ) => val === undefined ) ;
65
+ if ( returnsUndefined ) return undefined ;
66
+ return possibleReturnValue ;
67
+ }
68
+ case 'CallExpression' : {
69
+ const calleeName = returnNode . callee . name ;
70
+ const calleeNode = variableUtil . variablesInScope ( context ) . find ( ( item ) => item . name === calleeName ) ;
71
+ const calleeDefinitionNode = calleeNode && calleeNode . defs && calleeNode . defs [ 0 ] && calleeNode . defs [ 0 ] . node ;
72
+ const calleeReturnStatement = astUtil . findReturnStatement ( calleeDefinitionNode ) ;
73
+ const calleeReturnNode = ( calleeReturnStatement && calleeReturnStatement . argument )
74
+ || ( calleeDefinitionNode . init && calleeDefinitionNode . init . body ) ;
75
+ return getReturnValue ( calleeReturnNode ) ;
76
+ }
77
+ case 'ArrayExpression' : {
78
+ return returnNode . elements ;
79
+ }
80
+ case 'JSXElement' : {
81
+ return returnNode ;
82
+ }
83
+ default :
84
+ return returnNode . value ;
80
85
}
81
-
82
- return returnNode . value ;
83
86
}
84
87
85
88
const isReturningUndefined = ( returnStatement ) => {
@@ -102,7 +105,7 @@ module.exports = {
102
105
const fnName = ( node . id && node . id . name ) || node . parent . id . name ;
103
106
104
107
// Considering functions starting with Uppercase letters are react components
105
- const isReactComponent = fnName [ 0 ] === fnName [ 0 ] . toUpperCase ( ) ;
108
+ const isReactComponent = isFirstLetterCapitalized ( fnName ) ;
106
109
const returnStatement = astUtil . findReturnStatement ( node ) ;
107
110
108
111
if ( ! isReactComponent ) return ;
0 commit comments