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 { FunctionEntity } from '../declaration-node/functionDeclaration'; 19import { 20 getCallbackStatement, 21 getReturnStatement, 22 getWarnConsole, 23 getReturnData, 24 getOverloadedFunctionCallbackStatement, 25 overloadedFunctionArr 26} from './generateCommonUtil'; 27 28/** 29 * generate function 30 * @param rootName 31 * @param functionArray 32 * @param sourceFile 33 * @param mockApi 34 * @param isRoot 35 * @returns 36 */ 37export function generateCommonFunction( 38 rootName: string, 39 functionArray: Array<FunctionEntity>, 40 sourceFile: SourceFile, 41 mockApi: string, 42 isRoot: boolean 43): string { 44 let functionBody = ''; 45 const functionEntity = functionArray[0]; 46 if (isRoot) { 47 functionBody = `const ${functionEntity.functionName} = function(...args) {`; 48 } else { 49 functionBody = `${functionEntity.functionName}: function(...args) {`; 50 } 51 functionBody += getWarnConsole(rootName, functionEntity.functionName); 52 53 if (functionArray.length === 1) { 54 const args = functionEntity.args; 55 const len = args.length; 56 if (args.length > 0 && args[len - 1].paramName.toLowerCase().includes('callback')) { 57 functionBody += getCallbackStatement(mockApi, args[len - 1]?.paramTypeString); 58 } 59 if (functionEntity.returnType.returnKind !== SyntaxKind.VoidKeyword) { 60 if (rootName === 'featureAbility' && functionEntity.returnType.returnKindName === 'Context') { 61 functionBody += 'return _Context;'; 62 } else if (rootName === 'inputMethod' && functionEntity.returnType.returnKindName === 'InputMethodSubtype') { 63 functionBody += 'return mockInputMethodSubtype().InputMethodSubtype;'; 64 } else { 65 functionBody += getReturnStatement(functionEntity.returnType, sourceFile); 66 } 67 } 68 } else { 69 const argSet: Set<string> = new Set<string>(); 70 let argParamsSet: string = ''; 71 const returnSet: Set<string> = new Set<string>(); 72 let isCallBack = false; 73 let needOverloaded = false; 74 functionArray.forEach(value => { 75 returnSet.add(value.returnType.returnKindName); 76 value.args.forEach(arg => { 77 argSet.add(arg.paramName); 78 if (arg.paramName.toLowerCase().includes('callback')) { 79 isCallBack = true; 80 if (arg.paramTypeString) { 81 argParamsSet = arg.paramTypeString; 82 } 83 } 84 if ( 85 arg.paramTypeString.startsWith("'") && arg.paramTypeString.endsWith("'") || 86 arg.paramTypeString.startsWith('"') && arg.paramTypeString.endsWith('"') 87 ) { 88 needOverloaded = true; 89 } 90 }); 91 }); 92 if (isCallBack) { 93 if (overloadedFunctionArr.includes(functionEntity.functionName) && needOverloaded) { 94 functionBody += getOverloadedFunctionCallbackStatement(functionArray, sourceFile, mockApi); 95 } else { 96 functionBody += getCallbackStatement(mockApi, argParamsSet); 97 } 98 } 99 let isReturnPromise = false; 100 let promiseReturnValue = ''; 101 let otherReturnValue = ''; 102 returnSet.forEach(value => { 103 if (value.includes('Promise<')) { 104 isReturnPromise = true; 105 promiseReturnValue = value; 106 } else { 107 if (!otherReturnValue) { 108 otherReturnValue = value; 109 } 110 } 111 }); 112 if (isReturnPromise) { 113 if (promiseReturnValue) { 114 let returnType = null; 115 functionArray.forEach(value => { 116 if (value.returnType.returnKindName === promiseReturnValue) { 117 returnType = value.returnType; 118 } 119 }); 120 functionBody += getReturnData(isCallBack, isReturnPromise, returnType, sourceFile, mockApi); 121 } else { 122 functionBody += ` 123 return new Promise((resolve, reject) => { 124 resolve('[PC Preview] unknow boolean'); 125 }) 126 `; 127 } 128 } else if (otherReturnValue) { 129 let returnType = null; 130 functionArray.forEach(value => { 131 if (value.returnType.returnKindName === otherReturnValue) { 132 returnType = value.returnType; 133 } 134 }); 135 functionBody += getReturnData(isCallBack, isReturnPromise, returnType, sourceFile, mockApi); 136 } 137 } 138 functionBody += isRoot ? '};' : '},'; 139 if (isRoot) { 140 functionBody += ` 141 if (!global.${functionEntity.functionName}) { 142 global.${functionEntity.functionName} = ${functionEntity.functionName}; 143 } 144 `; 145 } 146 return functionBody; 147} 148