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

Add basic support for await with the pipeline operator #7154

Closed
wants to merge 7 commits into from
Closed
13 changes: 12 additions & 1 deletion packages/babel-plugin-proposal-pipeline-operator/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export default function() {

let optimizeArrow =
t.isArrowFunctionExpression(right) && t.isExpression(right.body);
const isAwait = t.isAwaitExpression(right);
const isYield = t.isYieldExpression(right);
Copy link
Member

Choose a reason for hiding this comment

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

foo |> yield bar shouldn't be transfrmed; it should become something like (_temp = yield bar, _temp(foo)).

If there isn't a way of transforming |> await foo before transforming the async function, you can workaround the problem by adding node.__wasAwait to yield expressions generated by the async functions plugin and then do

const isAwait = t.isAwaitExpression(right) || right.__wasAwait;

let param;

if (optimizeArrow) {
Expand All @@ -24,6 +26,11 @@ export default function() {
} else if (params.length > 0) {
optimizeArrow = false;
}
} else if (isAwait || isYield) {
right.argument = t.sequenceExpression([
t.numericLiteral(0),
right.argument,
]);
} else if (t.isIdentifier(right, { name: "eval" })) {
right = t.sequenceExpression([t.numericLiteral(0), right]);
}
Expand All @@ -44,7 +51,11 @@ export default function() {

const call = optimizeArrow
? right.body
: t.callExpression(right, [placeholder]);
: isAwait || isYield
? t[isAwait ? "awaitExpression" : "yieldExpression"](
t.callExpression(right.argument, [placeholder]),
)
: t.callExpression(right, [placeholder]);
path.replaceWith(
t.sequenceExpression([
t.assignmentExpression("=", placeholder, left),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var incPromise = (x) => Promise.resolve(x + 1);
Copy link
Member

Choose a reason for hiding this comment

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

i'd prefer to have the same code in actual / exec if they are in the same directory, exec differs slightly right now and it makes it hard to compare those files (assert ofc should stay just in exec)

Copy link
Author

Choose a reason for hiding this comment

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

I made them match now. I think I may have saved the file with Prettier on at some point.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I didnt mean that they do not match in sense of formatting - i think there was some differenc in how incFuncPromise was used

Copy link
Author

Choose a reason for hiding this comment

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

Oh, got it. I also changed the usage of that variable down below. Either way, they're the same now.

Copy link
Member

Choose a reason for hiding this comment

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

Cool!

var double = (x) => x * 2;

var result = async () => 10 |> await incPromise;
var result2 = async () => 10 |> await incPromise |> double;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var incPromise = x => Promise.resolve(x + 1);
var double = x => x * 2;

var result = async () => 10 |> await incPromise;
var result2 = async () => 10 |> await incPromise |> double;

return Promise.all([result(), result2()]).then(([r, r2]) => {
assert.equal(r, 11);
assert.equal(r2, 22);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var incPromise = x => Promise.resolve(x + 1);

var double = x => x * 2;

var result =
/*#__PURE__*/
function () {
var _ref = babelHelpers.asyncToGenerator(function* () {
var _;

return _ = 10, yield (0, incPromise)(_);
});

return function result() {
return _ref.apply(this, arguments);
};
}();

var result2 =
/*#__PURE__*/
function () {
var _ref2 = babelHelpers.asyncToGenerator(function* () {
var _ref3, _2;

return _ref3 = (_2 = 10, yield (0, incPromise)(_2)), double(_ref3);
});

return function result2() {
return _ref2.apply(this, arguments);
};
}();
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"plugins": ["proposal-pipeline-operator", "external-helpers", "transform-async-to-generator"],
"parserOpts": {
"allowReturnOutsideFunction": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var incPromise = (x) => Promise.resolve(x + 1);
var double = (x) => x * 2;

var result = async () => 10 |> await incPromise;
var result2 = async () => 10 |> await incPromise |> double;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
var incPromise = x => Promise.resolve(x + 1);
var double = x => x * 2;

var result = async () => 10 |> await incPromise;
var result2 = async () => 10 |> await incPromise |> double;

return Promise.all([result(), result2()]).then(([r, r2]) => {
assert.equal(r, 11);
assert.equal(r2, 22);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var incPromise = x => Promise.resolve(x + 1);

var double = x => x * 2;

var result = async () => {
var _;

return _ = 10, await (0, incPromise)(_);
};

var result2 = async () => {
var _ref, _2;

return _ref = (_2 = 10, await (0, incPromise)(_2)), double(_ref);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"minNodeVersion": "7.6.0",
"parserOpts": {
"allowReturnOutsideFunction": true
}
}