import type * as ts from 'typescript' ; import { Strings } from '../mockIdentifier/mockIdentifier' ; import { core } from '../core/core' ; import { KeyCounter } from './keyCounter' ; export type PossibleDeclaration = ts . InterfaceDeclaration | ts . ClassDeclaration | ts . TypeAliasDeclaration ; export class FactoryUniqueName { private _keyCounter : KeyCounter ; constructor ( ) { this . _keyCounter = new KeyCounter ( ) ; } public createForDeclaration ( declaration ) { const declarationNameIdentifier = declaration . name ; return this . _createUniqueName ( declarationNameIdentifier && declarationNameIdentifier . text ) ; } public createForIntersection ( nodes ) { const nameOfDeclarations = nodes . reduce ( ( acc , declaration ) => { if ( core . ts . isTypeLiteralNode ( declaration ) ) { acc += Strings . MockCallLiteralText ; } if ( core . ts . isInterfaceDeclaration ( declaration ) || core . ts . isTypeAliasDeclaration ( declaration ) || core . ts . isClassDeclaration ( declaration ) ) { acc += declaration . name ?. text || '' ; } return acc ; } , '' ) ; return this . _createUniqueName ( nameOfDeclarations ) ; } private _createUniqueName ( name ? ) { const declarationNameSanitized = name || Strings . MockCallAnonymousText ; const baseFactoryName = `@${ declarationNameSanitized }` ; const count = this . _keyCounter . getFor ( baseFactoryName ) ; return `${ baseFactoryName }_${ count }` ; } }