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