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 { EnumDeclaration, SourceFile } from 'typescript'; 17import { getExportKeyword, getPropertyName } from '../common/commonUtils'; 18 19/** 20 * get enum info 21 * @param node 22 * @param sourceFile 23 * @returns 24 */ 25export function getEnumDeclaration(node: EnumDeclaration, sourceFile: SourceFile): EnumEntity { 26 const enumName = node.name.escapedText.toString(); 27 const enumMembers: Array<MemberEntity> = []; 28 let exportModifiers: Array<number> = []; 29 30 if (node.modifiers !== undefined) { 31 exportModifiers = getExportKeyword(node.modifiers); 32 } 33 34 node.members.forEach(value => { 35 const enumValueName = getPropertyName(value.name, sourceFile); 36 let enumValue = ''; 37 if (value.initializer !== undefined) { 38 enumValue = sourceFile.text.substring(value.initializer.pos, value.initializer.end).trimEnd().trimStart(); 39 } 40 const enumKind = value.initializer?.kind === undefined ? -1 : value.initializer?.kind; 41 enumMembers.push({ enumValueName: enumValueName, enumValue: enumValue, enumKind: enumKind }); 42 }); 43 44 return { 45 enumName: enumName, 46 enumMembers: enumMembers, 47 exportModifiers: exportModifiers 48 }; 49} 50 51export interface EnumEntity { 52 enumName: string, 53 enumMembers: Array<MemberEntity>, 54 exportModifiers: Array<number> 55} 56 57export interface MemberEntity { 58 enumValueName: string, 59 enumValue: string, 60 enumKind: number, 61} 62