All files / src/matchers class.ts

53.33% Statements 8/15
25% Branches 4/16
50% Functions 3/6
53.33% Lines 8/15

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 43 44 45 46 47 48 49 50 51        17x               17x 28x       28x 28x 28x             23x                                           28x    
// Dependencies:
import { TSQueryMatchers, TSQueryNode, TSQuerySelectorNode } from '../tsquery-types';
 
// Constants:
const CLASS_MATCHERS: TSQueryMatchers = {
    declaration,
    expression,
    'function': fn,
    pattern,
    statement
};
 
export function classs (node: TSQueryNode, selector: TSQuerySelectorNode, ancestry: Array<TSQueryNode>): boolean {
    Iif (!node.kindName) {
        return false;
    }
 
    const matcher = CLASS_MATCHERS[selector.name.toLowerCase()];
    Eif (matcher) {
        return matcher(node, selector, ancestry);
    }
 
    throw new Error(`Unknown class name: ${selector.name}`);
}
 
function declaration (node: TSQueryNode): boolean {
    return node.kindName.endsWith('Declaration');
}
 
function expression (node: TSQueryNode): boolean {
    const { kindName } = node;
    return kindName.endsWith('Expression') ||
        kindName.endsWith('Literal') ||
        (kindName === 'Identifier' && !!node.parent && (node.parent as TSQueryNode).kindName !== 'MetaProperty') ||
        kindName === 'MetaProperty';
}
 
function fn (node: TSQueryNode): boolean {
    const { kindName } = node;
    return kindName.startsWith('Function') ||
        kindName === 'ArrowFunction';
}
 
function pattern (node: TSQueryNode): boolean {
    return node.kindName.endsWith('Pattern') || expression(node);
}
 
function statement (node: TSQueryNode): boolean {
    return node.kindName.endsWith('Statement') || declaration(node);
}