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

Do not remove bindings when removing assignment expression path #16131

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
8 changes: 8 additions & 0 deletions packages/babel-traverse/test/removal.js
Expand Up @@ -144,4 +144,12 @@ describe("removal", function () {
expect(ifPath.get("alternate").node).toBeNull();
});
});

it("of AssignmentExpression does not remove binding", function () {
const rootPath = getPath("var x; x = 1;");
const path = rootPath.get("body.1.expression");
path.remove();

expect(rootPath.scope.hasBinding("x")).toBe(true);
});
});
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