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