• 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 { ConstructorDeclaration, isIdentifier, Node, SourceFile } from 'typescript';
17
18/**
19 * get constructors info
20 * @param node
21 * @param sourceFile
22 * @returns
23 */
24export function getConstructorDeclaration(node: Node, sourceFile: SourceFile): Array<ConstructorEntity> {
25  const constructorNode = node as ConstructorDeclaration;
26  const constructors: Array<ConstructorEntity> = [];
27  constructorNode.parameters.forEach(value => {
28    const paramElement = value.name;
29    let name = '';
30    let typeName = '';
31    let typeKind = -1;
32    if (isIdentifier(paramElement)) {
33      name = paramElement.escapedText.toString();
34    } else {
35      name = sourceFile.text.substring(paramElement.pos, paramElement.end).trimStart().trimEnd();
36    }
37
38    const paramTypeElement = value.type;
39    if (paramTypeElement !== undefined) {
40      typeName = sourceFile.text.substring(paramTypeElement.pos, paramTypeElement.end).trimStart().trimEnd();
41      typeKind = paramTypeElement.kind;
42    }
43
44    constructors.push(
45      {
46        name: name,
47        typeName: typeName,
48        typeKind: typeKind
49      }
50    );
51  });
52  return constructors;
53}
54
55export interface ConstructorEntity {
56  name: string,
57  typeName: string,
58  typeKind: number
59}
60