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 { 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  const fileText = sourceFile.getFullText();
30
31  if (node.modifiers !== undefined) {
32    exportModifiers = getExportKeyword(node.modifiers);
33  }
34
35  node.members.forEach(value => {
36    const enumValueName = getPropertyName(value.name, sourceFile);
37    let enumValue = '';
38    if (value.initializer !== undefined) {
39      enumValue = fileText.slice(value.initializer.pos, value.initializer.end).trim();
40    }
41    const enumKind = value.initializer?.kind === undefined ? -1 : value.initializer?.kind;
42    enumMembers.push({ enumValueName: enumValueName, enumValue: enumValue, enumKind: enumKind });
43  });
44
45  return {
46    enumName: enumName,
47    enumMembers: enumMembers,
48    exportModifiers: exportModifiers
49  };
50}
51
52export interface EnumEntity {
53  enumName: string;
54  enumMembers: Array<MemberEntity>;
55  exportModifiers: Array<number>;
56}
57
58export interface MemberEntity {
59  enumValueName: string;
60  enumValue: string;
61  enumKind: number;
62}
63