Repository navigation
Expand file tree
/
Copy pathtreeSitterUtils.ts
More file actions
68 lines (52 loc) · 1.89 KB
/
Copy pathtreeSitterUtils.ts
File metadata and controls
68 lines (52 loc) · 1.89 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import type { SyntaxNode } from "web-tree-sitter";
export const getValueNode = (node: SyntaxNode) =>
node.childForFieldName("value");
export const getLeftNode = (node: SyntaxNode) => node.childForFieldName("left");
export const getNameNode = (node: SyntaxNode) => node.childForFieldName("name");
export const getKeyNode = (node: SyntaxNode) => node.childForFieldName("key");
export const getDefinitionNode = (node: SyntaxNode) =>
node.childForFieldName("definition");
export const getDeclarationNode = (node: SyntaxNode) =>
node.childForFieldName("declarator");
export function getChildNodesForFieldName(
node: SyntaxNode,
fieldName: string,
): SyntaxNode[] {
const treeCursor = node.walk();
treeCursor.gotoFirstChild();
const ret = [];
let hasNext = true;
while (hasNext) {
if (treeCursor.currentFieldName === fieldName) {
ret.push(treeCursor.currentNode);
}
hasNext = treeCursor.gotoNextSibling();
}
return ret;
}
/**
* Returns a list of the node's ancestors, including the node itself if
* `includeNode` is `true`
* @param node The node to iterate ancestors from
* @param includeNode Whether to include the node itself in the returned list
* @returns A list of ancestors possibly including the includeNode node itself
*/
export function getAncestors(node: SyntaxNode, includeNode: boolean = true) {
const ancestors: SyntaxNode[] = includeNode ? [node] : [];
for (
let currentNode: SyntaxNode | null = node.parent;
currentNode != null;
currentNode = currentNode.parent
) {
ancestors.push(currentNode);
}
return ancestors;
}
/**
* Determines whether the given node or one of its ancestors is an error node
* @param node The node to check
* @returns True if the given node is contained in an error node
*/
export function isContainedInErrorNode(node: SyntaxNode) {
return getAncestors(node).some((ancestor) => ancestor.type === "ERROR");
}