forked from phenomnomnominal/tsquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
41 lines (35 loc) · 1.1 KB
/
utils.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
// Dependencies:
import { Node } from 'typescript';
import { addProperties } from './traverse';
import { TSQueryNode } from './tsquery-types';
export function getPath (obj: any, path: string): any {
const keys = path.split('.');
for (const key of keys) {
if (obj == null) {
return obj;
}
// HACK: We need to make sure that the extra properties
// have been added on nodes that haven't been fully traversed yet...
if (['text', 'name', 'kindName'].includes(key)) {
addProperties(obj as TSQueryNode);
}
obj = obj[key];
}
return obj;
}
export function inPath (node: Node, ancestor: Node, path: Array<string>): boolean {
if (path.length === 0) {
return node === ancestor;
}
if (ancestor == null) {
return false;
}
const [first] = path;
const field = (ancestor as any)[first];
const remainingPath = path.slice(1);
if (Array.isArray(field)) {
return field.some(item => inPath(node, item, remainingPath));
} else {
return inPath(node, field, remainingPath);
}
}