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