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

Fix recursive async function expressions #9039

Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 14 additions & 2 deletions packages/babel-helper-wrap-function/src/index.js
Expand Up @@ -3,7 +3,7 @@ import nameFunction from "@babel/helper-function-name";
import template from "@babel/template";
import * as t from "@babel/types";

const buildExpressionWrapper = template.expression(`
const buildAnonymousExpressionWrapper = template.expression(`
(function () {
var REF = FUNCTION;
return function NAME(PARAMS) {
Expand All @@ -12,6 +12,16 @@ const buildExpressionWrapper = template.expression(`
})()
`);

const buildNamedExpressionWrapper = template.expression(`
(function () {
var REF = FUNCTION;
function NAME(PARAMS) {
return REF.apply(this, arguments);
}
return NAME;
})()
`);

const buildDeclarationWrapper = template(`
function NAME(PARAMS) { return REF.apply(this, arguments); }
function REF() {
Expand Down Expand Up @@ -53,7 +63,9 @@ function plainFunction(path: NodePath, callId: Object) {
const functionId = node.id;
const wrapper = isDeclaration
? buildDeclarationWrapper
: buildExpressionWrapper;
: functionId
? buildNamedExpressionWrapper
: buildAnonymousExpressionWrapper;

if (path.isArrowFunctionExpression()) {
path.arrowFunctionToExpression();
Expand Down
Expand Up @@ -50,9 +50,11 @@ regeneratorRuntime.mark(function _callee2() {
}, _callee3, this);
}));

return function notIIFE() {
function notIIFE() {
return _notIIFE.apply(this, arguments);
};
}

return notIIFE;
})();

/*#__PURE__*/
Expand Down
Expand Up @@ -47,9 +47,11 @@ regeneratorRuntime.mark(function _callee2() {
}, _callee3, this);
}));

return function notIIFE() {
function notIIFE() {
return _notIIFE.apply(this, arguments);
};
}

return notIIFE;
})();

/*#__PURE__*/
Expand Down
Expand Up @@ -11,9 +11,11 @@ babelHelpers.asyncToGenerator(function* () {
yield 'ok';
});

return function notIIFE() {
function notIIFE() {
return _notIIFE.apply(this, arguments);
};
}

return notIIFE;
})();

/*#__PURE__*/
Expand Down
Expand Up @@ -5,7 +5,9 @@ function () {
console.log(bar);
Copy link
Member Author

Choose a reason for hiding this comment

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

We even had a test for this 🤦‍♂️

});

return function bar() {
function bar() {
return _bar.apply(this, arguments);
};
}

return bar;
}();
Expand Up @@ -7,7 +7,9 @@ function () {
console.log(bar);
});

return function bar() {
function bar() {
return _bar.apply(this, arguments);
};
}

return bar;
}();
@@ -0,0 +1,14 @@
let log = [];

let resolve;
const main = new Promise(r => { resolve = r });

(async function poll(count) {
log.push(await Promise.resolve(count))
if (count < 3) setTimeout(poll, 10, count + 1);
else resolve();
})(0)

return main.then(() => {
expect(log).toEqual([0, 1, 2, 3]);
});
@@ -0,0 +1,4 @@
(async function poll() {
console.log(await Promise.resolve('Hello'))
setTimeout(poll, 1000);
})();
@@ -0,0 +1,6 @@
{
"plugins": ["transform-async-to-generator"],
"parserOpts": {
"allowReturnOutsideFunction": true
}
}
@@ -0,0 +1,16 @@
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

(function () {
var _poll = _asyncToGenerator(function* () {
console.log((yield Promise.resolve('Hello')));
setTimeout(poll, 1000);
});

function poll() {
return _poll.apply(this, arguments);
}

return poll;
})()();