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 { isComputedPropertyName } from 'typescript'; 17import type { MethodDeclaration, Node, SourceFile } from 'typescript'; 18import { 19 getFunctionAndMethodReturnInfo, getModifiers, getParameter, getPropertyName 20} from '../common/commonUtils'; 21import type { ParameterEntity, ReturnTypeEntity } from '../common/commonUtils'; 22 23/** 24 * get method info 25 * @param node 26 * @param sourceFile 27 * @returns 28 */ 29export function getMethodDeclaration(node: Node, sourceFile: SourceFile): MethodEntity { 30 const methodNode = node as MethodDeclaration; 31 const functionName = { 32 name: '', 33 expressionKind: -1, 34 kind: -1 35 }; 36 37 const args: Array<ParameterEntity> = []; 38 let modifiers: Array<number> = []; 39 40 if (methodNode.modifiers !== undefined) { 41 modifiers = getModifiers(methodNode.modifiers); 42 } 43 44 const returnType = getFunctionAndMethodReturnInfo(methodNode, sourceFile); 45 functionName.name = getPropertyName(methodNode.name, sourceFile); 46 if (isComputedPropertyName(methodNode.name)) { 47 functionName.expressionKind = methodNode.name.expression.kind; 48 } 49 50 functionName.kind = methodNode.name.kind; 51 52 methodNode.parameters.forEach(value => { 53 args.push(getParameter(value, sourceFile)); 54 }); 55 56 return { 57 modifiers: modifiers, 58 functionName: functionName, 59 returnType: returnType, 60 args: args 61 }; 62} 63 64export interface StaticMethodEntity { 65 className: string, 66 methodEntity: MethodEntity 67} 68 69export interface MethodEntity { 70 modifiers: Array<number>, 71 functionName: FunctionNameEntity, 72 returnType: ReturnTypeEntity, 73 args: Array<ParameterEntity> 74} 75 76export interface FunctionNameEntity { 77 name: string, 78 expressionKind: number, 79 kind: number 80} 81