import * as parser from '@babel/parser' ; import traverse from '@babel/traverse' ; type Condition = ( path : any ) => boolean ; type Predicate = ( path : any ) => any ; export const valueFromTraverse = ( condition , predicate = ( p ) => p ) => ( code ) => { let value = '' ; const ast = parser . parse ( code , { plugins : [ 'jsx' ] } ) ; traverse ( ast as any , { enter ( path ) { if ( condition ( path ) ) { value = predicate ( path ) ; path . stop ( ) ; return ; } } , } ) ; return value ; } ; export const codeFromNode = ( condition ) => ( code ) => valueFromTraverse ( condition , ( p ) => code . slice ( p . node . start , p . node . end ) ) ( code ) ;