import * as ts from 'typescript' ; import { TransformerFactoryCreator , TypeScriptMacroNodeTransformFunction , } from './types' ; import { getBindingIdentifiers , IdentifiersMap , } from './utils/getBindingIdentifiers' ; import { getMacroImportDeclarations } from './utils/getMacroImportDeclarations' ; import { isMacroImportDeclaration } from './utils/isMacroImportDeclaration' ; export const transformerFactoryCreator = languageService => { const beforeTransformerFactory = context => node => { const visitEachChild = < T extends ts . Node > ( node , visitor ) => ts . visitEachChild ( node , visitor , context ) ; const visitorFactory = ( { bindings , transFunc , } ) => { const visitor = node => { if ( ts . isImportDeclaration ( node ) ) { return isMacroImportDeclaration ( node ) ? undefined : node ; } const ret = transFunc ( { reference ( node ) { const a = Object . entries ( bindings ) . find ( ( [ key , ids ] ) => ids . includes ( node ) ) ; return a === undefined ? false : a [ 0 ] ; } , node , languageService } ) ; return ret === undefined || ts . isExportAssignment ( ret ) || ts . isExportDeclaration ( ret ) ? ret : visitEachChild ( ret , visitor ) ; } ; return visitor ; } ; const imDeclarations = getMacroImportDeclarations ( node ) . map ( x => ( { bindings : getBindingIdentifiers ( x ) , transFunc : require ( x . moduleSpecifier . getText ( ) . slice ( 1 , - 1 ) ) . ____$$$$____typescriptMacroNodeTransformFunction____$$$$____ as TypeScriptMacroNodeTransformFunction } ) ) ; const a = imDeclarations . reduce ( ( node , curr ) => visitEachChild ( node , visitorFactory ( curr ) ) , node ) ; return a ; } ; return { before : [ beforeTransformerFactory ] , after : [ ] } ; } ;