Prevent multiple return statements in a loop when replacing expressions (#5030)

This commit is contained in:
Brian Ng
2017-02-09 15:06:41 -06:00
committed by Logan Smyth
parent 1a325ce5d5
commit 6da9bb83df
3 changed files with 32 additions and 3 deletions

View File

@@ -219,10 +219,16 @@ export function replaceExpressionWithStatements(nodes: Array<Object>) {
const loop = path.findParent((path) => path.isLoop());
if (loop) {
const callee = this.get("callee");
let uid = loop.getData("expressionReplacementReturnUid");
const uid = callee.scope.generateDeclaredUidIdentifier("ret");
callee.get("body").pushContainer("body", t.returnStatement(uid));
if (!uid) {
const callee = this.get("callee");
uid = callee.scope.generateDeclaredUidIdentifier("ret");
callee.get("body").pushContainer("body", t.returnStatement(uid));
loop.setData("expressionReplacementReturnUid", uid);
} else {
uid = t.identifier(uid.name);
}
path.get("expression").replaceWith(
t.assignmentExpression("=", uid, path.node.expression)