UNPKG

@lcap/nasl

Version:

NetEase Application Specific Language

45 lines 2.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.transform2Enum = void 0; const utils_1 = require("./utils"); const transform2TypeAnnotation_1 = require("./transform2TypeAnnotation"); function transform2Enum(node, leadingComments, parseState) { const enumeration = new utils_1.naslTypes.Enum(); parseState.nodeName = enumeration.name = node.id.name; enumeration.description = (0, utils_1.fetchFromComments)(leadingComments); enumeration.valueType = (0, transform2TypeAnnotation_1.transform2TypeAnnotation)(node.superTypeParameters.params[0], parseState) || undefined; node.body.body.forEach((item) => { const enumItem = new utils_1.naslTypes.EnumItem(); if (item.type === 'ClassProperty') { let key = ''; if (item.key.type === 'Identifier') { key = item.key.name; } else if (item.key.type === 'StringLiteral') { key = item.key.value; } else if (item.key.type === 'NumericLiteral') { key = String(item.key.value); } if (item.value.type === "NewExpression") { const args = item.value.arguments; const value = String(args[0].value); const label = args[1].value; if (item.value?.callee?.type === 'Identifier' && item.value.callee.name !== enumeration.name) { (0, utils_1.throwError)(parseState, `类型不匹配:字段 ${value} 期望一个 ${enumeration.name} 的实例,但实际获得了 ${item.value.callee.name}`, item); } if (value !== key) { (0, utils_1.throwError)(parseState, `枚举项等号左边的键 ${key} 与 new 的第1个参数值 ${value} 必须相同`, item); } enumItem.value = value; enumItem.label = new utils_1.naslTypes.StaticString({ value: label, }); enumeration.enumItems.push(enumItem); } } }); return enumeration; } exports.transform2Enum = transform2Enum; //# sourceMappingURL=transform2Enum.js.map