import { Node } from 'typescript' ; import { MATCHERS } from './matchers' ; import { traverseChildren } from './traverse' ; import { TSQueryOptions , TSQuerySelectorNode } from './tsquery-types' ; export function match < T extends Node = Node > ( node , selector , options = { } ) { const results = [ ] ; if ( ! selector ) { return results ; } traverseChildren ( node , ( childNode , ancestry ) => { if ( findMatches ( childNode , selector , ancestry , options ) ) { results . push ( childNode as T ) ; } } , options ) ; return results ; } export function findMatches ( node , selector , ancestry = [ ] , options = { } ) { if ( ! selector ) { return true ; } if ( ! node ) { return false ; } const matcher = MATCHERS [ selector . type ] ; if ( matcher ) { return matcher ( node , selector , ancestry , options ) ; } throw new Error ( `Unknown selector type: ${ selector . type }` ) ; }