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
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -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) {
@@ -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() {
@@ -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();
@@ -50,9 +50,11 @@ regeneratorRuntime.mark(function _callee2() {
}, _callee3, this);
}));

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

return notIIFE;
})();

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

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

return notIIFE;
})();

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

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

return notIIFE;
})();

/*#__PURE__*/
@@ -5,7 +5,9 @@ function () {
console.log(bar);

This comment has been minimized.

Copy link
@nicolo-ribaudo

nicolo-ribaudo Nov 18, 2018

Author Member

We even had a test for this 🤦‍♂

});

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

return bar;
}();
@@ -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;
})()();
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.