Prevent multiple return statements in a loop when replacing expressions (#5030)
This commit is contained in:
parent
1a325ce5d5
commit
6da9bb83df
10
packages/babel-plugin-transform-do-expressions/test/fixtures/do-expressions/while-if/actual.js
vendored
Normal file
10
packages/babel-plugin-transform-do-expressions/test/fixtures/do-expressions/while-if/actual.js
vendored
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
let p
|
||||||
|
let a = do {
|
||||||
|
while (p = p.parentPath) {
|
||||||
|
if (a) {
|
||||||
|
'a'
|
||||||
|
} else {
|
||||||
|
'b'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
13
packages/babel-plugin-transform-do-expressions/test/fixtures/do-expressions/while-if/expected.js
vendored
Normal file
13
packages/babel-plugin-transform-do-expressions/test/fixtures/do-expressions/while-if/expected.js
vendored
Normal 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;
|
||||||
|
}();
|
||||||
@ -219,10 +219,16 @@ export function replaceExpressionWithStatements(nodes: Array<Object>) {
|
|||||||
|
|
||||||
const loop = path.findParent((path) => path.isLoop());
|
const loop = path.findParent((path) => path.isLoop());
|
||||||
if (loop) {
|
if (loop) {
|
||||||
const callee = this.get("callee");
|
let uid = loop.getData("expressionReplacementReturnUid");
|
||||||
|
|
||||||
const uid = callee.scope.generateDeclaredUidIdentifier("ret");
|
if (!uid) {
|
||||||
callee.get("body").pushContainer("body", t.returnStatement(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(
|
path.get("expression").replaceWith(
|
||||||
t.assignmentExpression("=", uid, path.node.expression)
|
t.assignmentExpression("=", uid, path.node.expression)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user