Skip to content

Commit

Permalink
Fix it
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Nov 28, 2023
1 parent 14fb70e commit cc8280a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/babel-traverse/src/path/removal.ts
Expand Up @@ -4,6 +4,7 @@ import { hooks } from "./lib/removal-hooks.ts";
import { getCachedPaths } from "../cache.ts";
import type NodePath from "./index.ts";
import { REMOVED, SHOULD_SKIP } from "./index.ts";
import { getBindingIdentifiers } from "@babel/types";

export function remove(this: NodePath) {
this._assertUnremoved();
Expand All @@ -24,7 +25,7 @@ export function remove(this: NodePath) {
}

export function _removeFromScope(this: NodePath) {
const bindings = this.getBindingIdentifiers();
const bindings = getBindingIdentifiers(this.node, false, false, true);
Object.keys(bindings).forEach(name => this.scope.removeBinding(name));
}

Expand Down
18 changes: 18 additions & 0 deletions packages/babel-types/src/retrievers/getBindingIdentifiers.ts
Expand Up @@ -5,6 +5,8 @@ import {
isFunctionDeclaration,
isFunctionExpression,
isExportAllDeclaration,
isAssignmentExpression,
isUnaryExpression,
} from "../validators/generated/index.ts";
import type * as t from "../index.ts";

Expand All @@ -14,18 +16,21 @@ function getBindingIdentifiers(
node: t.Node,
duplicates: true,
outerOnly?: boolean,
newBindingsOnly?: boolean,
): Record<string, Array<t.Identifier>>;

function getBindingIdentifiers(
node: t.Node,
duplicates?: false,
outerOnly?: boolean,
newBindingsOnly?: boolean,
): Record<string, t.Identifier>;

function getBindingIdentifiers(
node: t.Node,
duplicates?: boolean,
outerOnly?: boolean,
newBindingsOnly?: boolean,
): Record<string, t.Identifier> | Record<string, Array<t.Identifier>>;

/**
Expand All @@ -35,6 +40,7 @@ function getBindingIdentifiers(
node: t.Node,
duplicates?: boolean,
outerOnly?: boolean,
newBindingsOnly?: boolean,
): Record<string, t.Identifier> | Record<string, Array<t.Identifier>> {
const search: t.Node[] = [].concat(node);
const ids = Object.create(null);
Expand All @@ -43,6 +49,18 @@ function getBindingIdentifiers(
const id = search.shift();
if (!id) continue;

if (
newBindingsOnly &&
// These two nodes do not introduce _new_ bindings, but they are included
// in getBindingIdentifiers.keys for backwards compatibility.
// TODO(@nicolo-ribaudo): Check if we can remove them from .keys in a
// backward-compatible way, and if not what we need to do to remove them
// in Babel 8.
(isAssignmentExpression(id) || isUnaryExpression(id))
) {
continue;
}

const keys =
// @ts-expect-error getBindingIdentifiers.keys do not cover all AST types
getBindingIdentifiers.keys[id.type];
Expand Down

0 comments on commit cc8280a

Please sign in to comment.