Skip to content

Commit

Permalink
environmentVisitor merged but issue still persists
Browse files Browse the repository at this point in the history
  • Loading branch information
Yokubjon-J authored and nicolo-ribaudo committed Jun 23, 2022
1 parent 1341c76 commit cad4911
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 59 deletions.
4 changes: 3 additions & 1 deletion packages/babel-helper-remap-async-to-generator/package.json
Expand Up @@ -16,7 +16,9 @@
"dependencies": {
"@babel/helper-annotate-as-pure": "workspace:^",
"@babel/helper-wrap-function": "workspace:^",
"@babel/types": "workspace:^"
"@babel/helper-environment-visitor":"workspace:^",
"@babel/types": "workspace:^",
"@babel/core":"workspace:^"
},
"devDependencies": {
"@babel/traverse": "workspace:^"
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 }>([
{
Function(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 @@ -19,7 +19,8 @@
"dependencies": {
"@babel/helper-plugin-utils": "workspace:^",
"@babel/helper-remap-async-to-generator": "workspace:^",
"@babel/plugin-syntax-async-generators": "^7.8.4"
"@babel/plugin-syntax-async-generators": "^7.8.4",
"@babel/helper-environment-visitor":"workspace:^"
},
"peerDependencies": {
"@babel/core": "^7.0.0-0"
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>([
{
Function(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>([
{
Function(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 comments on commit cad4911

Please sign in to comment.