import traverse from '@babel/traverse' ; import * as t from '@babel/types' ; export const getRootNode = ( ast ) : t . JSXElement => { let rootNode ; traverse ( ast , { JSXElement : ( path ) => { if ( path . key === 'expression' ) { rootNode = path . node ; } } , } ) ; return rootNode ; } ; param rootNode param indexes export function getSelectedNode ( rootNode , indexes = [ ] ) { const newIndexes = indexes . slice ( 1 ) ; const selectedNode = newIndexes . reduce ( ( acc , index ) => { const children = acc . children . filter ( ( child ) => child . type === 'JSXElement' ) ; return children [ index ] ; } , rootNode ) ; return selectedNode as t . JSXElement ; } param element param rootElement param acc export function getAncestorsIndexes ( element , rootElement , acc = [ ] ) { if ( ! element . parentElement ) { return [ ] ; } const index = [ ] . indexOf . call ( element . parentElement . children , element ) ; const result = [ index , ... acc ] ; if ( element . parentElement !== rootElement ) { return getAncestorsIndexes ( element . parentElement , rootElement , result ) ; } return result ; } export function getSelectedElement ( rootElement , ancestorIndexes = [ ] ) { const result = ancestorIndexes . reduce ( ( prev , index ) => { return prev . children [ index ] ; } , rootElement ) ; return result ; }