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 { SyntaxKind } from 'typescript'; 17import type { EnumEntity } from '../declaration-node/enumDeclaration'; 18 19const colorSpaceSpecialData = { 20 LINEAR_BT709: 24, 21 DISPLAY_SRGB: 4, 22 DISPLAY_P3_SRGB: 3, 23 DISPLAY_P3_HLG: 11, 24 DISPLAY_P3_PQ: 12 25}; 26 27/** 28 * generate enum 29 * @param rootName 30 * @param enumDeclaration 31 * @returns 32 */ 33export function generateEnumDeclaration(rootName: string, enumDeclaration: EnumEntity): string { 34 let enumBody = ''; 35 if (enumDeclaration.exportModifiers.length !== 0) { 36 enumBody += `export const ${enumDeclaration.enumName} = {\n`; 37 } else { 38 enumBody += `const ${enumDeclaration.enumName} = {\n`; 39 } 40 41 let defaultValue = 0; 42 enumDeclaration.enumMembers.forEach(member => { 43 if (member.enumKind === SyntaxKind.TypeReference) { 44 enumBody += `${member.enumValueName}: new ${member.enumValue},\n`; 45 } else if (member.enumKind === SyntaxKind.NumericLiteral) { 46 defaultValue = parseInt(member.enumValue.replace(/"/g, '').replace(/'/g, '')); 47 enumBody += `${member.enumValueName}: ${defaultValue},\n`; 48 defaultValue++; 49 } else if (member.enumKind === SyntaxKind.StringLiteral) { 50 enumBody += `${member.enumValueName}: ${member.enumValue},\n`; 51 } else { 52 if (member.enumValue === '' || member.enumValue === null || member.enumValue === undefined) { 53 enumBody += `${member.enumValueName}: ${defaultValue},\n`; 54 defaultValue++; 55 } else { 56 if (Object.keys(colorSpaceSpecialData).includes(member.enumValueName)) { 57 enumBody += `${member.enumValueName}: ${colorSpaceSpecialData[member.enumValueName]},\n`; 58 } else { 59 enumBody += `${member.enumValueName}: ${member.enumValue},\n`; 60 } 61 } 62 } 63 }); 64 enumBody += '}\n'; 65 return enumBody; 66} 67