Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Feb 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,10 @@
let p
let a = do {
while (p = p.parentPath) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd feel better if there was a let p before the let a 😃

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

while (p = p.parentPath) {
if (a) {
_ret = 'a';
} else {
_ret = 'b';
}
}
return _ret;
}();
14 changes: 10 additions & 4 deletions packages/babel-traverse/src/path/replacement.js
Expand Up @@ -219,10 +219,16 @@ export function replaceExpressionWithStatements(nodes: Array<Object>) {

const loop = path.findParent((path) => path.isLoop());
if (loop) {
const callee = this.get("callee");

const uid = callee.scope.generateDeclaredUidIdentifier("ret");
callee.get("body").pushContainer("body", t.returnStatement(uid));
let uid = loop.getData("expressionReplacementReturnUid");

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)
Expand Down