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 { FunctionDeclaration, Node, SourceFile } from 'typescript'; 17import { SyntaxKind } from 'typescript'; 18import { getFunctionAndMethodReturnInfo, getParameter } from '../common/commonUtils'; 19import type { ParameterEntity, ReturnTypeEntity } from '../common/commonUtils'; 20 21/** 22 * get function info 23 * @param node 24 * @param sourceFile 25 * @returns 26 */ 27export function getFunctionDeclaration(node: Node, sourceFile: SourceFile): FunctionEntity { 28 const funcNode = node as FunctionDeclaration; 29 let functionName = ''; 30 const args: Array<ParameterEntity> = []; 31 let isExport = false; 32 const returnType = getFunctionAndMethodReturnInfo(funcNode, sourceFile); 33 functionName = funcNode.name?.escapedText === undefined ? 'undefind' : funcNode.name.escapedText.toString(); 34 funcNode.parameters.forEach(value => { 35 args.push(getParameter(value, sourceFile)); 36 }); 37 node.modifiers?.forEach(modify => { 38 if (modify.kind === SyntaxKind.ExportKeyword) { 39 isExport = true; 40 } 41 }); 42 43 return { 44 functionName: functionName, 45 returnType: returnType, 46 args: args, 47 isExport: isExport 48 }; 49} 50 51export interface FunctionEntity { 52 functionName: string, 53 returnType: ReturnTypeEntity, 54 args: Array<ParameterEntity>, 55 isExport: boolean 56} 57