Fix recursive async function expressions (#9039)

* Fix recursive async function expressions

* Update fixtures
This commit is contained in:
Nicolò Ribaudo 2018-11-19 17:19:54 +01:00 committed by GitHub
parent 8c7d4b55c9
commit c11cdcb6d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 90 additions and 20 deletions

View File

@ -43,9 +43,11 @@ function () {
}, _callee, this);
}));
return function bar() {
function bar() {
return _bar.apply(this, arguments);
};
}
return bar;
}()
}]);

View File

@ -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();

View File

@ -7,7 +7,9 @@
return 3;
});
return function agf() {
function agf() {
return _agf.apply(this, arguments);
};
}
return agf;
})();

View File

@ -7,7 +7,9 @@ const foo = function () {
return _functionSent;
});
return function gen() {
function gen() {
return _gen.apply(this, arguments);
};
}
return gen;
}();

View File

@ -50,9 +50,11 @@ regeneratorRuntime.mark(function _callee2() {
}, _callee3, this);
}));
return function notIIFE() {
function notIIFE() {
return _notIIFE.apply(this, arguments);
};
}
return notIIFE;
})();
/*#__PURE__*/

View File

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

View File

@ -11,9 +11,11 @@ babelHelpers.asyncToGenerator(function* () {
yield 'ok';
});
return function notIIFE() {
function notIIFE() {
return _notIIFE.apply(this, arguments);
};
}
return notIIFE;
})();
/*#__PURE__*/

View File

@ -5,7 +5,9 @@ function () {
console.log(bar);
});
return function bar() {
function bar() {
return _bar.apply(this, arguments);
};
}
return bar;
}();

View File

@ -7,7 +7,9 @@ function () {
console.log(bar);
});
return function bar() {
function bar() {
return _bar.apply(this, arguments);
};
}
return bar;
}();

View File

@ -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]);
});

View File

@ -0,0 +1,4 @@
(async function poll() {
console.log(await Promise.resolve('Hello'))
setTimeout(poll, 1000);
})();

View File

@ -0,0 +1,6 @@
{
"plugins": ["transform-async-to-generator"],
"parserOpts": {
"allowReturnOutsideFunction": true
}
}

View File

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

View File

@ -32,9 +32,11 @@ function () {
}, _callee, this);
}));
return function test1() {
function test1() {
return _test.apply(this, arguments);
};
}
return test1;
}();
_proto.test2 =