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 32 const modifiersNode = typeAliasNode.modifiers; 33 if (modifiersNode !== undefined) { 34 modifiersNode.forEach(value => { 35 modifiers.push(value.kind); 36 }); 37 } 38 39 const typeAliasTypeElementsNode = typeAliasNode.type; 40 if (typeAliasTypeElementsNode !== undefined) { 41 if (isUnionTypeNode(typeAliasTypeElementsNode)) { 42 typeAliasTypeElementsNode.types.forEach(value => { 43 const typeName = sourceFile.text.substring(value.pos, value.end).trimStart().trimEnd(); 44 const typeKind = value.kind; 45 typeAliasTypeElements.push({ typeName: typeName, typeKind: typeKind }); 46 }); 47 } else if (isTypeReferenceNode(typeAliasTypeElementsNode)) { 48 const typeName = sourceFile.text.substring(typeAliasTypeElementsNode.typeName.pos, typeAliasTypeElementsNode.typeName.end).trimStart().trimEnd(); 49 typeAliasTypeElements.push({ typeName: typeName, typeKind: typeAliasTypeElementsNode.typeName.kind }); 50 } else if (isTypeLiteralNode(typeAliasTypeElementsNode)) { 51 typeAliasTypeElementsNode.members.forEach(value => { 52 const typeName = sourceFile.text.substring(value.pos, value.end).trimStart().trimEnd(); 53 const typeKind = value.kind; 54 typeAliasTypeElements.push({ typeName: typeName, typeKind: typeKind }); 55 }); 56 } else { 57 typeAliasTypeElements.push( 58 { 59 typeName: sourceFile.text.substring(typeAliasTypeElementsNode.pos, typeAliasTypeElementsNode.end), 60 typeKind: typeAliasTypeElementsNode.kind 61 } 62 ); 63 } 64 } 65 66 return { 67 typeAliasName: typeAliasName, 68 typeAliasTypeKind: typeAliasTypeKind, 69 typeAliasTypeElements: typeAliasTypeElements, 70 modifiers: modifiers 71 }; 72} 73 74export interface TypeAliasEntity { 75 typeAliasName: string, 76 typeAliasTypeKind: number, 77 typeAliasTypeElements: Array<TypeAliasTypeEntity>, 78 modifiers: Array<number> 79} 80 81export interface TypeAliasTypeEntity { 82 typeName: string, 83 typeKind: number 84} 85