import Class from "./class" ; import Constructor from "./constructor" ; export default class Locator { private ast : any ; constructor ( ast ) { this . ast = ast ; } findClass ( cursorAtLine ? ) { let firstClass ; const cursorClass = filter ( this . ast , 'class' , function ( node ) { if ( ! firstClass ) { firstClass = new Class ( node ) ; } if ( ! cursorAtLine || ( cursorAtLine >= node . loc . start . line && cursorAtLine <= node . loc . end . line ) ) { return new Class ( node ) ; } } ) ; return cursorClass || firstClass ; } findConstructor ( ) { return filter ( this . ast , 'method' , function ( node ) { if ( node . name ?. name === '__construct' ) { return new Constructor ( node ) ; } } ) ; } } function filter ( ast , kind , matcher ) { let result ; if ( Array . isArray ( ast ) ) { for ( var i = 0 ; i < ast . length ; i ++ ) { result = filter ( ast [ i ] , kind , matcher ) ; if ( result ) { return result ; } } } else { if ( ast ?. kind === kind ) { result = matcher ( ast ) ; if ( result ) { return result ; } } for ( const node in ast ) { if ( ast . hasOwnProperty ( node ) && ast [ node ] !== ast ) { result = filter ( ast [ node ] , kind , matcher ) ; if ( result ) { return result ; } } } } }