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 { firstCharacterToUppercase } from '../common/commonUtils'; 18import { StaticMethodEntity } from '../declaration-node/methodDeclaration'; 19import { generateSymbolIterator, getCallbackStatement, getReturnStatement, getWarnConsole } from './generateCommonUtil'; 20 21/** 22 * generate static method 23 * @param staticMethod 24 * @param isSystem 25 * @param sourceFile 26 * @returns 27 */ 28export function generateStaticFunction(staticMethod: StaticMethodEntity, isSystem: boolean, sourceFile: SourceFile): string { 29 let methodBody = ''; 30 const rootName = staticMethod.className; 31 const methodEntity = staticMethod.methodEntity; 32 if (isSystem) { 33 methodBody += `${methodEntity.functionName.name}: function(...args) {`; 34 } else { 35 methodBody += `${firstCharacterToUppercase(staticMethod.className)}.${methodEntity.functionName.name} = function(...args) {`; 36 } 37 38 methodBody += getWarnConsole(rootName, methodEntity.functionName.name); 39 if (methodEntity.functionName.name === 'Symbol.iterator') { 40 methodBody += generateSymbolIterator(methodEntity); 41 methodBody += '}'; 42 return methodBody; 43 } 44 45 const args = methodEntity.args; 46 const len = args.length; 47 if (args.length > 0 && args[len - 1].paramName === 'callback') { 48 methodBody += getCallbackStatement(); 49 } 50 51 if (methodEntity.returnType.returnKind !== SyntaxKind.VoidKeyword) { 52 methodBody += getReturnStatement(methodEntity.returnType, sourceFile); 53 } 54 methodBody += '}'; 55 56 if (isSystem) { 57 methodBody += ','; 58 } else { 59 methodBody += ';'; 60 } 61 return methodBody; 62} 63