Skip to content

Commit

Permalink
Set correct async/generator in IIFE for params (#11346)
Browse files Browse the repository at this point in the history
* Set correct async/generator in IIFE for params

* Reject in async params

* Skip async test on node 6 (it doesn't support async fns)
  • Loading branch information
nicolo-ribaudo committed Mar 29, 2020
1 parent 71e7a7b commit c8a8be7
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 0 deletions.
7 changes: 7 additions & 0 deletions packages/babel-plugin-transform-parameters/src/params.js
Expand Up @@ -181,6 +181,13 @@ export default function convertFunctionParams(path, loose) {
const bodyPath = path.get("body.body");
const arrowPath = bodyPath[bodyPath.length - 1].get("argument.callee");
arrowPath.arrowFunctionToExpression();

arrowPath.node.generator = path.node.generator;
arrowPath.node.async = path.node.async;

// We don't reset "async" because if the default value of a parameter
// throws, it must reject asynchronously.
path.node.generator = false;
} else {
path.get("body").unshiftContainer("body", body);
}
Expand Down
@@ -0,0 +1,13 @@
const err = new Error();

async function f(a = (() => { throw err })()) {
throw 1;
var a = await a;
return a;
}

return (async () => {
let p;
expect(() => { p = f() }).not.toThrow();
await expect(p).rejects.toThrow(err);
})();
@@ -0,0 +1,9 @@
{
"plugins": [
"transform-parameters"
],
"parserOpts": {
"allowReturnOutsideFunction": true
},
"minNodeVersion": "8.0.0"
}
@@ -0,0 +1,14 @@
function* f(a = 1) {
var a = yield a;
return a;
}

async function g(a = 1) {
var a = await a;
return a;
}

async function* h(a = 1) {
var a = await (yield a);
return a;
}
@@ -0,0 +1,5 @@
{
"plugins": [
"transform-parameters"
]
}
@@ -0,0 +1,23 @@
function f() {
let a = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
return function* (a) {
var a = yield a;
return a;
}(a);
}

async function g() {
let a = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
return async function (a) {
var a = await a;
return a;
}(a);
}

async function h() {
let a = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 1;
return async function* (a) {
var a = await (yield a);
return a;
}(a);
}

0 comments on commit c8a8be7

Please sign in to comment.