|
1 | | -import * as ts from 'typescript'; |
| 1 | +import ts from 'typescript'; |
2 | 2 | import { TypescriptCreator } from '../../helper/creator'; |
3 | 3 | import { MockDefiner } from '../../mockDefiner/mockDefiner'; |
4 | 4 | import { ModuleName } from '../../mockDefiner/modules/moduleName'; |
5 | 5 | import { TypescriptHelper } from '../helper/helper'; |
6 | 6 |
|
7 | | -export function GetMethodDescriptor(propertyName: ts.PropertyName, returnValue: ts.Expression): ts.Expression { |
| 7 | +export interface MethodSignature { |
| 8 | + parameters?: ts.ParameterDeclaration[]; |
| 9 | + returnValue: ts.Expression; |
| 10 | +} |
| 11 | + |
| 12 | +export function GetMethodDescriptor(propertyName: ts.PropertyName, methodSignatures: MethodSignature[]): ts.Expression { |
8 | 13 | const providerGetMethod: ts.PropertyAccessExpression = CreateProviderGetMethod(); |
9 | 14 |
|
10 | 15 | const propertyNameString: string = TypescriptHelper.GetStringPropertyName(propertyName); |
11 | 16 | const propertyNameStringLiteral: ts.StringLiteral = ts.createStringLiteral(propertyNameString); |
12 | 17 |
|
13 | | - const propertyValueFunction: ts.ArrowFunction = TypescriptCreator.createArrowFunction(ts.createBlock( |
14 | | - [ts.createReturn(returnValue)], |
| 18 | + const [signatureWithMostParameters]: MethodSignature[] = [...methodSignatures].sort( |
| 19 | + ( |
| 20 | + { parameters: leftParameters = [] }: MethodSignature, |
| 21 | + { parameters: rightParameters = [] }: MethodSignature, |
| 22 | + ) => rightParameters.length - leftParameters.length, |
| 23 | + ); |
| 24 | + |
| 25 | + const longestParameterList: ts.ParameterDeclaration[] = signatureWithMostParameters.parameters || []; |
| 26 | + |
| 27 | + const block: ts.Block = ts.createBlock( |
| 28 | + [ |
| 29 | + ResolveSignatureElseBranch(methodSignatures, longestParameterList), |
| 30 | + ], |
15 | 31 | true, |
16 | | - )); |
| 32 | + ); |
| 33 | + |
| 34 | + const propertyValueFunction: ts.ArrowFunction = TypescriptCreator.createArrowFunction( |
| 35 | + block, |
| 36 | + longestParameterList, |
| 37 | + ); |
17 | 38 |
|
18 | 39 | return TypescriptCreator.createCall(providerGetMethod, [propertyNameStringLiteral, propertyValueFunction]); |
19 | 40 | } |
20 | 41 |
|
| 42 | +function CreateTypeEquality(signatureType: ts.TypeNode | undefined, primaryDeclaration: ts.ParameterDeclaration): ts.Expression { |
| 43 | + const identifier: ts.Identifier = ts.createIdentifier(primaryDeclaration.name.getText()); |
| 44 | + |
| 45 | + if (!signatureType) { |
| 46 | + return ts.createPrefix( |
| 47 | + ts.SyntaxKind.ExclamationToken, |
| 48 | + ts.createPrefix( |
| 49 | + ts.SyntaxKind.ExclamationToken, |
| 50 | + identifier, |
| 51 | + ), |
| 52 | + ); |
| 53 | + } |
| 54 | + |
| 55 | + if (TypescriptHelper.isLiteralRuntimeTypeNode(signatureType)) { |
| 56 | + return ts.createStrictEquality( |
| 57 | + ts.createTypeOf(identifier), |
| 58 | + signatureType ? ts.createStringLiteral(signatureType.getText()) : ts.createVoidZero(), |
| 59 | + ); |
| 60 | + } else { |
| 61 | + return ts.createBinary(identifier, ts.SyntaxKind.InstanceOfKeyword, ts.createIdentifier(signatureType.getText())); |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +function CreateUnionTypeOfEquality(signatureType: ts.TypeNode | undefined, primaryDeclaration: ts.ParameterDeclaration): ts.Expression { |
| 66 | + const typeNodes: ts.TypeNode[] = []; |
| 67 | + |
| 68 | + if (signatureType) { |
| 69 | + if (ts.isUnionTypeNode(signatureType)) { |
| 70 | + typeNodes.push(...signatureType.types); |
| 71 | + } else { |
| 72 | + typeNodes.push(signatureType); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + const [firstType, ...remainingTypes]: ts.TypeNode[] = typeNodes; |
| 77 | + |
| 78 | + return remainingTypes.reduce( |
| 79 | + (prevStatement: ts.Expression, typeNode: ts.TypeNode) => |
| 80 | + ts.createLogicalOr( |
| 81 | + prevStatement, |
| 82 | + CreateTypeEquality(typeNode, primaryDeclaration), |
| 83 | + ), |
| 84 | + CreateTypeEquality(firstType, primaryDeclaration), |
| 85 | + ); |
| 86 | +} |
| 87 | + |
| 88 | +function ResolveParameterBranch(declarations: ts.ParameterDeclaration[], allDeclarations: ts.ParameterDeclaration[], returnValue: ts.Expression, elseBranch: ts.Statement): ts.Statement { |
| 89 | + const [firstDeclaration, ...remainingDeclarations]: ts.ParameterDeclaration[] = declarations; |
| 90 | + |
| 91 | + const condition: ts.Expression = remainingDeclarations.reduce( |
| 92 | + (prevStatement: ts.Expression, declaration: ts.ParameterDeclaration, index: number) => |
| 93 | + ts.createLogicalAnd( |
| 94 | + prevStatement, |
| 95 | + CreateUnionTypeOfEquality(declaration.type, allDeclarations[index + 1]), |
| 96 | + ), |
| 97 | + CreateUnionTypeOfEquality(firstDeclaration.type, allDeclarations[0]), |
| 98 | + ); |
| 99 | + |
| 100 | + |
| 101 | + return ts.createIf(condition, ts.createReturn(returnValue), elseBranch); |
| 102 | +} |
| 103 | + |
| 104 | +function ResolveSignatureElseBranch(signatures: MethodSignature[], longestParameterList: ts.ParameterDeclaration[]): ts.Statement { |
| 105 | + const [signature, ...remainingSignatures]: MethodSignature[] = signatures; |
| 106 | + |
| 107 | + if (remainingSignatures.length) { |
| 108 | + const elseBranch: ts.Statement = ResolveSignatureElseBranch(remainingSignatures, longestParameterList); |
| 109 | + |
| 110 | + const currentParameters: ts.ParameterDeclaration[] = signature.parameters || []; |
| 111 | + return ResolveParameterBranch(currentParameters, longestParameterList, signature.returnValue, elseBranch); |
| 112 | + } else { |
| 113 | + return ts.createReturn(signature.returnValue); |
| 114 | + } |
| 115 | +} |
| 116 | + |
21 | 117 | function CreateProviderGetMethod(): ts.PropertyAccessExpression { |
22 | 118 | return ts.createPropertyAccess( |
23 | 119 | ts.createPropertyAccess( |
|
0 commit comments