@hey-api/codegen-core 0.3.2 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts DELETED
@@ -1,628 +0,0 @@
1
- //#region src/bimap/types.d.ts
2
- /**
3
- * Bi-directional map interface.
4
- *
5
- * Keys map to values and values map back to keys.
6
- *
7
- * @template Key Type of the map keys
8
- * @template Value Type of the map values
9
- */
10
- interface IBiMap<Key, Value> {
11
- /**
12
- * Deletes a key and its associated value from the map.
13
- *
14
- * @param key The key to delete.
15
- */
16
- delete(key: Key): boolean;
17
- /**
18
- * Deletes a value and its associated key from the map.
19
- *
20
- * @param value The value to delete.
21
- */
22
- deleteValue(value: Value): boolean;
23
- /**
24
- * Returns an iterator of [key, value] pairs.
25
- */
26
- entries(): IterableIterator<[Key, Value]>;
27
- /**
28
- * Gets the value associated with a key.
29
- *
30
- * @param key The key to look up.
31
- */
32
- get(key: Key): Value | undefined;
33
- /**
34
- * Gets the key associated with a value.
35
- *
36
- * @param value The value to look up.
37
- */
38
- getKey(value: Value): Key | undefined;
39
- /**
40
- * Checks if a key exists in the map.
41
- *
42
- * @param key The key to check.
43
- */
44
- hasKey(key: Key): boolean;
45
- /**
46
- * Checks if a value exists in the map.
47
- *
48
- * @param value The value to check.
49
- */
50
- hasValue(value: Value): boolean;
51
- /**
52
- * Returns an iterator of keys.
53
- */
54
- keys(): IterableIterator<Key>;
55
- /**
56
- * Sets a key-value pair in the map.
57
- *
58
- * @param key The key.
59
- * @param value The value.
60
- * @returns This instance for chaining.
61
- */
62
- set(key: Key, value: Value): this;
63
- /**
64
- * Number of key-value pairs in the map.
65
- */
66
- readonly size: number;
67
- /**
68
- * Returns an iterator of values.
69
- */
70
- values(): IterableIterator<Value>;
71
- /**
72
- * Enables iteration with `for...of`.
73
- */
74
- [Symbol.iterator](): IterableIterator<[Key, Value]>;
75
- }
76
- //#endregion
77
- //#region src/bindings/types.d.ts
78
- interface IBinding {
79
- /**
80
- * Optional aliasing map for named symbols.
81
- *
82
- * Keys must be a subset of `names`, values are aliases.
83
- *
84
- * @example { User: "ImportedUser" }
85
- */
86
- aliases?: Record<string, string>;
87
- /**
88
- * Name of the default binding, if any.
89
- *
90
- * @example "React"
91
- */
92
- defaultBinding?: string;
93
- /**
94
- * Source file or external module from which symbols are imported.
95
- *
96
- * @example "./models/user"
97
- * @example "node:path"
98
- */
99
- from: string;
100
- /**
101
- * Names of the symbols imported from the source.
102
- *
103
- * Must be non-empty unless `namespaceBinding` is true.
104
- * All imported names, regardless of whether they are used as types or values.
105
- *
106
- * @example ["User", "UserDTO"]
107
- */
108
- names?: ReadonlyArray<string>;
109
- /**
110
- * If this import is a namespace import (e.g. `import * as ns from "..."`),
111
- * this should be the namespace alias. Set to `true` if no alias is needed.
112
- *
113
- * @example "utils"
114
- * @example true
115
- */
116
- namespaceBinding?: boolean | string;
117
- /**
118
- * Whether the default binding is type-only.
119
- *
120
- * @example true
121
- */
122
- typeDefaultBinding?: boolean;
123
- /**
124
- * Subset of `names` that are imported using the `type` modifier.
125
- * These symbols will be emitted as type-only imports in TypeScript.
126
- *
127
- * @example ["UserDTO"]
128
- */
129
- typeNames?: ReadonlyArray<string>;
130
- /**
131
- * Whether the namespace binding is type-only.
132
- *
133
- * @example true
134
- */
135
- typeNamespaceBinding?: boolean;
136
- }
137
- //#endregion
138
- //#region src/files/types.d.ts
139
- /**
140
- * Selector array used to reference files.
141
- *
142
- * @example ["foo", "bar"]
143
- */
144
- type IFileSelector = ReadonlyArray<string>;
145
- type IFileIdentifier = number | IFileSelector;
146
- interface IFileIn {
147
- /**
148
- * File extension, if any.
149
- */
150
- readonly extension?: string;
151
- /**
152
- * Indicates whether the file is external, meaning it is not generated
153
- * as part of the project but is referenced (e.g., a module from
154
- * node_modules).
155
- *
156
- * @example true
157
- */
158
- readonly external?: boolean;
159
- /**
160
- * Unique file ID. If one is not provided, it will be auto-generated.
161
- */
162
- readonly id?: number;
163
- /**
164
- * The desired name for the file within the project. If there are multiple files
165
- * with the same desired name, this might not end up being the actual name.
166
- *
167
- * @example "UserModel"
168
- */
169
- readonly name?: string;
170
- /**
171
- * Absolute logical output path for the file.
172
- *
173
- * @example "/src/models/user.ts"
174
- */
175
- readonly path?: string;
176
- /**
177
- * Selector array used to select this file.
178
- *
179
- * @example ["foo", "bar"]
180
- */
181
- readonly selector?: IFileSelector;
182
- }
183
- interface IFileOut extends IFileIn {
184
- /**
185
- * Unique file ID.
186
- */
187
- readonly id: number;
188
- /**
189
- * Map holding resolved names for symbols in this file.
190
- */
191
- readonly resolvedNames: IBiMap<number, string>;
192
- /**
193
- * Symbols in this file, categorized by their role.
194
- */
195
- readonly symbols: {
196
- /**
197
- * Symbols declared in the body of this file.
198
- */
199
- body: Array<number>;
200
- /**
201
- * Symbols re-exported from other files.
202
- */
203
- exports: Array<number>;
204
- /**
205
- * Symbols imported from other files.
206
- */
207
- imports: Array<number>;
208
- };
209
- }
210
- interface IFileRegistry {
211
- /**
212
- * Get a file.
213
- *
214
- * @param identifier File identifier to reference.
215
- * @returns The file, or undefined if not found.
216
- */
217
- get(identifier: IFileIdentifier): IFileOut | undefined;
218
- /**
219
- * Returns the current file ID and increments it.
220
- *
221
- * @returns File ID before being incremented
222
- */
223
- readonly id: number;
224
- /**
225
- * Returns whether a file is registered in the registry.
226
- *
227
- * @param identifier File identifier to check.
228
- * @returns True if the file is registered, false otherwise.
229
- */
230
- isRegistered(identifier: IFileIdentifier): boolean;
231
- /**
232
- * Returns a file by identifier, registering it if it doesn't exist.
233
- *
234
- * @param identifier File identifier to reference.
235
- * @returns The referenced or newly registered file.
236
- */
237
- reference(identifier: IFileIdentifier): IFileOut;
238
- /**
239
- * Get all unregistered files in the order they were referenced.
240
- *
241
- * @returns Array of all unregistered files, in reference order.
242
- */
243
- referenced(): IterableIterator<IFileOut>;
244
- /**
245
- * Register a file globally.
246
- *
247
- * Deduplicates identical files by ID.
248
- *
249
- * @param file File to register.
250
- * @returns true if added, false if duplicate.
251
- */
252
- register(file: IFileIn): IFileOut;
253
- /**
254
- * Get all files in the order they were registered.
255
- *
256
- * @returns Array of all registered files, in insert order.
257
- */
258
- registered(): IterableIterator<IFileOut>;
259
- }
260
- //#endregion
261
- //#region src/extensions/types.d.ts
262
- /**
263
- * Arbitrary metadata passed to the project's render function.
264
- *
265
- * Implementers should extend this interface for their own needs.
266
- */
267
- interface IProjectRenderMeta {
268
- [key: string]: unknown;
269
- }
270
- /**
271
- * Additional metadata about the symbol.
272
- *
273
- * Implementers should extend this interface for their own needs.
274
- */
275
- interface ISymbolMeta {
276
- [key: string]: unknown;
277
- }
278
- //#endregion
279
- //#region src/symbols/types.d.ts
280
- type ISymbolIdentifier = number | ISymbolMeta;
281
- interface ISymbolIn {
282
- /**
283
- * Array of file names (without extensions) from which this symbol is re-exported.
284
- *
285
- * @default undefined
286
- */
287
- readonly exportFrom?: ReadonlyArray<string>;
288
- /**
289
- * Whether this symbol is exported from its own file.
290
- *
291
- * @default false
292
- */
293
- readonly exported?: boolean;
294
- /**
295
- * External module name if this symbol is imported from a module not managed
296
- * by the project (e.g. "zod", "lodash").
297
- *
298
- * @default undefined
299
- */
300
- readonly external?: string;
301
- /**
302
- * Optional output strategy to override default behavior.
303
- *
304
- * @returns The file path to output the symbol to, or undefined to fallback to default behavior.
305
- */
306
- readonly getFilePath?: (symbol: ISymbolOut) => string | undefined;
307
- /**
308
- * Unique symbol ID. If one is not provided, it will be auto-generated.
309
- */
310
- readonly id?: number;
311
- /**
312
- * Kind of import if this symbol represents an import.
313
- */
314
- readonly importKind?: 'namespace' | 'default' | 'named';
315
- /**
316
- * Kind of symbol.
317
- */
318
- readonly kind?: 'type';
319
- /**
320
- * Arbitrary metadata about the symbol.
321
- *
322
- * @default undefined
323
- */
324
- readonly meta?: ISymbolMeta;
325
- /**
326
- * The desired name for the symbol within its file. If there are multiple symbols
327
- * with the same desired name, this might not end up being the actual name.
328
- *
329
- * @example "UserModel"
330
- */
331
- readonly name?: string;
332
- /**
333
- * Placeholder name for the symbol to be replaced later with the final value.
334
- *
335
- * @example "_heyapi_31_"
336
- */
337
- readonly placeholder?: string;
338
- }
339
- interface ISymbolOut extends ISymbolIn {
340
- /**
341
- * Array of file names (without extensions) from which this symbol is re-exported.
342
- */
343
- readonly exportFrom: ReadonlyArray<string>;
344
- /**
345
- * Unique symbol ID.
346
- */
347
- readonly id: number;
348
- /**
349
- * Placeholder name for the symbol to be replaced later with the final value.
350
- *
351
- * @example "_heyapi_31_"
352
- */
353
- readonly placeholder: string;
354
- }
355
- interface ISymbolRegistry {
356
- /**
357
- * Get a symbol.
358
- *
359
- * @param identifier Symbol identifier to reference.
360
- * @returns The symbol, or undefined if not found.
361
- */
362
- get(identifier: ISymbolIdentifier): ISymbolOut | undefined;
363
- /**
364
- * Returns the value associated with a symbol ID.
365
- *
366
- * @param symbolId Symbol ID.
367
- * @return The value associated with the symbol ID, or undefined if not found.
368
- */
369
- getValue(symbolId: number): unknown;
370
- /**
371
- * Checks if the registry has a value associated with a symbol ID.
372
- *
373
- * @param symbolId Symbol ID.
374
- * @returns True if the registry has a value for symbol ID, false otherwise.
375
- */
376
- hasValue(symbolId: number): boolean;
377
- /**
378
- * Returns the current symbol ID and increments it.
379
- *
380
- * @returns Symbol ID before being incremented.
381
- */
382
- readonly id: number;
383
- /**
384
- * Returns whether a symbol is registered in the registry.
385
- *
386
- * @param identifier Symbol identifier to check.
387
- * @returns True if the symbol is registered, false otherwise.
388
- */
389
- isRegistered(identifier: ISymbolIdentifier): boolean;
390
- /**
391
- * Queries symbols by metadata filter.
392
- *
393
- * @param filter Metadata filter to query symbols by.
394
- * @returns Array of symbols matching the filter.
395
- */
396
- query(filter: ISymbolMeta): ReadonlyArray<ISymbolOut>;
397
- /**
398
- * References a symbol.
399
- *
400
- * @param meta Metadata filter to reference symbol by.
401
- * @returns The referenced symbol.
402
- */
403
- reference(meta: ISymbolMeta): ISymbolOut;
404
- /**
405
- * Register a symbol globally.
406
- *
407
- * Deduplicates identical symbols by ID.
408
- *
409
- * @param symbol Symbol to register.
410
- * @returns The registered symbol.
411
- */
412
- register(symbol: ISymbolIn): ISymbolOut;
413
- /**
414
- * Get all symbols in the order they were registered.
415
- *
416
- * @returns Array of all registered symbols, in insert order.
417
- */
418
- registered(): IterableIterator<ISymbolOut>;
419
- /**
420
- * Sets a value for a symbol by its ID.
421
- *
422
- * @param symbolId Symbol ID.
423
- * @param value The value to set.
424
- * @returns void
425
- */
426
- setValue(symbolId: number, value: unknown): Map<number, unknown>;
427
- }
428
- //#endregion
429
- //#region src/bindings/utils.d.ts
430
- declare const createBinding: ({
431
- file,
432
- modulePath,
433
- symbol,
434
- symbolFile
435
- }: {
436
- file: IFileOut;
437
- modulePath: string;
438
- symbol: ISymbolOut;
439
- symbolFile: IFileOut;
440
- }) => IBinding;
441
- declare const mergeBindings: (target: IBinding, source: IBinding) => void;
442
- //#endregion
443
- //#region src/output/types.d.ts
444
- interface IOutput {
445
- /**
446
- * The main content of the file to output.
447
- *
448
- * A raw string representing source code.
449
- *
450
- * @example "function foo(): void {\n // implementation\n}\n"
451
- */
452
- content: string;
453
- /**
454
- * Logical output path (used for writing the file).
455
- *
456
- * @example "models/user.ts"
457
- */
458
- path: string;
459
- }
460
- //#endregion
461
- //#region src/files/registry.d.ts
462
- declare class FileRegistry implements IFileRegistry {
463
- private _id;
464
- private referenceOrder;
465
- private registerOrder;
466
- private selectorToId;
467
- private values;
468
- get(identifier: IFileIdentifier): IFileOut | undefined;
469
- get id(): number;
470
- private identifierToFile;
471
- isRegistered(identifier: IFileIdentifier): boolean;
472
- reference(identifier: IFileIdentifier): IFileOut;
473
- referenced(): IterableIterator<IFileOut>;
474
- register(file: IFileIn): IFileOut;
475
- registered(): IterableIterator<IFileOut>;
476
- }
477
- //#endregion
478
- //#region src/project/types.d.ts
479
- /**
480
- * Represents a code generation project consisting of multiple codegen files.
481
- * Manages imports, symbols, and output generation across the project.
482
- */
483
- interface IProject {
484
- /**
485
- * The default file to assign symbols without a specific file selector.
486
- *
487
- * @default 'main'
488
- */
489
- readonly defaultFileName?: string;
490
- /**
491
- * Optional function to transform file names before they are used.
492
- *
493
- * @param name The original file name.
494
- * @returns The transformed file name.
495
- */
496
- readonly fileName?: (name: string) => string;
497
- /**
498
- * Centralized file registry for the project.
499
- */
500
- readonly files: IFileRegistry;
501
- /**
502
- * Produces output representations for all files in the project.
503
- *
504
- * @param meta Arbitrary metadata.
505
- * @returns Array of outputs ready for writing or further processing.
506
- * @example
507
- * project.render().forEach(output => writeFile(output));
508
- */
509
- render(meta?: IProjectRenderMeta): ReadonlyArray<IOutput>;
510
- /**
511
- * Map of available renderers by file extension.
512
- *
513
- * @example
514
- * {
515
- * ".ts": tsRenderer,
516
- * ".js": jsRenderer,
517
- * }
518
- */
519
- readonly renderers: Record<string, IRenderer>;
520
- /**
521
- * The absolute path to the root folder of the project.
522
- */
523
- readonly root: string;
524
- /**
525
- * Retrieves files that include symbol ID. The first file is the one
526
- * where the symbol is declared, the rest are files that re-export it.
527
- *
528
- * @param symbolId The symbol ID to find.
529
- * @returns An array of files containing the symbol.
530
- * @example
531
- * const files = project.symbolIdToFiles(31);
532
- * for (const file of files) {
533
- * console.log(file.path);
534
- * }
535
- */
536
- symbolIdToFiles(symbolId: number): ReadonlyArray<IFileOut>;
537
- /**
538
- * Centralized symbol registry for the project.
539
- */
540
- readonly symbols: ISymbolRegistry;
541
- }
542
- //#endregion
543
- //#region src/renderer/types.d.ts
544
- interface IRenderer {
545
- /**
546
- * Renders content with replaced symbols.
547
- *
548
- * @param content Content to render.
549
- * @param file The file to render.
550
- * @param project The parent project the file belongs to.
551
- * @returns Rendered content.
552
- */
553
- renderFile(content: string, file: IFile, project: IProject, meta?: IProjectRenderMeta): string;
554
- /**
555
- * Returns printable data containing symbols and exports.
556
- *
557
- * @param file The file to render.
558
- * @param project The parent project the file belongs to.
559
- * @param meta Arbitrary metadata.
560
- * @returns Printable string containing symbols and exports.
561
- */
562
- renderSymbols(file: IFileOut, project: IProject, meta?: IProjectRenderMeta): string;
563
- }
564
- //#endregion
565
- //#region src/symbols/registry.d.ts
566
- type SymbolId = number;
567
- declare class SymbolRegistry implements ISymbolRegistry {
568
- private _id;
569
- private indices;
570
- private nodes;
571
- private queryCache;
572
- private queryCacheDependencies;
573
- private registerOrder;
574
- private stubCache;
575
- private stubs;
576
- private values;
577
- get(identifier: ISymbolIdentifier): ISymbolOut | undefined;
578
- getValue(symbolId: SymbolId): unknown;
579
- hasValue(symbolId: SymbolId): boolean;
580
- get id(): SymbolId;
581
- isRegistered(identifier: ISymbolIdentifier): boolean;
582
- query(filter: ISymbolMeta): ReadonlyArray<ISymbolOut>;
583
- reference(meta: ISymbolMeta): ISymbolOut;
584
- register(symbol: ISymbolIn): ISymbolOut;
585
- registered(): IterableIterator<ISymbolOut>;
586
- setValue(symbolId: SymbolId, value: unknown): Map<SymbolId, unknown>;
587
- private buildCacheKey;
588
- private buildIndexKeySpace;
589
- private indexSymbol;
590
- private invalidateCache;
591
- private isSubset;
592
- private replaceStubs;
593
- private serializeIndexEntry;
594
- }
595
- //#endregion
596
- //#region src/project/project.d.ts
597
- declare class Project implements IProject {
598
- private symbolIdToFileIds;
599
- readonly defaultFileName: string;
600
- readonly files: FileRegistry;
601
- readonly fileName?: (name: string) => string;
602
- readonly renderers: Record<string, IRenderer>;
603
- readonly root: string;
604
- readonly symbols: SymbolRegistry;
605
- constructor({
606
- defaultFileName,
607
- fileName,
608
- renderers,
609
- root
610
- }: Pick<IProject, 'defaultFileName' | 'fileName' | 'renderers' | 'root'>);
611
- private getRenderer;
612
- private prepareFiles;
613
- render(meta?: IProjectRenderMeta): ReadonlyArray<IOutput>;
614
- symbolIdToFiles(symbolId: number): ReadonlyArray<IFileOut>;
615
- private symbolToFileSelector;
616
- }
617
- //#endregion
618
- //#region src/renderer/utils.d.ts
619
- /**
620
- *
621
- * @param source The source string to replace.
622
- * @param replacerFn Accepts a symbol ID, returns resolved symbol name.
623
- * @returns The replaced source string.
624
- */
625
- declare const renderIds: (source: string, replacerFn: (symbolId: number) => string | undefined) => string;
626
- //#endregion
627
- export { type IBiMap as BiMap, type IBinding as Binding, type IFileOut as File, type IFileIdentifier as FileIdentifier, type IFileIn as FileIn, type IProject, type IOutput as Output, Project, type IProjectRenderMeta as ProjectRenderMeta, type IRenderer as Renderer, type ISymbolOut as Symbol, type ISymbolIdentifier as SymbolIdentifier, type ISymbolIn as SymbolIn, type ISymbolMeta as SymbolMeta, createBinding, mergeBindings, renderIds };
628
- //# sourceMappingURL=index.d.ts.map
package/dist/index.js DELETED
@@ -1,2 +0,0 @@
1
- import e from"node:path";const t=({file:e,modulePath:t,symbol:n,symbolFile:r})=>{let i=[],a=[],o={aliases:{},from:t};if(n.importKind&&(n.importKind===`default`?(o.defaultBinding=n.placeholder,n.kind===`type`&&(o.typeDefaultBinding=!0)):n.importKind===`namespace`&&(o.namespaceBinding=n.placeholder,n.kind===`type`&&(o.typeNamespaceBinding=!0))),n.importKind===`named`||!i.length&&!o.defaultBinding&&!o.namespaceBinding){let t=n.placeholder,s=e.resolvedNames.get(n.id);if(s){let e=r.resolvedNames.get(n.id);e?e!==s&&(t=e,o.aliases[t]=s):n.name&&s!==n.name&&(t=n.name,o.aliases[t]=n.placeholder)}i.push(t),n.kind===`type`&&a.push(t)}for(let e of a)i.includes(e)||i.push(e);return o.names=i,o.typeNames=a,o},n=(e,t)=>{e.aliases={...e.aliases,...t.aliases},t.defaultBinding!==void 0&&(e.defaultBinding=t.defaultBinding),e.names=[...new Set([...e.names??[],...t.names??[]])],t.namespaceBinding!==void 0&&(e.namespaceBinding=t.namespaceBinding),t.typeDefaultBinding!==void 0&&(e.typeDefaultBinding=t.typeDefaultBinding),e.typeNames=[...new Set([...e.typeNames??[],...t.typeNames??[]])],t.typeNamespaceBinding!==void 0&&(e.typeNamespaceBinding=t.typeNamespaceBinding)};var r=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 n=this.map.get(e);n!==void 0&&n!==t&&this.reverse.delete(n);let r=this.reverse.get(t);return r!==void 0&&r!==e&&this.map.delete(r),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]()}},i=class{_id=0;referenceOrder=new Set;registerOrder=new Set;selectorToId=new Map;values=new Map;get(e){let t=this.identifierToFile(e);if(t.id!==void 0)return this.values.get(t.id);let n=t.selector===void 0?void 0:JSON.stringify(t.selector);if(n){let e=this.selectorToId.get(n);if(e!==void 0)return this.values.get(e)}}get id(){return this._id++}identifierToFile(e){return typeof e==`number`?{id:e}:{selector:e}}isRegistered(e){let t=this.get(e);return t?this.registerOrder.has(t.id):!1}reference(e){let t=this.identifierToFile(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 t=this.values.get(e.id);if(!t)throw Error(`File with ID ${e.id} not found. To register a new file, leave the ID undefined.`);return t}let t=Object.keys(e).some(e=>![`id`,`selector`].includes(e)),n,i=e.selector===void 0?void 0:JSON.stringify(e.selector);if(i){let e=this.selectorToId.get(i);if(e!==void 0){if(n=this.values.get(e),!n)throw Error(`File with ID ${e} 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 n}}let a=n?.id===void 0?this.id:n.id;return n={...n,...e,id:a,resolvedNames:n?.resolvedNames??new r,symbols:n?.symbols??{body:[],exports:[],imports:[]}},this.values.set(a,n),t?(this.registerOrder.add(a),this.referenceOrder.has(a)&&this.referenceOrder.delete(a)):this.referenceOrder.add(a),i&&this.selectorToId.set(i,a),n}*registered(){for(let e of this.registerOrder.values())yield this.values.get(e)}};const a=e=>`_heyapi_${e}_`,o=e=>e.slice(8,-1),s=()=>new RegExp(a(`\\d+`),`g`),c=(e,t)=>e.replace(s(),e=>t(Number.parseInt(o(e),10))||e);var l=class{_id=0;indices=new Map;nodes=new Map;queryCache=new Map;queryCacheDependencies=new Map;registerOrder=new Set;stubCache=new Map;stubs=new Set;values=new Map;get(e){return typeof e==`number`?this.values.get(e):this.query(e)[0]}getValue(e){return this.nodes.get(e)}hasValue(e){return this.nodes.has(e)}get id(){return this._id++}isRegistered(e){let t=this.get(e);return t?this.registerOrder.has(t.id):!1}query(e){let t=this.buildCacheKey(e),n=this.queryCache.get(t);if(n)return n.map(e=>this.values.get(e));let r=[],i=this.buildIndexKeySpace(e),a=new Set,o=!1;for(let e of i){a.add(this.serializeIndexEntry(e));let t=this.indices.get(e[0]);if(!t){o=!0;break}let n=t.get(e[1]);if(!n){o=!0;break}r.push(n)}if(o||!r.length)return this.queryCacheDependencies.set(t,a),this.queryCache.set(t,[]),[];let s=new Set(r[0]);for(let e of r.slice(1))s=new Set([...s].filter(t=>e.has(t)));let c=[...s];return this.queryCacheDependencies.set(t,a),this.queryCache.set(t,c),c.map(e=>this.values.get(e))}reference(e){let[t]=this.query(e);if(t)return t;let n=this.buildCacheKey(e),r=this.stubCache.get(n);if(r!==void 0)return this.values.get(r);let i=this.id,o={exportFrom:[],id:i,meta:e,placeholder:a(String(i))};return this.values.set(o.id,o),this.stubs.add(o.id),this.stubCache.set(n,o.id),o}register(e){let t=e.id===void 0?this.id:e.id,n={...e,exportFrom:e.exportFrom??[],id:t,placeholder:e.placeholder??a(String(t))};if(this.values.set(n.id,n),this.registerOrder.add(n.id),n.meta){let e=this.buildIndexKeySpace(n.meta);this.indexSymbol(n.id,e),this.invalidateCache(e),this.replaceStubs(n,e)}return n}*registered(){for(let e of this.registerOrder.values())yield this.values.get(e)}setValue(e,t){return this.nodes.set(e,t)}buildCacheKey(e){return this.buildIndexKeySpace(e).map(e=>this.serializeIndexEntry(e)).sort().join(`|`)}buildIndexKeySpace(e,t=``){let n=[];for(let[r,i]of Object.entries(e)){let e=t?`${t}.${r}`:r;i&&typeof i==`object`&&!Array.isArray(i)?n.push(...this.buildIndexKeySpace(i,e)):n.push([e,i])}return n}indexSymbol(e,t){for(let[n,r]of t){this.indices.has(n)||this.indices.set(n,new Map);let t=this.indices.get(n),i=t.get(r)??new Set;i.add(e),t.set(r,i)}}invalidateCache(e){let t=e.map(e=>this.serializeIndexEntry(e));for(let[e,n]of this.queryCacheDependencies.entries())for(let r of t)if(n.has(r)){this.queryCacheDependencies.delete(e),this.queryCache.delete(e);break}}isSubset(e,t){let n=new Map(t);for(let[t,r]of e)if(!n.has(t)||n.get(t)!==r)return!1;return!0}replaceStubs(e,t){for(let n of this.stubs.values()){let r=this.values.get(n);if(r?.meta&&this.isSubset(this.buildIndexKeySpace(r.meta),t)){let t=this.buildCacheKey(r.meta);this.stubCache.delete(t),this.values.set(n,Object.assign(r,e)),this.stubs.delete(n)}}}serializeIndexEntry(e){return`${e[0]}:${JSON.stringify(e[1])}`}},u=class{symbolIdToFileIds=new Map;defaultFileName;files=new i;fileName;renderers={};root;symbols=new l;constructor({defaultFileName:e,fileName:t,renderers:n,root:r}){this.defaultFileName=e??`main`,this.fileName=typeof t==`string`?()=>t:t,this.renderers=n,this.root=r}getRenderer(e){return e.extension?this.renderers[e.extension]:void 0}prepareFiles(){for(let e of this.symbols.registered()){let t=this.symbolToFileSelector(e),n=this.files.reference(t);n.symbols.body.push(e.id);let r=this.symbolIdToFileIds.get(e.id)??new Set;r.add(n.id),this.symbolIdToFileIds.set(e.id,r);for(let t of e.exportFrom){let r=[t],i=this.files.reference(r);i.id!==n.id&&i.symbols.exports.push(e.id)}}for(let t of this.files.referenced()){if(!t.selector)continue;if(t.selector[0]===`@`){let n=t.selector[1];if(!n){this.files.register({external:!0,selector:t.selector});continue}let r=e.extname(n);if(!r){this.files.register({external:!0,path:n,selector:t.selector});continue}this.files.register({extension:r,external:!0,path:n,selector:t.selector});continue}let n=t.selector.slice(0,-1),r=t.selector[t.selector.length-1];r=this.fileName?.(r)||r,this.files.register({extension:`.ts`,name:r,path:e.resolve(this.root,...n,`${r}.ts`),selector:t.selector})}}render(e){this.prepareFiles();let t=new Map;for(let n of this.files.registered()){if(n.external||!n.path)continue;let r=this.getRenderer(n);r&&t.set(n.id,{content:r.renderSymbols(n,this,e),path:n.path})}for(let[n,r]of t.entries()){let i=this.files.get(n),a=this.getRenderer(i).renderFile(r.content,i,this,e);a?t.set(i.id,{...r,content:a}):t.delete(i.id)}return Array.from(t.values())}symbolIdToFiles(e){let t=this.symbolIdToFileIds.get(e);return Array.from(t??[]).map(e=>this.files.get(e))}symbolToFileSelector(e){if(e.external)return[`@`,e.external];let t=e.getFilePath?.(e);return t?t.split(`/`):[this.defaultFileName]}};export{u as Project,t as createBinding,n as mergeBindings,c as renderIds};
2
- //# sourceMappingURL=index.js.map