import { template } from "@babel/core" ; import { Node } from "@babel/types" ; import { NodePath } from "@babel/traverse" ; import { default as Binding } from "@babel/traverse/lib/scope/binding" ; import { parse } from "@babel/parser" ; import generate , { GeneratorOptions } from "@babel/generator" ; export function ToAst < T = any > ( code ) { return ( template . ast ( code , { plugins : [ "typescript" , "jsx" ] } ) as unknown ) as T ; } export function ToNodePath < T = any > ( code ) { const path = new NodePath < T > ( null , null ) ; path . node = ( ToAst ( code ) as unknown ) as T ; return path ; } /**
 * only used in test to construct required binding param
 */ export function ToBinding ( code ) { const binding = new Binding ( { path : ToNodePath < Node > ( code ) , existing : null , identifier : null , scope : null , kind : "let" , } ) ; return binding ; } export function ToFile ( raw ) { const code = raw . replace ( new RegExp ( "^<" , "gm" ) , ";<" ) ; return parse ( code , { sourceType : "module" , plugins : [ "typescript" , "jsx" , [ "decorators" , { decoratorsBeforeExport : true } ] ] , } ) ; } export function ToString ( node , options ? ) { return generate ( node , options || { } ) . code ; }