-
-
Notifications
You must be signed in to change notification settings - Fork 96
Expand file tree
/
Copy pathisEven.ts
More file actions
30 lines (26 loc) · 855 Bytes
/
Copy pathisEven.ts
File metadata and controls
30 lines (26 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import type { SimpleSyntaxNode } from "./QueryCapture";
/**
* Checks if a node is at an even index within its parent's field.
*
* @param node - The node to check.
* @param fieldName - The name of the field in the parent node.
* @returns True if the node is at an even index, false otherwise.
*/
export function isEven(node: SimpleSyntaxNode, fieldName: string): boolean {
if (node.parent == null) {
throw Error("Node has no parent");
}
const treeCursor = node.parent.walk();
let hasNext = treeCursor.gotoFirstChild();
let even = true;
while (hasNext) {
if (treeCursor.currentFieldName === fieldName) {
if (treeCursor.currentNode.id === node.id) {
return even;
}
even = !even;
}
hasNext = treeCursor.gotoNextSibling();
}
throw Error(`Node not found in parent for field: ${fieldName}`);
}