Skip to content

Commit

Permalink
Move helper-environment-visitor into @babel/traverse
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Jun 19, 2024
1 parent de08971 commit d1dfe1c
Show file tree
Hide file tree
Showing 32 changed files with 320 additions and 467 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@
],
"dependencies": {
"@babel/helper-annotate-as-pure": "workspace:^",
"@babel/helper-environment-visitor": "workspace:^",
"@babel/helper-function-name": "workspace:^",
"@babel/helper-member-expression-to-functions": "workspace:^",
"@babel/helper-optimise-call-expression": "workspace:^",
"@babel/helper-replace-supers": "workspace:^",
"@babel/helper-skip-transparent-expression-wrappers": "workspace:^",
"@babel/helper-split-export-declaration": "workspace:^",
"@babel/traverse": "workspace:^",
"semver": "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.1"
},
"peerDependencies": {
Expand Down
66 changes: 30 additions & 36 deletions packages/babel-helper-create-class-features-plugin/src/fields.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { template, traverse, types as t } from "@babel/core";
import { template, types as t } from "@babel/core";
import type { File, NodePath, Visitor, Scope } from "@babel/core";
import { visitors } from "@babel/traverse";
import ReplaceSupers from "@babel/helper-replace-supers";
import environmentVisitor from "@babel/helper-environment-visitor";
import memberExpressionToFunctions from "@babel/helper-member-expression-to-functions";
import type {
Handler,
Expand Down Expand Up @@ -205,10 +205,7 @@ export function privateNameVisitorFactory<S, V>(
) {
// Traverses the outer portion of a class, without touching the class's inner
// scope, for private names.
const nestedVisitor = traverse.visitors.merge([
{ ...visitor },
environmentVisitor,
]);
const nestedVisitor = visitors.environmentVisitor({ ...visitor });

const privateNameVisitor: Visitor<
PrivateNameVisitorState<V & PrivateNameMetadata> & S
Expand Down Expand Up @@ -1404,38 +1401,35 @@ type ReplaceThisState = {

type ReplaceInnerBindingReferenceState = ReplaceThisState;

const thisContextVisitor = traverse.visitors.merge<ReplaceThisState>([
{
Identifier(path, state) {
if (state.argumentsPath && path.node.name === "arguments") {
state.argumentsPath.push(path);
}
},
UnaryExpression(path) {
// Replace `delete this` with `true`
const { node } = path;
if (node.operator === "delete") {
const argument = skipTransparentExprWrapperNodes(node.argument);
if (t.isThisExpression(argument)) {
path.replaceWith(t.booleanLiteral(true));
}
}
},
ThisExpression(path, state) {
state.needsClassRef = true;
path.replaceWith(t.cloneNode(state.thisRef));
},
MetaProperty(path) {
const { node, scope } = path;
// if there are `new.target` in static field
// we should replace it with `undefined`
if (node.meta.name === "new" && node.property.name === "target") {
path.replaceWith(scope.buildUndefinedNode());
const thisContextVisitor = visitors.environmentVisitor<ReplaceThisState>({
Identifier(path, state) {
if (state.argumentsPath && path.node.name === "arguments") {
state.argumentsPath.push(path);
}
},
UnaryExpression(path) {
// Replace `delete this` with `true`
const { node } = path;
if (node.operator === "delete") {
const argument = skipTransparentExprWrapperNodes(node.argument);
if (t.isThisExpression(argument)) {
path.replaceWith(t.booleanLiteral(true));
}
},
}
},
environmentVisitor,
]);
ThisExpression(path, state) {
state.needsClassRef = true;
path.replaceWith(t.cloneNode(state.thisRef));
},
MetaProperty(path) {
const { node, scope } = path;
// if there are `new.target` in static field
// we should replace it with `undefined`
if (node.meta.name === "new" && node.property.name === "target") {
path.replaceWith(scope.buildUndefinedNode());
}
},
});

const innerReferencesVisitor: Visitor<ReplaceInnerBindingReferenceState> = {
ReferencedIdentifier(path, state) {
Expand Down
25 changes: 12 additions & 13 deletions packages/babel-helper-create-class-features-plugin/src/misc.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { template, traverse, types as t } from "@babel/core";
import { template, types as t } from "@babel/core";
import type { File, NodePath, Scope, Visitor } from "@babel/core";
import environmentVisitor from "@babel/helper-environment-visitor";

const findBareSupers = traverse.visitors.merge<NodePath<t.CallExpression>[]>([
{
Super(path) {
const { node, parentPath } = path;
if (parentPath.isCallExpression({ callee: node })) {
this.push(parentPath);
}
},
import { visitors } from "@babel/traverse";

const findBareSupers = visitors.environmentVisitor<
NodePath<t.CallExpression>[]
>({
Super(path) {
const { node, parentPath } = path;
if (parentPath.isCallExpression({ callee: node })) {
this.push(parentPath);
}
},
environmentVisitor,
]);
});

const referenceVisitor: Visitor<{ scope: Scope }> = {
"TSTypeAnnotation|TypeAnnotation"(
Expand Down
3 changes: 0 additions & 3 deletions packages/babel-helper-environment-visitor/.npmignore

This file was deleted.

19 changes: 0 additions & 19 deletions packages/babel-helper-environment-visitor/README.md

This file was deleted.

50 changes: 0 additions & 50 deletions packages/babel-helper-environment-visitor/package.json

This file was deleted.

59 changes: 0 additions & 59 deletions packages/babel-helper-environment-visitor/src/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/babel-helper-module-transforms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
},
"main": "./lib/index.js",
"dependencies": {
"@babel/helper-environment-visitor": "workspace:^",
"@babel/helper-module-imports": "workspace:^",
"@babel/helper-simple-access": "workspace:^",
"@babel/helper-split-export-declaration": "workspace:^",
"@babel/helper-validator-identifier": "workspace:^"
"@babel/helper-validator-identifier": "workspace:^",
"@babel/traverse": "workspace:^"
},
"devDependencies": {
"@babel/core": "workspace:^"
Expand Down
19 changes: 8 additions & 11 deletions packages/babel-helper-module-transforms/src/rewrite-this.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import environmentVisitor from "@babel/helper-environment-visitor";
import { traverse, types as t } from "@babel/core";
const { numericLiteral, unaryExpression } = t;

import type { NodePath, Visitor } from "@babel/core";
import { types as t } from "@babel/core";
import traverse, { visitors } from "@babel/traverse";

const { numericLiteral, unaryExpression } = t;

/**
* A visitor to walk the tree, rewriting all `this` references in the top-level scope to be
* `void 0` (undefined).
*/
const rewriteThisVisitor: Visitor = traverse.visitors.merge([
environmentVisitor,
{
ThisExpression(path) {
path.replaceWith(unaryExpression("void", numericLiteral(0), true));
},
const rewriteThisVisitor: Visitor = visitors.environmentVisitor({
ThisExpression(path) {
path.replaceWith(unaryExpression("void", numericLiteral(0), true));
},
]);
});

export default function rewriteThis(programPath: NodePath) {
// Rewrite "this" to be "undefined".
Expand Down
1 change: 0 additions & 1 deletion packages/babel-helper-plugin-utils/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
"../../packages/babel-generator/src/**/*.ts",
"../../packages/babel-helper-check-duplicate-nodes/src/**/*.ts",
"../../packages/babel-helper-compilation-targets/src/**/*.ts",
"../../packages/babel-helper-environment-visitor/src/**/*.ts",
"../../packages/babel-helper-fixtures/src/**/*.ts",
"../../packages/babel-helper-function-name/src/**/*.ts",
"../../packages/babel-helper-hoist-variables/src/**/*.ts",
Expand Down
7 changes: 3 additions & 4 deletions packages/babel-helper-remap-async-to-generator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
"main": "./lib/index.js",
"dependencies": {
"@babel/helper-annotate-as-pure": "workspace:^",
"@babel/helper-environment-visitor": "workspace:^",
"@babel/helper-wrap-function": "workspace:^"
"@babel/helper-wrap-function": "workspace:^",
"@babel/traverse": "workspace:^"
},
"devDependencies": {
"@babel/core": "workspace:^",
"@babel/traverse": "workspace:^"
"@babel/core": "workspace:^"
},
"peerDependencies": {
"@babel/core": "^7.0.0"
Expand Down
35 changes: 16 additions & 19 deletions packages/babel-helper-remap-async-to-generator/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import type { NodePath } from "@babel/core";
import wrapFunction from "@babel/helper-wrap-function";
import annotateAsPure from "@babel/helper-annotate-as-pure";
import environmentVisitor from "@babel/helper-environment-visitor";
import { traverse, types as t } from "@babel/core";
import { types as t } from "@babel/core";
import { visitors } from "@babel/traverse";
const {
callExpression,
cloneNode,
Expand All @@ -13,26 +13,23 @@ const {
yieldExpression,
} = t;

const awaitVisitor = traverse.visitors.merge<{ wrapAwait: t.Expression }>([
{
ArrowFunctionExpression(path) {
path.skip();
},
const awaitVisitor = visitors.environmentVisitor<{ 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<t.Function>,
Expand Down
Loading

0 comments on commit d1dfe1c

Please sign in to comment.