import type * as ts from 'typescript' ; import { SetTsAutoMockOptions , TsAutoMockOptions } from '../../options/options' ; import { InitIdentifiers } from '../mockIdentifier/mockIdentifier' ; import { MockDefiner } from '../mockDefiner/mockDefiner' ; import { TypescriptHelper } from '../descriptor/helper/helper' ; import { CustomFunction , getMatchingCustomFunction } from '../matcher/matcher' ; import { GetIsFilesExcludedFromOptions } from '../../options/files' ; import { updateSourceFileNode } from '../../typescriptFactory/typescriptFactory' ; import { InitCore , core } from '../core/core' ; export function baseTransformer ( customFunctions , typescript ) { return ( program , options ? ) : ts . TransformerFactory < ts . SourceFile > => { if ( options ) { SetTsAutoMockOptions ( options ) ; } InitCore ( typescript , program ) ; InitIdentifiers ( ) ; const isFileExcluded = GetIsFilesExcludedFromOptions ( ) ; return ( context ) : ( ( file : ts . SourceFile ) => ts . SourceFile ) => ( file ) : ts . SourceFile => { if ( isFileExcluded ( file ) ) { return file ; } MockDefiner . instance . initFile ( file ) ; let sourceFile = visitNodeAndChildren ( file , context , customFunctions ) ; sourceFile = updateSourceFileNode ( sourceFile , [ ... MockDefiner . instance . getTopStatementsForFile ( sourceFile ) , ... sourceFile . statements , ] ) ; return sourceFile ; } ; } ; } function visitNodeAndChildren ( node , context , customFunctions ) ; function visitNodeAndChildren ( node , context , customFunctions ) ; function visitNodeAndChildren ( node , context , customFunctions ) { return core . ts . visitEachChild ( visitNode ( node , customFunctions ) , ( childNode ) => visitNodeAndChildren ( childNode , context , customFunctions ) , context ) ; } function visitNode ( node , customFunctions ) { if ( ! core . ts . isCallExpression ( node ) ) { return node ; } const signature = TypescriptHelper . getSignatureOfCallExpression ( node ) ; if ( ! signature || ! signature . declaration ) { return node ; } const matchingCustomFunction = getMatchingCustomFunction ( node , signature . declaration , customFunctions ) ; if ( ! matchingCustomFunction ) { return node ; } MockDefiner . instance . setFileNameFromNode ( node ) ; MockDefiner . instance . setTsAutoMockImportIdentifier ( ) ; return matchingCustomFunction . run ( node ) ; }