Skip to content

Commit

Permalink
Transform await in computed class keys (#14391)
Browse files Browse the repository at this point in the history
* test added

* import correct tests

* config file simplified

Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>

* environmentVisitor merged but issue still persists

* <<yarn --immutable-cache && git add yarn.lock>> is run

* Rename test file

* peerDependencies added, path.skip() removed and ArrowFunctionExpression skip added

* Update packages/babel-plugin-proposal-async-generator-functions/src/index.ts

Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>

* modified yarn.lock

Co-authored-by: Nicol貌 Ribaudo <nicolo.ribaudo@gmail.com>
  • Loading branch information
Yokubjon-J and nicolo-ribaudo committed Jun 27, 2022
1 parent 767c7b4 commit 6347eaf
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 57 deletions.
4 changes: 4 additions & 0 deletions packages/babel-helper-remap-async-to-generator/package.json
Expand Up @@ -15,12 +15,16 @@
"main": "./lib/index.js",
"dependencies": {
"@babel/helper-annotate-as-pure": "workspace:^",
"@babel/helper-environment-visitor": "workspace:^",
"@babel/helper-wrap-function": "workspace:^",
"@babel/types": "workspace:^"
},
"devDependencies": {
"@babel/traverse": "workspace:^"
},
"peerDependencies": {
"@babel/core": "workspace:^"
},
"engines": {
"node": ">=6.9.0"
},
Expand Down
35 changes: 20 additions & 15 deletions packages/babel-helper-remap-async-to-generator/src/index.ts
@@ -1,8 +1,10 @@
/* @noflow */

import type { NodePath, Visitor } from "@babel/traverse";
import type { NodePath } from "@babel/traverse";
import wrapFunction from "@babel/helper-wrap-function";
import annotateAsPure from "@babel/helper-annotate-as-pure";
import environmentVisitor from "@babel/helper-environment-visitor";
import { traverse } from "@babel/core";
import {
callExpression,
cloneNode,
Expand All @@ -12,23 +14,26 @@ import {
} from "@babel/types";
import type * as t from "@babel/types";

const awaitVisitor: Visitor<{ wrapAwait: t.Expression }> = {
Function(path) {
path.skip();
},
const awaitVisitor = traverse.visitors.merge<{ wrapAwait: t.Expression }>([
{
ArrowFunctionExpression(path) {
path.skip();
},

AwaitExpression(path, { wrapAwait }) {
const argument = path.get("argument");
AwaitExpression(path, { wrapAwait }) {
const argument = path.get("argument");

path.replaceWith(
yieldExpression(
wrapAwait
? callExpression(cloneNode(wrapAwait), [argument.node])
: argument.node,
),
);
path.replaceWith(
yieldExpression(
wrapAwait
? callExpression(cloneNode(wrapAwait), [argument.node])
: argument.node,
),
);
},
},
};
environmentVisitor,
]);

export default function (
path: NodePath<any>,
Expand Down
Expand Up @@ -17,6 +17,7 @@
"babel-plugin"
],
"dependencies": {
"@babel/helper-environment-visitor": "workspace:^",
"@babel/helper-plugin-utils": "workspace:^",
"@babel/helper-remap-async-to-generator": "workspace:^",
"@babel/plugin-syntax-async-generators": "^7.8.4"
Expand Down
@@ -1,66 +1,72 @@
import { declare } from "@babel/helper-plugin-utils";
import remapAsyncToGenerator from "@babel/helper-remap-async-to-generator";
import syntaxAsyncGenerators from "@babel/plugin-syntax-async-generators";
import { types as t } from "@babel/core";
import type { PluginPass } from "@babel/core";
import type { NodePath, Visitor } from "@babel/traverse";
import { traverse, types as t, type PluginPass } from "@babel/core";
import rewriteForAwait from "./for-await";
import environmentVisitor from "@babel/helper-environment-visitor";

export default declare(api => {
api.assertVersion(7);

const yieldStarVisitor: Visitor<PluginPass> = {
Function(path) {
path.skip();
},
const yieldStarVisitor = traverse.visitors.merge<PluginPass>([
{
ArrowFunctionExpression(path) {
path.skip();
},

YieldExpression({ node }, state) {
if (!node.delegate) return;
const callee = state.addHelper("asyncGeneratorDelegate");
node.argument = t.callExpression(callee, [
t.callExpression(state.addHelper("asyncIterator"), [node.argument]),
state.addHelper("awaitAsyncGenerator"),
]);
YieldExpression({ node }, state) {
if (!node.delegate) return;
const callee = state.addHelper("asyncGeneratorDelegate");
node.argument = t.callExpression(callee, [
t.callExpression(state.addHelper("asyncIterator"), [node.argument]),
state.addHelper("awaitAsyncGenerator"),
]);
},
},
};
environmentVisitor,
]);

const forAwaitVisitor: Visitor<PluginPass> = {
Function(path) {
path.skip();
},
const forAwaitVisitor = traverse.visitors.merge<PluginPass>([
{
ArrowFunctionExpression(path) {
path.skip();
},

ForOfStatement(path: NodePath<t.ForOfStatement>, { file }) {
const { node } = path;
if (!node.await) return;
ForOfStatement(path: NodePath<t.ForOfStatement>, { file }) {
const { node } = path;
if (!node.await) return;

const build = rewriteForAwait(path, {
getAsyncIterator: file.addHelper("asyncIterator"),
});
const build = rewriteForAwait(path, {
getAsyncIterator: file.addHelper("asyncIterator"),
});

const { declar, loop } = build;
const block = loop.body as t.BlockStatement;
const { declar, loop } = build;
const block = loop.body as t.BlockStatement;

// ensure that it's a block so we can take all its statements
path.ensureBlock();
// ensure that it's a block so we can take all its statements
path.ensureBlock();

// add the value declaration to the new loop body
if (declar) {
block.body.push(declar);
}
// add the value declaration to the new loop body
if (declar) {
block.body.push(declar);
}

// push the rest of the original loop body onto our new body
block.body.push(...path.node.body.body);
// push the rest of the original loop body onto our new body
block.body.push(...path.node.body.body);

t.inherits(loop, node);
t.inherits(loop.body, node.body);
t.inherits(loop, node);
t.inherits(loop.body, node.body);

if (build.replaceParent) {
path.parentPath.replaceWithMultiple(build.node);
} else {
path.replaceWithMultiple(build.node);
}
if (build.replaceParent) {
path.parentPath.replaceWithMultiple(build.node);
} else {
path.replaceWithMultiple(build.node);
}
},
},
};
environmentVisitor,
]);

const visitor: Visitor<PluginPass> = {
Function(path, state) {
Expand Down
@@ -0,0 +1,9 @@
async function* fn() {
class A {
[yield 1]() {}
}

class B extends A {
[await 1]() {}
}
}
@@ -0,0 +1,3 @@
{
"plugins": ["proposal-async-generator-functions"]
}
@@ -0,0 +1,18 @@
function fn() {
return _fn.apply(this, arguments);
}

function _fn() {
_fn = babelHelpers.wrapAsyncGenerator(function* () {
class A {
[yield 1]() {}

}

class B extends A {
[yield babelHelpers.awaitAsyncGenerator(1)]() {}

}
});
return _fn.apply(this, arguments);
}
4 changes: 4 additions & 0 deletions yarn.lock
Expand Up @@ -845,9 +845,12 @@ __metadata:
resolution: "@babel/helper-remap-async-to-generator@workspace:packages/babel-helper-remap-async-to-generator"
dependencies:
"@babel/helper-annotate-as-pure": "workspace:^"
"@babel/helper-environment-visitor": "workspace:^"
"@babel/helper-wrap-function": "workspace:^"
"@babel/traverse": "workspace:^"
"@babel/types": "workspace:^"
peerDependencies:
"@babel/core": "workspace:^"
languageName: unknown
linkType: soft

Expand Down Expand Up @@ -1197,6 +1200,7 @@ __metadata:
resolution: "@babel/plugin-proposal-async-generator-functions@workspace:packages/babel-plugin-proposal-async-generator-functions"
dependencies:
"@babel/core": "workspace:^"
"@babel/helper-environment-visitor": "workspace:^"
"@babel/helper-plugin-test-runner": "workspace:^"
"@babel/helper-plugin-utils": "workspace:^"
"@babel/helper-remap-async-to-generator": "workspace:^"
Expand Down

0 comments on commit 6347eaf

Please sign in to comment.