• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 { ClassDeclaration, SourceFile } from 'typescript';
17import {
18  isConstructorDeclaration, isMethodDeclaration, isPropertyDeclaration, isTypeParameterDeclaration, SyntaxKind
19} from 'typescript';
20import { getExportKeyword } from '../common/commonUtils';
21import { getConstructorDeclaration } from './constructorDeclaration';
22import type { ConstructorEntity } from './constructorDeclaration';
23import { getHeritageClauseDeclaration } from './heritageClauseDeclaration';
24import type { HeritageClauseEntity } from './heritageClauseDeclaration';
25import { getMethodDeclaration } from './methodDeclaration';
26import type { MethodEntity, StaticMethodEntity } from './methodDeclaration';
27import { getPropertyDeclaration } from './propertyDeclaration';
28import type { PropertyEntity } from './propertyDeclaration';
29import { getTypeParameterDeclaration } from './typeParameterDeclaration';
30import type { TypeParameterEntity } from './typeParameterDeclaration';
31
32/**
33 * get class info
34 * @param classNode
35 * @param sourceFile
36 * @returns
37 */
38export function getClassDeclaration(classNode: ClassDeclaration, sourceFile: SourceFile): ClassEntity {
39  let exportModifiers: Array<number> = [];
40  if (classNode.modifiers !== undefined) {
41    exportModifiers = getExportKeyword(classNode.modifiers);
42  }
43
44  const className = classNode.name === undefined ? '' : classNode.name.escapedText.toString();
45  const heritageClauses: Array<HeritageClauseEntity> = [];
46  const classConstructor: Array<Array<ConstructorEntity>> = [];
47  const classMethod: Map<string, Array<MethodEntity>> = new Map<string, Array<MethodEntity>>();
48  const classProperty: Array<PropertyEntity> = [];
49  const typeParameters: Array<TypeParameterEntity> = [];
50  const staticMethods: Array<StaticMethodEntity> = [];
51
52  if (classNode.heritageClauses !== undefined) {
53    classNode.heritageClauses.forEach(value => {
54      heritageClauses.push(getHeritageClauseDeclaration(value, sourceFile));
55    });
56  }
57
58  classNode.members.forEach(value => {
59    if (isMethodDeclaration(value)) {
60      const methodEntity = getMethodDeclaration(value, sourceFile);
61      if (methodEntity.modifiers.includes(SyntaxKind.StaticKeyword)) {
62        staticMethods.push({ className: className, methodEntity: methodEntity });
63      } else {
64        if (classMethod.get(methodEntity.functionName.name) !== undefined) {
65          classMethod.get(methodEntity.functionName.name)?.push(methodEntity);
66        } else {
67          const methodArray: Array<MethodEntity> = [];
68          methodArray.push(methodEntity);
69          classMethod.set(methodEntity.functionName.name, methodArray);
70        }
71      }
72    } else if (isPropertyDeclaration(value)) {
73      classProperty.push(getPropertyDeclaration(value, sourceFile));
74    } else if (isConstructorDeclaration(value)) {
75      classConstructor.push(getConstructorDeclaration(value, sourceFile));
76    } else if (isTypeParameterDeclaration(value)) {
77      typeParameters.push(getTypeParameterDeclaration(value, sourceFile));
78    } else {
79      console.log('--------------------------- uncaught class type start -----------------------');
80      console.log('className: ' + className);
81      console.log(value);
82      console.log('--------------------------- uncaught class type end -----------------------');
83    }
84  });
85
86  return {
87    className: className,
88    typeParameters: typeParameters,
89    heritageClauses: heritageClauses,
90    classConstructor: classConstructor,
91    classMethod: classMethod,
92    classProperty: classProperty,
93    exportModifiers: exportModifiers,
94    staticMethods: staticMethods
95  };
96}
97
98export interface ClassEntity {
99  className: string,
100  typeParameters: Array<TypeParameterEntity>,
101  heritageClauses: Array<HeritageClauseEntity>,
102  classConstructor: Array<Array<ConstructorEntity>>,
103  classMethod: Map<string, Array<MethodEntity>>,
104  classProperty: Array<PropertyEntity>,
105  exportModifiers: Array<number>,
106  staticMethods: Array<StaticMethodEntity>
107}
108