Fix recursive async function expressions (#9039)
* Fix recursive async function expressions * Update fixtures
This commit is contained in:
parent
8c7d4b55c9
commit
c11cdcb6d8
@ -43,9 +43,11 @@ function () {
|
||||
}, _callee, this);
|
||||
}));
|
||||
|
||||
return function bar() {
|
||||
function bar() {
|
||||
return _bar.apply(this, arguments);
|
||||
};
|
||||
}
|
||||
|
||||
return bar;
|
||||
}()
|
||||
}]);
|
||||
|
||||
|
||||
@ -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();
|
||||
|
||||
@ -7,7 +7,9 @@
|
||||
return 3;
|
||||
});
|
||||
|
||||
return function agf() {
|
||||
function agf() {
|
||||
return _agf.apply(this, arguments);
|
||||
};
|
||||
}
|
||||
|
||||
return agf;
|
||||
})();
|
||||
|
||||
@ -7,7 +7,9 @@ const foo = function () {
|
||||
return _functionSent;
|
||||
});
|
||||
|
||||
return function gen() {
|
||||
function gen() {
|
||||
return _gen.apply(this, arguments);
|
||||
};
|
||||
}
|
||||
|
||||
return gen;
|
||||
}();
|
||||
|
||||
@ -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);
|
||||
});
|
||||
|
||||
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;
|
||||
}();
|
||||
|
||||
14
packages/babel-plugin-transform-async-to-generator/test/fixtures/regression/8783/exec.js
vendored
Normal file
14
packages/babel-plugin-transform-async-to-generator/test/fixtures/regression/8783/exec.js
vendored
Normal 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]);
|
||||
});
|
||||
4
packages/babel-plugin-transform-async-to-generator/test/fixtures/regression/8783/input.js
vendored
Normal file
4
packages/babel-plugin-transform-async-to-generator/test/fixtures/regression/8783/input.js
vendored
Normal file
@ -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
|
||||
}
|
||||
}
|
||||
16
packages/babel-plugin-transform-async-to-generator/test/fixtures/regression/8783/output.js
vendored
Normal file
16
packages/babel-plugin-transform-async-to-generator/test/fixtures/regression/8783/output.js
vendored
Normal 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;
|
||||
})()();
|
||||
@ -32,9 +32,11 @@ function () {
|
||||
}, _callee, this);
|
||||
}));
|
||||
|
||||
return function test1() {
|
||||
function test1() {
|
||||
return _test.apply(this, arguments);
|
||||
};
|
||||
}
|
||||
|
||||
return test1;
|
||||
}();
|
||||
|
||||
_proto.test2 =
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user