@@ -23,11 +23,12 @@ const undefinedKeywords = new Set([
23
23
* Checks if a node has a return statement. Void return does not count.
24
24
*
25
25
* @param {object } node
26
+ * @param {boolean } throwOnNullReturn
26
27
* @param {PromiseFilter } promFilter
27
28
* @returns {boolean|Node }
28
29
*/
29
30
// eslint-disable-next-line complexity
30
- const allBrancheshaveReturnValues = ( node , promFilter ) => {
31
+ const hasReturnValue = ( node , throwOnNullReturn , promFilter ) => {
31
32
if ( ! node ) {
32
33
return false ;
33
34
}
@@ -40,18 +41,19 @@ const allBrancheshaveReturnValues = (node, promFilter) => {
40
41
return type && ! undefinedKeywords . has ( type ) ;
41
42
}
42
43
43
- // case 'MethodDefinition':
44
- // return allBrancheshaveReturnValues (node.value, promFilter);
44
+ case 'MethodDefinition' :
45
+ return hasReturnValue ( node . value , throwOnNullReturn , promFilter ) ;
45
46
case 'FunctionExpression' :
46
47
case 'FunctionDeclaration' :
47
48
case 'ArrowFunctionExpression' : {
48
49
return node . expression && ( ! isNewPromiseExpression ( node . body ) || ! isVoidPromise ( node . body ) ) ||
49
- allBrancheshaveReturnValues ( node . body , promFilter ) ;
50
+ hasReturnValue ( node . body , throwOnNullReturn , promFilter ) ;
50
51
}
51
52
52
53
case 'BlockStatement' : {
53
- const lastBodyNode = node . body . slice ( - 1 ) [ 0 ] ;
54
- return allBrancheshaveReturnValues ( lastBodyNode , promFilter ) ;
54
+ return node . body . some ( ( bodyNode ) => {
55
+ return bodyNode . type !== 'FunctionDeclaration' && hasReturnValue ( bodyNode , throwOnNullReturn , promFilter ) ;
56
+ } ) ;
55
57
}
56
58
57
59
case 'LabeledStatement' :
@@ -61,36 +63,37 @@ const allBrancheshaveReturnValues = (node, promFilter) => {
61
63
case 'ForInStatement' :
62
64
case 'ForOfStatement' :
63
65
case 'WithStatement' : {
64
- return allBrancheshaveReturnValues ( node . body , promFilter ) ;
66
+ return hasReturnValue ( node . body , throwOnNullReturn , promFilter ) ;
65
67
}
66
68
67
69
case 'IfStatement' : {
68
- return allBrancheshaveReturnValues ( node . consequent , promFilter ) && allBrancheshaveReturnValues ( node . alternate , promFilter ) ;
70
+ return hasReturnValue ( node . consequent , throwOnNullReturn , promFilter ) ||
71
+ hasReturnValue ( node . alternate , throwOnNullReturn , promFilter ) ;
69
72
}
70
73
71
74
case 'TryStatement' : {
72
- return allBrancheshaveReturnValues ( node . block , promFilter ) &&
73
- allBrancheshaveReturnValues ( node . handler && node . handler . body , promFilter ) &&
74
- allBrancheshaveReturnValues ( node . finalizer , promFilter ) ;
75
+ return hasReturnValue ( node . block , throwOnNullReturn , promFilter ) ||
76
+ hasReturnValue ( node . handler && node . handler . body , throwOnNullReturn , promFilter ) ||
77
+ hasReturnValue ( node . finalizer , throwOnNullReturn , promFilter ) ;
75
78
}
76
79
77
80
case 'SwitchStatement' : {
78
- return node . cases . every (
81
+ return node . cases . some (
79
82
( someCase ) => {
80
- return someCase . consequent . every ( ( nde ) => {
81
- return allBrancheshaveReturnValues ( nde , promFilter ) ;
83
+ return someCase . consequent . some ( ( nde ) => {
84
+ return hasReturnValue ( nde , throwOnNullReturn , promFilter ) ;
82
85
} ) ;
83
86
} ,
84
87
) ;
85
88
}
86
89
87
- case 'ThrowStatement' : {
88
- return true ;
89
- }
90
-
91
90
case 'ReturnStatement' : {
92
91
// void return does not count.
93
92
if ( node . argument === null ) {
93
+ if ( throwOnNullReturn ) {
94
+ throw new Error ( 'Null return' ) ;
95
+ }
96
+
94
97
return false ;
95
98
}
96
99
@@ -109,22 +112,15 @@ const allBrancheshaveReturnValues = (node, promFilter) => {
109
112
}
110
113
} ;
111
114
112
- /**
113
- * @callback PromiseFilter
114
- * @param {object } node
115
- * @returns {boolean }
116
- */
117
-
118
115
/**
119
116
* Checks if a node has a return statement. Void return does not count.
120
117
*
121
118
* @param {object } node
122
- * @param {boolean } throwOnNullReturn
123
119
* @param {PromiseFilter } promFilter
124
120
* @returns {boolean|Node }
125
121
*/
126
122
// eslint-disable-next-line complexity
127
- const hasReturnValue = ( node , throwOnNullReturn , promFilter ) => {
123
+ const allBrancheshaveReturnValues = ( node , promFilter ) => {
128
124
if ( ! node ) {
129
125
return false ;
130
126
}
@@ -137,19 +133,18 @@ const hasReturnValue = (node, throwOnNullReturn, promFilter) => {
137
133
return type && ! undefinedKeywords . has ( type ) ;
138
134
}
139
135
140
- case 'MethodDefinition' :
141
- return hasReturnValue ( node . value , throwOnNullReturn , promFilter ) ;
136
+ // case 'MethodDefinition':
137
+ // return allBrancheshaveReturnValues (node.value, promFilter);
142
138
case 'FunctionExpression' :
143
139
case 'FunctionDeclaration' :
144
140
case 'ArrowFunctionExpression' : {
145
141
return node . expression && ( ! isNewPromiseExpression ( node . body ) || ! isVoidPromise ( node . body ) ) ||
146
- hasReturnValue ( node . body , throwOnNullReturn , promFilter ) ;
142
+ allBrancheshaveReturnValues ( node . body , promFilter ) ;
147
143
}
148
144
149
145
case 'BlockStatement' : {
150
- return node . body . some ( ( bodyNode ) => {
151
- return bodyNode . type !== 'FunctionDeclaration' && hasReturnValue ( bodyNode , throwOnNullReturn , promFilter ) ;
152
- } ) ;
146
+ const lastBodyNode = node . body . slice ( - 1 ) [ 0 ] ;
147
+ return allBrancheshaveReturnValues ( lastBodyNode , promFilter ) ;
153
148
}
154
149
155
150
case 'LabeledStatement' :
@@ -159,37 +154,35 @@ const hasReturnValue = (node, throwOnNullReturn, promFilter) => {
159
154
case 'ForInStatement' :
160
155
case 'ForOfStatement' :
161
156
case 'WithStatement' : {
162
- return hasReturnValue ( node . body , throwOnNullReturn , promFilter ) ;
157
+ return allBrancheshaveReturnValues ( node . body , promFilter ) ;
163
158
}
164
159
165
160
case 'IfStatement' : {
166
- return hasReturnValue ( node . consequent , throwOnNullReturn , promFilter ) ||
167
- hasReturnValue ( node . alternate , throwOnNullReturn , promFilter ) ;
161
+ return allBrancheshaveReturnValues ( node . consequent , promFilter ) && allBrancheshaveReturnValues ( node . alternate , promFilter ) ;
168
162
}
169
163
170
164
case 'TryStatement' : {
171
- return hasReturnValue ( node . block , throwOnNullReturn , promFilter ) ||
172
- hasReturnValue ( node . handler && node . handler . body , throwOnNullReturn , promFilter ) ||
173
- hasReturnValue ( node . finalizer , throwOnNullReturn , promFilter ) ;
165
+ return allBrancheshaveReturnValues ( node . block , promFilter ) &&
166
+ allBrancheshaveReturnValues ( node . handler && node . handler . body , promFilter ) &&
167
+ allBrancheshaveReturnValues ( node . finalizer , promFilter ) ;
174
168
}
175
169
176
170
case 'SwitchStatement' : {
177
- return node . cases . some (
171
+ return node . cases . every (
178
172
( someCase ) => {
179
- return someCase . consequent . some ( ( nde ) => {
180
- return hasReturnValue ( nde , throwOnNullReturn , promFilter ) ;
181
- } ) ;
173
+ const nde = someCase . consequent . slice ( - 1 ) [ 0 ] ;
174
+ return allBrancheshaveReturnValues ( nde , promFilter ) ;
182
175
} ,
183
176
) ;
184
177
}
185
178
179
+ case 'ThrowStatement' : {
180
+ return true ;
181
+ }
182
+
186
183
case 'ReturnStatement' : {
187
184
// void return does not count.
188
185
if ( node . argument === null ) {
189
- if ( throwOnNullReturn ) {
190
- throw new Error ( 'Null return' ) ;
191
- }
192
-
193
186
return false ;
194
187
}
195
188
@@ -208,6 +201,12 @@ const hasReturnValue = (node, throwOnNullReturn, promFilter) => {
208
201
}
209
202
} ;
210
203
204
+ /**
205
+ * @callback PromiseFilter
206
+ * @param {object } node
207
+ * @returns {boolean }
208
+ */
209
+
211
210
/**
212
211
* Avoids further checking child nodes if a nested function shadows the
213
212
* resolver, but otherwise, if name is used (by call or passed in as an
0 commit comments