Do not remove bindings when removing assignment expression path (#16131) · babel/babel@da7dc40

@@ -5,6 +5,8 @@ import {

55

isFunctionDeclaration,

66

isFunctionExpression,

77

isExportAllDeclaration,

8+

isAssignmentExpression,

9+

isUnaryExpression,

810

} from "../validators/generated/index.ts";

911

import type * as t from "../index.ts";

1012

@@ -14,18 +16,21 @@ function getBindingIdentifiers(

1416

node: t.Node,

1517

duplicates: true,

1618

outerOnly?: boolean,

19+

newBindingsOnly?: boolean,

1720

): Record<string, Array<t.Identifier>>;

18211922

function getBindingIdentifiers(

2023

node: t.Node,

2124

duplicates?: false,

2225

outerOnly?: boolean,

26+

newBindingsOnly?: boolean,

2327

): Record<string, t.Identifier>;

24282529

function getBindingIdentifiers(

2630

node: t.Node,

2731

duplicates?: boolean,

2832

outerOnly?: boolean,

33+

newBindingsOnly?: boolean,

2934

): Record<string, t.Identifier> | Record<string, Array<t.Identifier>>;

30353136

/**

@@ -35,6 +40,7 @@ function getBindingIdentifiers(

3540

node: t.Node,

3641

duplicates?: boolean,

3742

outerOnly?: boolean,

43+

newBindingsOnly?: boolean,

3844

): Record<string, t.Identifier> | Record<string, Array<t.Identifier>> {

3945

const search: t.Node[] = [].concat(node);

4046

const ids = Object.create(null);

@@ -43,6 +49,18 @@ function getBindingIdentifiers(

4349

const id = search.shift();

4450

if (!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+4664

const keys =

4765

// @ts-expect-error getBindingIdentifiers.keys do not cover all AST types

4866

getBindingIdentifiers.keys[id.type];