import _ts from 'typescript' ; export function findTargetSyntaxKind < SK extends _ts . SyntaxKind > ( ts , node , syntaxKind ) { type TargetNode = SK extends _ts . SyntaxKind . ExportAssignment ? _ts . ExportAssignment : SK extends _ts . SyntaxKind . ObjectLiteralExpression ? _ts . ObjectLiteralExpression : SK extends _ts . SyntaxKind . CallExpression ? _ts . CallExpression : SK extends _ts . SyntaxKind . ArrayLiteralExpression ? _ts . ArrayLiteralExpression : _ts . Node ; function find ( node ) { if ( node . kind === syntaxKind ) { return node as any ; } return ts . forEachChild ( node , find ) ; } return find ( node ) as undefined | TargetNode ; } export function findNode ( sourceFile , position , lastChild = false ) { function find ( node ) { if ( position >= node . getStart ( ) && position < node . getEnd ( ) ) { return _ts . forEachChild ( node , find ) || node ; } } const target = find ( sourceFile ) ; if ( lastChild ) { return target && findLastChild ( target ) ; } else { return target ; } } function findLastChild ( node ) { function find ( n ) { return ! n . getChildCount ( ) ? n : find ( n . getChildren ( ) [ 0 ] ) ; } return find ( node ) ; }