• 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 { isTypeLiteralNode, isTypeReferenceNode, isUnionTypeNode } from 'typescript';
17import type { Node, SourceFile, TypeAliasDeclaration } from 'typescript';
18
19/**
20 * get type alias info
21 * @param node
22 * @param sourceFile
23 * @returns
24 */
25export function getTypeAliasDeclaration(node: Node, sourceFile: SourceFile): TypeAliasEntity {
26  const typeAliasNode = node as TypeAliasDeclaration;
27  const typeAliasName = typeAliasNode.name.escapedText.toString();
28  const typeAliasTypeKind = typeAliasNode.type.kind;
29  const typeAliasTypeElements: Array<TypeAliasTypeEntity> = [];
30  const modifiers: Array<number> = [];
31  const modifiersNode = typeAliasNode.modifiers;
32  if (modifiersNode !== undefined) {
33    modifiersNode.forEach(value => {
34      modifiers.push(value.kind);
35    });
36  }
37  const typeAliasTypeElementsNode = typeAliasNode.type;
38  if (typeAliasTypeElementsNode !== undefined) {
39    if (isUnionTypeNode(typeAliasTypeElementsNode)) {
40      typeAliasTypeElementsNode.types.forEach(value => {
41        const typeName = sourceFile.text.substring(value.pos, value.end).trim();
42        const typeKind = value.kind;
43        typeAliasTypeElements.push({ typeName: typeName, typeKind: typeKind });
44      });
45    } else if (isTypeReferenceNode(typeAliasTypeElementsNode)) {
46      const typeName = sourceFile.text.substring(typeAliasTypeElementsNode.typeName.pos, typeAliasTypeElementsNode.typeName.end).trim();
47      typeAliasTypeElements.push({ typeName: typeName, typeKind: typeAliasTypeElementsNode.typeName.kind });
48    } else if (isTypeLiteralNode(typeAliasTypeElementsNode)) {
49      typeAliasTypeElementsNode.members.forEach(value => {
50        const typeName = sourceFile.text.substring(value.pos, value.end).trim();
51        const typeKind = value.kind;
52        typeAliasTypeElements.push({ typeName: typeName, typeKind: typeKind });
53      });
54    } else {
55      typeAliasTypeElements.push(
56        {
57          typeName: sourceFile.text.substring(typeAliasTypeElementsNode.pos, typeAliasTypeElementsNode.end),
58          typeKind: typeAliasTypeElementsNode.kind
59        }
60      );
61    }
62  }
63  return {
64    typeAliasName: typeAliasName,
65    typeAliasTypeKind: typeAliasTypeKind,
66    typeAliasTypeElements: typeAliasTypeElements,
67    modifiers: modifiers
68  };
69}
70
71export interface TypeAliasEntity {
72  typeAliasName: string,
73  typeAliasTypeKind: number,
74  typeAliasTypeElements: Array<TypeAliasTypeEntity>,
75  modifiers: Array<number>
76}
77
78export interface TypeAliasTypeEntity {
79  typeName: string,
80  typeKind: number
81}
82