import type * as ts from 'typescript' ; import { TypescriptHelper } from '../descriptor/helper/helper' ; import { GenericDeclaration } from '../genericDeclaration/genericDeclaration' ; import { IGenericDeclaration } from '../genericDeclaration/genericDeclaration.interface' ; import { extensionExpressionSupported , GenericDeclarationSupported , } from '../genericDeclaration/genericDeclarationSupported' ; import { MockDefiner } from '../mockDefiner/mockDefiner' ; import { Identifiers } from '../mockIdentifier/mockIdentifier' ; import { Scope } from '../scope/scope' ; import { createArrayLiteral , createCall , } from '../../typescriptFactory/typescriptFactory' ; import { core } from '../core/core' ; export function GetMockFactoryCall ( typeReferenceNode , declaration , scope ) { return getDeclarationMockFactoryCall ( declaration , typeReferenceNode , scope ) ; } export function CreateMockFactory ( typeReferenceNode , declaration , scope ) { MockDefiner . instance . createMockFactory ( declaration , scope ) ; return getDeclarationMockFactoryCall ( declaration , typeReferenceNode , scope ) ; } export function GetMockFactoryCallIntersection ( intersection , scope ) { const genericDeclaration = GenericDeclaration ( scope ) ; const declarations = intersection . types . map ( ( type ) => { if ( core . ts . isTypeReferenceNode ( type ) ) { const declaration = TypescriptHelper . GetDeclarationFromNode ( type . typeName ) ; const declarationKey = MockDefiner . instance . getDeclarationKeyMapBasedOnScope ( declaration , scope ) ; genericDeclaration . addFromTypeReferenceNode ( type , declarationKey ) ; addFromDeclarationExtensions ( declaration as GenericDeclarationSupported , declarationKey , genericDeclaration , scope ) ; return declaration ; } return type as ts . TypeLiteralNode ; } ) ; const genericsParametersExpression = genericDeclaration . getExpressionForAllGenerics ( ) ; const mockFactoryCall = MockDefiner . instance . getMockFactoryIntersection ( declarations , intersection , scope ) ; return createCall ( mockFactoryCall , [ createArrayLiteral ( genericsParametersExpression ) , ] ) ; } export function GetMockFactoryCallTypeofEnum ( declaration , scope ) { const mockFactoryCall = MockDefiner . instance . getMockFactoryTypeofEnum ( declaration , scope ) ; return createCall ( mockFactoryCall , [ ] ) ; } export function GetMockFactoryCallForThis ( mockKey ) { const mockFactoryCall = MockDefiner . instance . getMockFactoryByKey ( mockKey ) ; return createCall ( mockFactoryCall , [ Identifiers . MockIdentifierGenericParameter , ] ) ; } function getDeclarationMockFactoryCall ( declaration , typeReferenceNode , scope ) { const declarationKey = MockDefiner . instance . getDeclarationKeyMapBasedOnScope ( declaration , scope ) ; if ( ! declarationKey ) { throw new Error ( `Failed to look up declaration key in MockDefiner for \`${ declaration . getText ( ) }'.` ) ; } const mockFactoryCall = MockDefiner . instance . getMockFactoryByKey ( declarationKey ) ; const genericDeclaration = GenericDeclaration ( scope ) ; genericDeclaration . addFromTypeReferenceNode ( typeReferenceNode , declarationKey ) ; addFromDeclarationExtensions ( declaration as GenericDeclarationSupported , declarationKey , genericDeclaration , scope ) ; const genericsParametersExpression = genericDeclaration . getExpressionForAllGenerics ( ) ; return createCall ( mockFactoryCall , [ createArrayLiteral ( genericsParametersExpression ) , ] ) ; } function addFromDeclarationExtensions ( declaration , declarationKey , genericDeclaration , scope ) { if ( declaration . heritageClauses ) { declaration . heritageClauses . forEach ( ( clause ) => { clause . types . forEach ( ( extension ) => { if ( ! extensionExpressionSupported ( extension . expression ) ) { return ; } const extensionDeclaration = TypescriptHelper . GetDeclarationFromNode ( extension . expression ) ; const extensionDeclarationKey = MockDefiner . instance . getDeclarationKeyMapBasedOnScope ( extensionDeclaration , scope ) ; genericDeclaration . addFromDeclarationExtension ( declarationKey , extensionDeclaration as GenericDeclarationSupported , extensionDeclarationKey , extension ) ; addFromDeclarationExtensions ( extensionDeclaration as GenericDeclarationSupported , extensionDeclarationKey , genericDeclaration , scope ) ; } ) ; } ) ; } }