Debian Codegraph @

packages/babel-types/src/validators/isBinding.ts

raw
1 import getBindingIdentifiers from "../retrievers/getBindingIdentifiers";
2 import type * as t from "..";
3 /**
4 * Check if the input `node` is a binding identifier.
5 */
6 export default function isBinding(
7 node: t.Node,
8 parent: t.Node,
9 grandparent?: t.Node,
10 ): boolean {
11 if (
12 grandparent &&
13 node.type === "Identifier" &&
14 parent.type === "ObjectProperty" &&
15 grandparent.type === "ObjectExpression"
16 ) {
17 // We need to special-case this, because getBindingIdentifiers
18 // has an ObjectProperty->value entry for destructuring patterns.
19 return false;
20 }
21
22 const keys =
23 // @ts-expect-error getBindingIdentifiers.keys does not cover all AST types
24 getBindingIdentifiers.keys[parent.type];
25 if (keys) {
26 for (let i = 0; i < keys.length; i++) {
27 const key = keys[i];
28 const val =
29 // @ts-expect-error key must present in parent
30 parent[key];
31 if (Array.isArray(val)) {
32 if (val.indexOf(node) >= 0) return true;
33 } else {
34 if (val === node) return true;
35 }
36 }
37 }
38
39 return false;
40 }