@checkdigit/eslint-plugin 6.6.0-PR.75-e80b → 6.6.0-PR.75-b2d2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,31 @@
1
+ // tree.ts
2
+
3
+ /*
4
+ * Copyright (c) 2021-2024 Check Digit, LLC
5
+ *
6
+ * This code is licensed under the MIT license (see LICENSE.txt for details).
7
+ */
8
+
9
+ import type { Node } from 'estree';
10
+
11
+ type NodeParent = Node | undefined | null;
12
+
13
+ interface NodeParentExtension {
14
+ parent: NodeParent;
15
+ }
16
+
17
+ export function getParent(node: Node): Node | undefined | null {
18
+ return (node as unknown as NodeParentExtension).parent;
19
+ }
20
+
21
+ export function getAncestor(node: Node, matcher: string | ((testNode: Node) => boolean), typeToExit?: string) {
22
+ const parent = getParent(node);
23
+ if (!parent || (typeToExit !== undefined && parent.type === typeToExit)) {
24
+ return undefined;
25
+ } else if (typeof matcher === 'string' && parent.type === matcher) {
26
+ return parent;
27
+ } else if (typeof matcher === 'function' && matcher(parent)) {
28
+ return parent;
29
+ }
30
+ return getAncestor(parent, matcher, typeToExit);
31
+ }