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

@ -0,0 +1,10 @@
let p
let a = do {
while (p = p.parentPath) {
if (a) {
'a'
} else {
'b'
}
}
};

View File

@ -0,0 +1,13 @@
let p;
let a = function () {
var _ret;
while (p = p.parentPath) {
if (a) {
_ret = 'a';
} else {
_ret = 'b';
}
}
return _ret;
}();

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)