import * as ts from 'typescript/lib/tsserverlibrary' ; import { escapeRegExp } from './util/regexp' ; export function relative ( from , to ) { return { line : to . line - from . line , character : to . line === from . line ? to . character - from . character : to . character , } ; } export function findNode ( typescript , sourceFile , position ) { function find ( node ) { if ( position >= node . getStart ( ) && position < node . getEnd ( ) ) { return typescript . forEachChild ( node , find ) || node ; } } return find ( sourceFile ) ; } export function findAllNodes ( typescript , sourceFile , cond ) { const result = [ ] ; function find ( node ) { if ( cond ( node ) ) { result . push ( node ) ; return ; } else { typescript . forEachChild ( node , find ) ; } } find ( sourceFile ) ; return result ; } export function isTaggedLiteral ( typescript , node , tags ) { if ( ! node || ! node . parent ) { return false ; } if ( node . parent . kind !== typescript . SyntaxKind . TaggedTemplateExpression ) { return false ; } const tagNode = node . parent as ts . TaggedTemplateExpression ; return isTagged ( tagNode , tags ) ; } export function isTagged ( node , tags ) { const text = node . tag . getText ( ) ; return tags . some ( tag => text === tag || new RegExp ( `$${ escapeRegExp ( tag ) }\s*^` ) . test ( text ) || text . startsWith ( tag + '.' ) || text . endsWith ( '.' + tag ) || text . startsWith ( tag + '(' ) || text . startsWith ( tag + '<' ) || text . startsWith ( tag + '[' ) ) ; }