1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import type { SourceFile } from 'typescript'; 17import { SyntaxKind } from 'typescript'; 18import type { MethodSignatureEntity } from '../declaration-node/methodSignatureDeclaration'; 19import { getCallbackStatement, getReturnStatement, getWarnConsole } from './generateCommonUtil'; 20 21/** 22 * generate interface signature method 23 * @param rootName 24 * @param methodSignatureArray 25 * @param sourceFile 26 * @returns 27 */ 28export function generateCommonMethodSignature(rootName: string, methodSignatureArray: Array<MethodSignatureEntity>, sourceFile: SourceFile, mockApi: string): string { 29 let methodSignatureBody = ''; 30 const methodEntity = methodSignatureArray[0]; 31 methodSignatureBody += `${methodEntity.functionName}: function(...args) {`; 32 methodSignatureBody += getWarnConsole(rootName, methodEntity.functionName); 33 if (methodSignatureArray.length === 1) { 34 const args = methodEntity.args; 35 const len = args.length; 36 if (args.length > 0 && args[len - 1].paramName.toLowerCase().includes('callback')) { 37 methodSignatureBody += getCallbackStatement(mockApi, args[len - 1]?.paramTypeString); 38 } 39 if (methodEntity.returnType.returnKind !== SyntaxKind.VoidKeyword) { 40 if (rootName === 'Context' && methodEntity.returnType.returnKindName === 'Context') { 41 methodSignatureBody += 'return Context;'; 42 } else { 43 methodSignatureBody += getReturnStatement(methodEntity.returnType, sourceFile); 44 } 45 } 46 } else { 47 const argSet: Set<string> = new Set<string>(); 48 let argParamsSet: string = ''; 49 const returnSet: Set<string> = new Set<string>(); 50 let isCallBack = false; 51 methodSignatureArray.forEach(value => { 52 returnSet.add(value.returnType.returnKindName); 53 value.args.forEach(arg => { 54 argSet.add(arg.paramName); 55 if (arg.paramName.toLowerCase().includes('callback')) { 56 isCallBack = true; 57 if (arg.paramTypeString) { 58 argParamsSet = arg.paramTypeString; 59 } 60 } 61 }); 62 }); 63 if (isCallBack) { 64 methodSignatureBody += getCallbackStatement(mockApi, argParamsSet); 65 } 66 let isReturnPromise = false; 67 returnSet.forEach(value => { 68 if (value.startsWith('Promise')) { 69 isReturnPromise = true; 70 } 71 }); 72 if (isReturnPromise && isCallBack) { 73 methodSignatureBody += `else { 74 return new Promise((resolve, reject) => { 75 resolve('[PC Preview] unknow boolean'); 76 }) 77 }`; 78 } 79 } 80 methodSignatureBody += '},\n'; 81 return methodSignatureBody; 82} 83