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