-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
Copy pathisContainedInErrorNode.ts
41 lines (34 loc) · 1.08 KB
/
isContainedInErrorNode.ts
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
import type { SyntaxNode } from "web-tree-sitter";
/**
* 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) {
// This node or one of it descendants is an error node
if (node.hasError) {
return true;
}
let ancestorNode: SyntaxNode | null = node.parent;
while (ancestorNode != null) {
// Ancestral node is an error node
if (ancestorNode.isError) {
return true;
}
// Ancestral node has errors, but it was not siblings to the previous node.
// We don't want to discard a node when a sibling that isn't adjacent is
// erroring.
if (ancestorNode.hasError) {
return false;
}
// A adjacent sibling node was causing the problem. ie we are right next to the error node.
if (
ancestorNode.previousSibling?.isError ||
ancestorNode.nextSibling?.isError
) {
return true;
}
ancestorNode = ancestorNode.parent;
}
return false;
}