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