import type * as ts from 'typescript' ; import { Identifiers } from '../../mockIdentifier/mockIdentifier' ; import { Scope } from '../../scope/scope' ; import { core } from '../../core/core' ; import { GetBooleanTrueDescriptor } from '../boolean/booleanTrue' ; import { GetDescriptor } from '../descriptor' ; import { TypescriptHelper } from '../helper/helper' ; import { createBinaryExpression , createBlock , createCall , createConditional , createElementAccess , createExpressionStatement , createIdentifier , createMethod , createObjectLiteral , createPropertyAccess , createPropertyAssignment , createPunctuationToken , createReturn , createStringLiteral , } from '../../../typescriptFactory/typescriptFactory' ; import { PropertyLike } from './propertyLike' ; export interface PropertyAssignments { lazy : ts . PropertyAssignment [ ] ; literals : ts . PropertyAssignment [ ] ; } export function GetMockPropertiesAssignments ( properties , scope ) { return properties . reduce ( ( acc , member ) : PropertyAssignments => { const descriptor = GetDescriptor ( member , scope ) ; if ( core . ts . isCallLikeExpression ( descriptor ) ) { acc . lazy . push ( GetLazyMockProperty ( descriptor , member ) ) ; } else { acc . literals . push ( GetLiteralMockProperty ( descriptor , member ) ) ; } return acc ; } , { lazy : [ ] , literals : [ ] } ) ; } function GetLiteralMockProperty ( descriptor , member ) { const propertyName = TypescriptHelper . GetStringPropertyName ( member . name ) ; return createPropertyAssignment ( createStringLiteral ( propertyName ) , descriptor ) ; } function GetLazyMockProperty ( descriptor , member ) { const propertyName = TypescriptHelper . GetStringPropertyName ( member . name ) ; const stringPropertyName = createStringLiteral ( propertyName ) ; const variableDeclarationName = createElementAccess ( Identifiers . MockIdentifierInternalValues , stringPropertyName ) ; const setVariableParameterName = Identifiers . MockIdentifierSetParameterName ; const expressionGetAssignment = createBinaryExpression ( variableDeclarationName , core . ts . SyntaxKind . EqualsToken , descriptor ) ; const hasOwnProperty = createCall ( createPropertyAccess ( Identifiers . MockIdentifierInternalValues , 'hasOwnProperty' ) , [ stringPropertyName ] ) ; const getExpressionBody = createConditional ( hasOwnProperty , createPunctuationToken ( core . ts . SyntaxKind . QuestionToken ) , variableDeclarationName , createPunctuationToken ( core . ts . SyntaxKind . ColonToken ) , expressionGetAssignment ) ; const setExpressionBody = createBinaryExpression ( variableDeclarationName , core . ts . SyntaxKind . EqualsToken , setVariableParameterName ) ; const returnGetStatement = createReturn ( getExpressionBody ) ; const getBody = createBlock ( [ returnGetStatement ] ) ; const returnSetStatement = createExpressionStatement ( setExpressionBody ) ; const setBody = createBlock ( [ returnSetStatement ] ) ; const get = createMethod ( 'get' , getBody , [ ] ) ; const set = createMethod ( 'set' , setBody , [ setVariableParameterName , ] ) ; const literal = createObjectLiteral ( [ get , set , createPropertyAssignment ( createIdentifier ( 'enumerable' ) , GetBooleanTrueDescriptor ( ) ) , ] ) ; return createPropertyAssignment ( createStringLiteral ( propertyName ) , literal ) ; }