Skip to content

Commit

Permalink
Fix recursive async function expressions (#9039)
Browse files Browse the repository at this point in the history
* Fix recursive async function expressions

* Update fixtures
  • Loading branch information
nicolo-ribaudo committed Nov 19, 2018
1 parent 8c7d4b5 commit c11cdcb
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 20 deletions.
Expand Up @@ -43,9 +43,11 @@ function () {
}, _callee, this);
}));

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

return bar;
}()
}]);

Expand Down
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 @@ -7,7 +7,9 @@
return 3;
});

return function agf() {
function agf() {
return _agf.apply(this, arguments);
};
}

return agf;
})();
Expand Up @@ -7,7 +7,9 @@ const foo = function () {
return _functionSent;
});

return function gen() {
function gen() {
return _gen.apply(this, arguments);
};
}

return gen;
}();
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);
});

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;
})()();
Expand Up @@ -32,9 +32,11 @@ function () {
}, _callee, this);
}));

return function test1() {
function test1() {
return _test.apply(this, arguments);
};
}

return test1;
}();

_proto.test2 =
Expand Down

1 comment on commit c11cdcb

@fraywing
Copy link
Contributor

Choose a reason for hiding this comment

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

When will this be released?

Please sign in to comment.