Skip to content

Commit b8bb77a

Browse files
author
Keyan Zhang
committed
better way to detect early returns in getInitialState
1 parent 231f629 commit b8bb77a

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

transforms/class.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,13 @@ module.exports = (file, api, options) => {
293293
return functionExpressionAST
294294
.find(j.ReturnStatement)
295295
.forEach(path => {
296+
let shouldInsertReturnAfterAssignment = false;
297+
298+
// if the return statement is not a direct child of the function body
299+
if (getInitialState.value.body.body.indexOf(path.value) === -1) {
300+
shouldInsertReturnAfterAssignment = true;
301+
}
302+
296303
j(path).replaceWith(j.expressionStatement(
297304
j.assignmentExpression(
298305
'=',
@@ -305,12 +312,7 @@ module.exports = (file, api, options) => {
305312
)
306313
));
307314

308-
if ( // FIXME is there a better way to check this?
309-
j(path).closest(j.IfStatement).size() ||
310-
j(path).closest(j.SwitchStatement).size() ||
311-
j(path).closest(j.WhileStatement).size() ||
312-
j(path).closest(j.ForStatement).size()
313-
) {
315+
if (shouldInsertReturnAfterAssignment) {
314316
j(path).insertAfter(j.returnStatement(null));
315317
}
316318
}).getAST()[0].value.body.body;

0 commit comments

Comments
 (0)