All files / src utils.ts

100% Statements 23/23
100% Branches 10/10
100% Functions 3/3
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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    17x     17x 623x   799x 799x 149x         650x 355x     650x   474x     17x 139x 46x   93x 25x     68x 68x 68x 68x 4x   64x      
// 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);
    }
}