@hey-api/codegen-core
Advanced tools
+1
-1
@@ -1,2 +0,2 @@ | ||
| 'use strict';var i=require('path');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var i__default=/*#__PURE__*/_interopDefault(i);var d=class{map=new Map;reverse=new Map;delete(e){let t=this.map.get(e);return t!==void 0&&this.reverse.delete(t),this.map.delete(e)}deleteValue(e){let t=this.reverse.get(e);return t!==void 0&&this.map.delete(t),this.reverse.delete(e)}entries(){return this.map.entries()}get(e){return this.map.get(e)}getKey(e){return this.reverse.get(e)}hasKey(e){return this.map.has(e)}hasValue(e){return this.reverse.has(e)}keys(){return this.map.keys()}set(e,t){return this.map.set(e,t),this.reverse.set(t,e),this}get size(){return this.map.size}values(){return this.map.values()}[Symbol.iterator](){return this.map[Symbol.iterator]()}};var a=l=>`_heyapi_${l}_`,h=l=>l.slice(8,-1),u=()=>new RegExp(a("\\d+"),"g"),y=(l,e)=>l.replace(u(),t=>{let r=Number.parseInt(h(t),10);return e(r)||t});var m=class l{constructor(e,t,r={}){this.path=e;this.project=t;this.meta=r;let o=l.pathToFilePath(e);r.path&&(typeof r.path=="function"?o=r.path(o):o=r.path.replace("{{path}}",o)),this.id=t.incrementFileId(),this.path=o;}cache={};renderSymbols=[];state={exports:new Map,imports:new Map,symbols:new Map};id;resolvedNames=new d;addExport(e){return this.addImportExport(e,"exports")}addImport(e){return this.addImportExport(e,"imports")}addImportExport(e,t){let r=this.getImportExportKey(e),o=this.state[t].get(r);e.names||(e.names=[]);for(let s of e.typeNames??[])e.names.includes(s)||(e.names=[...e.names,s]);o?(this.mergeImportExportValues(o,e),this.state[t].set(r,o)):this.state[t].set(r,{...e}),this.cache[t]=void 0;}addRenderSymbol(e){this.renderSymbols.push(e),this.cache.symbols=void 0;}addSymbol(e){let t=this.project.incrementSymbolId(),r={...e,file:this,id:t,placeholder:a(String(t)),update:o=>this.updateSymbol(t,o)};return r.value===void 0?r.headless=true:r.headless||delete r.headless,this.state.symbols.set(t,r),this.project.registerSymbol(r,this),r.headless||this.addRenderSymbol(t),r}ensureSymbol(e){return this.selectSymbolFirst(e.selector)||this.addSymbol({name:"",...e})}get exports(){return this.cache.exports||(this.cache.exports=Array.from(this.state.exports.values())),this.cache.exports}getAllSymbols(){return [...this.symbols,...this.imports.flatMap(e=>(e.names??[]).map(t=>({name:e.aliases?.[t]??t}))),...this.exports.flatMap(e=>(e.names??[]).map(t=>({name:e.aliases?.[t]??t})))]}getImportExportKey(e){return typeof e.from=="string"?e.from:e.from.path}getSymbolById(e){return this.state.symbols.get(e)}hasContent(){return this.state.exports.size>0||this.symbols.length>0}hasSymbol(e){return this.state.symbols.has(e)}get imports(){return this.cache.imports||(this.cache.imports=Array.from(this.state.imports.values())),this.cache.imports}mergeImportExportValues(e,t){e.aliases={...e.aliases,...t.aliases},t.defaultImport!==void 0&&(e.defaultImport=t.defaultImport),e.names=[...new Set([...e.names??[],...t.names??[]])],t.namespaceImport!==void 0&&(e.namespaceImport=t.namespaceImport),t.typeDefaultImport!==void 0&&(e.typeDefaultImport=t.typeDefaultImport),e.typeNames=[...new Set([...e.typeNames??[],...t.typeNames??[]])],t.typeNamespaceImport!==void 0&&(e.typeNamespaceImport=t.typeNamespaceImport);}static pathToFilePath(e){return e.includes("/")?e.split("/").filter(Boolean).join(i__default.default.sep):e.includes("\\")?e.split("\\").filter(Boolean).join(i__default.default.sep):e.split(i__default.default.sep).filter(Boolean).join(i__default.default.sep)}relativePathFromFile(e){let t=i__default.default.posix.relative(i__default.default.posix.dirname(e.path),this.path);return t.startsWith(".")||(t=`./${t}`),t}relativePathToFile(e){let t=i__default.default.posix.relative(i__default.default.posix.dirname(this.path.split(i__default.default.sep).join("/")),e.path.split(i__default.default.sep).join("/"));return !t.startsWith(".")&&t!==""&&(t=`./${t}`),t}selectSymbolAll(e){return this.project.selectSymbolAll(e,this)}selectSymbolFirst(e){return this.project.selectSymbolFirst(e,this)}selectSymbolFirstOrThrow(e){return this.project.selectSymbolFirstOrThrow(e,this)}selectSymbolLast(e){return this.project.selectSymbolLast(e,this)}get symbols(){return this.cache.symbols||(this.cache.symbols=this.renderSymbols.map(e=>this.getSymbolById(e))),this.cache.symbols}updateSymbol(e,t){let r=this.getSymbolById(e);if(!r)throw new Error(`symbol with id ${e} not found`);let o={...r,...t,id:e};return (!o.headless||o.value)&&delete o.headless,this.state.symbols.set(o.id,o),r.headless&&!o.headless&&this.addRenderSymbol(e),o}};var I=class{fileId=0;fileIdToFile=new Map;fileOrder=[];filePathToFileId=new Map;renderers=new Map;selectorToSymbolIds=new Map;symbolId=0;symbolIdToFileId=new Map;addExport(e,t){this.ensureFile(e).addExport(t);}addImport(e,t){this.ensureFile(e).addImport(t);}addSymbol(e,t){return this.ensureFile(e).addSymbol(t)}createFile(e,t={}){let{renderer:r,...o}=t;r&&this.ensureRenderer(r);let s=this.getFileByPath(e);if(s)return r?.id&&r.id!==s.meta.renderer&&(s.meta.renderer=r.id),s;let n=new m(e,this,{...o,renderer:r?.id});return this.fileOrder.push(n),this.filePathToFileId.set(e,n.id),this.fileIdToFile.set(n.id,n),n}ensureFile(e){if(typeof e!="string")return e;let t=this.getFileByPath(e);return t||this.createFile(e)}ensureRenderer(e){return this.renderers.has(e.id)||this.renderers.set(e.id,e),this.renderers.get(e.id)}get files(){return [...this.fileOrder]}getAllSymbols(){return this.fileOrder.flatMap(e=>e.getAllSymbols())}getFileByPath(e){let t=this.filePathToFileId.get(e);return t!==void 0?this.fileIdToFile.get(t):void 0}getFileBySymbolId(e){let t=this.symbolIdToFileId.get(e);return t!==void 0?this.fileIdToFile.get(t):void 0}getFileRenderer(e){return e.meta.renderer?this.renderers.get(e.meta.renderer):void 0}getSymbolById(e){return this.getFileBySymbolId(e)?.getSymbolById(e)}incrementFileId(){return this.fileId++}incrementSymbolId(){return this.symbolId++}registerSymbol(e,t){if(this.symbolIdToFileId.set(e.id,t.id),e.selector){let r=JSON.stringify(e.selector),o=this.selectorToSymbolIds.get(r)??[];o.push(e.id),this.selectorToSymbolIds.set(r,o);}}render(e){let t=[];return this.fileOrder.forEach((r,o)=>{let s=this.getFileRenderer(r);s&&(t[o]={content:s.renderSymbols(r,e),meta:r.meta,path:`${r.path}${r.meta.extension??""}`});}),this.fileOrder.forEach((r,o)=>{let s=this.getFileRenderer(r);if(!s||!t[o])return;let n=s.renderHeader(r,e),p=y(t[o].content,g=>s.replacerFn({file:r,symbolId:g}));t[o].content=`${n}${p}`;}),t.filter(Boolean)}selectSymbolAll(e,t){let r=this.selectorToSymbolIds.get(JSON.stringify(e))??[],o=[];for(let s of r){let n=this.getFileBySymbolId(s);if(!n||t&&t!==n)continue;let p=n.getSymbolById(s);p&&o.push(p);}return o}selectSymbolFirst(e,t){return this.selectSymbolAll(e,t)[0]}selectSymbolFirstOrThrow(e,t){let r=this.selectSymbolFirst(e,t);if(!r)throw new Error(`symbol for selector not found: ${JSON.stringify(e)}`);return r}selectSymbolLast(e,t){let r=this.selectSymbolAll(e,t);return r[r.length-1]}};exports.BiMap=d;exports.CodegenFile=m;exports.CodegenProject=I;exports.replaceWrappedIds=y;//# sourceMappingURL=index.cjs.map | ||
| 'use strict';var h=require('path');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var h__default=/*#__PURE__*/_interopDefault(h);var I=({file:l,modulePath:e,symbol:t,symbolFile:r})=>{let i=[],s=[],n={aliases:{},from:e};if(t.meta?.importKind&&(t.meta.importKind==="default"?(n.defaultBinding=t.placeholder,t.meta.kind==="type"&&(n.typeDefaultBinding=true)):t.meta.importKind==="namespace"&&(n.namespaceBinding=t.placeholder,t.meta.kind==="type"&&(n.typeNamespaceBinding=true))),t.meta?.importKind==="named"||!i.length&&!n.defaultBinding&&!n.namespaceBinding){let o=t.placeholder,d=l.resolvedNames.get(t.id);if(d){let m=r.resolvedNames.get(t.id);m?m!==d&&(o=m,n.aliases[o]=d):t.name&&d!==t.name&&(o=t.name,n.aliases[o]=t.placeholder);}i.push(o),t.meta?.kind==="type"&&s.push(o);}for(let o of s)i.includes(o)||i.push(o);return n.names=i,n.typeNames=s,n},g=(l,e)=>{l.aliases={...l.aliases,...e.aliases},e.defaultBinding!==void 0&&(l.defaultBinding=e.defaultBinding),l.names=[...new Set([...l.names??[],...e.names??[]])],e.namespaceBinding!==void 0&&(l.namespaceBinding=e.namespaceBinding),e.typeDefaultBinding!==void 0&&(l.typeDefaultBinding=e.typeDefaultBinding),l.typeNames=[...new Set([...l.typeNames??[],...e.typeNames??[]])],e.typeNamespaceBinding!==void 0&&(l.typeNamespaceBinding=e.typeNamespaceBinding);};var a=class{map=new Map;reverse=new Map;delete(e){let t=this.map.get(e);return t!==void 0&&this.reverse.delete(t),this.map.delete(e)}deleteValue(e){let t=this.reverse.get(e);return t!==void 0&&this.map.delete(t),this.reverse.delete(e)}entries(){return this.map.entries()}get(e){return this.map.get(e)}getKey(e){return this.reverse.get(e)}hasKey(e){return this.map.has(e)}hasValue(e){return this.reverse.has(e)}keys(){return this.map.keys()}set(e,t){let r=this.map.get(e);r!==void 0&&r!==t&&this.reverse.delete(r);let i=this.reverse.get(t);return i!==void 0&&i!==e&&this.map.delete(i),this.map.set(e,t),this.reverse.set(t,e),this}get size(){return this.map.size}values(){return this.map.values()}[Symbol.iterator](){return this.map[Symbol.iterator]()}};var p=class{_id=0;referenceOrder=new Set;registerOrder=new Set;selectorToId=new Map;values=new Map;get(e){let t=this.idOrSelector(e);if(t.id!==void 0)return this.values.get(t.id);let r=t.selector!==void 0?JSON.stringify(t.selector):void 0;if(r){let i=this.selectorToId.get(r);if(i!==void 0)return this.values.get(i)}}get id(){return this._id++}idOrSelector(e){return typeof e=="number"?{id:e}:{selector:e}}reference(e){let t=this.idOrSelector(e);return this.register(t)}*referenced(){for(let e of this.referenceOrder.values())yield this.values.get(e);}register(e){if(e.id!==void 0){let n=this.values.get(e.id);if(!n)throw new Error(`File with ID ${e.id} not found. To register a new file, leave the ID undefined.`);return n}let t=Object.keys(e).some(n=>!["id","selector"].includes(n)),r,i=e.selector!==void 0?JSON.stringify(e.selector):void 0;if(i){let n=this.selectorToId.get(i);if(n!==void 0){if(r=this.values.get(n),!r)throw new Error(`File with ID ${n} not found. The selector ${i} matched an ID, but there was no result. This is likely an issue with the application logic.`);if(!t)return r}}let s=r?.id!==void 0?r.id:this.id;return r={...r,...e,id:s,resolvedNames:r?.resolvedNames??new a,symbols:r?.symbols??{body:[],exports:[],imports:[]}},this.values.set(s,r),t?this.registerOrder.add(s):this.referenceOrder.add(s),i&&this.selectorToId.set(i,s),r}*registered(){for(let e of this.registerOrder.values())yield this.values.get(e);}};var f=l=>`_heyapi_${l}_`,b=l=>l.slice(8,-1),S=()=>new RegExp(f("\\d+"),"g"),v=(l,e)=>l.replace(S(),t=>{let r=Number.parseInt(b(t),10);return e(r)||t});var u=class{_id=0;nodes=new Map;registerOrder=new Set;selectorToId=new Map;values=new Map;get(e){let t=this.idOrSelector(e);if(t.id!==void 0)return this.values.get(t.id);let r=t.selector!==void 0?JSON.stringify(t.selector):void 0;if(r){let i=this.selectorToId.get(r);if(i!==void 0)return this.values.get(i)}}getValue(e){return this.nodes.get(e)}hasValue(e){return this.nodes.has(e)}get id(){return this._id++}idOrSelector(e){return typeof e=="number"?{id:e}:{selector:e}}reference(e){let t=this.idOrSelector(e);return this.register(t)}register(e){if(e.id!==void 0){let o=this.values.get(e.id);if(!o)throw new Error(`Symbol with ID ${e.id} not found. To register a new symbol, leave the ID undefined.`);return o}let t=Object.keys(e).some(o=>!["id","selector"].includes(o)),r,i=e.selector!==void 0?JSON.stringify(e.selector):void 0;if(i){let o=this.selectorToId.get(i);if(o!==void 0){if(r=this.values.get(o),!r)throw new Error(`Symbol with ID ${o} not found. The selector ${i} matched an ID, but there was no result. This is likely an issue with the application logic.`);if(!t)return r}}let s=r?.id!==void 0?r.id:this.id,n=r?.exportFrom?[...r.exportFrom]:[];return e.exportFrom&&n.push(...e.exportFrom),r={...r,...e,exportFrom:n,id:s,placeholder:r?.placeholder??e.placeholder??f(String(s))},this.values.set(s,r),t&&this.registerOrder.add(s),i&&this.selectorToId.set(i,s),r}*registered(){for(let e of this.registerOrder.values())yield this.values.get(e);}setValue(e,t){return this.nodes.set(e,t)}};var y="@",c=class{symbolIdToFileIds=new Map;defaultFileName;files=new p;fileName;renderers={};root;symbols=new u;constructor({defaultFileName:e,fileName:t,renderers:r,root:i}){this.defaultFileName=e??"main",this.fileName=typeof t=="string"?()=>t:t,this.renderers=r,this.root=i;}getRenderer(e){return e.extension?this.renderers[e.extension]:void 0}prepareFiles(){for(let t of this.symbols.registered()){let r=this.symbolToFileSelector(t),i=this.files.reference(r);i.symbols.body.push(t.id);let s=this.symbolIdToFileIds.get(t.id)??new Set;s.add(i.id),this.symbolIdToFileIds.set(t.id,s);for(let n of t.exportFrom){let o=[n],d=this.files.reference(o);d.id!==i.id&&d.symbols.exports.push(t.id);}}for(let t of this.files.referenced()){if(!t.selector)continue;if(t.selector[0]===y){let s=t.selector[1];if(!s){this.files.register({external:true,selector:t.selector});continue}let n=h__default.default.extname(s);if(!n){this.files.register({external:true,path:s,selector:t.selector});continue}this.files.register({extension:n,external:true,path:s,selector:t.selector});continue}let r=t.selector.slice(0,-1),i=t.selector[t.selector.length-1];i=this.fileName?.(i)||i,this.files.register({extension:".ts",name:i,path:h__default.default.resolve(this.root,...r,`${i}.ts`),selector:t.selector});}}render(e){this.prepareFiles();let t=new Map;for(let r of this.files.registered()){if(r.external||!r.path)continue;let i=this.getRenderer(r);i&&t.set(r.id,{content:i.renderSymbols(r,this,e),path:r.path});}for(let[r,i]of t.entries()){let s=this.files.get(r),o=this.getRenderer(s).renderFile(i.content,s,this,e);o?t.set(s.id,{...i,content:o}):t.delete(s.id);}return Array.from(t.values())}symbolIdToFiles(e){let t=this.symbolIdToFileIds.get(e);return Array.from(t??[]).map(r=>this.files.get(r))}symbolToFileSelector(e){if(e.external)return [y,e.external];let t=e.getFilePath?.(e);return t?t.split("/"):[this.defaultFileName]}};exports.Project=c;exports.createBinding=I;exports.mergeBindings=g;exports.renderIds=v;//# sourceMappingURL=index.cjs.map | ||
| //# sourceMappingURL=index.cjs.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["../src/bimap/bimap.ts","../src/renderers/renderer.ts","../src/files/file.ts","../src/project/project.ts"],"names":["BiMap","key","value","wrapId","symbolId","unwrapId","wrappedId","createPlaceholderRegExp","replaceWrappedIds","source","replacerFn","match","CodegenFile","_CodegenFile","path","project","meta","filePath","exp","imp","field","existing","typeName","id","symbol","inserted","values","name","target","file","relativePath","selector","updated","CodegenProject","fileOrPath","renderer","_meta","existingFile","fileId","ids","results","index","header","content","symbols","f"],"mappings":"mJAEO,IAAMA,EAAN,KAA6D,CAC1D,IAAM,IAAI,GAAA,CACV,QAAU,IAAI,GAAA,CAEtB,OAAOC,CAAAA,CAAmB,CACxB,IAAMC,CAAAA,CAAQ,IAAA,CAAK,IAAI,GAAA,CAAID,CAAG,EAC9B,OAAIC,CAAAA,GAAU,MAAA,EACZ,IAAA,CAAK,QAAQ,MAAA,CAAOA,CAAK,EAEpB,IAAA,CAAK,GAAA,CAAI,OAAOD,CAAG,CAC5B,CAEA,WAAA,CAAYC,CAAAA,CAAuB,CACjC,IAAMD,CAAAA,CAAM,KAAK,OAAA,CAAQ,GAAA,CAAIC,CAAK,CAAA,CAClC,OAAID,CAAAA,GAAQ,MAAA,EACV,KAAK,GAAA,CAAI,MAAA,CAAOA,CAAG,CAAA,CAEd,IAAA,CAAK,QAAQ,MAAA,CAAOC,CAAK,CAClC,CAEA,OAAA,EAA0C,CACxC,OAAO,IAAA,CAAK,IAAI,OAAA,EAClB,CAEA,GAAA,CAAID,CAAAA,CAA6B,CAC/B,OAAO,IAAA,CAAK,IAAI,GAAA,CAAIA,CAAG,CACzB,CAEA,MAAA,CAAOC,EAA+B,CACpC,OAAO,KAAK,OAAA,CAAQ,GAAA,CAAIA,CAAK,CAC/B,CAEA,OAAOD,CAAAA,CAAmB,CACxB,OAAO,IAAA,CAAK,GAAA,CAAI,GAAA,CAAIA,CAAG,CACzB,CAEA,QAAA,CA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ASgB,CAAAA,CAAI,SAChC,IAAMrB,CAAAA,CAASqB,CAAAA,CAAE,cAActB,CAAE,CAAA,CAC5BC,GACLoB,CAAAA,CAAQ,IAAA,CAAKpB,CAAM,EACrB,CACA,OAAOoB,CACT,CAEA,kBACEb,CAAAA,CACAF,CAAAA,CAC+B,CAE/B,OADgB,IAAA,CAAK,gBAAgBE,CAAAA,CAAUF,CAAI,EACpC,CAAC,CAClB,CAEA,wBAAA,CACEE,EACAF,CAAAA,CACmB,CACnB,IAAML,CAAAA,CAAS,IAAA,CAAK,kBAAkBO,CAAAA,CAAUF,CAAI,EACpD,GAAI,CAACL,EACH,MAAM,IAAI,MACR,CAAA,+BAAA,EAAkC,IAAA,CAAK,UAAUO,CAAQ,CAAC,EAC5D,CAAA,CACF,OAAOP,CACT,CAEA,gBAAA,CACEO,EACAF,CAAAA,CAC+B,CAC/B,IAAMe,CAAAA,CAAU,IAAA,CAAK,gBAAgBb,CAAAA,CAAUF,CAAI,EACnD,OAAOe,CAAAA,CAAQA,EAAQ,MAAA,CAAS,CAAC,CACnC,CACF","file":"index.cjs","sourcesContent":["import type { ICodegenBiMap } from './types';\n\nexport class BiMap<Key, Value> implements ICodegenBiMap<Key, Value> {\n private map = new Map<Key, Value>();\n private reverse = new Map<Value, Key>();\n\n delete(key: Key): boolean {\n const value = this.map.get(key);\n if (value !== undefined) {\n this.reverse.delete(value);\n }\n return this.map.delete(key);\n }\n\n deleteValue(value: Value): boolean {\n const key = this.reverse.get(value);\n if (key !== undefined) {\n this.map.delete(key);\n }\n return this.reverse.delete(value);\n }\n\n entries(): IterableIterator<[Key, Value]> {\n return this.map.entries();\n }\n\n get(key: Key): Value | undefined {\n return this.map.get(key);\n }\n\n getKey(value: Value): Key | undefined {\n return this.reverse.get(value);\n }\n\n hasKey(key: Key): boolean {\n return this.map.has(key);\n }\n\n hasValue(value: Value): boolean {\n return this.reverse.has(value);\n }\n\n keys(): IterableIterator<Key> {\n return this.map.keys();\n }\n\n set(key: Key, value: Value): this {\n this.map.set(key, value);\n this.reverse.set(value, key);\n return this;\n }\n\n get size(): number {\n return this.map.size;\n }\n\n values(): IterableIterator<Value> {\n return this.map.values();\n }\n\n [Symbol.iterator](): IterableIterator<[Key, Value]> {\n return this.map[Symbol.iterator]();\n }\n}\n","/**\n * Wraps an ID in namespace to avoid collisions when replacing it.\n *\n * @param symbolId Stringified symbol ID to use.\n * @returns The wrapped placeholder ID.\n */\nexport const wrapId = (symbolId: string): string => `_heyapi_${symbolId}_`;\n\n/**\n * Unwraps an ID from namespace.\n *\n * @param wrappedId The wrapped placeholder ID.\n * @returns Stringified ID to use.\n */\nconst unwrapId = (wrappedId: string): string =>\n wrappedId.slice('_heyapi_'.length, -1);\n\n/**\n * Returns a RegExp instance to match ID placeholders.\n *\n * @returns RegExp instance to match ID placeholders.\n */\nconst createPlaceholderRegExp = (): RegExp => new RegExp(wrapId('\\\\d+'), 'g');\n\n/**\n *\n * @param source The source string to replace.\n * @param replacerFn Accepts a symbol ID, returns resolved symbol name.\n * @returns The replaced source string.\n */\nexport const replaceWrappedIds = (\n source: string,\n replacerFn: (symbolId: number) => string | undefined,\n): string =>\n source.replace(createPlaceholderRegExp(), (match) => {\n const symbolId = Number.parseInt(unwrapId(match), 10);\n return replacerFn(symbolId) || match;\n });\n","import path from 'node:path';\n\nimport { BiMap } from '../bimap/bimap';\nimport type { ICodegenBiMap } from '../bimap/types';\nimport type { ICodegenImport } from '../imports/types';\nimport type { ICodegenProject } from '../project/types';\nimport { wrapId } from '../renderers/renderer';\nimport type {\n ICodegenSymbolIn,\n ICodegenSymbolOut,\n ICodegenSymbolSelector,\n} from '../symbols/types';\nimport type { ICodegenFile } from './types';\n\nexport class CodegenFile implements ICodegenFile {\n private cache: {\n exports?: ReadonlyArray<ICodegenImport>;\n imports?: ReadonlyArray<ICodegenImport>;\n symbols?: ReadonlyArray<ICodegenSymbolOut>;\n } = {};\n\n private renderSymbols: Array<number> = [];\n\n private state: {\n exports: Map<string, ICodegenImport>;\n imports: Map<string, ICodegenImport>;\n symbols: Map<number, ICodegenSymbolOut>;\n } = {\n exports: new Map(),\n imports: new Map(),\n symbols: new Map(),\n };\n\n id: number;\n resolvedNames: ICodegenBiMap<number, string> = new BiMap();\n\n constructor(\n public path: string,\n public project: ICodegenProject,\n public meta: ICodegenFile['meta'] = {},\n ) {\n let filePath = CodegenFile.pathToFilePath(path);\n if (meta.path) {\n if (typeof meta.path === 'function') {\n filePath = meta.path(filePath);\n } else {\n filePath = meta.path.replace('{{path}}', filePath);\n }\n }\n this.id = project.incrementFileId();\n this.path = filePath;\n }\n\n addExport(exp: ICodegenImport): void {\n return this.addImportExport(exp, 'exports');\n }\n\n addImport(imp: ICodegenImport): void {\n return this.addImportExport(imp, 'imports');\n }\n\n private addImportExport(\n value: ICodegenImport,\n field: 'exports' | 'imports',\n ): void {\n const key = this.getImportExportKey(value);\n const existing = this.state[field].get(key);\n // cast type names to names to allow for cleaner API,\n // otherwise users would have to define the same values twice\n if (!value.names) value.names = [];\n for (const typeName of value.typeNames ?? []) {\n if (!value.names.includes(typeName)) {\n value.names = [...value.names, typeName];\n }\n }\n if (existing) {\n this.mergeImportExportValues(existing, value);\n this.state[field].set(key, existing);\n } else {\n this.state[field].set(key, { ...value }); // clone to avoid mutation\n }\n this.cache[field] = undefined; // invalidate cache\n }\n\n private addRenderSymbol(id: number): void {\n this.renderSymbols.push(id);\n this.cache.symbols = undefined; // invalidate cache\n }\n\n addSymbol(symbol: ICodegenSymbolIn): ICodegenSymbolOut {\n const id = this.project.incrementSymbolId();\n const inserted: ICodegenSymbolOut = {\n ...symbol, // clone to avoid mutation\n file: this,\n id,\n placeholder: wrapId(String(id)),\n update: (values) => this.updateSymbol(id, values),\n };\n if (inserted.value === undefined) {\n // register symbols without value as headless\n inserted.headless = true;\n } else if (!inserted.headless) {\n delete inserted.headless;\n }\n this.state.symbols.set(id, inserted);\n this.project.registerSymbol(inserted, this);\n if (!inserted.headless) {\n this.addRenderSymbol(id);\n }\n return inserted;\n }\n\n ensureSymbol(\n symbol: Partial<ICodegenSymbolIn> &\n Pick<Required<ICodegenSymbolIn>, 'selector'>,\n ): ICodegenSymbolOut {\n return (\n this.selectSymbolFirst(symbol.selector) ||\n this.addSymbol({ name: '', ...symbol })\n );\n }\n\n get exports(): ReadonlyArray<ICodegenImport> {\n if (!this.cache.exports) {\n this.cache.exports = Array.from(this.state.exports.values());\n }\n return this.cache.exports;\n }\n\n getAllSymbols(): ReadonlyArray<Pick<ICodegenSymbolOut, 'name'>> {\n return [\n ...this.symbols,\n ...this.imports.flatMap((imp) =>\n (imp.names ?? []).map((name) => ({\n name: imp.aliases?.[name] ?? name,\n })),\n ),\n ...this.exports.flatMap((imp) =>\n (imp.names ?? []).map((name) => ({\n name: imp.aliases?.[name] ?? name,\n })),\n ),\n ];\n }\n\n private getImportExportKey(value: ICodegenImport): string {\n if (typeof value.from === 'string') {\n return value.from;\n }\n return value.from.path;\n }\n\n getSymbolById(id: number): ICodegenSymbolOut | undefined {\n return this.state.symbols.get(id);\n }\n\n hasContent(): boolean {\n return this.state.exports.size > 0 || this.symbols.length > 0;\n }\n\n hasSymbol(id: number): boolean {\n return this.state.symbols.has(id);\n }\n\n get imports(): ReadonlyArray<ICodegenImport> {\n if (!this.cache.imports) {\n this.cache.imports = Array.from(this.state.imports.values());\n }\n return this.cache.imports;\n }\n\n private mergeImportExportValues(\n target: ICodegenImport,\n source: ICodegenImport,\n ): void {\n target.aliases = { ...target.aliases, ...source.aliases };\n if (source.defaultImport !== undefined) {\n target.defaultImport = source.defaultImport;\n }\n target.names = [\n ...new Set([...(target.names ?? []), ...(source.names ?? [])]),\n ];\n if (source.namespaceImport !== undefined) {\n target.namespaceImport = source.namespaceImport;\n }\n if (source.typeDefaultImport !== undefined) {\n target.typeDefaultImport = source.typeDefaultImport;\n }\n target.typeNames = [\n ...new Set([...(target.typeNames ?? []), ...(source.typeNames ?? [])]),\n ];\n if (source.typeNamespaceImport !== undefined) {\n target.typeNamespaceImport = source.typeNamespaceImport;\n }\n }\n\n static pathToFilePath(source: string): string {\n if (source.includes('/')) {\n return source.split('/').filter(Boolean).join(path.sep);\n }\n if (source.includes('\\\\')) {\n return source.split('\\\\').filter(Boolean).join(path.sep);\n }\n return source.split(path.sep).filter(Boolean).join(path.sep);\n }\n\n relativePathFromFile(file: Pick<ICodegenFile, 'path'>): string {\n let relativePath = path.posix.relative(\n path.posix.dirname(file.path),\n this.path,\n );\n if (!relativePath.startsWith('.')) {\n relativePath = `./${relativePath}`;\n }\n return relativePath;\n }\n\n relativePathToFile(file: Pick<ICodegenFile, 'path'>): string {\n let relativePath = path.posix.relative(\n path.posix.dirname(\n this.path.split(path.sep).join('/'), // normalize to posix\n ),\n file.path.split(path.sep).join('/'), // normalize to posix\n );\n if (!relativePath.startsWith('.') && relativePath !== '') {\n relativePath = `./${relativePath}`;\n }\n return relativePath;\n }\n\n selectSymbolAll(\n selector: ICodegenSymbolSelector,\n ): ReadonlyArray<ICodegenSymbolOut> {\n return this.project.selectSymbolAll(selector, this);\n }\n\n selectSymbolFirst(\n selector: ICodegenSymbolSelector,\n ): ICodegenSymbolOut | undefined {\n return this.project.selectSymbolFirst(selector, this);\n }\n\n selectSymbolFirstOrThrow(\n selector: ICodegenSymbolSelector,\n ): ICodegenSymbolOut {\n return this.project.selectSymbolFirstOrThrow(selector, this);\n }\n\n selectSymbolLast(\n selector: ICodegenSymbolSelector,\n ): ICodegenSymbolOut | undefined {\n return this.project.selectSymbolLast(selector, this);\n }\n\n get symbols(): ReadonlyArray<ICodegenSymbolOut> {\n if (!this.cache.symbols) {\n this.cache.symbols = this.renderSymbols.map(\n (id) => this.getSymbolById(id)!,\n );\n }\n return this.cache.symbols;\n }\n\n updateSymbol(\n id: number,\n symbol: Partial<ICodegenSymbolOut>,\n ): ICodegenSymbolOut {\n const existing = this.getSymbolById(id);\n if (!existing) {\n throw new Error(`symbol with id ${id} not found`);\n }\n const updated: ICodegenSymbolOut = { ...existing, ...symbol, id };\n // symbols with value can't be headless, clear redundant flag otherwise\n if (!updated.headless || updated.value) {\n delete updated.headless;\n }\n this.state.symbols.set(updated.id, updated);\n if (existing.headless && !updated.headless) {\n this.addRenderSymbol(id);\n }\n return updated;\n }\n}\n","import { CodegenFile } from '../files/file';\nimport type { ICodegenFile } from '../files/types';\nimport type { ICodegenImport } from '../imports/types';\nimport type { ICodegenMeta } from '../meta/types';\nimport type { ICodegenOutput } from '../output/types';\nimport { replaceWrappedIds } from '../renderers/renderer';\nimport type { ICodegenRenderer } from '../renderers/types';\nimport type {\n ICodegenSymbolIn,\n ICodegenSymbolOut,\n ICodegenSymbolSelector,\n} from '../symbols/types';\nimport type { ICodegenProject } from './types';\n\nexport class CodegenProject implements ICodegenProject {\n private fileId: number = 0;\n private fileIdToFile: Map<number, ICodegenFile> = new Map();\n private fileOrder: Array<ICodegenFile> = [];\n private filePathToFileId: Map<string, number> = new Map();\n private renderers: Map<string, ICodegenRenderer> = new Map();\n private selectorToSymbolIds: Map<string, Array<number>> = new Map();\n private symbolId: number = 0;\n private symbolIdToFileId: Map<number, number> = new Map();\n\n addExport(fileOrPath: ICodegenFile | string, imp: ICodegenImport): void {\n const file = this.ensureFile(fileOrPath);\n file.addExport(imp);\n }\n\n addImport(fileOrPath: ICodegenFile | string, imp: ICodegenImport): void {\n const file = this.ensureFile(fileOrPath);\n file.addImport(imp);\n }\n\n addSymbol(\n fileOrPath: ICodegenFile | string,\n symbol: ICodegenSymbolIn,\n ): ICodegenSymbolOut {\n const file = this.ensureFile(fileOrPath);\n return file.addSymbol(symbol);\n }\n\n createFile(\n path: string,\n meta: Omit<ICodegenFile['meta'], 'renderer'> & {\n /**\n * Renderer to use to render this file.\n */\n renderer?: ICodegenRenderer;\n } = {},\n ): ICodegenFile {\n const { renderer, ..._meta } = meta;\n if (renderer) {\n this.ensureRenderer(renderer);\n }\n\n const existing = this.getFileByPath(path);\n if (existing) {\n // Whoever is creating the file will override the renderer\n if (renderer?.id && renderer.id !== existing.meta.renderer) {\n existing.meta.renderer = renderer.id;\n }\n return existing;\n }\n\n const file = new CodegenFile(path, this, {\n ..._meta,\n renderer: renderer?.id,\n });\n this.fileOrder.push(file);\n this.filePathToFileId.set(path, file.id);\n this.fileIdToFile.set(file.id, file);\n return file;\n }\n\n ensureFile(fileOrPath: ICodegenFile | string): ICodegenFile {\n if (typeof fileOrPath !== 'string') {\n return fileOrPath;\n }\n const existingFile = this.getFileByPath(fileOrPath);\n if (existingFile) {\n return existingFile;\n }\n return this.createFile(fileOrPath);\n }\n\n private ensureRenderer(renderer: ICodegenRenderer): ICodegenRenderer {\n if (!this.renderers.has(renderer.id)) {\n this.renderers.set(renderer.id, renderer);\n }\n return this.renderers.get(renderer.id)!;\n }\n\n get files(): ReadonlyArray<ICodegenFile> {\n return [...this.fileOrder];\n }\n\n getAllSymbols(): ReadonlyArray<Pick<ICodegenSymbolOut, 'name'>> {\n return this.fileOrder.flatMap((file) => file.getAllSymbols());\n }\n\n getFileByPath(path: string): ICodegenFile | undefined {\n const fileId = this.filePathToFileId.get(path);\n return fileId !== undefined ? this.fileIdToFile.get(fileId) : undefined;\n }\n\n getFileBySymbolId(id: number): ICodegenFile | undefined {\n const fileId = this.symbolIdToFileId.get(id);\n return fileId !== undefined ? this.fileIdToFile.get(fileId) : undefined;\n }\n\n private getFileRenderer(file: ICodegenFile): ICodegenRenderer | undefined {\n return file.meta.renderer\n ? this.renderers.get(file.meta.renderer)\n : undefined;\n }\n\n getSymbolById(id: number): ICodegenSymbolOut | undefined {\n const file = this.getFileBySymbolId(id);\n return file?.getSymbolById(id);\n }\n\n incrementFileId(): number {\n return this.fileId++;\n }\n\n incrementSymbolId(): number {\n return this.symbolId++;\n }\n\n registerSymbol(symbol: ICodegenSymbolOut, file: ICodegenFile): void {\n this.symbolIdToFileId.set(symbol.id, file.id);\n if (symbol.selector) {\n const selector = JSON.stringify(symbol.selector);\n const ids = this.selectorToSymbolIds.get(selector) ?? [];\n ids.push(symbol.id);\n this.selectorToSymbolIds.set(selector, ids);\n }\n }\n\n render(meta?: ICodegenMeta): ReadonlyArray<ICodegenOutput> {\n const results: Array<ICodegenOutput> = [];\n this.fileOrder.forEach((file, index) => {\n const renderer = this.getFileRenderer(file);\n if (!renderer) return;\n results[index] = {\n content: renderer.renderSymbols(file, meta),\n meta: file.meta,\n path: `${file.path}${file.meta.extension ?? ''}`,\n };\n });\n this.fileOrder.forEach((file, index) => {\n const renderer = this.getFileRenderer(file);\n if (!renderer || !results[index]) return;\n const header = renderer.renderHeader(file, meta);\n const content = replaceWrappedIds(results[index].content, (symbolId) =>\n renderer.replacerFn({ file, symbolId }),\n );\n results[index].content = `${header}${content}`;\n });\n return results.filter(Boolean);\n }\n\n selectSymbolAll(\n selector: ICodegenSymbolSelector,\n file?: ICodegenFile,\n ): ReadonlyArray<ICodegenSymbolOut> {\n const ids = this.selectorToSymbolIds.get(JSON.stringify(selector)) ?? [];\n const symbols: Array<ICodegenSymbolOut> = [];\n for (const id of ids) {\n const f = this.getFileBySymbolId(id);\n if (!f || (file && file !== f)) continue;\n const symbol = f.getSymbolById(id);\n if (!symbol) continue;\n symbols.push(symbol);\n }\n return symbols;\n }\n\n selectSymbolFirst(\n selector: ICodegenSymbolSelector,\n file?: ICodegenFile,\n ): ICodegenSymbolOut | undefined {\n const symbols = this.selectSymbolAll(selector, file);\n return symbols[0];\n }\n\n selectSymbolFirstOrThrow(\n selector: ICodegenSymbolSelector,\n file?: ICodegenFile,\n ): ICodegenSymbolOut {\n const symbol = this.selectSymbolFirst(selector, file);\n if (!symbol)\n throw new Error(\n `symbol for selector not found: ${JSON.stringify(selector)}`,\n );\n return symbol;\n }\n\n selectSymbolLast(\n selector: ICodegenSymbolSelector,\n file?: ICodegenFile,\n ): ICodegenSymbolOut | undefined {\n const symbols = this.selectSymbolAll(selector, file);\n return symbols[symbols.length - 1];\n }\n}\n"]} | ||
| {"version":3,"sources":["../src/bindings/utils.ts","../src/bimap/bimap.ts","../src/files/registry.ts","../src/renderer/utils.ts","../src/symbols/registry.ts","../src/project/project.ts"],"names":["createBinding","file","modulePath","symbol","symbolFile","names","typeNames","binding","name","fileResolvedName","symbolFileResolvedName","typeName","mergeBindings","target","source","BiMap","key","value","oldValue","oldKey","FileRegistry","fileIdOrSelector","selector","id","symbolIdOrSelector","result","hasOtherKeys","wrapId","symbolId","unwrapId","wrappedId","createPlaceholderRegExp","renderIds","replacerFn","match","SymbolRegistry","exportFrom","externalSourceSymbol","Project","defaultFileName","fileName","renderers","root","symbolIdToFileIds","exportSelector","exportFile","filePath","extension","path","dirs","meta","files","renderer","fileId","content","fileIds"],"mappings":"mJAIO,IAAMA,CAAAA,CAAgB,CAAC,CAC5B,IAAA,CAAAC,EACA,UAAA,CAAAC,CAAAA,CACA,OAAAC,CAAAA,CACA,UAAA,CAAAC,CACF,CAAA,GAKgB,CACd,IAAMC,CAAAA,CAAuB,GACvBC,CAAAA,CAA2B,GAC3BC,CAAAA,CAAmE,CACvE,QAAS,EAAC,CACV,KAAML,CACR,CAAA,CAeA,GAdIC,CAAAA,CAAO,IAAA,EAAM,aACXA,CAAAA,CAAO,IAAA,CAAK,aAAe,SAAA,EAC7BI,CAAAA,CAAQ,eAAiBJ,CAAAA,CAAO,WAAA,CAC5BA,EAAO,IAAA,CAAK,IAAA,GAAS,SACvBI,CAAAA,CAAQ,kBAAA,CAAqB,OAEtBJ,CAAAA,CAAO,IAAA,CAAK,aAAe,WAAA,GACpCI,CAAAA,CAAQ,iBAAmBJ,CAAAA,CAAO,WAAA,CAC9BA,EAAO,IAAA,CAAK,IAAA,GAAS,SACvBI,CAAAA,CAAQ,oBAAA,CAAuB,QAMnCJ,CAAAA,CAAO,IAAA,EAAM,aAAe,OAAA,EAC3B,CAACE,EAAM,MAAA,EAAU,CAACE,EAAQ,cAAA,EAAkB,CAACA,EAAQ,gBAAA,CACtD,CACA,IAAIC,CAAAA,CAAOL,CAAAA,CAAO,YACZM,CAAAA,CAAmBR,CAAAA,CAAK,cAAc,GAAA,CAAIE,C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cesContent":["import type { IFileOut } from '../files/types';\nimport type { ISymbolOut } from '../symbols/types';\nimport type { IBinding } from './types';\n\nexport const createBinding = ({\n file,\n modulePath,\n symbol,\n symbolFile,\n}: {\n file: IFileOut;\n modulePath: string;\n symbol: ISymbolOut;\n symbolFile: IFileOut;\n}): IBinding => {\n const names: Array<string> = [];\n const typeNames: Array<string> = [];\n const binding: IBinding & Pick<Required<IBinding>, 'aliases' | 'from'> = {\n aliases: {},\n from: modulePath,\n };\n if (symbol.meta?.importKind) {\n if (symbol.meta.importKind === 'default') {\n binding.defaultBinding = symbol.placeholder;\n if (symbol.meta.kind === 'type') {\n binding.typeDefaultBinding = true;\n }\n } else if (symbol.meta.importKind === 'namespace') {\n binding.namespaceBinding = symbol.placeholder;\n if (symbol.meta.kind === 'type') {\n binding.typeNamespaceBinding = true;\n }\n }\n }\n // default to named binding\n if (\n symbol.meta?.importKind === 'named' ||\n (!names.length && !binding.defaultBinding && !binding.namespaceBinding)\n ) {\n let name = symbol.placeholder;\n const fileResolvedName = file.resolvedNames.get(symbol.id);\n if (fileResolvedName) {\n const symbolFileResolvedName = symbolFile.resolvedNames.get(symbol.id);\n if (symbolFileResolvedName) {\n if (symbolFileResolvedName !== fileResolvedName) {\n name = symbolFileResolvedName;\n binding.aliases[name] = fileResolvedName;\n }\n } else if (symbol.name && fileResolvedName !== symbol.name) {\n name = symbol.name;\n binding.aliases[name] = symbol.placeholder;\n }\n }\n names.push(name);\n if (symbol.meta?.kind === 'type') {\n typeNames.push(name);\n }\n }\n // cast type names to names to allow for cleaner API,\n // otherwise users would have to define the same values twice\n for (const typeName of typeNames) {\n if (!names.includes(typeName)) {\n names.push(typeName);\n }\n }\n binding.names = names;\n binding.typeNames = typeNames;\n return binding;\n};\n\nexport const mergeBindings = (target: IBinding, source: IBinding): void => {\n target.aliases = { ...target.aliases, ...source.aliases };\n if (source.defaultBinding !== undefined) {\n target.defaultBinding = source.defaultBinding;\n }\n target.names = [\n ...new Set([...(target.names ?? []), ...(source.names ?? [])]),\n ];\n if (source.namespaceBinding !== undefined) {\n target.namespaceBinding = source.namespaceBinding;\n }\n if (source.typeDefaultBinding !== undefined) {\n target.typeDefaultBinding = source.typeDefaultBinding;\n }\n target.typeNames = [\n ...new Set([...(target.typeNames ?? []), ...(source.typeNames ?? [])]),\n ];\n if (source.typeNamespaceBinding !== undefined) {\n target.typeNamespaceBinding = source.typeNamespaceBinding;\n }\n};\n","import type { IBiMap } from './types';\n\nexport class BiMap<Key, Value> implements IBiMap<Key, Value> {\n private map = new Map<Key, Value>();\n private reverse = new Map<Value, Key>();\n\n delete(key: Key): boolean {\n const value = this.map.get(key);\n if (value !== undefined) {\n this.reverse.delete(value);\n }\n return this.map.delete(key);\n }\n\n deleteValue(value: Value): boolean {\n const key = this.reverse.get(value);\n if (key !== undefined) {\n this.map.delete(key);\n }\n return this.reverse.delete(value);\n }\n\n entries(): IterableIterator<[Key, Value]> {\n return this.map.entries();\n }\n\n get(key: Key): Value | undefined {\n return this.map.get(key);\n }\n\n getKey(value: Value): Key | undefined {\n return this.reverse.get(value);\n }\n\n hasKey(key: Key): boolean {\n return this.map.has(key);\n }\n\n hasValue(value: Value): boolean {\n return this.reverse.has(value);\n }\n\n keys(): IterableIterator<Key> {\n return this.map.keys();\n }\n\n set(key: Key, value: Value): this {\n const oldValue = this.map.get(key);\n if (oldValue !== undefined && oldValue !== value) {\n this.reverse.delete(oldValue);\n }\n const oldKey = this.reverse.get(value);\n if (oldKey !== undefined && oldKey !== key) {\n this.map.delete(oldKey);\n }\n this.map.set(key, value);\n this.reverse.set(value, key);\n return this;\n }\n\n get size(): number {\n return this.map.size;\n }\n\n values(): IterableIterator<Value> {\n return this.map.values();\n }\n\n [Symbol.iterator](): IterableIterator<[Key, Value]> {\n return this.map[Symbol.iterator]();\n }\n}\n","import { BiMap } from '../bimap/bimap';\nimport type { ISelector } from '../selectors/types';\nimport type { IFileIn, IFileOut, IFileRegistry } from './types';\n\nexport class FileRegistry implements IFileRegistry {\n private _id: number = 0;\n private referenceOrder: Set<number> = new Set();\n private registerOrder: Set<number> = new Set();\n private selectorToId: Map<string, number> = new Map();\n private values: Map<number, IFileOut> = new Map();\n\n get(fileIdOrSelector: number | ISelector): IFileOut | undefined {\n const symbol = this.idOrSelector(fileIdOrSelector);\n\n if (symbol.id !== undefined) {\n return this.values.get(symbol.id);\n }\n\n const selector =\n symbol.selector !== undefined\n ? JSON.stringify(symbol.selector)\n : undefined;\n\n if (selector) {\n const id = this.selectorToId.get(selector);\n if (id !== undefined) {\n return this.values.get(id);\n }\n }\n\n return;\n }\n\n get id(): number {\n return this._id++;\n }\n\n private idOrSelector(\n symbolIdOrSelector: number | ISelector,\n ): Pick<IFileIn, 'id' | 'selector'> {\n return typeof symbolIdOrSelector === 'number'\n ? { id: symbolIdOrSelector }\n : { selector: symbolIdOrSelector };\n }\n\n reference(fileIdOrSelector: number | ISelector): IFileOut {\n const file = this.idOrSelector(fileIdOrSelector);\n return this.register(file);\n }\n\n *referenced(): IterableIterator<IFileOut> {\n for (const id of this.referenceOrder.values()) {\n yield this.values.get(id)!;\n }\n }\n\n register(file: IFileIn): IFileOut {\n if (file.id !== undefined) {\n const result = this.values.get(file.id);\n if (!result) {\n throw new Error(\n `File with ID ${file.id} not found. To register a new file, leave the ID undefined.`,\n );\n }\n return result;\n }\n\n const hasOtherKeys = Object.keys(file).some(\n (key) => !['id', 'selector'].includes(key),\n );\n\n let result: IFileOut | undefined;\n\n const selector =\n file.selector !== undefined ? JSON.stringify(file.selector) : undefined;\n if (selector) {\n const id = this.selectorToId.get(selector);\n if (id !== undefined) {\n result = this.values.get(id);\n if (!result) {\n throw new Error(\n `File with ID ${id} not found. The selector ${selector} matched an ID, but there was no result. This is likely an issue with the application logic.`,\n );\n }\n if (!hasOtherKeys) {\n return result;\n }\n }\n }\n\n const id = result?.id !== undefined ? result.id : this.id;\n result = {\n ...result,\n ...file, // clone to avoid mutation\n id,\n resolvedNames: result?.resolvedNames ?? new BiMap(),\n symbols: result?.symbols ?? {\n body: [],\n exports: [],\n imports: [],\n },\n };\n this.values.set(id, result);\n\n if (hasOtherKeys) {\n this.registerOrder.add(id);\n } else {\n this.referenceOrder.add(id);\n }\n\n if (selector) {\n this.selectorToId.set(selector, id);\n }\n\n return result;\n }\n\n *registered(): IterableIterator<IFileOut> {\n for (const id of this.registerOrder.values()) {\n yield this.values.get(id)!;\n }\n }\n}\n","/**\n * Wraps an ID in namespace to avoid collisions when replacing it.\n *\n * @param symbolId Stringified symbol ID to use.\n * @returns The wrapped placeholder ID.\n */\nexport const wrapId = (symbolId: string): string => `_heyapi_${symbolId}_`;\n\n/**\n * Unwraps an ID from namespace.\n *\n * @param wrappedId The wrapped placeholder ID.\n * @returns Stringified ID to use.\n */\nconst unwrapId = (wrappedId: string): string =>\n wrappedId.slice('_heyapi_'.length, -1);\n\n/**\n * Returns a RegExp instance to match ID placeholders.\n *\n * @returns RegExp instance to match ID placeholders.\n */\nconst createPlaceholderRegExp = (): RegExp => new RegExp(wrapId('\\\\d+'), 'g');\n\n/**\n *\n * @param source The source string to replace.\n * @param replacerFn Accepts a symbol ID, returns resolved symbol name.\n * @returns The replaced source string.\n */\nexport const renderIds = (\n source: string,\n replacerFn: (symbolId: number) => string | undefined,\n): string =>\n source.replace(createPlaceholderRegExp(), (match) => {\n const symbolId = Number.parseInt(unwrapId(match), 10);\n return replacerFn(symbolId) || match;\n });\n","import { wrapId } from '../renderer/utils';\nimport type { ISelector } from '../selectors/types';\nimport type { ISymbolIn, ISymbolOut, ISymbolRegistry } from './types';\n\nexport class SymbolRegistry implements ISymbolRegistry {\n private _id: number = 0;\n private nodes: Map<number, unknown> = new Map();\n private registerOrder: Set<number> = new Set();\n private selectorToId: Map<string, number> = new Map();\n private values: Map<number, ISymbolOut> = new Map();\n\n get(symbolIdOrSelector: number | ISelector): ISymbolOut | undefined {\n const symbol = this.idOrSelector(symbolIdOrSelector);\n\n if (symbol.id !== undefined) {\n return this.values.get(symbol.id);\n }\n\n const selector =\n symbol.selector !== undefined\n ? JSON.stringify(symbol.selector)\n : undefined;\n\n if (selector) {\n const id = this.selectorToId.get(selector);\n if (id !== undefined) {\n return this.values.get(id);\n }\n }\n\n return;\n }\n\n getValue(symbolId: number): unknown {\n return this.nodes.get(symbolId);\n }\n\n hasValue(symbolId: number): boolean {\n return this.nodes.has(symbolId);\n }\n\n get id(): number {\n return this._id++;\n }\n\n private idOrSelector(\n symbolIdOrSelector: number | ISelector,\n ): Pick<ISymbolIn, 'id' | 'selector'> {\n return typeof symbolIdOrSelector === 'number'\n ? { id: symbolIdOrSelector }\n : { selector: symbolIdOrSelector };\n }\n\n reference(symbolIdOrSelector: number | ISelector): ISymbolOut {\n const symbol = this.idOrSelector(symbolIdOrSelector);\n return this.register(symbol);\n }\n\n register(symbol: ISymbolIn): ISymbolOut {\n if (symbol.id !== undefined) {\n const result = this.values.get(symbol.id);\n if (!result) {\n throw new Error(\n `Symbol with ID ${symbol.id} not found. To register a new symbol, leave the ID undefined.`,\n );\n }\n return result;\n }\n\n const hasOtherKeys = Object.keys(symbol).some(\n (key) => !['id', 'selector'].includes(key),\n );\n\n let result: ISymbolOut | undefined;\n\n const selector =\n symbol.selector !== undefined\n ? JSON.stringify(symbol.selector)\n : undefined;\n if (selector) {\n const id = this.selectorToId.get(selector);\n if (id !== undefined) {\n result = this.values.get(id);\n if (!result) {\n throw new Error(\n `Symbol with ID ${id} not found. The selector ${selector} matched an ID, but there was no result. This is likely an issue with the application logic.`,\n );\n }\n if (!hasOtherKeys) {\n return result;\n }\n }\n }\n\n const id = result?.id !== undefined ? result.id : this.id;\n const exportFrom: Array<string> = result?.exportFrom\n ? [...result.exportFrom]\n : [];\n if (symbol.exportFrom) {\n exportFrom.push(...symbol.exportFrom);\n }\n result = {\n ...result,\n ...symbol, // clone to avoid mutation\n exportFrom,\n id,\n placeholder:\n result?.placeholder ?? symbol.placeholder ?? wrapId(String(id)),\n };\n this.values.set(id, result);\n\n if (hasOtherKeys) {\n this.registerOrder.add(id);\n }\n\n if (selector) {\n this.selectorToId.set(selector, id);\n }\n\n return result;\n }\n\n *registered(): IterableIterator<ISymbolOut> {\n for (const id of this.registerOrder.values()) {\n yield this.values.get(id)!;\n }\n }\n\n setValue(symbolId: number, value: unknown): Map<number, unknown> {\n return this.nodes.set(symbolId, value);\n }\n}\n","import path from 'node:path';\n\nimport type { IProjectRenderMeta } from '../extensions/types';\nimport { FileRegistry } from '../files/registry';\nimport type { IFileOut } from '../files/types';\nimport type { IOutput } from '../output/types';\nimport type { IRenderer } from '../renderer/types';\nimport type { ISelector } from '../selectors/types';\nimport { SymbolRegistry } from '../symbols/registry';\nimport type { ISymbolOut } from '../symbols/types';\nimport type { IProject } from './types';\n\nconst externalSourceSymbol = '@';\n\nexport class Project implements IProject {\n private symbolIdToFileIds: Map<number, Set<number>> = new Map();\n\n readonly defaultFileName: string;\n readonly files = new FileRegistry();\n readonly fileName?: (name: string) => string;\n readonly renderers: Record<string, IRenderer> = {};\n readonly root: string;\n readonly symbols = new SymbolRegistry();\n\n constructor({\n defaultFileName,\n fileName,\n renderers,\n root,\n }: Pick<IProject, 'defaultFileName' | 'fileName' | 'renderers' | 'root'>) {\n this.defaultFileName = defaultFileName ?? 'main';\n this.fileName = typeof fileName === 'string' ? () => fileName : fileName;\n this.renderers = renderers;\n this.root = root;\n }\n\n private getRenderer(file: IFileOut): IRenderer | undefined {\n return file.extension ? this.renderers[file.extension] : undefined;\n }\n\n private prepareFiles(): void {\n // TODO: infer extension from symbols\n const extension = '.ts';\n for (const symbol of this.symbols.registered()) {\n const selector = this.symbolToFileSelector(symbol);\n const file = this.files.reference(selector);\n file.symbols.body.push(symbol.id);\n // update symbol->files map\n const symbolIdToFileIds =\n this.symbolIdToFileIds.get(symbol.id) ?? new Set();\n symbolIdToFileIds.add(file.id);\n this.symbolIdToFileIds.set(symbol.id, symbolIdToFileIds);\n // update re-exports\n for (const exportFrom of symbol.exportFrom) {\n const exportSelector = [exportFrom];\n const exportFile = this.files.reference(exportSelector);\n if (exportFile.id !== file.id) {\n exportFile.symbols.exports.push(symbol.id);\n }\n }\n }\n for (const file of this.files.referenced()) {\n if (!file.selector) continue;\n if (file.selector[0] === externalSourceSymbol) {\n const filePath = file.selector[1];\n if (!filePath) {\n this.files.register({\n external: true,\n selector: file.selector,\n });\n continue;\n }\n const extension = path.extname(filePath);\n if (!extension) {\n this.files.register({\n external: true,\n path: filePath,\n selector: file.selector,\n });\n continue;\n }\n this.files.register({\n extension,\n external: true,\n path: filePath,\n selector: file.selector,\n });\n continue;\n }\n const dirs = file.selector.slice(0, -1);\n let name = file.selector[file.selector.length - 1]!;\n name = this.fileName?.(name) || name;\n this.files.register({\n extension,\n name,\n path: path.resolve(this.root, ...dirs, `${name}${extension}`),\n selector: file.selector,\n });\n }\n\n // TODO: track symbol dependencies and inject imports into files\n // based on symbol references so the render step can just render\n }\n\n render(meta?: IProjectRenderMeta): ReadonlyArray<IOutput> {\n this.prepareFiles();\n const files: Map<number, IOutput> = new Map();\n for (const file of this.files.registered()) {\n if (file.external || !file.path) continue;\n const renderer = this.getRenderer(file);\n if (!renderer) continue;\n files.set(file.id, {\n content: renderer.renderSymbols(file, this, meta),\n path: file.path,\n });\n }\n for (const [fileId, value] of files.entries()) {\n const file = this.files.get(fileId)!;\n const renderer = this.getRenderer(file)!;\n const content = renderer.renderFile(value.content, file, this, meta);\n if (content) {\n files.set(file.id, { ...value, content });\n } else {\n files.delete(file.id);\n }\n }\n return Array.from(files.values());\n }\n\n symbolIdToFiles(symbolId: number): ReadonlyArray<IFileOut> {\n const fileIds = this.symbolIdToFileIds.get(symbolId);\n return Array.from(fileIds ?? []).map((fileId) => this.files.get(fileId)!);\n }\n\n private symbolToFileSelector(symbol: ISymbolOut): ISelector {\n if (symbol.external) {\n return [externalSourceSymbol, symbol.external];\n }\n const filePath = symbol.getFilePath?.(symbol);\n if (filePath) {\n return filePath.split('/');\n }\n return [this.defaultFileName];\n }\n}\n"]} |
+338
-468
@@ -9,3 +9,3 @@ /** | ||
| */ | ||
| interface ICodegenBiMap<Key, Value> { | ||
| interface IBiMap<Key, Value> { | ||
| /** | ||
@@ -77,123 +77,89 @@ * Deletes a key and its associated value from the map. | ||
| declare class BiMap<Key, Value> implements ICodegenBiMap<Key, Value> { | ||
| private map; | ||
| private reverse; | ||
| delete(key: Key): boolean; | ||
| deleteValue(value: Value): boolean; | ||
| entries(): IterableIterator<[Key, Value]>; | ||
| get(key: Key): Value | undefined; | ||
| getKey(value: Value): Key | undefined; | ||
| hasKey(key: Key): boolean; | ||
| hasValue(value: Value): boolean; | ||
| keys(): IterableIterator<Key>; | ||
| set(key: Key, value: Value): this; | ||
| get size(): number; | ||
| values(): IterableIterator<Value>; | ||
| [Symbol.iterator](): IterableIterator<[Key, Value]>; | ||
| } | ||
| /** | ||
| * Arbitrary metadata passed to render functions. | ||
| * | ||
| * Implementors should extend this interface for their own needs. | ||
| */ | ||
| interface ICodegenMeta { | ||
| [key: string]: unknown; | ||
| } | ||
| interface ICodegenOutput { | ||
| interface IBinding { | ||
| /** | ||
| * The main content of the file to output. | ||
| * Optional aliasing map for named symbols. | ||
| * | ||
| * A raw string representing source code. | ||
| * Keys must be a subset of `names`, values are aliases. | ||
| * | ||
| * @example "function foo(): void {\n // implementation\n}\n" | ||
| * @example { User: "ImportedUser" } | ||
| */ | ||
| content: string; | ||
| aliases?: Record<string, string>; | ||
| /** | ||
| * Optional metadata or hints for the emitter, such as formatting options, | ||
| * source maps, or language-specific flags. | ||
| * Name of the default binding, if any. | ||
| * | ||
| * @example { format: "prettier", sourceMap: true } | ||
| * @example "React" | ||
| */ | ||
| meta: Record<string, unknown>; | ||
| defaultBinding?: string; | ||
| /** | ||
| * Logical output path (used for writing the file). | ||
| * Source file or external module from which symbols are imported. | ||
| * | ||
| * @example "models/user.ts" | ||
| * @example "./models/user" | ||
| * @example "node:path" | ||
| */ | ||
| path: string; | ||
| } | ||
| interface ICodegenRenderer { | ||
| from: string; | ||
| /** | ||
| * Optional: hook for renderer-level setup logic (e.g., formatting, config) | ||
| * Names of the symbols imported from the source. | ||
| * | ||
| * Must be non-empty unless `namespaceBinding` is true. | ||
| * All imported names, regardless of whether they are used as types or values. | ||
| * | ||
| * @example ["User", "UserDTO"] | ||
| */ | ||
| configure?(options: Record<string, unknown>): void; | ||
| names?: ReadonlyArray<string>; | ||
| /** | ||
| * Unique identifier for this renderer. | ||
| * If this import is a namespace import (e.g. `import * as ns from "..."`), | ||
| * this should be the namespace alias. Set to `true` if no alias is needed. | ||
| * | ||
| * @example "typescript" | ||
| * @example "utils" | ||
| * @example true | ||
| */ | ||
| id: string; | ||
| namespaceBinding?: boolean | string; | ||
| /** | ||
| * Returns printable data containing header and imports. | ||
| * Whether the default binding is type-only. | ||
| * | ||
| * @param file The file to render. | ||
| * @param meta Arbitrary metadata. | ||
| * @returns Printable string containing header and imports. | ||
| * @example true | ||
| */ | ||
| renderHeader(file: ICodegenFile, meta?: ICodegenMeta): string; | ||
| typeDefaultBinding?: boolean; | ||
| /** | ||
| * Returns printable data containing symbols and exports. | ||
| * Subset of `names` that are imported using the `type` modifier. | ||
| * These symbols will be emitted as type-only imports in TypeScript. | ||
| * | ||
| * @param file The file to render. | ||
| * @param meta Arbitrary metadata. | ||
| * @returns Printable string containing symbols and exports. | ||
| * @example ["UserDTO"] | ||
| */ | ||
| renderSymbols(file: ICodegenFile, meta?: ICodegenMeta): string; | ||
| typeNames?: ReadonlyArray<string>; | ||
| /** | ||
| * Function replacing symbols with resolved names. | ||
| * Whether the namespace binding is type-only. | ||
| * | ||
| * @returns String with replaced symbols. | ||
| * @example true | ||
| */ | ||
| replacerFn(args: { | ||
| file: ICodegenFile; | ||
| headless?: boolean; | ||
| scope?: 'file' | 'project'; | ||
| symbolId: number; | ||
| }): string | undefined; | ||
| typeNamespaceBinding?: boolean; | ||
| } | ||
| /** | ||
| * Selector array used to select symbols. It doesn't have to be | ||
| * unique, but in practice it might be desirable. | ||
| * Selector array used to reference resources. We don't enforce | ||
| * uniqueness, but in practice it's desirable. | ||
| * | ||
| * @example ["zod", "#/components/schemas/Foo"] | ||
| */ | ||
| type ICodegenSymbolSelector = ReadonlyArray<string>; | ||
| type ISelector = ReadonlyArray<string>; | ||
| interface ICodegenSymbolIn { | ||
| interface IFileIn { | ||
| /** | ||
| * Symbols can be **headed** or **headless**. | ||
| * File extension, if any. | ||
| */ | ||
| readonly extension?: string; | ||
| /** | ||
| * Indicates whether the file is external, meaning it is not generated | ||
| * as part of the project but is referenced (e.g., a module from | ||
| * node_modules). | ||
| * | ||
| * Headless symbols never render their `value`. Headed symbols render their | ||
| * `value` if defined. | ||
| * | ||
| * Symbols are rendered in the order they were registered as headed. | ||
| * | ||
| * Example 1: We register headless symbol `foo`, headed `bar`, and headed | ||
| * `foo`. The render order is [`bar`, `foo`]. | ||
| * | ||
| * Example 2: We register headed symbol `foo` and headed `bar`. The render | ||
| * order is [`foo`, `bar`]. | ||
| * | ||
| * Headless symbols can be used to claim a symbol or to represent imports | ||
| * or exports. | ||
| * | ||
| * @default false | ||
| * @example true | ||
| */ | ||
| headless?: boolean; | ||
| readonly external?: boolean; | ||
| /** | ||
| * The desired name for the symbol within its file. If there are multiple symbols | ||
| * Unique file ID. If one is not provided, it will be auto-generated. | ||
| */ | ||
| readonly id?: number; | ||
| /** | ||
| * The desired name for the file within the project. If there are multiple files | ||
| * with the same desired name, this might not end up being the actual name. | ||
@@ -203,5 +169,11 @@ * | ||
| */ | ||
| readonly name: string; | ||
| readonly name?: string; | ||
| /** | ||
| * Selector array used to select this symbol. It doesn't have to be | ||
| * Absolute logical output path for the file. | ||
| * | ||
| * @example "/src/models/user.ts" | ||
| */ | ||
| readonly path?: string; | ||
| /** | ||
| * Selector array used to select this file. It doesn't have to be | ||
| * unique, but in practice it might be desirable. | ||
@@ -211,330 +183,257 @@ * | ||
| */ | ||
| readonly selector?: ICodegenSymbolSelector; | ||
| /** | ||
| * Internal representation of the symbol (e.g. AST node, IR object, raw code). | ||
| * Used to generate output. If left undefined, this symbol becomes `headless`. | ||
| */ | ||
| readonly value?: unknown; | ||
| readonly selector?: ISelector; | ||
| } | ||
| interface ICodegenSymbolOut extends ICodegenSymbolIn { | ||
| interface IFileOut extends IFileIn { | ||
| /** | ||
| * The file this symbol is located in. | ||
| * Unique file ID. | ||
| */ | ||
| readonly file: ICodegenFile; | ||
| /** | ||
| * Unique symbol ID. | ||
| */ | ||
| readonly id: number; | ||
| /** | ||
| * Placeholder name for the symbol to be replaced later with the final value. | ||
| * | ||
| * @example "_heyapi_31_" | ||
| * Map holding resolved names for symbols in this file. | ||
| */ | ||
| readonly placeholder: string; | ||
| readonly resolvedNames: IBiMap<number, string>; | ||
| /** | ||
| * Updates this symbol. | ||
| * | ||
| * @param symbol The values to update. | ||
| * @returns The updated symbol. | ||
| * Symbols in this file, categorized by their role. | ||
| */ | ||
| readonly update: (symbol: Partial<ICodegenSymbolOut>) => ICodegenSymbolOut; | ||
| readonly symbols: { | ||
| /** | ||
| * Symbols declared in the body of this file. | ||
| */ | ||
| body: Array<number>; | ||
| /** | ||
| * Symbols re-exported from other files. | ||
| */ | ||
| exports: Array<number>; | ||
| /** | ||
| * Symbols imported from other files. | ||
| */ | ||
| imports: Array<number>; | ||
| }; | ||
| } | ||
| interface SelectorMethods { | ||
| interface IFileRegistry { | ||
| /** | ||
| * Retrieves symbols matching the selector. | ||
| * Get a file by its ID. | ||
| * | ||
| * @param selector The symbol selector to find. | ||
| * @param file Find symbols only in this file. | ||
| * @returns The array of all symbols matching the selector. | ||
| * @example | ||
| * const symbols = project.selectSymbolAll(["zod", "#/components/schemas/Foo"]); | ||
| * @param fileIdOrSelector File ID or selector to reference. | ||
| * @returns The file, or undefined if not found. | ||
| */ | ||
| selectSymbolAll( | ||
| selector: ICodegenSymbolSelector, | ||
| file?: ICodegenFile, | ||
| ): ReadonlyArray<ICodegenSymbolOut>; | ||
| get(fileIdOrSelector: number | ISelector): IFileOut | undefined; | ||
| /** | ||
| * Retrieves the first symbol from all symbols matching the selector. | ||
| * Returns the current file ID and increments it. | ||
| * | ||
| * @param selector The symbol selector to find. | ||
| * @param file Find symbols only in this file. | ||
| * @returns The symbol if found, or undefined otherwise. | ||
| * @example | ||
| * const symbol = project.selectSymbolFirst(["zod", "#/components/schemas/Foo"]); | ||
| * @returns File ID before being incremented | ||
| */ | ||
| selectSymbolFirst( | ||
| selector: ICodegenSymbolSelector, | ||
| file?: ICodegenFile, | ||
| ): ICodegenSymbolOut | undefined; | ||
| readonly id: number; | ||
| /** | ||
| * Retrieves the first symbol from all symbols matching the selector. | ||
| * Returns a file by ID or selector, registering it if it doesn't exist. | ||
| * | ||
| * @param selector The symbol selector to find. | ||
| * @param file Find symbols only in this file. | ||
| * @returns The symbol if found, or throw otherwise. | ||
| * @example | ||
| * const symbol = project.selectSymbolFirstOrThrow(["zod", "#/components/schemas/Foo"]); | ||
| * @param fileIdOrSelector File ID or selector to reference. | ||
| * @returns The referenced or newly registered file. | ||
| */ | ||
| selectSymbolFirstOrThrow( | ||
| selector: ICodegenSymbolSelector, | ||
| file?: ICodegenFile, | ||
| ): ICodegenSymbolOut; | ||
| reference(fileIdOrSelector: number | ISelector): IFileOut; | ||
| /** | ||
| * Retrieves the last symbol from all symbols matching the selector. | ||
| * Get all unregistered files in the order they were referenced. | ||
| * | ||
| * @param selector The symbol selector to find. | ||
| * @param file Find symbols only in this file. | ||
| * @returns The symbol if found, or undefined otherwise. | ||
| * @example | ||
| * const symbol = project.selectSymbolLast(["zod", "#/components/schemas/Foo"]); | ||
| * @returns Array of all unregistered files, in reference order. | ||
| */ | ||
| selectSymbolLast( | ||
| selector: ICodegenSymbolSelector, | ||
| file?: ICodegenFile, | ||
| ): ICodegenSymbolOut | undefined; | ||
| } | ||
| /** | ||
| * Represents a code generation project consisting of multiple codegen files. | ||
| * Manages imports, symbols, and output generation across the project. | ||
| */ | ||
| interface ICodegenProject extends SelectorMethods { | ||
| referenced(): IterableIterator<IFileOut>; | ||
| /** | ||
| * Adds an export declaration to a specific file, creating the file if it doesn't exist. | ||
| * Register a file globally. | ||
| * | ||
| * @param fileOrPath - File instance or file path where to add the export. | ||
| * @param imp - The export declaration to add. | ||
| * @example | ||
| * project.addExport("models/user.ts", { from: "lib", names: ["User"] }); | ||
| * Deduplicates identical files by ID. | ||
| * | ||
| * @param file File to register. | ||
| * @returns true if added, false if duplicate. | ||
| */ | ||
| addExport(fileOrPath: ICodegenFile | string, imp: ICodegenImport): void; | ||
| register(file: IFileIn): IFileOut; | ||
| /** | ||
| * Adds an import declaration to a specific file, creating the file if it doesn't exist. | ||
| * Get all files in the order they were registered. | ||
| * | ||
| * @param fileOrPath - File instance or file path where to add the import. | ||
| * @param imp - The import declaration to add. | ||
| * @example | ||
| * project.addImport("models/user.ts", { from: "lib", names: ["User"] }); | ||
| * @returns Array of all registered files, in insert order. | ||
| */ | ||
| addImport(fileOrPath: ICodegenFile | string, imp: ICodegenImport): void; | ||
| registered(): IterableIterator<IFileOut>; | ||
| } | ||
| /** | ||
| * Arbitrary metadata passed to the project's render function. | ||
| * | ||
| * Implementers should extend this interface for their own needs. | ||
| */ | ||
| interface IProjectRenderMeta { | ||
| [key: string]: unknown; | ||
| } | ||
| /** | ||
| * Additional metadata about the symbol. | ||
| * | ||
| * Implementers should extend this interface for their own needs. | ||
| */ | ||
| interface ISymbolMeta { | ||
| [key: string]: unknown; | ||
| } | ||
| interface ISymbolIn { | ||
| /** | ||
| * Adds a symbol to a specific file, creating the file if it doesn't exist. | ||
| * Array of file names (without extensions) from which this symbol is re-exported. | ||
| * | ||
| * @param fileOrPath - File instance or file path where to add the symbol. | ||
| * @param symbol - The symbol to add. | ||
| * @returns The inserted symbol. | ||
| * @example | ||
| * project.addSymbol("models/user.ts", { name: "User", value: tsNode }); | ||
| * @default undefined | ||
| */ | ||
| addSymbol( | ||
| fileOrPath: ICodegenFile | string, | ||
| symbol: ICodegenSymbolIn, | ||
| ): ICodegenSymbolOut; | ||
| readonly exportFrom?: ReadonlyArray<string>; | ||
| /** | ||
| * Creates a new codegen file with optional metadata and adds it to the project. | ||
| * Whether this symbol is exported from its own file. | ||
| * | ||
| * If a file with the same path already exists, it is returned instead. | ||
| * | ||
| * @param path - The logical output path for the file (e.g. "models/user.ts"). | ||
| * @param meta - Optional renderer and metadata to attach to the file (e.g. { isInternal: true }). | ||
| * @returns The newly created file instance. | ||
| * @example | ||
| * const file = project.createFile("models/user.ts", { isInternal: true }); | ||
| * @default false | ||
| */ | ||
| createFile( | ||
| path: string, | ||
| meta?: ICodegenFile['meta'] & { renderer?: ICodegenRenderer }, | ||
| ): ICodegenFile; | ||
| readonly exported?: boolean; | ||
| /** | ||
| * Ensures a codegen file exists and returns it. | ||
| * External module name if this symbol is imported from a module not managed | ||
| * by the project (e.g. "zod", "lodash"). | ||
| * | ||
| * If a file does not exist yet, it is created with minimal information. | ||
| * Later, it is expected `createFile()` will be called which will fill in | ||
| * the missing information such as optional metadata. | ||
| * | ||
| * @param fileOrPath - The logical output path for the file or the file itself. | ||
| * @returns The file instance. | ||
| * @example | ||
| * const file = project.ensureFile("models/user.ts"); | ||
| * @default undefined | ||
| */ | ||
| ensureFile(fileOrPath: ICodegenFile | string): ICodegenFile; | ||
| readonly external?: string; | ||
| /** | ||
| * Returns all files in the project in insertion order. | ||
| * Optional output strategy to override default behavior. | ||
| * | ||
| * @example | ||
| * project.files.forEach(file => console.log(file.path)); | ||
| * @returns The file path to output the symbol to, or undefined to fallback to default behavior. | ||
| */ | ||
| readonly files: ReadonlyArray<ICodegenFile>; | ||
| readonly getFilePath?: (symbol: ISymbolOut) => string | undefined; | ||
| /** | ||
| * Returns all symbols declared or imported across all files. | ||
| * | ||
| * @returns Flattened list of all codegen symbols. | ||
| * @example | ||
| * project.getAllSymbols().filter(s => s.name === "User"); | ||
| * Unique symbol ID. If one is not provided, it will be auto-generated. | ||
| */ | ||
| getAllSymbols(): ReadonlyArray<Pick<ICodegenSymbolOut, 'name'>>; | ||
| readonly id?: number; | ||
| /** | ||
| * Retrieves a file by its logical output path. | ||
| * Arbitrary metadata about the symbol. | ||
| * | ||
| * @param path - The file path to find. | ||
| * @returns The file if found, or undefined otherwise. | ||
| * @example | ||
| * const file = project.getFileByPath("models/user.ts"); | ||
| * @default undefined | ||
| */ | ||
| getFileByPath(path: string): ICodegenFile | undefined; | ||
| readonly meta?: ISymbolMeta & { | ||
| /** | ||
| * Kind of import if this symbol represents an import. | ||
| */ | ||
| importKind?: 'namespace' | 'default' | 'named'; | ||
| /** | ||
| * Kind of symbol. | ||
| */ | ||
| kind?: 'type'; | ||
| }; | ||
| /** | ||
| * Retrieves a file from symbol ID included in the file. | ||
| * The desired name for the symbol within its file. If there are multiple symbols | ||
| * with the same desired name, this might not end up being the actual name. | ||
| * | ||
| * @param id The symbol ID to find. | ||
| * @returns The file if found, undefined otherwise. | ||
| * @example | ||
| * const file = project.getFileBySymbolId(31); | ||
| * @example "UserModel" | ||
| */ | ||
| getFileBySymbolId(id: number): ICodegenFile | undefined; | ||
| readonly name?: string; | ||
| /** | ||
| * Retrieves a symbol from ID included in the project. | ||
| * Placeholder name for the symbol to be replaced later with the final value. | ||
| * | ||
| * @param id The symbol ID to find. | ||
| * @returns The symbol if found, undefined otherwise. | ||
| * @example | ||
| * const symbol = project.getSymbolById(31); | ||
| * @example "_heyapi_31_" | ||
| */ | ||
| getSymbolById(id: number): ICodegenSymbolOut | undefined; | ||
| readonly placeholder?: string; | ||
| /** | ||
| * Returns the current file ID and increments it. | ||
| * Selector array used to select this symbol. It doesn't have to be | ||
| * unique, but in practice it might be desirable. | ||
| * | ||
| * @returns File ID before being incremented | ||
| * @example ["zod", "#/components/schemas/Foo"] | ||
| */ | ||
| incrementFileId(): number; | ||
| readonly selector?: ISelector; | ||
| } | ||
| interface ISymbolOut extends ISymbolIn { | ||
| /** | ||
| * Returns the current symbol ID and increments it. | ||
| * | ||
| * @returns Symbol ID before being incremented | ||
| * Array of file names (without extensions) from which this symbol is re-exported. | ||
| */ | ||
| incrementSymbolId(): number; | ||
| readonly exportFrom: ReadonlyArray<string>; | ||
| /** | ||
| * Tracks added symbol across the project. | ||
| * | ||
| * @param symbol The symbol added to file. | ||
| * @param file The file containing the added symbol. | ||
| * Unique symbol ID. | ||
| */ | ||
| registerSymbol(symbol: ICodegenSymbolOut, file: ICodegenFile): void; | ||
| readonly id: number; | ||
| /** | ||
| * Produces output representations for all files in the project. | ||
| * Placeholder name for the symbol to be replaced later with the final value. | ||
| * | ||
| * @param meta Arbitrary metadata. | ||
| * @returns Array of outputs ready for writing or further processing. | ||
| * @example | ||
| * project.render().forEach(output => writeFile(output)); | ||
| * @example "_heyapi_31_" | ||
| */ | ||
| render(meta?: ICodegenMeta): ReadonlyArray<ICodegenOutput>; | ||
| readonly placeholder: string; | ||
| } | ||
| interface ICodegenFile extends SelectorMethods { | ||
| interface ISymbolRegistry { | ||
| /** | ||
| * Adds an export to this file. | ||
| * Get a symbol by its ID. | ||
| * | ||
| * This is also known as a re-export. | ||
| * | ||
| * @param exp The export to add | ||
| * @param symbolIdOrSelector Symbol ID or selector to reference. | ||
| * @returns The symbol, or undefined if not found. | ||
| */ | ||
| addExport(exp: ICodegenImport): void; | ||
| get(symbolIdOrSelector: number | ISelector): ISymbolOut | undefined; | ||
| /** | ||
| * Adds an import to this file. | ||
| * Returns the value associated with a symbol ID. | ||
| * | ||
| * @param imp The import to add | ||
| * @param symbolId Symbol ID. | ||
| * @return The value associated with the symbol ID, or undefined if not found. | ||
| */ | ||
| addImport(imp: ICodegenImport): void; | ||
| getValue(symbolId: number): unknown; | ||
| /** | ||
| * Adds a symbol defined by this file. | ||
| * Checks if the registry has a value associated with a symbol ID. | ||
| * | ||
| * @param symbol The symbol to add | ||
| * @param symbolId Symbol ID. | ||
| * @returns True if the registry has a value for symbol ID, false otherwise. | ||
| */ | ||
| addSymbol(symbol: ICodegenSymbolIn): ICodegenSymbolOut; | ||
| hasValue(symbolId: number): boolean; | ||
| /** | ||
| * Ensures a symbol for the given selector exists, so it can be | ||
| * safely used. | ||
| * Returns the current symbol ID and increments it. | ||
| * | ||
| * @param symbol The symbol to find. The required selector is used | ||
| * to match a symbol. If there's no match, we create a headless | ||
| * instance with the provided fields. | ||
| * @returns The symbol if it exists, headless instance otherwise. | ||
| * @returns Symbol ID before being incremented. | ||
| */ | ||
| ensureSymbol( | ||
| symbol: Partial<ICodegenSymbolIn> & | ||
| Pick<Required<ICodegenSymbolIn>, 'selector'>, | ||
| ): ICodegenSymbolOut; | ||
| readonly id: number; | ||
| /** | ||
| * Symbols exported from other files. | ||
| **/ | ||
| exports: ReadonlyArray<ICodegenImport>; | ||
| /** | ||
| * Returns all symbols used in this file (declared + imported). | ||
| * Returns a symbol by ID or selector, registering it if it doesn't exist. | ||
| * | ||
| * @returns List of all symbols used in this file | ||
| * @param symbolIdOrSelector Symbol ID or selector to reference. | ||
| * @returns The referenced or newly registered symbol. | ||
| */ | ||
| getAllSymbols(): ReadonlyArray<Pick<ICodegenSymbolOut, 'name'>>; | ||
| reference(symbolIdOrSelector: number | ISelector): ISymbolOut; | ||
| /** | ||
| * Finds a symbol by symbol ID. | ||
| * Register a symbol globally. | ||
| * | ||
| * @param id Symbol ID | ||
| * @returns The symbol if it exists, undefined otherwise. | ||
| * Deduplicates identical symbols by ID. | ||
| * | ||
| * @param symbol Symbol to register. | ||
| * @returns The registered symbol. | ||
| */ | ||
| getSymbolById(id: number): ICodegenSymbolOut | undefined; | ||
| register(symbol: ISymbolIn): ISymbolOut; | ||
| /** | ||
| * Checks if this file contains any content. | ||
| * Get all symbols in the order they were registered. | ||
| * | ||
| * This is used to determine whether we want to process the file further. | ||
| * By default, we consider only symbols and exports as content. | ||
| * | ||
| * @returns True if the file contains content | ||
| * @returns Array of all registered symbols, in insert order. | ||
| */ | ||
| hasContent(): boolean; | ||
| registered(): IterableIterator<ISymbolOut>; | ||
| /** | ||
| * Checks if this file defines a symbol with the given name. | ||
| * Sets a value for a symbol by its ID. | ||
| * | ||
| * @param id Symbol ID to check | ||
| * @returns True if the symbol is defined by this file | ||
| * @param symbolId Symbol ID. | ||
| * @param value The value to set. | ||
| * @returns void | ||
| */ | ||
| hasSymbol(id: number): boolean; | ||
| setValue(symbolId: number, value: unknown): Map<number, unknown>; | ||
| } | ||
| declare const createBinding: ({ file, modulePath, symbol, symbolFile, }: { | ||
| file: IFileOut; | ||
| modulePath: string; | ||
| symbol: ISymbolOut; | ||
| symbolFile: IFileOut; | ||
| }) => IBinding; | ||
| declare const mergeBindings: (target: IBinding, source: IBinding) => void; | ||
| interface IOutput { | ||
| /** | ||
| * File ID within the project. | ||
| * The main content of the file to output. | ||
| * | ||
| * A raw string representing source code. | ||
| * | ||
| * @example "function foo(): void {\n // implementation\n}\n" | ||
| */ | ||
| id: number; | ||
| content: string; | ||
| /** | ||
| * Symbols imported from other files. | ||
| **/ | ||
| imports: ReadonlyArray<ICodegenImport>; | ||
| /** | ||
| * Optional metadata about the file. | ||
| **/ | ||
| meta: { | ||
| /** | ||
| * Optional file extension. | ||
| * | ||
| * @example ".ts" | ||
| */ | ||
| extension?: '.ts' | (string & {}); | ||
| /** | ||
| * Optional logical module or package name. | ||
| * | ||
| * @example "models.user" | ||
| */ | ||
| moduleName?: string; | ||
| /** | ||
| * Optional path transformer. | ||
| * | ||
| * @param path Original file path passed to the constructor. | ||
| */ | ||
| path?: ((path: string) => string) | string; | ||
| /** | ||
| * Renderer ID. | ||
| * | ||
| * @example "typescript" | ||
| */ | ||
| renderer?: ICodegenRenderer['id']; | ||
| }; | ||
| /** | ||
| * Logical output path (used for writing the file). | ||
@@ -545,173 +444,144 @@ * | ||
| path: string; | ||
| } | ||
| declare class FileRegistry implements IFileRegistry { | ||
| private _id; | ||
| private referenceOrder; | ||
| private registerOrder; | ||
| private selectorToId; | ||
| private values; | ||
| get(fileIdOrSelector: number | ISelector): IFileOut | undefined; | ||
| get id(): number; | ||
| private idOrSelector; | ||
| reference(fileIdOrSelector: number | ISelector): IFileOut; | ||
| referenced(): IterableIterator<IFileOut>; | ||
| register(file: IFileIn): IFileOut; | ||
| registered(): IterableIterator<IFileOut>; | ||
| } | ||
| /** | ||
| * Represents a code generation project consisting of multiple codegen files. | ||
| * Manages imports, symbols, and output generation across the project. | ||
| */ | ||
| interface IProject { | ||
| /** | ||
| * Parent project this file belongs to. | ||
| */ | ||
| project: ICodegenProject; | ||
| /** | ||
| * Returns a relative path to this file from another file. | ||
| * The default file to assign symbols without a specific file selector. | ||
| * | ||
| * @param file The file from which we want the relative path to this file. | ||
| * @example "./this-file.ts" | ||
| * @default 'main' | ||
| */ | ||
| relativePathFromFile(file: Pick<ICodegenFile, 'path'>): string; | ||
| readonly defaultFileName?: string; | ||
| /** | ||
| * Returns a relative path to file from this file. | ||
| * Optional function to transform file names before they are used. | ||
| * | ||
| * @param file The file to which we want the relative path. | ||
| * @example "./another-file.ts" | ||
| * @param name The original file name. | ||
| * @returns The transformed file name. | ||
| */ | ||
| relativePathToFile(file: Pick<ICodegenFile, 'path'>): string; | ||
| readonly fileName?: (name: string) => string; | ||
| /** | ||
| * Map holding resolved names for symbols in this file. | ||
| * Centralized file registry for the project. | ||
| */ | ||
| resolvedNames: ICodegenBiMap<number, string>; | ||
| readonly files: IFileRegistry; | ||
| /** | ||
| * Top-level symbols declared in this file. | ||
| **/ | ||
| symbols: ReadonlyArray<ICodegenSymbolOut>; | ||
| /** | ||
| * Updates a symbol defined by this file. | ||
| * Produces output representations for all files in the project. | ||
| * | ||
| * @param id ID of symbol to update. | ||
| * @param symbol The values to update. | ||
| * @returns The updated symbol. | ||
| * @param meta Arbitrary metadata. | ||
| * @returns Array of outputs ready for writing or further processing. | ||
| * @example | ||
| * project.render().forEach(output => writeFile(output)); | ||
| */ | ||
| updateSymbol( | ||
| id: number, | ||
| symbol: Partial<ICodegenSymbolOut>, | ||
| ): ICodegenSymbolOut; | ||
| } | ||
| interface ICodegenImport { | ||
| render(meta?: IProjectRenderMeta): ReadonlyArray<IOutput>; | ||
| /** | ||
| * Optional aliasing map for imported symbols. | ||
| * Map of available renderers by file extension. | ||
| * | ||
| * Keys must be a subset of `names`, values are aliases. | ||
| * | ||
| * @example { User: "ImportedUser" } | ||
| * @example | ||
| * { | ||
| * ".ts": tsRenderer, | ||
| * ".js": jsRenderer, | ||
| * } | ||
| */ | ||
| aliases?: Record<string, string>; | ||
| readonly renderers: Record<string, IRenderer>; | ||
| /** | ||
| * Name of the default import, if any. | ||
| * | ||
| * @example "React" | ||
| * The absolute path to the root folder of the project. | ||
| */ | ||
| defaultImport?: string; | ||
| readonly root: string; | ||
| /** | ||
| * Source file or external module from which symbols are imported. | ||
| * Retrieves files that include symbol ID. The first file is the one | ||
| * where the symbol is declared, the rest are files that re-export it. | ||
| * | ||
| * For internal files, this should be a ICodegenFile instance to enable | ||
| * dynamic path computation. For external or system modules, use a string. | ||
| * | ||
| * @example "./models/user" | ||
| * @example "node:path" | ||
| * @param symbolId The symbol ID to find. | ||
| * @returns An array of files containing the symbol. | ||
| * @example | ||
| * const files = project.symbolIdToFiles(31); | ||
| * for (const file of files) { | ||
| * console.log(file.path); | ||
| * } | ||
| */ | ||
| from: ICodegenFile | string; | ||
| symbolIdToFiles(symbolId: number): ReadonlyArray<IFileOut>; | ||
| /** | ||
| * Names of the symbols imported from the source. | ||
| * | ||
| * Must be non-empty unless `isNamespaceImport` is true. | ||
| * All imported names, regardless of whether they are used as types or values. | ||
| * | ||
| * @example ["User", "UserDTO"] | ||
| * Centralized symbol registry for the project. | ||
| */ | ||
| names?: ReadonlyArray<string>; | ||
| readonly symbols: ISymbolRegistry; | ||
| } | ||
| interface IRenderer { | ||
| /** | ||
| * If this import is a namespace import (e.g. `import * as ns from "..."`), | ||
| * this should be the namespace alias. Set to `true` if no alias is needed. | ||
| * Renders content with replaced symbols. | ||
| * | ||
| * @example "utils" | ||
| * @example true | ||
| * @param content Content to render. | ||
| * @param file The file to render. | ||
| * @param project The parent project the file belongs to. | ||
| * @returns Rendered content. | ||
| */ | ||
| namespaceImport?: boolean | string; | ||
| renderFile( | ||
| content: string, | ||
| file: IFile, | ||
| project: IProject, | ||
| meta?: IProjectRenderMeta, | ||
| ): string; | ||
| /** | ||
| * Whether the default import is type-only. | ||
| * Returns printable data containing symbols and exports. | ||
| * | ||
| * @example true | ||
| * @param file The file to render. | ||
| * @param project The parent project the file belongs to. | ||
| * @param meta Arbitrary metadata. | ||
| * @returns Printable string containing symbols and exports. | ||
| */ | ||
| typeDefaultImport?: boolean; | ||
| /** | ||
| * Subset of `names` that are imported using the `type` modifier. | ||
| * These symbols will be emitted as type-only imports in TypeScript. | ||
| * | ||
| * @example ["UserDTO"] | ||
| */ | ||
| typeNames?: ReadonlyArray<string>; | ||
| /** | ||
| * Whether the namespace import is type-only. | ||
| * | ||
| * @example true | ||
| */ | ||
| typeNamespaceImport?: boolean; | ||
| renderSymbols( | ||
| file: IFileOut, | ||
| project: IProject, | ||
| meta?: IProjectRenderMeta, | ||
| ): string; | ||
| } | ||
| declare class CodegenFile implements ICodegenFile { | ||
| path: string; | ||
| project: ICodegenProject; | ||
| meta: ICodegenFile['meta']; | ||
| private cache; | ||
| private renderSymbols; | ||
| private state; | ||
| id: number; | ||
| resolvedNames: ICodegenBiMap<number, string>; | ||
| constructor(path: string, project: ICodegenProject, meta?: ICodegenFile['meta']); | ||
| addExport(exp: ICodegenImport): void; | ||
| addImport(imp: ICodegenImport): void; | ||
| private addImportExport; | ||
| private addRenderSymbol; | ||
| addSymbol(symbol: ICodegenSymbolIn): ICodegenSymbolOut; | ||
| ensureSymbol(symbol: Partial<ICodegenSymbolIn> & Pick<Required<ICodegenSymbolIn>, 'selector'>): ICodegenSymbolOut; | ||
| get exports(): ReadonlyArray<ICodegenImport>; | ||
| getAllSymbols(): ReadonlyArray<Pick<ICodegenSymbolOut, 'name'>>; | ||
| private getImportExportKey; | ||
| getSymbolById(id: number): ICodegenSymbolOut | undefined; | ||
| hasContent(): boolean; | ||
| hasSymbol(id: number): boolean; | ||
| get imports(): ReadonlyArray<ICodegenImport>; | ||
| private mergeImportExportValues; | ||
| static pathToFilePath(source: string): string; | ||
| relativePathFromFile(file: Pick<ICodegenFile, 'path'>): string; | ||
| relativePathToFile(file: Pick<ICodegenFile, 'path'>): string; | ||
| selectSymbolAll(selector: ICodegenSymbolSelector): ReadonlyArray<ICodegenSymbolOut>; | ||
| selectSymbolFirst(selector: ICodegenSymbolSelector): ICodegenSymbolOut | undefined; | ||
| selectSymbolFirstOrThrow(selector: ICodegenSymbolSelector): ICodegenSymbolOut; | ||
| selectSymbolLast(selector: ICodegenSymbolSelector): ICodegenSymbolOut | undefined; | ||
| get symbols(): ReadonlyArray<ICodegenSymbolOut>; | ||
| updateSymbol(id: number, symbol: Partial<ICodegenSymbolOut>): ICodegenSymbolOut; | ||
| declare class SymbolRegistry implements ISymbolRegistry { | ||
| private _id; | ||
| private nodes; | ||
| private registerOrder; | ||
| private selectorToId; | ||
| private values; | ||
| get(symbolIdOrSelector: number | ISelector): ISymbolOut | undefined; | ||
| getValue(symbolId: number): unknown; | ||
| hasValue(symbolId: number): boolean; | ||
| get id(): number; | ||
| private idOrSelector; | ||
| reference(symbolIdOrSelector: number | ISelector): ISymbolOut; | ||
| register(symbol: ISymbolIn): ISymbolOut; | ||
| registered(): IterableIterator<ISymbolOut>; | ||
| setValue(symbolId: number, value: unknown): Map<number, unknown>; | ||
| } | ||
| declare class CodegenProject implements ICodegenProject { | ||
| private fileId; | ||
| private fileIdToFile; | ||
| private fileOrder; | ||
| private filePathToFileId; | ||
| private renderers; | ||
| private selectorToSymbolIds; | ||
| private symbolId; | ||
| private symbolIdToFileId; | ||
| addExport(fileOrPath: ICodegenFile | string, imp: ICodegenImport): void; | ||
| addImport(fileOrPath: ICodegenFile | string, imp: ICodegenImport): void; | ||
| addSymbol(fileOrPath: ICodegenFile | string, symbol: ICodegenSymbolIn): ICodegenSymbolOut; | ||
| createFile(path: string, meta?: Omit<ICodegenFile['meta'], 'renderer'> & { | ||
| /** | ||
| * Renderer to use to render this file. | ||
| */ | ||
| renderer?: ICodegenRenderer; | ||
| }): ICodegenFile; | ||
| ensureFile(fileOrPath: ICodegenFile | string): ICodegenFile; | ||
| private ensureRenderer; | ||
| get files(): ReadonlyArray<ICodegenFile>; | ||
| getAllSymbols(): ReadonlyArray<Pick<ICodegenSymbolOut, 'name'>>; | ||
| getFileByPath(path: string): ICodegenFile | undefined; | ||
| getFileBySymbolId(id: number): ICodegenFile | undefined; | ||
| private getFileRenderer; | ||
| getSymbolById(id: number): ICodegenSymbolOut | undefined; | ||
| incrementFileId(): number; | ||
| incrementSymbolId(): number; | ||
| registerSymbol(symbol: ICodegenSymbolOut, file: ICodegenFile): void; | ||
| render(meta?: ICodegenMeta): ReadonlyArray<ICodegenOutput>; | ||
| selectSymbolAll(selector: ICodegenSymbolSelector, file?: ICodegenFile): ReadonlyArray<ICodegenSymbolOut>; | ||
| selectSymbolFirst(selector: ICodegenSymbolSelector, file?: ICodegenFile): ICodegenSymbolOut | undefined; | ||
| selectSymbolFirstOrThrow(selector: ICodegenSymbolSelector, file?: ICodegenFile): ICodegenSymbolOut; | ||
| selectSymbolLast(selector: ICodegenSymbolSelector, file?: ICodegenFile): ICodegenSymbolOut | undefined; | ||
| declare class Project implements IProject { | ||
| private symbolIdToFileIds; | ||
| readonly defaultFileName: string; | ||
| readonly files: FileRegistry; | ||
| readonly fileName?: (name: string) => string; | ||
| readonly renderers: Record<string, IRenderer>; | ||
| readonly root: string; | ||
| readonly symbols: SymbolRegistry; | ||
| constructor({ defaultFileName, fileName, renderers, root, }: Pick<IProject, 'defaultFileName' | 'fileName' | 'renderers' | 'root'>); | ||
| private getRenderer; | ||
| private prepareFiles; | ||
| render(meta?: IProjectRenderMeta): ReadonlyArray<IOutput>; | ||
| symbolIdToFiles(symbolId: number): ReadonlyArray<IFileOut>; | ||
| private symbolToFileSelector; | ||
| } | ||
@@ -725,4 +595,4 @@ | ||
| */ | ||
| declare const replaceWrappedIds: (source: string, replacerFn: (symbolId: number) => string | undefined) => string; | ||
| declare const renderIds: (source: string, replacerFn: (symbolId: number) => string | undefined) => string; | ||
| export { BiMap, CodegenFile, CodegenProject, type ICodegenBiMap, type ICodegenFile, type ICodegenImport, type ICodegenMeta, type ICodegenOutput, type ICodegenProject, type ICodegenRenderer, type ICodegenSymbolIn, type ICodegenSymbolOut, type ICodegenSymbolSelector, replaceWrappedIds }; | ||
| export { type IBiMap as BiMap, type IBinding as Binding, type IFileOut as File, type IFileIn as FileIn, type IProject, type IOutput as Output, Project, type IProjectRenderMeta as ProjectRenderMeta, type IRenderer as Renderer, type ISelector as Selector, type ISymbolOut as Symbol, type ISymbolIn as SymbolIn, type ISymbolMeta as SymbolMeta, createBinding, mergeBindings, renderIds }; |
+338
-468
@@ -9,3 +9,3 @@ /** | ||
| */ | ||
| interface ICodegenBiMap<Key, Value> { | ||
| interface IBiMap<Key, Value> { | ||
| /** | ||
@@ -77,123 +77,89 @@ * Deletes a key and its associated value from the map. | ||
| declare class BiMap<Key, Value> implements ICodegenBiMap<Key, Value> { | ||
| private map; | ||
| private reverse; | ||
| delete(key: Key): boolean; | ||
| deleteValue(value: Value): boolean; | ||
| entries(): IterableIterator<[Key, Value]>; | ||
| get(key: Key): Value | undefined; | ||
| getKey(value: Value): Key | undefined; | ||
| hasKey(key: Key): boolean; | ||
| hasValue(value: Value): boolean; | ||
| keys(): IterableIterator<Key>; | ||
| set(key: Key, value: Value): this; | ||
| get size(): number; | ||
| values(): IterableIterator<Value>; | ||
| [Symbol.iterator](): IterableIterator<[Key, Value]>; | ||
| } | ||
| /** | ||
| * Arbitrary metadata passed to render functions. | ||
| * | ||
| * Implementors should extend this interface for their own needs. | ||
| */ | ||
| interface ICodegenMeta { | ||
| [key: string]: unknown; | ||
| } | ||
| interface ICodegenOutput { | ||
| interface IBinding { | ||
| /** | ||
| * The main content of the file to output. | ||
| * Optional aliasing map for named symbols. | ||
| * | ||
| * A raw string representing source code. | ||
| * Keys must be a subset of `names`, values are aliases. | ||
| * | ||
| * @example "function foo(): void {\n // implementation\n}\n" | ||
| * @example { User: "ImportedUser" } | ||
| */ | ||
| content: string; | ||
| aliases?: Record<string, string>; | ||
| /** | ||
| * Optional metadata or hints for the emitter, such as formatting options, | ||
| * source maps, or language-specific flags. | ||
| * Name of the default binding, if any. | ||
| * | ||
| * @example { format: "prettier", sourceMap: true } | ||
| * @example "React" | ||
| */ | ||
| meta: Record<string, unknown>; | ||
| defaultBinding?: string; | ||
| /** | ||
| * Logical output path (used for writing the file). | ||
| * Source file or external module from which symbols are imported. | ||
| * | ||
| * @example "models/user.ts" | ||
| * @example "./models/user" | ||
| * @example "node:path" | ||
| */ | ||
| path: string; | ||
| } | ||
| interface ICodegenRenderer { | ||
| from: string; | ||
| /** | ||
| * Optional: hook for renderer-level setup logic (e.g., formatting, config) | ||
| * Names of the symbols imported from the source. | ||
| * | ||
| * Must be non-empty unless `namespaceBinding` is true. | ||
| * All imported names, regardless of whether they are used as types or values. | ||
| * | ||
| * @example ["User", "UserDTO"] | ||
| */ | ||
| configure?(options: Record<string, unknown>): void; | ||
| names?: ReadonlyArray<string>; | ||
| /** | ||
| * Unique identifier for this renderer. | ||
| * If this import is a namespace import (e.g. `import * as ns from "..."`), | ||
| * this should be the namespace alias. Set to `true` if no alias is needed. | ||
| * | ||
| * @example "typescript" | ||
| * @example "utils" | ||
| * @example true | ||
| */ | ||
| id: string; | ||
| namespaceBinding?: boolean | string; | ||
| /** | ||
| * Returns printable data containing header and imports. | ||
| * Whether the default binding is type-only. | ||
| * | ||
| * @param file The file to render. | ||
| * @param meta Arbitrary metadata. | ||
| * @returns Printable string containing header and imports. | ||
| * @example true | ||
| */ | ||
| renderHeader(file: ICodegenFile, meta?: ICodegenMeta): string; | ||
| typeDefaultBinding?: boolean; | ||
| /** | ||
| * Returns printable data containing symbols and exports. | ||
| * Subset of `names` that are imported using the `type` modifier. | ||
| * These symbols will be emitted as type-only imports in TypeScript. | ||
| * | ||
| * @param file The file to render. | ||
| * @param meta Arbitrary metadata. | ||
| * @returns Printable string containing symbols and exports. | ||
| * @example ["UserDTO"] | ||
| */ | ||
| renderSymbols(file: ICodegenFile, meta?: ICodegenMeta): string; | ||
| typeNames?: ReadonlyArray<string>; | ||
| /** | ||
| * Function replacing symbols with resolved names. | ||
| * Whether the namespace binding is type-only. | ||
| * | ||
| * @returns String with replaced symbols. | ||
| * @example true | ||
| */ | ||
| replacerFn(args: { | ||
| file: ICodegenFile; | ||
| headless?: boolean; | ||
| scope?: 'file' | 'project'; | ||
| symbolId: number; | ||
| }): string | undefined; | ||
| typeNamespaceBinding?: boolean; | ||
| } | ||
| /** | ||
| * Selector array used to select symbols. It doesn't have to be | ||
| * unique, but in practice it might be desirable. | ||
| * Selector array used to reference resources. We don't enforce | ||
| * uniqueness, but in practice it's desirable. | ||
| * | ||
| * @example ["zod", "#/components/schemas/Foo"] | ||
| */ | ||
| type ICodegenSymbolSelector = ReadonlyArray<string>; | ||
| type ISelector = ReadonlyArray<string>; | ||
| interface ICodegenSymbolIn { | ||
| interface IFileIn { | ||
| /** | ||
| * Symbols can be **headed** or **headless**. | ||
| * File extension, if any. | ||
| */ | ||
| readonly extension?: string; | ||
| /** | ||
| * Indicates whether the file is external, meaning it is not generated | ||
| * as part of the project but is referenced (e.g., a module from | ||
| * node_modules). | ||
| * | ||
| * Headless symbols never render their `value`. Headed symbols render their | ||
| * `value` if defined. | ||
| * | ||
| * Symbols are rendered in the order they were registered as headed. | ||
| * | ||
| * Example 1: We register headless symbol `foo`, headed `bar`, and headed | ||
| * `foo`. The render order is [`bar`, `foo`]. | ||
| * | ||
| * Example 2: We register headed symbol `foo` and headed `bar`. The render | ||
| * order is [`foo`, `bar`]. | ||
| * | ||
| * Headless symbols can be used to claim a symbol or to represent imports | ||
| * or exports. | ||
| * | ||
| * @default false | ||
| * @example true | ||
| */ | ||
| headless?: boolean; | ||
| readonly external?: boolean; | ||
| /** | ||
| * The desired name for the symbol within its file. If there are multiple symbols | ||
| * Unique file ID. If one is not provided, it will be auto-generated. | ||
| */ | ||
| readonly id?: number; | ||
| /** | ||
| * The desired name for the file within the project. If there are multiple files | ||
| * with the same desired name, this might not end up being the actual name. | ||
@@ -203,5 +169,11 @@ * | ||
| */ | ||
| readonly name: string; | ||
| readonly name?: string; | ||
| /** | ||
| * Selector array used to select this symbol. It doesn't have to be | ||
| * Absolute logical output path for the file. | ||
| * | ||
| * @example "/src/models/user.ts" | ||
| */ | ||
| readonly path?: string; | ||
| /** | ||
| * Selector array used to select this file. It doesn't have to be | ||
| * unique, but in practice it might be desirable. | ||
@@ -211,330 +183,257 @@ * | ||
| */ | ||
| readonly selector?: ICodegenSymbolSelector; | ||
| /** | ||
| * Internal representation of the symbol (e.g. AST node, IR object, raw code). | ||
| * Used to generate output. If left undefined, this symbol becomes `headless`. | ||
| */ | ||
| readonly value?: unknown; | ||
| readonly selector?: ISelector; | ||
| } | ||
| interface ICodegenSymbolOut extends ICodegenSymbolIn { | ||
| interface IFileOut extends IFileIn { | ||
| /** | ||
| * The file this symbol is located in. | ||
| * Unique file ID. | ||
| */ | ||
| readonly file: ICodegenFile; | ||
| /** | ||
| * Unique symbol ID. | ||
| */ | ||
| readonly id: number; | ||
| /** | ||
| * Placeholder name for the symbol to be replaced later with the final value. | ||
| * | ||
| * @example "_heyapi_31_" | ||
| * Map holding resolved names for symbols in this file. | ||
| */ | ||
| readonly placeholder: string; | ||
| readonly resolvedNames: IBiMap<number, string>; | ||
| /** | ||
| * Updates this symbol. | ||
| * | ||
| * @param symbol The values to update. | ||
| * @returns The updated symbol. | ||
| * Symbols in this file, categorized by their role. | ||
| */ | ||
| readonly update: (symbol: Partial<ICodegenSymbolOut>) => ICodegenSymbolOut; | ||
| readonly symbols: { | ||
| /** | ||
| * Symbols declared in the body of this file. | ||
| */ | ||
| body: Array<number>; | ||
| /** | ||
| * Symbols re-exported from other files. | ||
| */ | ||
| exports: Array<number>; | ||
| /** | ||
| * Symbols imported from other files. | ||
| */ | ||
| imports: Array<number>; | ||
| }; | ||
| } | ||
| interface SelectorMethods { | ||
| interface IFileRegistry { | ||
| /** | ||
| * Retrieves symbols matching the selector. | ||
| * Get a file by its ID. | ||
| * | ||
| * @param selector The symbol selector to find. | ||
| * @param file Find symbols only in this file. | ||
| * @returns The array of all symbols matching the selector. | ||
| * @example | ||
| * const symbols = project.selectSymbolAll(["zod", "#/components/schemas/Foo"]); | ||
| * @param fileIdOrSelector File ID or selector to reference. | ||
| * @returns The file, or undefined if not found. | ||
| */ | ||
| selectSymbolAll( | ||
| selector: ICodegenSymbolSelector, | ||
| file?: ICodegenFile, | ||
| ): ReadonlyArray<ICodegenSymbolOut>; | ||
| get(fileIdOrSelector: number | ISelector): IFileOut | undefined; | ||
| /** | ||
| * Retrieves the first symbol from all symbols matching the selector. | ||
| * Returns the current file ID and increments it. | ||
| * | ||
| * @param selector The symbol selector to find. | ||
| * @param file Find symbols only in this file. | ||
| * @returns The symbol if found, or undefined otherwise. | ||
| * @example | ||
| * const symbol = project.selectSymbolFirst(["zod", "#/components/schemas/Foo"]); | ||
| * @returns File ID before being incremented | ||
| */ | ||
| selectSymbolFirst( | ||
| selector: ICodegenSymbolSelector, | ||
| file?: ICodegenFile, | ||
| ): ICodegenSymbolOut | undefined; | ||
| readonly id: number; | ||
| /** | ||
| * Retrieves the first symbol from all symbols matching the selector. | ||
| * Returns a file by ID or selector, registering it if it doesn't exist. | ||
| * | ||
| * @param selector The symbol selector to find. | ||
| * @param file Find symbols only in this file. | ||
| * @returns The symbol if found, or throw otherwise. | ||
| * @example | ||
| * const symbol = project.selectSymbolFirstOrThrow(["zod", "#/components/schemas/Foo"]); | ||
| * @param fileIdOrSelector File ID or selector to reference. | ||
| * @returns The referenced or newly registered file. | ||
| */ | ||
| selectSymbolFirstOrThrow( | ||
| selector: ICodegenSymbolSelector, | ||
| file?: ICodegenFile, | ||
| ): ICodegenSymbolOut; | ||
| reference(fileIdOrSelector: number | ISelector): IFileOut; | ||
| /** | ||
| * Retrieves the last symbol from all symbols matching the selector. | ||
| * Get all unregistered files in the order they were referenced. | ||
| * | ||
| * @param selector The symbol selector to find. | ||
| * @param file Find symbols only in this file. | ||
| * @returns The symbol if found, or undefined otherwise. | ||
| * @example | ||
| * const symbol = project.selectSymbolLast(["zod", "#/components/schemas/Foo"]); | ||
| * @returns Array of all unregistered files, in reference order. | ||
| */ | ||
| selectSymbolLast( | ||
| selector: ICodegenSymbolSelector, | ||
| file?: ICodegenFile, | ||
| ): ICodegenSymbolOut | undefined; | ||
| } | ||
| /** | ||
| * Represents a code generation project consisting of multiple codegen files. | ||
| * Manages imports, symbols, and output generation across the project. | ||
| */ | ||
| interface ICodegenProject extends SelectorMethods { | ||
| referenced(): IterableIterator<IFileOut>; | ||
| /** | ||
| * Adds an export declaration to a specific file, creating the file if it doesn't exist. | ||
| * Register a file globally. | ||
| * | ||
| * @param fileOrPath - File instance or file path where to add the export. | ||
| * @param imp - The export declaration to add. | ||
| * @example | ||
| * project.addExport("models/user.ts", { from: "lib", names: ["User"] }); | ||
| * Deduplicates identical files by ID. | ||
| * | ||
| * @param file File to register. | ||
| * @returns true if added, false if duplicate. | ||
| */ | ||
| addExport(fileOrPath: ICodegenFile | string, imp: ICodegenImport): void; | ||
| register(file: IFileIn): IFileOut; | ||
| /** | ||
| * Adds an import declaration to a specific file, creating the file if it doesn't exist. | ||
| * Get all files in the order they were registered. | ||
| * | ||
| * @param fileOrPath - File instance or file path where to add the import. | ||
| * @param imp - The import declaration to add. | ||
| * @example | ||
| * project.addImport("models/user.ts", { from: "lib", names: ["User"] }); | ||
| * @returns Array of all registered files, in insert order. | ||
| */ | ||
| addImport(fileOrPath: ICodegenFile | string, imp: ICodegenImport): void; | ||
| registered(): IterableIterator<IFileOut>; | ||
| } | ||
| /** | ||
| * Arbitrary metadata passed to the project's render function. | ||
| * | ||
| * Implementers should extend this interface for their own needs. | ||
| */ | ||
| interface IProjectRenderMeta { | ||
| [key: string]: unknown; | ||
| } | ||
| /** | ||
| * Additional metadata about the symbol. | ||
| * | ||
| * Implementers should extend this interface for their own needs. | ||
| */ | ||
| interface ISymbolMeta { | ||
| [key: string]: unknown; | ||
| } | ||
| interface ISymbolIn { | ||
| /** | ||
| * Adds a symbol to a specific file, creating the file if it doesn't exist. | ||
| * Array of file names (without extensions) from which this symbol is re-exported. | ||
| * | ||
| * @param fileOrPath - File instance or file path where to add the symbol. | ||
| * @param symbol - The symbol to add. | ||
| * @returns The inserted symbol. | ||
| * @example | ||
| * project.addSymbol("models/user.ts", { name: "User", value: tsNode }); | ||
| * @default undefined | ||
| */ | ||
| addSymbol( | ||
| fileOrPath: ICodegenFile | string, | ||
| symbol: ICodegenSymbolIn, | ||
| ): ICodegenSymbolOut; | ||
| readonly exportFrom?: ReadonlyArray<string>; | ||
| /** | ||
| * Creates a new codegen file with optional metadata and adds it to the project. | ||
| * Whether this symbol is exported from its own file. | ||
| * | ||
| * If a file with the same path already exists, it is returned instead. | ||
| * | ||
| * @param path - The logical output path for the file (e.g. "models/user.ts"). | ||
| * @param meta - Optional renderer and metadata to attach to the file (e.g. { isInternal: true }). | ||
| * @returns The newly created file instance. | ||
| * @example | ||
| * const file = project.createFile("models/user.ts", { isInternal: true }); | ||
| * @default false | ||
| */ | ||
| createFile( | ||
| path: string, | ||
| meta?: ICodegenFile['meta'] & { renderer?: ICodegenRenderer }, | ||
| ): ICodegenFile; | ||
| readonly exported?: boolean; | ||
| /** | ||
| * Ensures a codegen file exists and returns it. | ||
| * External module name if this symbol is imported from a module not managed | ||
| * by the project (e.g. "zod", "lodash"). | ||
| * | ||
| * If a file does not exist yet, it is created with minimal information. | ||
| * Later, it is expected `createFile()` will be called which will fill in | ||
| * the missing information such as optional metadata. | ||
| * | ||
| * @param fileOrPath - The logical output path for the file or the file itself. | ||
| * @returns The file instance. | ||
| * @example | ||
| * const file = project.ensureFile("models/user.ts"); | ||
| * @default undefined | ||
| */ | ||
| ensureFile(fileOrPath: ICodegenFile | string): ICodegenFile; | ||
| readonly external?: string; | ||
| /** | ||
| * Returns all files in the project in insertion order. | ||
| * Optional output strategy to override default behavior. | ||
| * | ||
| * @example | ||
| * project.files.forEach(file => console.log(file.path)); | ||
| * @returns The file path to output the symbol to, or undefined to fallback to default behavior. | ||
| */ | ||
| readonly files: ReadonlyArray<ICodegenFile>; | ||
| readonly getFilePath?: (symbol: ISymbolOut) => string | undefined; | ||
| /** | ||
| * Returns all symbols declared or imported across all files. | ||
| * | ||
| * @returns Flattened list of all codegen symbols. | ||
| * @example | ||
| * project.getAllSymbols().filter(s => s.name === "User"); | ||
| * Unique symbol ID. If one is not provided, it will be auto-generated. | ||
| */ | ||
| getAllSymbols(): ReadonlyArray<Pick<ICodegenSymbolOut, 'name'>>; | ||
| readonly id?: number; | ||
| /** | ||
| * Retrieves a file by its logical output path. | ||
| * Arbitrary metadata about the symbol. | ||
| * | ||
| * @param path - The file path to find. | ||
| * @returns The file if found, or undefined otherwise. | ||
| * @example | ||
| * const file = project.getFileByPath("models/user.ts"); | ||
| * @default undefined | ||
| */ | ||
| getFileByPath(path: string): ICodegenFile | undefined; | ||
| readonly meta?: ISymbolMeta & { | ||
| /** | ||
| * Kind of import if this symbol represents an import. | ||
| */ | ||
| importKind?: 'namespace' | 'default' | 'named'; | ||
| /** | ||
| * Kind of symbol. | ||
| */ | ||
| kind?: 'type'; | ||
| }; | ||
| /** | ||
| * Retrieves a file from symbol ID included in the file. | ||
| * The desired name for the symbol within its file. If there are multiple symbols | ||
| * with the same desired name, this might not end up being the actual name. | ||
| * | ||
| * @param id The symbol ID to find. | ||
| * @returns The file if found, undefined otherwise. | ||
| * @example | ||
| * const file = project.getFileBySymbolId(31); | ||
| * @example "UserModel" | ||
| */ | ||
| getFileBySymbolId(id: number): ICodegenFile | undefined; | ||
| readonly name?: string; | ||
| /** | ||
| * Retrieves a symbol from ID included in the project. | ||
| * Placeholder name for the symbol to be replaced later with the final value. | ||
| * | ||
| * @param id The symbol ID to find. | ||
| * @returns The symbol if found, undefined otherwise. | ||
| * @example | ||
| * const symbol = project.getSymbolById(31); | ||
| * @example "_heyapi_31_" | ||
| */ | ||
| getSymbolById(id: number): ICodegenSymbolOut | undefined; | ||
| readonly placeholder?: string; | ||
| /** | ||
| * Returns the current file ID and increments it. | ||
| * Selector array used to select this symbol. It doesn't have to be | ||
| * unique, but in practice it might be desirable. | ||
| * | ||
| * @returns File ID before being incremented | ||
| * @example ["zod", "#/components/schemas/Foo"] | ||
| */ | ||
| incrementFileId(): number; | ||
| readonly selector?: ISelector; | ||
| } | ||
| interface ISymbolOut extends ISymbolIn { | ||
| /** | ||
| * Returns the current symbol ID and increments it. | ||
| * | ||
| * @returns Symbol ID before being incremented | ||
| * Array of file names (without extensions) from which this symbol is re-exported. | ||
| */ | ||
| incrementSymbolId(): number; | ||
| readonly exportFrom: ReadonlyArray<string>; | ||
| /** | ||
| * Tracks added symbol across the project. | ||
| * | ||
| * @param symbol The symbol added to file. | ||
| * @param file The file containing the added symbol. | ||
| * Unique symbol ID. | ||
| */ | ||
| registerSymbol(symbol: ICodegenSymbolOut, file: ICodegenFile): void; | ||
| readonly id: number; | ||
| /** | ||
| * Produces output representations for all files in the project. | ||
| * Placeholder name for the symbol to be replaced later with the final value. | ||
| * | ||
| * @param meta Arbitrary metadata. | ||
| * @returns Array of outputs ready for writing or further processing. | ||
| * @example | ||
| * project.render().forEach(output => writeFile(output)); | ||
| * @example "_heyapi_31_" | ||
| */ | ||
| render(meta?: ICodegenMeta): ReadonlyArray<ICodegenOutput>; | ||
| readonly placeholder: string; | ||
| } | ||
| interface ICodegenFile extends SelectorMethods { | ||
| interface ISymbolRegistry { | ||
| /** | ||
| * Adds an export to this file. | ||
| * Get a symbol by its ID. | ||
| * | ||
| * This is also known as a re-export. | ||
| * | ||
| * @param exp The export to add | ||
| * @param symbolIdOrSelector Symbol ID or selector to reference. | ||
| * @returns The symbol, or undefined if not found. | ||
| */ | ||
| addExport(exp: ICodegenImport): void; | ||
| get(symbolIdOrSelector: number | ISelector): ISymbolOut | undefined; | ||
| /** | ||
| * Adds an import to this file. | ||
| * Returns the value associated with a symbol ID. | ||
| * | ||
| * @param imp The import to add | ||
| * @param symbolId Symbol ID. | ||
| * @return The value associated with the symbol ID, or undefined if not found. | ||
| */ | ||
| addImport(imp: ICodegenImport): void; | ||
| getValue(symbolId: number): unknown; | ||
| /** | ||
| * Adds a symbol defined by this file. | ||
| * Checks if the registry has a value associated with a symbol ID. | ||
| * | ||
| * @param symbol The symbol to add | ||
| * @param symbolId Symbol ID. | ||
| * @returns True if the registry has a value for symbol ID, false otherwise. | ||
| */ | ||
| addSymbol(symbol: ICodegenSymbolIn): ICodegenSymbolOut; | ||
| hasValue(symbolId: number): boolean; | ||
| /** | ||
| * Ensures a symbol for the given selector exists, so it can be | ||
| * safely used. | ||
| * Returns the current symbol ID and increments it. | ||
| * | ||
| * @param symbol The symbol to find. The required selector is used | ||
| * to match a symbol. If there's no match, we create a headless | ||
| * instance with the provided fields. | ||
| * @returns The symbol if it exists, headless instance otherwise. | ||
| * @returns Symbol ID before being incremented. | ||
| */ | ||
| ensureSymbol( | ||
| symbol: Partial<ICodegenSymbolIn> & | ||
| Pick<Required<ICodegenSymbolIn>, 'selector'>, | ||
| ): ICodegenSymbolOut; | ||
| readonly id: number; | ||
| /** | ||
| * Symbols exported from other files. | ||
| **/ | ||
| exports: ReadonlyArray<ICodegenImport>; | ||
| /** | ||
| * Returns all symbols used in this file (declared + imported). | ||
| * Returns a symbol by ID or selector, registering it if it doesn't exist. | ||
| * | ||
| * @returns List of all symbols used in this file | ||
| * @param symbolIdOrSelector Symbol ID or selector to reference. | ||
| * @returns The referenced or newly registered symbol. | ||
| */ | ||
| getAllSymbols(): ReadonlyArray<Pick<ICodegenSymbolOut, 'name'>>; | ||
| reference(symbolIdOrSelector: number | ISelector): ISymbolOut; | ||
| /** | ||
| * Finds a symbol by symbol ID. | ||
| * Register a symbol globally. | ||
| * | ||
| * @param id Symbol ID | ||
| * @returns The symbol if it exists, undefined otherwise. | ||
| * Deduplicates identical symbols by ID. | ||
| * | ||
| * @param symbol Symbol to register. | ||
| * @returns The registered symbol. | ||
| */ | ||
| getSymbolById(id: number): ICodegenSymbolOut | undefined; | ||
| register(symbol: ISymbolIn): ISymbolOut; | ||
| /** | ||
| * Checks if this file contains any content. | ||
| * Get all symbols in the order they were registered. | ||
| * | ||
| * This is used to determine whether we want to process the file further. | ||
| * By default, we consider only symbols and exports as content. | ||
| * | ||
| * @returns True if the file contains content | ||
| * @returns Array of all registered symbols, in insert order. | ||
| */ | ||
| hasContent(): boolean; | ||
| registered(): IterableIterator<ISymbolOut>; | ||
| /** | ||
| * Checks if this file defines a symbol with the given name. | ||
| * Sets a value for a symbol by its ID. | ||
| * | ||
| * @param id Symbol ID to check | ||
| * @returns True if the symbol is defined by this file | ||
| * @param symbolId Symbol ID. | ||
| * @param value The value to set. | ||
| * @returns void | ||
| */ | ||
| hasSymbol(id: number): boolean; | ||
| setValue(symbolId: number, value: unknown): Map<number, unknown>; | ||
| } | ||
| declare const createBinding: ({ file, modulePath, symbol, symbolFile, }: { | ||
| file: IFileOut; | ||
| modulePath: string; | ||
| symbol: ISymbolOut; | ||
| symbolFile: IFileOut; | ||
| }) => IBinding; | ||
| declare const mergeBindings: (target: IBinding, source: IBinding) => void; | ||
| interface IOutput { | ||
| /** | ||
| * File ID within the project. | ||
| * The main content of the file to output. | ||
| * | ||
| * A raw string representing source code. | ||
| * | ||
| * @example "function foo(): void {\n // implementation\n}\n" | ||
| */ | ||
| id: number; | ||
| content: string; | ||
| /** | ||
| * Symbols imported from other files. | ||
| **/ | ||
| imports: ReadonlyArray<ICodegenImport>; | ||
| /** | ||
| * Optional metadata about the file. | ||
| **/ | ||
| meta: { | ||
| /** | ||
| * Optional file extension. | ||
| * | ||
| * @example ".ts" | ||
| */ | ||
| extension?: '.ts' | (string & {}); | ||
| /** | ||
| * Optional logical module or package name. | ||
| * | ||
| * @example "models.user" | ||
| */ | ||
| moduleName?: string; | ||
| /** | ||
| * Optional path transformer. | ||
| * | ||
| * @param path Original file path passed to the constructor. | ||
| */ | ||
| path?: ((path: string) => string) | string; | ||
| /** | ||
| * Renderer ID. | ||
| * | ||
| * @example "typescript" | ||
| */ | ||
| renderer?: ICodegenRenderer['id']; | ||
| }; | ||
| /** | ||
| * Logical output path (used for writing the file). | ||
@@ -545,173 +444,144 @@ * | ||
| path: string; | ||
| } | ||
| declare class FileRegistry implements IFileRegistry { | ||
| private _id; | ||
| private referenceOrder; | ||
| private registerOrder; | ||
| private selectorToId; | ||
| private values; | ||
| get(fileIdOrSelector: number | ISelector): IFileOut | undefined; | ||
| get id(): number; | ||
| private idOrSelector; | ||
| reference(fileIdOrSelector: number | ISelector): IFileOut; | ||
| referenced(): IterableIterator<IFileOut>; | ||
| register(file: IFileIn): IFileOut; | ||
| registered(): IterableIterator<IFileOut>; | ||
| } | ||
| /** | ||
| * Represents a code generation project consisting of multiple codegen files. | ||
| * Manages imports, symbols, and output generation across the project. | ||
| */ | ||
| interface IProject { | ||
| /** | ||
| * Parent project this file belongs to. | ||
| */ | ||
| project: ICodegenProject; | ||
| /** | ||
| * Returns a relative path to this file from another file. | ||
| * The default file to assign symbols without a specific file selector. | ||
| * | ||
| * @param file The file from which we want the relative path to this file. | ||
| * @example "./this-file.ts" | ||
| * @default 'main' | ||
| */ | ||
| relativePathFromFile(file: Pick<ICodegenFile, 'path'>): string; | ||
| readonly defaultFileName?: string; | ||
| /** | ||
| * Returns a relative path to file from this file. | ||
| * Optional function to transform file names before they are used. | ||
| * | ||
| * @param file The file to which we want the relative path. | ||
| * @example "./another-file.ts" | ||
| * @param name The original file name. | ||
| * @returns The transformed file name. | ||
| */ | ||
| relativePathToFile(file: Pick<ICodegenFile, 'path'>): string; | ||
| readonly fileName?: (name: string) => string; | ||
| /** | ||
| * Map holding resolved names for symbols in this file. | ||
| * Centralized file registry for the project. | ||
| */ | ||
| resolvedNames: ICodegenBiMap<number, string>; | ||
| readonly files: IFileRegistry; | ||
| /** | ||
| * Top-level symbols declared in this file. | ||
| **/ | ||
| symbols: ReadonlyArray<ICodegenSymbolOut>; | ||
| /** | ||
| * Updates a symbol defined by this file. | ||
| * Produces output representations for all files in the project. | ||
| * | ||
| * @param id ID of symbol to update. | ||
| * @param symbol The values to update. | ||
| * @returns The updated symbol. | ||
| * @param meta Arbitrary metadata. | ||
| * @returns Array of outputs ready for writing or further processing. | ||
| * @example | ||
| * project.render().forEach(output => writeFile(output)); | ||
| */ | ||
| updateSymbol( | ||
| id: number, | ||
| symbol: Partial<ICodegenSymbolOut>, | ||
| ): ICodegenSymbolOut; | ||
| } | ||
| interface ICodegenImport { | ||
| render(meta?: IProjectRenderMeta): ReadonlyArray<IOutput>; | ||
| /** | ||
| * Optional aliasing map for imported symbols. | ||
| * Map of available renderers by file extension. | ||
| * | ||
| * Keys must be a subset of `names`, values are aliases. | ||
| * | ||
| * @example { User: "ImportedUser" } | ||
| * @example | ||
| * { | ||
| * ".ts": tsRenderer, | ||
| * ".js": jsRenderer, | ||
| * } | ||
| */ | ||
| aliases?: Record<string, string>; | ||
| readonly renderers: Record<string, IRenderer>; | ||
| /** | ||
| * Name of the default import, if any. | ||
| * | ||
| * @example "React" | ||
| * The absolute path to the root folder of the project. | ||
| */ | ||
| defaultImport?: string; | ||
| readonly root: string; | ||
| /** | ||
| * Source file or external module from which symbols are imported. | ||
| * Retrieves files that include symbol ID. The first file is the one | ||
| * where the symbol is declared, the rest are files that re-export it. | ||
| * | ||
| * For internal files, this should be a ICodegenFile instance to enable | ||
| * dynamic path computation. For external or system modules, use a string. | ||
| * | ||
| * @example "./models/user" | ||
| * @example "node:path" | ||
| * @param symbolId The symbol ID to find. | ||
| * @returns An array of files containing the symbol. | ||
| * @example | ||
| * const files = project.symbolIdToFiles(31); | ||
| * for (const file of files) { | ||
| * console.log(file.path); | ||
| * } | ||
| */ | ||
| from: ICodegenFile | string; | ||
| symbolIdToFiles(symbolId: number): ReadonlyArray<IFileOut>; | ||
| /** | ||
| * Names of the symbols imported from the source. | ||
| * | ||
| * Must be non-empty unless `isNamespaceImport` is true. | ||
| * All imported names, regardless of whether they are used as types or values. | ||
| * | ||
| * @example ["User", "UserDTO"] | ||
| * Centralized symbol registry for the project. | ||
| */ | ||
| names?: ReadonlyArray<string>; | ||
| readonly symbols: ISymbolRegistry; | ||
| } | ||
| interface IRenderer { | ||
| /** | ||
| * If this import is a namespace import (e.g. `import * as ns from "..."`), | ||
| * this should be the namespace alias. Set to `true` if no alias is needed. | ||
| * Renders content with replaced symbols. | ||
| * | ||
| * @example "utils" | ||
| * @example true | ||
| * @param content Content to render. | ||
| * @param file The file to render. | ||
| * @param project The parent project the file belongs to. | ||
| * @returns Rendered content. | ||
| */ | ||
| namespaceImport?: boolean | string; | ||
| renderFile( | ||
| content: string, | ||
| file: IFile, | ||
| project: IProject, | ||
| meta?: IProjectRenderMeta, | ||
| ): string; | ||
| /** | ||
| * Whether the default import is type-only. | ||
| * Returns printable data containing symbols and exports. | ||
| * | ||
| * @example true | ||
| * @param file The file to render. | ||
| * @param project The parent project the file belongs to. | ||
| * @param meta Arbitrary metadata. | ||
| * @returns Printable string containing symbols and exports. | ||
| */ | ||
| typeDefaultImport?: boolean; | ||
| /** | ||
| * Subset of `names` that are imported using the `type` modifier. | ||
| * These symbols will be emitted as type-only imports in TypeScript. | ||
| * | ||
| * @example ["UserDTO"] | ||
| */ | ||
| typeNames?: ReadonlyArray<string>; | ||
| /** | ||
| * Whether the namespace import is type-only. | ||
| * | ||
| * @example true | ||
| */ | ||
| typeNamespaceImport?: boolean; | ||
| renderSymbols( | ||
| file: IFileOut, | ||
| project: IProject, | ||
| meta?: IProjectRenderMeta, | ||
| ): string; | ||
| } | ||
| declare class CodegenFile implements ICodegenFile { | ||
| path: string; | ||
| project: ICodegenProject; | ||
| meta: ICodegenFile['meta']; | ||
| private cache; | ||
| private renderSymbols; | ||
| private state; | ||
| id: number; | ||
| resolvedNames: ICodegenBiMap<number, string>; | ||
| constructor(path: string, project: ICodegenProject, meta?: ICodegenFile['meta']); | ||
| addExport(exp: ICodegenImport): void; | ||
| addImport(imp: ICodegenImport): void; | ||
| private addImportExport; | ||
| private addRenderSymbol; | ||
| addSymbol(symbol: ICodegenSymbolIn): ICodegenSymbolOut; | ||
| ensureSymbol(symbol: Partial<ICodegenSymbolIn> & Pick<Required<ICodegenSymbolIn>, 'selector'>): ICodegenSymbolOut; | ||
| get exports(): ReadonlyArray<ICodegenImport>; | ||
| getAllSymbols(): ReadonlyArray<Pick<ICodegenSymbolOut, 'name'>>; | ||
| private getImportExportKey; | ||
| getSymbolById(id: number): ICodegenSymbolOut | undefined; | ||
| hasContent(): boolean; | ||
| hasSymbol(id: number): boolean; | ||
| get imports(): ReadonlyArray<ICodegenImport>; | ||
| private mergeImportExportValues; | ||
| static pathToFilePath(source: string): string; | ||
| relativePathFromFile(file: Pick<ICodegenFile, 'path'>): string; | ||
| relativePathToFile(file: Pick<ICodegenFile, 'path'>): string; | ||
| selectSymbolAll(selector: ICodegenSymbolSelector): ReadonlyArray<ICodegenSymbolOut>; | ||
| selectSymbolFirst(selector: ICodegenSymbolSelector): ICodegenSymbolOut | undefined; | ||
| selectSymbolFirstOrThrow(selector: ICodegenSymbolSelector): ICodegenSymbolOut; | ||
| selectSymbolLast(selector: ICodegenSymbolSelector): ICodegenSymbolOut | undefined; | ||
| get symbols(): ReadonlyArray<ICodegenSymbolOut>; | ||
| updateSymbol(id: number, symbol: Partial<ICodegenSymbolOut>): ICodegenSymbolOut; | ||
| declare class SymbolRegistry implements ISymbolRegistry { | ||
| private _id; | ||
| private nodes; | ||
| private registerOrder; | ||
| private selectorToId; | ||
| private values; | ||
| get(symbolIdOrSelector: number | ISelector): ISymbolOut | undefined; | ||
| getValue(symbolId: number): unknown; | ||
| hasValue(symbolId: number): boolean; | ||
| get id(): number; | ||
| private idOrSelector; | ||
| reference(symbolIdOrSelector: number | ISelector): ISymbolOut; | ||
| register(symbol: ISymbolIn): ISymbolOut; | ||
| registered(): IterableIterator<ISymbolOut>; | ||
| setValue(symbolId: number, value: unknown): Map<number, unknown>; | ||
| } | ||
| declare class CodegenProject implements ICodegenProject { | ||
| private fileId; | ||
| private fileIdToFile; | ||
| private fileOrder; | ||
| private filePathToFileId; | ||
| private renderers; | ||
| private selectorToSymbolIds; | ||
| private symbolId; | ||
| private symbolIdToFileId; | ||
| addExport(fileOrPath: ICodegenFile | string, imp: ICodegenImport): void; | ||
| addImport(fileOrPath: ICodegenFile | string, imp: ICodegenImport): void; | ||
| addSymbol(fileOrPath: ICodegenFile | string, symbol: ICodegenSymbolIn): ICodegenSymbolOut; | ||
| createFile(path: string, meta?: Omit<ICodegenFile['meta'], 'renderer'> & { | ||
| /** | ||
| * Renderer to use to render this file. | ||
| */ | ||
| renderer?: ICodegenRenderer; | ||
| }): ICodegenFile; | ||
| ensureFile(fileOrPath: ICodegenFile | string): ICodegenFile; | ||
| private ensureRenderer; | ||
| get files(): ReadonlyArray<ICodegenFile>; | ||
| getAllSymbols(): ReadonlyArray<Pick<ICodegenSymbolOut, 'name'>>; | ||
| getFileByPath(path: string): ICodegenFile | undefined; | ||
| getFileBySymbolId(id: number): ICodegenFile | undefined; | ||
| private getFileRenderer; | ||
| getSymbolById(id: number): ICodegenSymbolOut | undefined; | ||
| incrementFileId(): number; | ||
| incrementSymbolId(): number; | ||
| registerSymbol(symbol: ICodegenSymbolOut, file: ICodegenFile): void; | ||
| render(meta?: ICodegenMeta): ReadonlyArray<ICodegenOutput>; | ||
| selectSymbolAll(selector: ICodegenSymbolSelector, file?: ICodegenFile): ReadonlyArray<ICodegenSymbolOut>; | ||
| selectSymbolFirst(selector: ICodegenSymbolSelector, file?: ICodegenFile): ICodegenSymbolOut | undefined; | ||
| selectSymbolFirstOrThrow(selector: ICodegenSymbolSelector, file?: ICodegenFile): ICodegenSymbolOut; | ||
| selectSymbolLast(selector: ICodegenSymbolSelector, file?: ICodegenFile): ICodegenSymbolOut | undefined; | ||
| declare class Project implements IProject { | ||
| private symbolIdToFileIds; | ||
| readonly defaultFileName: string; | ||
| readonly files: FileRegistry; | ||
| readonly fileName?: (name: string) => string; | ||
| readonly renderers: Record<string, IRenderer>; | ||
| readonly root: string; | ||
| readonly symbols: SymbolRegistry; | ||
| constructor({ defaultFileName, fileName, renderers, root, }: Pick<IProject, 'defaultFileName' | 'fileName' | 'renderers' | 'root'>); | ||
| private getRenderer; | ||
| private prepareFiles; | ||
| render(meta?: IProjectRenderMeta): ReadonlyArray<IOutput>; | ||
| symbolIdToFiles(symbolId: number): ReadonlyArray<IFileOut>; | ||
| private symbolToFileSelector; | ||
| } | ||
@@ -725,4 +595,4 @@ | ||
| */ | ||
| declare const replaceWrappedIds: (source: string, replacerFn: (symbolId: number) => string | undefined) => string; | ||
| declare const renderIds: (source: string, replacerFn: (symbolId: number) => string | undefined) => string; | ||
| export { BiMap, CodegenFile, CodegenProject, type ICodegenBiMap, type ICodegenFile, type ICodegenImport, type ICodegenMeta, type ICodegenOutput, type ICodegenProject, type ICodegenRenderer, type ICodegenSymbolIn, type ICodegenSymbolOut, type ICodegenSymbolSelector, replaceWrappedIds }; | ||
| export { type IBiMap as BiMap, type IBinding as Binding, type IFileOut as File, type IFileIn as FileIn, type IProject, type IOutput as Output, Project, type IProjectRenderMeta as ProjectRenderMeta, type IRenderer as Renderer, type ISelector as Selector, type ISymbolOut as Symbol, type ISymbolIn as SymbolIn, type ISymbolMeta as SymbolMeta, createBinding, mergeBindings, renderIds }; |
+2
-2
@@ -1,3 +0,3 @@ | ||
| import {createRequire}from'module';import i from'node:path';createRequire(import.meta.url); | ||
| var d=class{map=new Map;reverse=new Map;delete(e){let t=this.map.get(e);return t!==void 0&&this.reverse.delete(t),this.map.delete(e)}deleteValue(e){let t=this.reverse.get(e);return t!==void 0&&this.map.delete(t),this.reverse.delete(e)}entries(){return this.map.entries()}get(e){return this.map.get(e)}getKey(e){return this.reverse.get(e)}hasKey(e){return this.map.has(e)}hasValue(e){return this.reverse.has(e)}keys(){return this.map.keys()}set(e,t){return this.map.set(e,t),this.reverse.set(t,e),this}get size(){return this.map.size}values(){return this.map.values()}[Symbol.iterator](){return this.map[Symbol.iterator]()}};var a=l=>`_heyapi_${l}_`,h=l=>l.slice(8,-1),u=()=>new RegExp(a("\\d+"),"g"),y=(l,e)=>l.replace(u(),t=>{let r=Number.parseInt(h(t),10);return e(r)||t});var m=class l{constructor(e,t,r={}){this.path=e;this.project=t;this.meta=r;let o=l.pathToFilePath(e);r.path&&(typeof r.path=="function"?o=r.path(o):o=r.path.replace("{{path}}",o)),this.id=t.incrementFileId(),this.path=o;}cache={};renderSymbols=[];state={exports:new Map,imports:new Map,symbols:new Map};id;resolvedNames=new d;addExport(e){return this.addImportExport(e,"exports")}addImport(e){return this.addImportExport(e,"imports")}addImportExport(e,t){let r=this.getImportExportKey(e),o=this.state[t].get(r);e.names||(e.names=[]);for(let s of e.typeNames??[])e.names.includes(s)||(e.names=[...e.names,s]);o?(this.mergeImportExportValues(o,e),this.state[t].set(r,o)):this.state[t].set(r,{...e}),this.cache[t]=void 0;}addRenderSymbol(e){this.renderSymbols.push(e),this.cache.symbols=void 0;}addSymbol(e){let t=this.project.incrementSymbolId(),r={...e,file:this,id:t,placeholder:a(String(t)),update:o=>this.updateSymbol(t,o)};return r.value===void 0?r.headless=true:r.headless||delete r.headless,this.state.symbols.set(t,r),this.project.registerSymbol(r,this),r.headless||this.addRenderSymbol(t),r}ensureSymbol(e){return this.selectSymbolFirst(e.selector)||this.addSymbol({name:"",...e})}get exports(){return this.cache.exports||(this.cache.exports=Array.from(this.state.exports.values())),this.cache.exports}getAllSymbols(){return [...this.symbols,...this.imports.flatMap(e=>(e.names??[]).map(t=>({name:e.aliases?.[t]??t}))),...this.exports.flatMap(e=>(e.names??[]).map(t=>({name:e.aliases?.[t]??t})))]}getImportExportKey(e){return typeof e.from=="string"?e.from:e.from.path}getSymbolById(e){return this.state.symbols.get(e)}hasContent(){return this.state.exports.size>0||this.symbols.length>0}hasSymbol(e){return this.state.symbols.has(e)}get imports(){return this.cache.imports||(this.cache.imports=Array.from(this.state.imports.values())),this.cache.imports}mergeImportExportValues(e,t){e.aliases={...e.aliases,...t.aliases},t.defaultImport!==void 0&&(e.defaultImport=t.defaultImport),e.names=[...new Set([...e.names??[],...t.names??[]])],t.namespaceImport!==void 0&&(e.namespaceImport=t.namespaceImport),t.typeDefaultImport!==void 0&&(e.typeDefaultImport=t.typeDefaultImport),e.typeNames=[...new Set([...e.typeNames??[],...t.typeNames??[]])],t.typeNamespaceImport!==void 0&&(e.typeNamespaceImport=t.typeNamespaceImport);}static pathToFilePath(e){return e.includes("/")?e.split("/").filter(Boolean).join(i.sep):e.includes("\\")?e.split("\\").filter(Boolean).join(i.sep):e.split(i.sep).filter(Boolean).join(i.sep)}relativePathFromFile(e){let t=i.posix.relative(i.posix.dirname(e.path),this.path);return t.startsWith(".")||(t=`./${t}`),t}relativePathToFile(e){let t=i.posix.relative(i.posix.dirname(this.path.split(i.sep).join("/")),e.path.split(i.sep).join("/"));return !t.startsWith(".")&&t!==""&&(t=`./${t}`),t}selectSymbolAll(e){return this.project.selectSymbolAll(e,this)}selectSymbolFirst(e){return this.project.selectSymbolFirst(e,this)}selectSymbolFirstOrThrow(e){return this.project.selectSymbolFirstOrThrow(e,this)}selectSymbolLast(e){return this.project.selectSymbolLast(e,this)}get symbols(){return this.cache.symbols||(this.cache.symbols=this.renderSymbols.map(e=>this.getSymbolById(e))),this.cache.symbols}updateSymbol(e,t){let r=this.getSymbolById(e);if(!r)throw new Error(`symbol with id ${e} not found`);let o={...r,...t,id:e};return (!o.headless||o.value)&&delete o.headless,this.state.symbols.set(o.id,o),r.headless&&!o.headless&&this.addRenderSymbol(e),o}};var I=class{fileId=0;fileIdToFile=new Map;fileOrder=[];filePathToFileId=new Map;renderers=new Map;selectorToSymbolIds=new Map;symbolId=0;symbolIdToFileId=new Map;addExport(e,t){this.ensureFile(e).addExport(t);}addImport(e,t){this.ensureFile(e).addImport(t);}addSymbol(e,t){return this.ensureFile(e).addSymbol(t)}createFile(e,t={}){let{renderer:r,...o}=t;r&&this.ensureRenderer(r);let s=this.getFileByPath(e);if(s)return r?.id&&r.id!==s.meta.renderer&&(s.meta.renderer=r.id),s;let n=new m(e,this,{...o,renderer:r?.id});return this.fileOrder.push(n),this.filePathToFileId.set(e,n.id),this.fileIdToFile.set(n.id,n),n}ensureFile(e){if(typeof e!="string")return e;let t=this.getFileByPath(e);return t||this.createFile(e)}ensureRenderer(e){return this.renderers.has(e.id)||this.renderers.set(e.id,e),this.renderers.get(e.id)}get files(){return [...this.fileOrder]}getAllSymbols(){return this.fileOrder.flatMap(e=>e.getAllSymbols())}getFileByPath(e){let t=this.filePathToFileId.get(e);return t!==void 0?this.fileIdToFile.get(t):void 0}getFileBySymbolId(e){let t=this.symbolIdToFileId.get(e);return t!==void 0?this.fileIdToFile.get(t):void 0}getFileRenderer(e){return e.meta.renderer?this.renderers.get(e.meta.renderer):void 0}getSymbolById(e){return this.getFileBySymbolId(e)?.getSymbolById(e)}incrementFileId(){return this.fileId++}incrementSymbolId(){return this.symbolId++}registerSymbol(e,t){if(this.symbolIdToFileId.set(e.id,t.id),e.selector){let r=JSON.stringify(e.selector),o=this.selectorToSymbolIds.get(r)??[];o.push(e.id),this.selectorToSymbolIds.set(r,o);}}render(e){let t=[];return this.fileOrder.forEach((r,o)=>{let s=this.getFileRenderer(r);s&&(t[o]={content:s.renderSymbols(r,e),meta:r.meta,path:`${r.path}${r.meta.extension??""}`});}),this.fileOrder.forEach((r,o)=>{let s=this.getFileRenderer(r);if(!s||!t[o])return;let n=s.renderHeader(r,e),p=y(t[o].content,g=>s.replacerFn({file:r,symbolId:g}));t[o].content=`${n}${p}`;}),t.filter(Boolean)}selectSymbolAll(e,t){let r=this.selectorToSymbolIds.get(JSON.stringify(e))??[],o=[];for(let s of r){let n=this.getFileBySymbolId(s);if(!n||t&&t!==n)continue;let p=n.getSymbolById(s);p&&o.push(p);}return o}selectSymbolFirst(e,t){return this.selectSymbolAll(e,t)[0]}selectSymbolFirstOrThrow(e,t){let r=this.selectSymbolFirst(e,t);if(!r)throw new Error(`symbol for selector not found: ${JSON.stringify(e)}`);return r}selectSymbolLast(e,t){let r=this.selectSymbolAll(e,t);return r[r.length-1]}};export{d as BiMap,m as CodegenFile,I as CodegenProject,y as replaceWrappedIds};//# sourceMappingURL=index.js.map | ||
| import {createRequire}from'module';import h from'node:path';createRequire(import.meta.url); | ||
| var I=({file:l,modulePath:e,symbol:t,symbolFile:r})=>{let i=[],s=[],n={aliases:{},from:e};if(t.meta?.importKind&&(t.meta.importKind==="default"?(n.defaultBinding=t.placeholder,t.meta.kind==="type"&&(n.typeDefaultBinding=true)):t.meta.importKind==="namespace"&&(n.namespaceBinding=t.placeholder,t.meta.kind==="type"&&(n.typeNamespaceBinding=true))),t.meta?.importKind==="named"||!i.length&&!n.defaultBinding&&!n.namespaceBinding){let o=t.placeholder,d=l.resolvedNames.get(t.id);if(d){let m=r.resolvedNames.get(t.id);m?m!==d&&(o=m,n.aliases[o]=d):t.name&&d!==t.name&&(o=t.name,n.aliases[o]=t.placeholder);}i.push(o),t.meta?.kind==="type"&&s.push(o);}for(let o of s)i.includes(o)||i.push(o);return n.names=i,n.typeNames=s,n},g=(l,e)=>{l.aliases={...l.aliases,...e.aliases},e.defaultBinding!==void 0&&(l.defaultBinding=e.defaultBinding),l.names=[...new Set([...l.names??[],...e.names??[]])],e.namespaceBinding!==void 0&&(l.namespaceBinding=e.namespaceBinding),e.typeDefaultBinding!==void 0&&(l.typeDefaultBinding=e.typeDefaultBinding),l.typeNames=[...new Set([...l.typeNames??[],...e.typeNames??[]])],e.typeNamespaceBinding!==void 0&&(l.typeNamespaceBinding=e.typeNamespaceBinding);};var a=class{map=new Map;reverse=new Map;delete(e){let t=this.map.get(e);return t!==void 0&&this.reverse.delete(t),this.map.delete(e)}deleteValue(e){let t=this.reverse.get(e);return t!==void 0&&this.map.delete(t),this.reverse.delete(e)}entries(){return this.map.entries()}get(e){return this.map.get(e)}getKey(e){return this.reverse.get(e)}hasKey(e){return this.map.has(e)}hasValue(e){return this.reverse.has(e)}keys(){return this.map.keys()}set(e,t){let r=this.map.get(e);r!==void 0&&r!==t&&this.reverse.delete(r);let i=this.reverse.get(t);return i!==void 0&&i!==e&&this.map.delete(i),this.map.set(e,t),this.reverse.set(t,e),this}get size(){return this.map.size}values(){return this.map.values()}[Symbol.iterator](){return this.map[Symbol.iterator]()}};var p=class{_id=0;referenceOrder=new Set;registerOrder=new Set;selectorToId=new Map;values=new Map;get(e){let t=this.idOrSelector(e);if(t.id!==void 0)return this.values.get(t.id);let r=t.selector!==void 0?JSON.stringify(t.selector):void 0;if(r){let i=this.selectorToId.get(r);if(i!==void 0)return this.values.get(i)}}get id(){return this._id++}idOrSelector(e){return typeof e=="number"?{id:e}:{selector:e}}reference(e){let t=this.idOrSelector(e);return this.register(t)}*referenced(){for(let e of this.referenceOrder.values())yield this.values.get(e);}register(e){if(e.id!==void 0){let n=this.values.get(e.id);if(!n)throw new Error(`File with ID ${e.id} not found. To register a new file, leave the ID undefined.`);return n}let t=Object.keys(e).some(n=>!["id","selector"].includes(n)),r,i=e.selector!==void 0?JSON.stringify(e.selector):void 0;if(i){let n=this.selectorToId.get(i);if(n!==void 0){if(r=this.values.get(n),!r)throw new Error(`File with ID ${n} not found. The selector ${i} matched an ID, but there was no result. This is likely an issue with the application logic.`);if(!t)return r}}let s=r?.id!==void 0?r.id:this.id;return r={...r,...e,id:s,resolvedNames:r?.resolvedNames??new a,symbols:r?.symbols??{body:[],exports:[],imports:[]}},this.values.set(s,r),t?this.registerOrder.add(s):this.referenceOrder.add(s),i&&this.selectorToId.set(i,s),r}*registered(){for(let e of this.registerOrder.values())yield this.values.get(e);}};var f=l=>`_heyapi_${l}_`,b=l=>l.slice(8,-1),S=()=>new RegExp(f("\\d+"),"g"),v=(l,e)=>l.replace(S(),t=>{let r=Number.parseInt(b(t),10);return e(r)||t});var u=class{_id=0;nodes=new Map;registerOrder=new Set;selectorToId=new Map;values=new Map;get(e){let t=this.idOrSelector(e);if(t.id!==void 0)return this.values.get(t.id);let r=t.selector!==void 0?JSON.stringify(t.selector):void 0;if(r){let i=this.selectorToId.get(r);if(i!==void 0)return this.values.get(i)}}getValue(e){return this.nodes.get(e)}hasValue(e){return this.nodes.has(e)}get id(){return this._id++}idOrSelector(e){return typeof e=="number"?{id:e}:{selector:e}}reference(e){let t=this.idOrSelector(e);return this.register(t)}register(e){if(e.id!==void 0){let o=this.values.get(e.id);if(!o)throw new Error(`Symbol with ID ${e.id} not found. To register a new symbol, leave the ID undefined.`);return o}let t=Object.keys(e).some(o=>!["id","selector"].includes(o)),r,i=e.selector!==void 0?JSON.stringify(e.selector):void 0;if(i){let o=this.selectorToId.get(i);if(o!==void 0){if(r=this.values.get(o),!r)throw new Error(`Symbol with ID ${o} not found. The selector ${i} matched an ID, but there was no result. This is likely an issue with the application logic.`);if(!t)return r}}let s=r?.id!==void 0?r.id:this.id,n=r?.exportFrom?[...r.exportFrom]:[];return e.exportFrom&&n.push(...e.exportFrom),r={...r,...e,exportFrom:n,id:s,placeholder:r?.placeholder??e.placeholder??f(String(s))},this.values.set(s,r),t&&this.registerOrder.add(s),i&&this.selectorToId.set(i,s),r}*registered(){for(let e of this.registerOrder.values())yield this.values.get(e);}setValue(e,t){return this.nodes.set(e,t)}};var y="@",c=class{symbolIdToFileIds=new Map;defaultFileName;files=new p;fileName;renderers={};root;symbols=new u;constructor({defaultFileName:e,fileName:t,renderers:r,root:i}){this.defaultFileName=e??"main",this.fileName=typeof t=="string"?()=>t:t,this.renderers=r,this.root=i;}getRenderer(e){return e.extension?this.renderers[e.extension]:void 0}prepareFiles(){for(let t of this.symbols.registered()){let r=this.symbolToFileSelector(t),i=this.files.reference(r);i.symbols.body.push(t.id);let s=this.symbolIdToFileIds.get(t.id)??new Set;s.add(i.id),this.symbolIdToFileIds.set(t.id,s);for(let n of t.exportFrom){let o=[n],d=this.files.reference(o);d.id!==i.id&&d.symbols.exports.push(t.id);}}for(let t of this.files.referenced()){if(!t.selector)continue;if(t.selector[0]===y){let s=t.selector[1];if(!s){this.files.register({external:true,selector:t.selector});continue}let n=h.extname(s);if(!n){this.files.register({external:true,path:s,selector:t.selector});continue}this.files.register({extension:n,external:true,path:s,selector:t.selector});continue}let r=t.selector.slice(0,-1),i=t.selector[t.selector.length-1];i=this.fileName?.(i)||i,this.files.register({extension:".ts",name:i,path:h.resolve(this.root,...r,`${i}.ts`),selector:t.selector});}}render(e){this.prepareFiles();let t=new Map;for(let r of this.files.registered()){if(r.external||!r.path)continue;let i=this.getRenderer(r);i&&t.set(r.id,{content:i.renderSymbols(r,this,e),path:r.path});}for(let[r,i]of t.entries()){let s=this.files.get(r),o=this.getRenderer(s).renderFile(i.content,s,this,e);o?t.set(s.id,{...i,content:o}):t.delete(s.id);}return Array.from(t.values())}symbolIdToFiles(e){let t=this.symbolIdToFileIds.get(e);return Array.from(t??[]).map(r=>this.files.get(r))}symbolToFileSelector(e){if(e.external)return [y,e.external];let t=e.getFilePath?.(e);return t?t.split("/"):[this.defaultFileName]}};export{c as Project,I as createBinding,g as mergeBindings,v as renderIds};//# sourceMappingURL=index.js.map | ||
| //# sourceMappingURL=index.js.map |
@@ -1,1 +0,1 @@ | ||
| {"version":3,"sources":["../src/bimap/bimap.ts","../src/renderers/renderer.ts","../src/files/file.ts","../src/project/project.ts"],"names":["BiMap","key","value","wrapId","symbolId","unwrapId","wrappedId","createPlaceholderRegExp","replaceWrappedIds","source","replacerFn","match","CodegenFile","_CodegenFile","path","project","meta","filePath","exp","imp","field","existing","typeName","id","symbol","inserted","values","name","target","file","relativePath","selector","updated","CodegenProject","fileOrPath","renderer","_meta","existingFile","fileId","ids","results","index","header","content","symbols","f"],"mappings":";AAEO,IAAMA,EAAN,KAA6D,CAC1D,IAAM,IAAI,GAAA,CACV,QAAU,IAAI,GAAA,CAEtB,OAAOC,CAAAA,CAAmB,CACxB,IAAMC,CAAAA,CAAQ,IAAA,CAAK,IAAI,GAAA,CAAID,CAAG,EAC9B,OAAIC,CAAAA,GAAU,MAAA,EACZ,IAAA,CAAK,QAAQ,MAAA,CAAOA,CAAK,EAEpB,IAAA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A,EAAQ,MAAA,CAAS,CAAC,CACnC,CACF","file":"index.js","sourcesContent":["import type { ICodegenBiMap } from './types';\n\nexport class BiMap<Key, Value> implements ICodegenBiMap<Key, Value> {\n private map = new Map<Key, Value>();\n private reverse = new Map<Value, Key>();\n\n delete(key: Key): boolean {\n const value = this.map.get(key);\n if (value !== undefined) {\n this.reverse.delete(value);\n }\n return this.map.delete(key);\n }\n\n deleteValue(value: Value): boolean {\n const key = this.reverse.get(value);\n if (key !== undefined) {\n this.map.delete(key);\n }\n return this.reverse.delete(value);\n }\n\n entries(): IterableIterator<[Key, Value]> {\n return this.map.entries();\n }\n\n get(key: Key): Value | undefined {\n return this.map.get(key);\n }\n\n getKey(value: Value): Key | undefined {\n return this.reverse.get(value);\n }\n\n hasKey(key: Key): boolean {\n return this.map.has(key);\n }\n\n hasValue(value: Value): boolean {\n return this.reverse.has(value);\n }\n\n keys(): IterableIterator<Key> {\n return this.map.keys();\n }\n\n set(key: Key, value: Value): this {\n this.map.set(key, value);\n this.reverse.set(value, key);\n return this;\n }\n\n get size(): number {\n return this.map.size;\n }\n\n values(): IterableIterator<Value> {\n return this.map.values();\n }\n\n [Symbol.iterator](): IterableIterator<[Key, Value]> {\n return this.map[Symbol.iterator]();\n }\n}\n","/**\n * Wraps an ID in namespace to avoid collisions when replacing it.\n *\n * @param symbolId Stringified symbol ID to use.\n * @returns The wrapped placeholder ID.\n */\nexport const wrapId = (symbolId: string): string => `_heyapi_${symbolId}_`;\n\n/**\n * Unwraps an ID from namespace.\n *\n * @param wrappedId The wrapped placeholder ID.\n * @returns Stringified ID to use.\n */\nconst unwrapId = (wrappedId: string): string =>\n wrappedId.slice('_heyapi_'.length, -1);\n\n/**\n * Returns a RegExp instance to match ID placeholders.\n *\n * @returns RegExp instance to match ID placeholders.\n */\nconst createPlaceholderRegExp = (): RegExp => new RegExp(wrapId('\\\\d+'), 'g');\n\n/**\n *\n * @param source The source string to replace.\n * @param replacerFn Accepts a symbol ID, returns resolved symbol name.\n * @returns The replaced source string.\n */\nexport const replaceWrappedIds = (\n source: string,\n replacerFn: (symbolId: number) => string | undefined,\n): string =>\n source.replace(createPlaceholderRegExp(), (match) => {\n const symbolId = Number.parseInt(unwrapId(match), 10);\n return replacerFn(symbolId) || match;\n });\n","import path from 'node:path';\n\nimport { BiMap } from '../bimap/bimap';\nimport type { ICodegenBiMap } from '../bimap/types';\nimport type { ICodegenImport } from '../imports/types';\nimport type { ICodegenProject } from '../project/types';\nimport { wrapId } from '../renderers/renderer';\nimport type {\n ICodegenSymbolIn,\n ICodegenSymbolOut,\n ICodegenSymbolSelector,\n} from '../symbols/types';\nimport type { ICodegenFile } from './types';\n\nexport class CodegenFile implements ICodegenFile {\n private cache: {\n exports?: ReadonlyArray<ICodegenImport>;\n imports?: ReadonlyArray<ICodegenImport>;\n symbols?: ReadonlyArray<ICodegenSymbolOut>;\n } = {};\n\n private renderSymbols: Array<number> = [];\n\n private state: {\n exports: Map<string, ICodegenImport>;\n imports: Map<string, ICodegenImport>;\n symbols: Map<number, ICodegenSymbolOut>;\n } = {\n exports: new Map(),\n imports: new Map(),\n symbols: new Map(),\n };\n\n id: number;\n resolvedNames: ICodegenBiMap<number, string> = new BiMap();\n\n constructor(\n public path: string,\n public project: ICodegenProject,\n public meta: ICodegenFile['meta'] = {},\n ) {\n let filePath = CodegenFile.pathToFilePath(path);\n if (meta.path) {\n if (typeof meta.path === 'function') {\n filePath = meta.path(filePath);\n } else {\n filePath = meta.path.replace('{{path}}', filePath);\n }\n }\n this.id = project.incrementFileId();\n this.path = filePath;\n }\n\n addExport(exp: ICodegenImport): void {\n return this.addImportExport(exp, 'exports');\n }\n\n addImport(imp: ICodegenImport): void {\n return this.addImportExport(imp, 'imports');\n }\n\n private addImportExport(\n value: ICodegenImport,\n field: 'exports' | 'imports',\n ): void {\n const key = this.getImportExportKey(value);\n const existing = this.state[field].get(key);\n // cast type names to names to allow for cleaner API,\n // otherwise users would have to define the same values twice\n if (!value.names) value.names = [];\n for (const typeName of value.typeNames ?? []) {\n if (!value.names.includes(typeName)) {\n value.names = [...value.names, typeName];\n }\n }\n if (existing) {\n this.mergeImportExportValues(existing, value);\n this.state[field].set(key, existing);\n } else {\n this.state[field].set(key, { ...value }); // clone to avoid mutation\n }\n this.cache[field] = undefined; // invalidate cache\n }\n\n private addRenderSymbol(id: number): void {\n this.renderSymbols.push(id);\n this.cache.symbols = undefined; // invalidate cache\n }\n\n addSymbol(symbol: ICodegenSymbolIn): ICodegenSymbolOut {\n const id = this.project.incrementSymbolId();\n const inserted: ICodegenSymbolOut = {\n ...symbol, // clone to avoid mutation\n file: this,\n id,\n placeholder: wrapId(String(id)),\n update: (values) => this.updateSymbol(id, values),\n };\n if (inserted.value === undefined) {\n // register symbols without value as headless\n inserted.headless = true;\n } else if (!inserted.headless) {\n delete inserted.headless;\n }\n this.state.symbols.set(id, inserted);\n this.project.registerSymbol(inserted, this);\n if (!inserted.headless) {\n this.addRenderSymbol(id);\n }\n return inserted;\n }\n\n ensureSymbol(\n symbol: Partial<ICodegenSymbolIn> &\n Pick<Required<ICodegenSymbolIn>, 'selector'>,\n ): ICodegenSymbolOut {\n return (\n this.selectSymbolFirst(symbol.selector) ||\n this.addSymbol({ name: '', ...symbol })\n );\n }\n\n get exports(): ReadonlyArray<ICodegenImport> {\n if (!this.cache.exports) {\n this.cache.exports = Array.from(this.state.exports.values());\n }\n return this.cache.exports;\n }\n\n getAllSymbols(): ReadonlyArray<Pick<ICodegenSymbolOut, 'name'>> {\n return [\n ...this.symbols,\n ...this.imports.flatMap((imp) =>\n (imp.names ?? []).map((name) => ({\n name: imp.aliases?.[name] ?? name,\n })),\n ),\n ...this.exports.flatMap((imp) =>\n (imp.names ?? []).map((name) => ({\n name: imp.aliases?.[name] ?? name,\n })),\n ),\n ];\n }\n\n private getImportExportKey(value: ICodegenImport): string {\n if (typeof value.from === 'string') {\n return value.from;\n }\n return value.from.path;\n }\n\n getSymbolById(id: number): ICodegenSymbolOut | undefined {\n return this.state.symbols.get(id);\n }\n\n hasContent(): boolean {\n return this.state.exports.size > 0 || this.symbols.length > 0;\n }\n\n hasSymbol(id: number): boolean {\n return this.state.symbols.has(id);\n }\n\n get imports(): ReadonlyArray<ICodegenImport> {\n if (!this.cache.imports) {\n this.cache.imports = Array.from(this.state.imports.values());\n }\n return this.cache.imports;\n }\n\n private mergeImportExportValues(\n target: ICodegenImport,\n source: ICodegenImport,\n ): void {\n target.aliases = { ...target.aliases, ...source.aliases };\n if (source.defaultImport !== undefined) {\n target.defaultImport = source.defaultImport;\n }\n target.names = [\n ...new Set([...(target.names ?? []), ...(source.names ?? [])]),\n ];\n if (source.namespaceImport !== undefined) {\n target.namespaceImport = source.namespaceImport;\n }\n if (source.typeDefaultImport !== undefined) {\n target.typeDefaultImport = source.typeDefaultImport;\n }\n target.typeNames = [\n ...new Set([...(target.typeNames ?? []), ...(source.typeNames ?? [])]),\n ];\n if (source.typeNamespaceImport !== undefined) {\n target.typeNamespaceImport = source.typeNamespaceImport;\n }\n }\n\n static pathToFilePath(source: string): string {\n if (source.includes('/')) {\n return source.split('/').filter(Boolean).join(path.sep);\n }\n if (source.includes('\\\\')) {\n return source.split('\\\\').filter(Boolean).join(path.sep);\n }\n return source.split(path.sep).filter(Boolean).join(path.sep);\n }\n\n relativePathFromFile(file: Pick<ICodegenFile, 'path'>): string {\n let relativePath = path.posix.relative(\n path.posix.dirname(file.path),\n this.path,\n );\n if (!relativePath.startsWith('.')) {\n relativePath = `./${relativePath}`;\n }\n return relativePath;\n }\n\n relativePathToFile(file: Pick<ICodegenFile, 'path'>): string {\n let relativePath = path.posix.relative(\n path.posix.dirname(\n this.path.split(path.sep).join('/'), // normalize to posix\n ),\n file.path.split(path.sep).join('/'), // normalize to posix\n );\n if (!relativePath.startsWith('.') && relativePath !== '') {\n relativePath = `./${relativePath}`;\n }\n return relativePath;\n }\n\n selectSymbolAll(\n selector: ICodegenSymbolSelector,\n ): ReadonlyArray<ICodegenSymbolOut> {\n return this.project.selectSymbolAll(selector, this);\n }\n\n selectSymbolFirst(\n selector: ICodegenSymbolSelector,\n ): ICodegenSymbolOut | undefined {\n return this.project.selectSymbolFirst(selector, this);\n }\n\n selectSymbolFirstOrThrow(\n selector: ICodegenSymbolSelector,\n ): ICodegenSymbolOut {\n return this.project.selectSymbolFirstOrThrow(selector, this);\n }\n\n selectSymbolLast(\n selector: ICodegenSymbolSelector,\n ): ICodegenSymbolOut | undefined {\n return this.project.selectSymbolLast(selector, this);\n }\n\n get symbols(): ReadonlyArray<ICodegenSymbolOut> {\n if (!this.cache.symbols) {\n this.cache.symbols = this.renderSymbols.map(\n (id) => this.getSymbolById(id)!,\n );\n }\n return this.cache.symbols;\n }\n\n updateSymbol(\n id: number,\n symbol: Partial<ICodegenSymbolOut>,\n ): ICodegenSymbolOut {\n const existing = this.getSymbolById(id);\n if (!existing) {\n throw new Error(`symbol with id ${id} not found`);\n }\n const updated: ICodegenSymbolOut = { ...existing, ...symbol, id };\n // symbols with value can't be headless, clear redundant flag otherwise\n if (!updated.headless || updated.value) {\n delete updated.headless;\n }\n this.state.symbols.set(updated.id, updated);\n if (existing.headless && !updated.headless) {\n this.addRenderSymbol(id);\n }\n return updated;\n }\n}\n","import { CodegenFile } from '../files/file';\nimport type { ICodegenFile } from '../files/types';\nimport type { ICodegenImport } from '../imports/types';\nimport type { ICodegenMeta } from '../meta/types';\nimport type { ICodegenOutput } from '../output/types';\nimport { replaceWrappedIds } from '../renderers/renderer';\nimport type { ICodegenRenderer } from '../renderers/types';\nimport type {\n ICodegenSymbolIn,\n ICodegenSymbolOut,\n ICodegenSymbolSelector,\n} from '../symbols/types';\nimport type { ICodegenProject } from './types';\n\nexport class CodegenProject implements ICodegenProject {\n private fileId: number = 0;\n private fileIdToFile: Map<number, ICodegenFile> = new Map();\n private fileOrder: Array<ICodegenFile> = [];\n private filePathToFileId: Map<string, number> = new Map();\n private renderers: Map<string, ICodegenRenderer> = new Map();\n private selectorToSymbolIds: Map<string, Array<number>> = new Map();\n private symbolId: number = 0;\n private symbolIdToFileId: Map<number, number> = new Map();\n\n addExport(fileOrPath: ICodegenFile | string, imp: ICodegenImport): void {\n const file = this.ensureFile(fileOrPath);\n file.addExport(imp);\n }\n\n addImport(fileOrPath: ICodegenFile | string, imp: ICodegenImport): void {\n const file = this.ensureFile(fileOrPath);\n file.addImport(imp);\n }\n\n addSymbol(\n fileOrPath: ICodegenFile | string,\n symbol: ICodegenSymbolIn,\n ): ICodegenSymbolOut {\n const file = this.ensureFile(fileOrPath);\n return file.addSymbol(symbol);\n }\n\n createFile(\n path: string,\n meta: Omit<ICodegenFile['meta'], 'renderer'> & {\n /**\n * Renderer to use to render this file.\n */\n renderer?: ICodegenRenderer;\n } = {},\n ): ICodegenFile {\n const { renderer, ..._meta } = meta;\n if (renderer) {\n this.ensureRenderer(renderer);\n }\n\n const existing = this.getFileByPath(path);\n if (existing) {\n // Whoever is creating the file will override the renderer\n if (renderer?.id && renderer.id !== existing.meta.renderer) {\n existing.meta.renderer = renderer.id;\n }\n return existing;\n }\n\n const file = new CodegenFile(path, this, {\n ..._meta,\n renderer: renderer?.id,\n });\n this.fileOrder.push(file);\n this.filePathToFileId.set(path, file.id);\n this.fileIdToFile.set(file.id, file);\n return file;\n }\n\n ensureFile(fileOrPath: ICodegenFile | string): ICodegenFile {\n if (typeof fileOrPath !== 'string') {\n return fileOrPath;\n }\n const existingFile = this.getFileByPath(fileOrPath);\n if (existingFile) {\n return existingFile;\n }\n return this.createFile(fileOrPath);\n }\n\n private ensureRenderer(renderer: ICodegenRenderer): ICodegenRenderer {\n if (!this.renderers.has(renderer.id)) {\n this.renderers.set(renderer.id, renderer);\n }\n return this.renderers.get(renderer.id)!;\n }\n\n get files(): ReadonlyArray<ICodegenFile> {\n return [...this.fileOrder];\n }\n\n getAllSymbols(): ReadonlyArray<Pick<ICodegenSymbolOut, 'name'>> {\n return this.fileOrder.flatMap((file) => file.getAllSymbols());\n }\n\n getFileByPath(path: string): ICodegenFile | undefined {\n const fileId = this.filePathToFileId.get(path);\n return fileId !== undefined ? this.fileIdToFile.get(fileId) : undefined;\n }\n\n getFileBySymbolId(id: number): ICodegenFile | undefined {\n const fileId = this.symbolIdToFileId.get(id);\n return fileId !== undefined ? this.fileIdToFile.get(fileId) : undefined;\n }\n\n private getFileRenderer(file: ICodegenFile): ICodegenRenderer | undefined {\n return file.meta.renderer\n ? this.renderers.get(file.meta.renderer)\n : undefined;\n }\n\n getSymbolById(id: number): ICodegenSymbolOut | undefined {\n const file = this.getFileBySymbolId(id);\n return file?.getSymbolById(id);\n }\n\n incrementFileId(): number {\n return this.fileId++;\n }\n\n incrementSymbolId(): number {\n return this.symbolId++;\n }\n\n registerSymbol(symbol: ICodegenSymbolOut, file: ICodegenFile): void {\n this.symbolIdToFileId.set(symbol.id, file.id);\n if (symbol.selector) {\n const selector = JSON.stringify(symbol.selector);\n const ids = this.selectorToSymbolIds.get(selector) ?? [];\n ids.push(symbol.id);\n this.selectorToSymbolIds.set(selector, ids);\n }\n }\n\n render(meta?: ICodegenMeta): ReadonlyArray<ICodegenOutput> {\n const results: Array<ICodegenOutput> = [];\n this.fileOrder.forEach((file, index) => {\n const renderer = this.getFileRenderer(file);\n if (!renderer) return;\n results[index] = {\n content: renderer.renderSymbols(file, meta),\n meta: file.meta,\n path: `${file.path}${file.meta.extension ?? ''}`,\n };\n });\n this.fileOrder.forEach((file, index) => {\n const renderer = this.getFileRenderer(file);\n if (!renderer || !results[index]) return;\n const header = renderer.renderHeader(file, meta);\n const content = replaceWrappedIds(results[index].content, (symbolId) =>\n renderer.replacerFn({ file, symbolId }),\n );\n results[index].content = `${header}${content}`;\n });\n return results.filter(Boolean);\n }\n\n selectSymbolAll(\n selector: ICodegenSymbolSelector,\n file?: ICodegenFile,\n ): ReadonlyArray<ICodegenSymbolOut> {\n const ids = this.selectorToSymbolIds.get(JSON.stringify(selector)) ?? [];\n const symbols: Array<ICodegenSymbolOut> = [];\n for (const id of ids) {\n const f = this.getFileBySymbolId(id);\n if (!f || (file && file !== f)) continue;\n const symbol = f.getSymbolById(id);\n if (!symbol) continue;\n symbols.push(symbol);\n }\n return symbols;\n }\n\n selectSymbolFirst(\n selector: ICodegenSymbolSelector,\n file?: ICodegenFile,\n ): ICodegenSymbolOut | undefined {\n const symbols = this.selectSymbolAll(selector, file);\n return symbols[0];\n }\n\n selectSymbolFirstOrThrow(\n selector: ICodegenSymbolSelector,\n file?: ICodegenFile,\n ): ICodegenSymbolOut {\n const symbol = this.selectSymbolFirst(selector, file);\n if (!symbol)\n throw new Error(\n `symbol for selector not found: ${JSON.stringify(selector)}`,\n );\n return symbol;\n }\n\n selectSymbolLast(\n selector: ICodegenSymbolSelector,\n file?: ICodegenFile,\n ): ICodegenSymbolOut | undefined {\n const symbols = this.selectSymbolAll(selector, file);\n return symbols[symbols.length - 1];\n }\n}\n"]} | ||
| {"version":3,"sources":["../src/bindings/utils.ts","../src/bimap/bimap.ts","../src/files/registry.ts","../src/renderer/utils.ts","../src/symbols/registry.ts","../src/project/project.ts"],"names":["createBinding","file","modulePath","symbol","symbolFile","names","typeNames","binding","name","fileResolvedName","symbolFileResolvedName","typeName","mergeBindings","target","source","BiMap","key","value","oldValue","oldKey","FileRegistry","fileIdOrSelector","selector","id","symbolIdOrSelector","result","hasOtherKeys","wrapId","symbolId","unwrapId","wrappedId","createPlaceholderRegExp","renderIds","replacerFn","match","SymbolRegistry","exportFrom","externalSourceSymbol","Project","defaultFileName","fileName","renderers","root","symbolIdToFileIds","exportSelector","exportFile","filePath","extension","path","dirs","meta","files","renderer","fileId","content","fileIds"],"mappings":";AAIO,IAAMA,CAAAA,CAAgB,CAAC,CAC5B,IAAA,CAAAC,EACA,UAAA,CAAAC,CAAAA,CACA,OAAAC,CAAAA,CACA,UAAA,CAAAC,CACF,CAAA,GAKgB,CACd,IAAMC,CAAAA,CAAuB,GACvBC,CAAAA,CAA2B,GAC3BC,CAAAA,CAAmE,CACvE,QAAS,EAAC,CACV,KAAML,CACR,CAAA,CAeA,GAd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AA,CAAK,EAAE,EAExB,CACA,OAAO,KAAA,CAAM,IAAA,CAAKkD,EAAM,MAAA,EAAQ,CAClC,CAEA,eAAA,CAAgBvB,EAA2C,CACzD,IAAM2B,EAAU,IAAA,CAAK,iBAAA,CAAkB,IAAI3B,CAAQ,CAAA,CACnD,OAAO,KAAA,CAAM,IAAA,CAAK2B,GAAW,EAAE,EAAE,GAAA,CAAKF,CAAAA,EAAW,KAAK,KAAA,CAAM,GAAA,CAAIA,CAAM,CAAE,CAC1E,CAEQ,oBAAA,CAAqBlD,CAAAA,CAA+B,CAC1D,GAAIA,CAAAA,CAAO,SACT,OAAO,CAACkC,EAAsBlC,CAAAA,CAAO,QAAQ,EAE/C,IAAM2C,CAAAA,CAAW3C,EAAO,WAAA,GAAcA,CAAM,EAC5C,OAAI2C,CAAAA,CACKA,EAAS,KAAA,CAAM,GAAG,EAEpB,CAAC,IAAA,CAAK,eAAe,CAC9B,CACF","file":"index.js","sourcesContent":["import type { IFileOut } from '../files/types';\nimport type { ISymbolOut } from '../symbols/types';\nimport type { IBinding } from './types';\n\nexport const createBinding = ({\n file,\n modulePath,\n symbol,\n symbolFile,\n}: {\n file: IFileOut;\n modulePath: string;\n symbol: ISymbolOut;\n symbolFile: IFileOut;\n}): IBinding => {\n const names: Array<string> = [];\n const typeNames: Array<string> = [];\n const binding: IBinding & Pick<Required<IBinding>, 'aliases' | 'from'> = {\n aliases: {},\n from: modulePath,\n };\n if (symbol.meta?.importKind) {\n if (symbol.meta.importKind === 'default') {\n binding.defaultBinding = symbol.placeholder;\n if (symbol.meta.kind === 'type') {\n binding.typeDefaultBinding = true;\n }\n } else if (symbol.meta.importKind === 'namespace') {\n binding.namespaceBinding = symbol.placeholder;\n if (symbol.meta.kind === 'type') {\n binding.typeNamespaceBinding = true;\n }\n }\n }\n // default to named binding\n if (\n symbol.meta?.importKind === 'named' ||\n (!names.length && !binding.defaultBinding && !binding.namespaceBinding)\n ) {\n let name = symbol.placeholder;\n const fileResolvedName = file.resolvedNames.get(symbol.id);\n if (fileResolvedName) {\n const symbolFileResolvedName = symbolFile.resolvedNames.get(symbol.id);\n if (symbolFileResolvedName) {\n if (symbolFileResolvedName !== fileResolvedName) {\n name = symbolFileResolvedName;\n binding.aliases[name] = fileResolvedName;\n }\n } else if (symbol.name && fileResolvedName !== symbol.name) {\n name = symbol.name;\n binding.aliases[name] = symbol.placeholder;\n }\n }\n names.push(name);\n if (symbol.meta?.kind === 'type') {\n typeNames.push(name);\n }\n }\n // cast type names to names to allow for cleaner API,\n // otherwise users would have to define the same values twice\n for (const typeName of typeNames) {\n if (!names.includes(typeName)) {\n names.push(typeName);\n }\n }\n binding.names = names;\n binding.typeNames = typeNames;\n return binding;\n};\n\nexport const mergeBindings = (target: IBinding, source: IBinding): void => {\n target.aliases = { ...target.aliases, ...source.aliases };\n if (source.defaultBinding !== undefined) {\n target.defaultBinding = source.defaultBinding;\n }\n target.names = [\n ...new Set([...(target.names ?? []), ...(source.names ?? [])]),\n ];\n if (source.namespaceBinding !== undefined) {\n target.namespaceBinding = source.namespaceBinding;\n }\n if (source.typeDefaultBinding !== undefined) {\n target.typeDefaultBinding = source.typeDefaultBinding;\n }\n target.typeNames = [\n ...new Set([...(target.typeNames ?? []), ...(source.typeNames ?? [])]),\n ];\n if (source.typeNamespaceBinding !== undefined) {\n target.typeNamespaceBinding = source.typeNamespaceBinding;\n }\n};\n","import type { IBiMap } from './types';\n\nexport class BiMap<Key, Value> implements IBiMap<Key, Value> {\n private map = new Map<Key, Value>();\n private reverse = new Map<Value, Key>();\n\n delete(key: Key): boolean {\n const value = this.map.get(key);\n if (value !== undefined) {\n this.reverse.delete(value);\n }\n return this.map.delete(key);\n }\n\n deleteValue(value: Value): boolean {\n const key = this.reverse.get(value);\n if (key !== undefined) {\n this.map.delete(key);\n }\n return this.reverse.delete(value);\n }\n\n entries(): IterableIterator<[Key, Value]> {\n return this.map.entries();\n }\n\n get(key: Key): Value | undefined {\n return this.map.get(key);\n }\n\n getKey(value: Value): Key | undefined {\n return this.reverse.get(value);\n }\n\n hasKey(key: Key): boolean {\n return this.map.has(key);\n }\n\n hasValue(value: Value): boolean {\n return this.reverse.has(value);\n }\n\n keys(): IterableIterator<Key> {\n return this.map.keys();\n }\n\n set(key: Key, value: Value): this {\n const oldValue = this.map.get(key);\n if (oldValue !== undefined && oldValue !== value) {\n this.reverse.delete(oldValue);\n }\n const oldKey = this.reverse.get(value);\n if (oldKey !== undefined && oldKey !== key) {\n this.map.delete(oldKey);\n }\n this.map.set(key, value);\n this.reverse.set(value, key);\n return this;\n }\n\n get size(): number {\n return this.map.size;\n }\n\n values(): IterableIterator<Value> {\n return this.map.values();\n }\n\n [Symbol.iterator](): IterableIterator<[Key, Value]> {\n return this.map[Symbol.iterator]();\n }\n}\n","import { BiMap } from '../bimap/bimap';\nimport type { ISelector } from '../selectors/types';\nimport type { IFileIn, IFileOut, IFileRegistry } from './types';\n\nexport class FileRegistry implements IFileRegistry {\n private _id: number = 0;\n private referenceOrder: Set<number> = new Set();\n private registerOrder: Set<number> = new Set();\n private selectorToId: Map<string, number> = new Map();\n private values: Map<number, IFileOut> = new Map();\n\n get(fileIdOrSelector: number | ISelector): IFileOut | undefined {\n const symbol = this.idOrSelector(fileIdOrSelector);\n\n if (symbol.id !== undefined) {\n return this.values.get(symbol.id);\n }\n\n const selector =\n symbol.selector !== undefined\n ? JSON.stringify(symbol.selector)\n : undefined;\n\n if (selector) {\n const id = this.selectorToId.get(selector);\n if (id !== undefined) {\n return this.values.get(id);\n }\n }\n\n return;\n }\n\n get id(): number {\n return this._id++;\n }\n\n private idOrSelector(\n symbolIdOrSelector: number | ISelector,\n ): Pick<IFileIn, 'id' | 'selector'> {\n return typeof symbolIdOrSelector === 'number'\n ? { id: symbolIdOrSelector }\n : { selector: symbolIdOrSelector };\n }\n\n reference(fileIdOrSelector: number | ISelector): IFileOut {\n const file = this.idOrSelector(fileIdOrSelector);\n return this.register(file);\n }\n\n *referenced(): IterableIterator<IFileOut> {\n for (const id of this.referenceOrder.values()) {\n yield this.values.get(id)!;\n }\n }\n\n register(file: IFileIn): IFileOut {\n if (file.id !== undefined) {\n const result = this.values.get(file.id);\n if (!result) {\n throw new Error(\n `File with ID ${file.id} not found. To register a new file, leave the ID undefined.`,\n );\n }\n return result;\n }\n\n const hasOtherKeys = Object.keys(file).some(\n (key) => !['id', 'selector'].includes(key),\n );\n\n let result: IFileOut | undefined;\n\n const selector =\n file.selector !== undefined ? JSON.stringify(file.selector) : undefined;\n if (selector) {\n const id = this.selectorToId.get(selector);\n if (id !== undefined) {\n result = this.values.get(id);\n if (!result) {\n throw new Error(\n `File with ID ${id} not found. The selector ${selector} matched an ID, but there was no result. This is likely an issue with the application logic.`,\n );\n }\n if (!hasOtherKeys) {\n return result;\n }\n }\n }\n\n const id = result?.id !== undefined ? result.id : this.id;\n result = {\n ...result,\n ...file, // clone to avoid mutation\n id,\n resolvedNames: result?.resolvedNames ?? new BiMap(),\n symbols: result?.symbols ?? {\n body: [],\n exports: [],\n imports: [],\n },\n };\n this.values.set(id, result);\n\n if (hasOtherKeys) {\n this.registerOrder.add(id);\n } else {\n this.referenceOrder.add(id);\n }\n\n if (selector) {\n this.selectorToId.set(selector, id);\n }\n\n return result;\n }\n\n *registered(): IterableIterator<IFileOut> {\n for (const id of this.registerOrder.values()) {\n yield this.values.get(id)!;\n }\n }\n}\n","/**\n * Wraps an ID in namespace to avoid collisions when replacing it.\n *\n * @param symbolId Stringified symbol ID to use.\n * @returns The wrapped placeholder ID.\n */\nexport const wrapId = (symbolId: string): string => `_heyapi_${symbolId}_`;\n\n/**\n * Unwraps an ID from namespace.\n *\n * @param wrappedId The wrapped placeholder ID.\n * @returns Stringified ID to use.\n */\nconst unwrapId = (wrappedId: string): string =>\n wrappedId.slice('_heyapi_'.length, -1);\n\n/**\n * Returns a RegExp instance to match ID placeholders.\n *\n * @returns RegExp instance to match ID placeholders.\n */\nconst createPlaceholderRegExp = (): RegExp => new RegExp(wrapId('\\\\d+'), 'g');\n\n/**\n *\n * @param source The source string to replace.\n * @param replacerFn Accepts a symbol ID, returns resolved symbol name.\n * @returns The replaced source string.\n */\nexport const renderIds = (\n source: string,\n replacerFn: (symbolId: number) => string | undefined,\n): string =>\n source.replace(createPlaceholderRegExp(), (match) => {\n const symbolId = Number.parseInt(unwrapId(match), 10);\n return replacerFn(symbolId) || match;\n });\n","import { wrapId } from '../renderer/utils';\nimport type { ISelector } from '../selectors/types';\nimport type { ISymbolIn, ISymbolOut, ISymbolRegistry } from './types';\n\nexport class SymbolRegistry implements ISymbolRegistry {\n private _id: number = 0;\n private nodes: Map<number, unknown> = new Map();\n private registerOrder: Set<number> = new Set();\n private selectorToId: Map<string, number> = new Map();\n private values: Map<number, ISymbolOut> = new Map();\n\n get(symbolIdOrSelector: number | ISelector): ISymbolOut | undefined {\n const symbol = this.idOrSelector(symbolIdOrSelector);\n\n if (symbol.id !== undefined) {\n return this.values.get(symbol.id);\n }\n\n const selector =\n symbol.selector !== undefined\n ? JSON.stringify(symbol.selector)\n : undefined;\n\n if (selector) {\n const id = this.selectorToId.get(selector);\n if (id !== undefined) {\n return this.values.get(id);\n }\n }\n\n return;\n }\n\n getValue(symbolId: number): unknown {\n return this.nodes.get(symbolId);\n }\n\n hasValue(symbolId: number): boolean {\n return this.nodes.has(symbolId);\n }\n\n get id(): number {\n return this._id++;\n }\n\n private idOrSelector(\n symbolIdOrSelector: number | ISelector,\n ): Pick<ISymbolIn, 'id' | 'selector'> {\n return typeof symbolIdOrSelector === 'number'\n ? { id: symbolIdOrSelector }\n : { selector: symbolIdOrSelector };\n }\n\n reference(symbolIdOrSelector: number | ISelector): ISymbolOut {\n const symbol = this.idOrSelector(symbolIdOrSelector);\n return this.register(symbol);\n }\n\n register(symbol: ISymbolIn): ISymbolOut {\n if (symbol.id !== undefined) {\n const result = this.values.get(symbol.id);\n if (!result) {\n throw new Error(\n `Symbol with ID ${symbol.id} not found. To register a new symbol, leave the ID undefined.`,\n );\n }\n return result;\n }\n\n const hasOtherKeys = Object.keys(symbol).some(\n (key) => !['id', 'selector'].includes(key),\n );\n\n let result: ISymbolOut | undefined;\n\n const selector =\n symbol.selector !== undefined\n ? JSON.stringify(symbol.selector)\n : undefined;\n if (selector) {\n const id = this.selectorToId.get(selector);\n if (id !== undefined) {\n result = this.values.get(id);\n if (!result) {\n throw new Error(\n `Symbol with ID ${id} not found. The selector ${selector} matched an ID, but there was no result. This is likely an issue with the application logic.`,\n );\n }\n if (!hasOtherKeys) {\n return result;\n }\n }\n }\n\n const id = result?.id !== undefined ? result.id : this.id;\n const exportFrom: Array<string> = result?.exportFrom\n ? [...result.exportFrom]\n : [];\n if (symbol.exportFrom) {\n exportFrom.push(...symbol.exportFrom);\n }\n result = {\n ...result,\n ...symbol, // clone to avoid mutation\n exportFrom,\n id,\n placeholder:\n result?.placeholder ?? symbol.placeholder ?? wrapId(String(id)),\n };\n this.values.set(id, result);\n\n if (hasOtherKeys) {\n this.registerOrder.add(id);\n }\n\n if (selector) {\n this.selectorToId.set(selector, id);\n }\n\n return result;\n }\n\n *registered(): IterableIterator<ISymbolOut> {\n for (const id of this.registerOrder.values()) {\n yield this.values.get(id)!;\n }\n }\n\n setValue(symbolId: number, value: unknown): Map<number, unknown> {\n return this.nodes.set(symbolId, value);\n }\n}\n","import path from 'node:path';\n\nimport type { IProjectRenderMeta } from '../extensions/types';\nimport { FileRegistry } from '../files/registry';\nimport type { IFileOut } from '../files/types';\nimport type { IOutput } from '../output/types';\nimport type { IRenderer } from '../renderer/types';\nimport type { ISelector } from '../selectors/types';\nimport { SymbolRegistry } from '../symbols/registry';\nimport type { ISymbolOut } from '../symbols/types';\nimport type { IProject } from './types';\n\nconst externalSourceSymbol = '@';\n\nexport class Project implements IProject {\n private symbolIdToFileIds: Map<number, Set<number>> = new Map();\n\n readonly defaultFileName: string;\n readonly files = new FileRegistry();\n readonly fileName?: (name: string) => string;\n readonly renderers: Record<string, IRenderer> = {};\n readonly root: string;\n readonly symbols = new SymbolRegistry();\n\n constructor({\n defaultFileName,\n fileName,\n renderers,\n root,\n }: Pick<IProject, 'defaultFileName' | 'fileName' | 'renderers' | 'root'>) {\n this.defaultFileName = defaultFileName ?? 'main';\n this.fileName = typeof fileName === 'string' ? () => fileName : fileName;\n this.renderers = renderers;\n this.root = root;\n }\n\n private getRenderer(file: IFileOut): IRenderer | undefined {\n return file.extension ? this.renderers[file.extension] : undefined;\n }\n\n private prepareFiles(): void {\n // TODO: infer extension from symbols\n const extension = '.ts';\n for (const symbol of this.symbols.registered()) {\n const selector = this.symbolToFileSelector(symbol);\n const file = this.files.reference(selector);\n file.symbols.body.push(symbol.id);\n // update symbol->files map\n const symbolIdToFileIds =\n this.symbolIdToFileIds.get(symbol.id) ?? new Set();\n symbolIdToFileIds.add(file.id);\n this.symbolIdToFileIds.set(symbol.id, symbolIdToFileIds);\n // update re-exports\n for (const exportFrom of symbol.exportFrom) {\n const exportSelector = [exportFrom];\n const exportFile = this.files.reference(exportSelector);\n if (exportFile.id !== file.id) {\n exportFile.symbols.exports.push(symbol.id);\n }\n }\n }\n for (const file of this.files.referenced()) {\n if (!file.selector) continue;\n if (file.selector[0] === externalSourceSymbol) {\n const filePath = file.selector[1];\n if (!filePath) {\n this.files.register({\n external: true,\n selector: file.selector,\n });\n continue;\n }\n const extension = path.extname(filePath);\n if (!extension) {\n this.files.register({\n external: true,\n path: filePath,\n selector: file.selector,\n });\n continue;\n }\n this.files.register({\n extension,\n external: true,\n path: filePath,\n selector: file.selector,\n });\n continue;\n }\n const dirs = file.selector.slice(0, -1);\n let name = file.selector[file.selector.length - 1]!;\n name = this.fileName?.(name) || name;\n this.files.register({\n extension,\n name,\n path: path.resolve(this.root, ...dirs, `${name}${extension}`),\n selector: file.selector,\n });\n }\n\n // TODO: track symbol dependencies and inject imports into files\n // based on symbol references so the render step can just render\n }\n\n render(meta?: IProjectRenderMeta): ReadonlyArray<IOutput> {\n this.prepareFiles();\n const files: Map<number, IOutput> = new Map();\n for (const file of this.files.registered()) {\n if (file.external || !file.path) continue;\n const renderer = this.getRenderer(file);\n if (!renderer) continue;\n files.set(file.id, {\n content: renderer.renderSymbols(file, this, meta),\n path: file.path,\n });\n }\n for (const [fileId, value] of files.entries()) {\n const file = this.files.get(fileId)!;\n const renderer = this.getRenderer(file)!;\n const content = renderer.renderFile(value.content, file, this, meta);\n if (content) {\n files.set(file.id, { ...value, content });\n } else {\n files.delete(file.id);\n }\n }\n return Array.from(files.values());\n }\n\n symbolIdToFiles(symbolId: number): ReadonlyArray<IFileOut> {\n const fileIds = this.symbolIdToFileIds.get(symbolId);\n return Array.from(fileIds ?? []).map((fileId) => this.files.get(fileId)!);\n }\n\n private symbolToFileSelector(symbol: ISymbolOut): ISelector {\n if (symbol.external) {\n return [externalSourceSymbol, symbol.external];\n }\n const filePath = symbol.getFilePath?.(symbol);\n if (filePath) {\n return filePath.split('/');\n }\n return [this.defaultFileName];\n }\n}\n"]} |
+3
-2
| { | ||
| "name": "@hey-api/codegen-core", | ||
| "version": "0.1.0", | ||
| "version": "0.2.0", | ||
| "description": "🧱 TypeScript framework for generating structured, multi-file source code from abstract syntax trees.", | ||
@@ -46,3 +46,4 @@ "homepage": "https://heyapi.dev/", | ||
| "dist", | ||
| "LICENSE.md" | ||
| "LICENSE.md", | ||
| "README.md" | ||
| ], | ||
@@ -49,0 +50,0 @@ "engines": { |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
115787
-8.44%630
-17.54%8
33.33%