Do not remove bindings when removing assignment expression path (#16131) · babel/babel@da7dc40
@@ -5,6 +5,8 @@ import {
55isFunctionDeclaration,
66isFunctionExpression,
77isExportAllDeclaration,
8+isAssignmentExpression,
9+isUnaryExpression,
810} from "../validators/generated/index.ts";
911import type * as t from "../index.ts";
1012@@ -14,18 +16,21 @@ function getBindingIdentifiers(
1416node: t.Node,
1517duplicates: true,
1618outerOnly?: boolean,
19+newBindingsOnly?: boolean,
1720): Record<string, Array<t.Identifier>>;
18211922function getBindingIdentifiers(
2023node: t.Node,
2124duplicates?: false,
2225outerOnly?: boolean,
26+newBindingsOnly?: boolean,
2327): Record<string, t.Identifier>;
24282529function getBindingIdentifiers(
2630node: t.Node,
2731duplicates?: boolean,
2832outerOnly?: boolean,
33+newBindingsOnly?: boolean,
2934): Record<string, t.Identifier> | Record<string, Array<t.Identifier>>;
30353136/**
@@ -35,6 +40,7 @@ function getBindingIdentifiers(
3540node: t.Node,
3641duplicates?: boolean,
3742outerOnly?: boolean,
43+newBindingsOnly?: boolean,
3844): Record<string, t.Identifier> | Record<string, Array<t.Identifier>> {
3945const search: t.Node[] = [].concat(node);
4046const ids = Object.create(null);
@@ -43,6 +49,18 @@ function getBindingIdentifiers(
4349const id = search.shift();
4450if (!id) continue;
455152+if (
53+newBindingsOnly &&
54+// These two nodes do not introduce _new_ bindings, but they are included
55+// in getBindingIdentifiers.keys for backwards compatibility.
56+// TODO(@nicolo-ribaudo): Check if we can remove them from .keys in a
57+// backward-compatible way, and if not what we need to do to remove them
58+// in Babel 8.
59+(isAssignmentExpression(id) || isUnaryExpression(id))
60+) {
61+continue;
62+}
63+4664const keys =
4765// @ts-expect-error getBindingIdentifiers.keys do not cover all AST types
4866getBindingIdentifiers.keys[id.type];