1/* 2 * Copyright (c) 2022 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 { SourceFile, SyntaxKind } from 'typescript'; 17import { MethodEntity } from '../declaration-node/methodDeclaration'; 18import { 19 generateSymbolIterator, getCallbackStatement, 20 getReturnStatement, getWarnConsole 21} from './generateCommonUtil'; 22 23/** 24 * generate class method 25 * @param rootName 26 * @param methodArray 27 * @param sourceFile 28 * @returns 29 */ 30export function generateCommonMethod(rootName: string, methodArray: Array<MethodEntity>, sourceFile: SourceFile): string { 31 let methodBody = ''; 32 const methodEntity = methodArray[0]; 33 if (methodEntity.functionName.name === 'Symbol.iterator') { 34 methodBody += `this[${methodEntity.functionName.name}] = function(...args) {`; 35 methodBody += getWarnConsole(rootName, methodEntity.functionName.name); 36 methodBody += generateSymbolIterator(methodEntity); 37 methodBody += '};\n'; 38 return methodBody; 39 } else { 40 methodBody += `this.${methodEntity.functionName.name} = function(...args) {`; 41 methodBody += getWarnConsole(rootName, methodEntity.functionName.name); 42 } 43 44 if (methodArray.length === 1) { 45 const args = methodEntity.args; 46 const len = args.length; 47 if (args.length > 0 && args[len - 1].paramName.toLowerCase().includes('callback')) { 48 methodBody += getCallbackStatement(); 49 } 50 if (methodEntity.returnType.returnKind !== SyntaxKind.VoidKeyword) { 51 if (methodEntity.functionName.name === 'getApplicationContext') { 52 methodBody += 'return new Context();'; 53 } else { 54 methodBody += getReturnStatement(methodEntity.returnType, sourceFile); 55 } 56 } 57 } else { 58 const argSet: Set<string> = new Set<string>(); 59 const returnSet: Set<string> = new Set<string>(); 60 let isCallBack = false; 61 methodArray.forEach(value => { 62 returnSet.add(value.returnType.returnKindName); 63 value.args.forEach(arg => { 64 argSet.add(arg.paramName); 65 if (arg.paramName.toLowerCase().includes('callback')) { 66 isCallBack = true; 67 } 68 }); 69 }); 70 if (isCallBack) { 71 methodBody += getCallbackStatement(); 72 } 73 let isReturnPromise = false; 74 returnSet.forEach(value => { 75 if (value.startsWith('Promise')) { 76 isReturnPromise = true; 77 } 78 }); 79 80 if (isReturnPromise && isCallBack) { 81 methodBody += `else { 82 return new Promise((resolve, reject) => { 83 resolve('[PC Preview] unknow boolean'); 84 }) 85 }`; 86 } else { 87 methodBody += `return new Promise((resolve, reject) => { 88 resolve('[PC preview] unknown type'); 89 });`; 90 } 91 } 92 methodBody += '};\n'; 93 return methodBody; 94} 95