Skip to content

Commit

Permalink
Do not remove bindings when removing assignment expression path (#16131)
Browse files Browse the repository at this point in the history
* Add failing test

* Fix it
  • Loading branch information
nicolo-ribaudo committed Nov 29, 2023
1 parent d4f3a22 commit da7dc40
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/babel-traverse/src/path/removal.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 da7dc40

Please sign in to comment.