UNPKG

anl

Version:
6 lines 4.53 kB
const e=require(`../../utils/logger.cjs`),t=require(`../../utils/index.cjs`),n=require(`../shared/format.cjs`),r=require(`../shared/naming.cjs`),i=require(`../shared/schema-utils.cjs`);var a=class{config;enumsMap=new Map;constructor(e){this.config=e}nullable(e){return i.nullableSuffix(e)}extractEnumMetadata(e){let n={},{enmuConfig:r}=this.config,i=e;if(e.example&&t.isValidJSON(e.example)&&(n.rawEnumJson=e.example),r?.varnames){let e=i[r.varnames];Array.isArray(e)&&(n.customNames=e.map(e=>`${e}`))}if(r?.comment){let e=i[r.comment];if(e&&typeof e==`object`&&!Array.isArray(e)){let t={};Object.entries(e).sort((e,t)=>e[0].localeCompare(t[0])).forEach(([e,n])=>{typeof n==`string`&&(t[e]=n)}),Object.keys(t).length&&(n.descriptionMap=t)}}return n}resolveEnumMemberName(e,t,n){let{customNames:i,isNumericEnum:a,treatStringAsNumeric:o}=n,s=i?.[t];return typeof s==`string`&&s.trim()?r.sanitizeIdentifierName(s):a||typeof e==`string`&&o?`NUMBER_${e}`:typeof e==`string`&&e?r.sanitizeIdentifierName(e.toUpperCase()):`ENUM_${t}`}convertJsonToEnumString(t,i){try{let e=JSON.parse(t);return this.config.enmuConfig.erasableSyntaxOnly?`export const ${i} = {\n${Object.entries(e).sort((e,t)=>e[0].localeCompare(t[0])).map(([e,t])=>`${n.getIndentation(this.config)}${r.sanitizeIdentifierName(e)}: '${String(t)}'`).join(`, `)}\n} as const;\n\nexport type ${r.getEnumTypeName(this.config,i)} = typeof ${i}[keyof typeof ${i}];`:`export enum ${i} {\n${Object.entries(e).sort((e,t)=>e[0].localeCompare(t[0])).map(([e,t])=>`${n.getIndentation(this.config)}${r.sanitizeIdentifierName(e)} = '${String(t)}'`).join(`, `)}\n}`}catch(t){return e.log.error(`JSON 解析失败: ${t instanceof Error?t.message:String(t)}`),``}}normalizeEnumValues(e){return e.map((e,t)=>{if(typeof e==`string`||typeof e==`number`)return e;if(e&&typeof e==`object`){let n=e;if(n.value!==void 0&&(typeof n.value==`string`||typeof n.value==`number`))return n.value;if(n.key!==void 0&&(typeof n.key==`string`||typeof n.key==`number`))return n.key;if(n.id!==void 0&&(typeof n.id==`string`||typeof n.id==`number`))return n.id;try{return JSON.stringify(n)}catch{return`ENUM_${t}`}}return`ENUM_${t}`})}parseEnum(e,t){if(!Array.isArray(e.enum))return null;let i=[`integer`,`number`],a=this.normalizeEnumValues(e.enum),o=!!(e.type&&i.includes(e.type)),s=e.type===`string`&&a.every(e=>typeof e==`string`&&!isNaN(Number(e))),{customNames:c,descriptionMap:l,rawEnumJson:u}=this.extractEnumMetadata(e),d=this.config.enmuConfig.erasableSyntaxOnly;if(u){let e=this.convertJsonToEnumString(u,t);if(e)return{headerRef:``,renderStr:e}}let f=a.map((e,t)=>{let r=this.resolveEnumMemberName(e,t,{customNames:c,isNumericEnum:o,treatStringAsNumeric:s}),i=o?`${e}`:`'${String(e)}'`,a=d?`${r}: ${i},`:`${r} = ${i},`,u=l?.[String(e)];return u?[`${n.getIndentation(this.config)}/** ${u} */`,`${n.getIndentation(this.config)}${a}`].join(` `):`${n.getIndentation(this.config)}${a}`});return f.length?d?{headerRef:``,renderStr:[`export const ${t} = {`,...f,`} as const;`,``,`export type ${r.getEnumTypeName(this.config,t)} = typeof ${t}[keyof typeof ${t}];`].join(` `)}:{headerRef:``,renderStr:[`export enum ${t} {`,...f,`}`].join(` `)}:null}handleEnum(e,i,a=!1){let o=r.wordsToPascalCase(r.resolveSchemaName(i)),s=r.getEnumTypeName(this.config,o),c=r.typeNameToFileName(o),l=r.appendEnumSegment(r.adjustImportPathForSegment(this.config.importEnumPath??``,r.getServerSegment(this.config)),r.getEnumSegment(this.config));if(t.isValidJSON(e.example)){let t=this.convertJsonToEnumString(e.example,o);return this.enumsMap.has(c)||this.enumsMap.set(c,{fileName:c,content:t}),{headerRef:`import type { ${s} } from '${l}';`,renderStr:`${n.getIndentation(this.config)}${i}${a?``:`?`}: ${s}${this.nullable(e.nullable)};`,comment:t,typeName:s}}let u=this.parseEnum(e,o);return u?(this.enumsMap.has(c)||this.enumsMap.set(c,{fileName:c,content:u.renderStr}),{headerRef:`import type { ${s} } from '${l}';`,renderStr:`${n.getIndentation(this.config)}${i}${a?``:`?`}: ${s}${this.nullable(e.nullable)};`,typeName:s}):null}addEnumByName(e,t){let n=r.typeNameToFileName(e);this.enumsMap.has(n)||this.enumsMap.set(n,{fileName:n,content:t})}hasEnum(e){let t=r.typeNameToFileName(e);return this.enumsMap.has(t)}getEnumTypeName(e){return r.getEnumTypeName(this.config,e)}getEnumImport(e){return`import type { ${r.getEnumTypeName(this.config,e)} } from '${r.appendEnumSegment(r.adjustImportPathForSegment(this.config.importEnumPath??``,r.getServerSegment(this.config)),r.getEnumSegment(this.config))}';`}};exports.EnumParser=a;