@hey-api/codegen-core 0.1.0 → 0.3.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 CHANGED
@@ -1,3 +1,4 @@
1
+ //#region src/bimap/types.d.ts
1
2
  /**
2
3
  * Bi-directional map interface.
3
4
  *
@@ -6,7 +7,7 @@
6
7
  * @template Key Type of the map keys
7
8
  * @template Value Type of the map values
8
9
  */
9
- interface ICodegenBiMap<Key, Value> {
10
+ interface IBiMap<Key, Value> {
10
11
  /**
11
12
  * Deletes a key and its associated value from the map.
12
13
  *
@@ -72,651 +73,531 @@ interface ICodegenBiMap<Key, Value> {
72
73
  */
73
74
  [Symbol.iterator](): IterableIterator<[Key, Value]>;
74
75
  }
75
-
76
- declare class BiMap<Key, Value> implements ICodegenBiMap<Key, Value> {
77
- private map;
78
- private reverse;
79
- delete(key: Key): boolean;
80
- deleteValue(value: Value): boolean;
81
- entries(): IterableIterator<[Key, Value]>;
82
- get(key: Key): Value | undefined;
83
- getKey(value: Value): Key | undefined;
84
- hasKey(key: Key): boolean;
85
- hasValue(value: Value): boolean;
86
- keys(): IterableIterator<Key>;
87
- set(key: Key, value: Value): this;
88
- get size(): number;
89
- values(): IterableIterator<Value>;
90
- [Symbol.iterator](): IterableIterator<[Key, Value]>;
91
- }
92
-
93
- /**
94
- * Arbitrary metadata passed to render functions.
95
- *
96
- * Implementors should extend this interface for their own needs.
97
- */
98
- interface ICodegenMeta {
99
- [key: string]: unknown;
100
- }
101
-
102
- interface ICodegenOutput {
76
+ //#endregion
77
+ //#region src/bindings/types.d.ts
78
+ interface IBinding {
103
79
  /**
104
- * The main content of the file to output.
80
+ * Optional aliasing map for named symbols.
105
81
  *
106
- * A raw string representing source code.
82
+ * Keys must be a subset of `names`, values are aliases.
107
83
  *
108
- * @example "function foo(): void {\n // implementation\n}\n"
84
+ * @example { User: "ImportedUser" }
109
85
  */
110
- content: string;
86
+ aliases?: Record<string, string>;
111
87
  /**
112
- * Optional metadata or hints for the emitter, such as formatting options,
113
- * source maps, or language-specific flags.
88
+ * Name of the default binding, if any.
114
89
  *
115
- * @example { format: "prettier", sourceMap: true }
90
+ * @example "React"
116
91
  */
117
- meta: Record<string, unknown>;
92
+ defaultBinding?: string;
118
93
  /**
119
- * Logical output path (used for writing the file).
94
+ * Source file or external module from which symbols are imported.
120
95
  *
121
- * @example "models/user.ts"
96
+ * @example "./models/user"
97
+ * @example "node:path"
122
98
  */
123
- path: string;
124
- }
125
-
126
- interface ICodegenRenderer {
99
+ from: string;
127
100
  /**
128
- * Optional: hook for renderer-level setup logic (e.g., formatting, config)
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"]
129
107
  */
130
- configure?(options: Record<string, unknown>): void;
108
+ names?: ReadonlyArray<string>;
131
109
  /**
132
- * Unique identifier for this renderer.
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.
133
112
  *
134
- * @example "typescript"
113
+ * @example "utils"
114
+ * @example true
135
115
  */
136
- id: string;
116
+ namespaceBinding?: boolean | string;
137
117
  /**
138
- * Returns printable data containing header and imports.
118
+ * Whether the default binding is type-only.
139
119
  *
140
- * @param file The file to render.
141
- * @param meta Arbitrary metadata.
142
- * @returns Printable string containing header and imports.
120
+ * @example true
143
121
  */
144
- renderHeader(file: ICodegenFile, meta?: ICodegenMeta): string;
122
+ typeDefaultBinding?: boolean;
145
123
  /**
146
- * Returns printable data containing symbols and exports.
124
+ * Subset of `names` that are imported using the `type` modifier.
125
+ * These symbols will be emitted as type-only imports in TypeScript.
147
126
  *
148
- * @param file The file to render.
149
- * @param meta Arbitrary metadata.
150
- * @returns Printable string containing symbols and exports.
127
+ * @example ["UserDTO"]
151
128
  */
152
- renderSymbols(file: ICodegenFile, meta?: ICodegenMeta): string;
129
+ typeNames?: ReadonlyArray<string>;
153
130
  /**
154
- * Function replacing symbols with resolved names.
131
+ * Whether the namespace binding is type-only.
155
132
  *
156
- * @returns String with replaced symbols.
133
+ * @example true
157
134
  */
158
- replacerFn(args: {
159
- file: ICodegenFile;
160
- headless?: boolean;
161
- scope?: 'file' | 'project';
162
- symbolId: number;
163
- }): string | undefined;
135
+ typeNamespaceBinding?: boolean;
164
136
  }
165
-
137
+ //#endregion
138
+ //#region src/selectors/types.d.ts
166
139
  /**
167
- * Selector array used to select symbols. It doesn't have to be
168
- * unique, but in practice it might be desirable.
140
+ * Selector array used to reference resources. We don't enforce
141
+ * uniqueness, but in practice it's desirable.
169
142
  *
170
143
  * @example ["zod", "#/components/schemas/Foo"]
171
144
  */
172
- type ICodegenSymbolSelector = ReadonlyArray<string>;
173
-
174
- interface ICodegenSymbolIn {
145
+ type ISelector = ReadonlyArray<string>;
146
+ //#endregion
147
+ //#region src/files/types.d.ts
148
+ interface IFileIn {
175
149
  /**
176
- * Symbols can be **headed** or **headless**.
177
- *
178
- * Headless symbols never render their `value`. Headed symbols render their
179
- * `value` if defined.
180
- *
181
- * Symbols are rendered in the order they were registered as headed.
182
- *
183
- * Example 1: We register headless symbol `foo`, headed `bar`, and headed
184
- * `foo`. The render order is [`bar`, `foo`].
185
- *
186
- * Example 2: We register headed symbol `foo` and headed `bar`. The render
187
- * order is [`foo`, `bar`].
188
- *
189
- * Headless symbols can be used to claim a symbol or to represent imports
190
- * or exports.
150
+ * File extension, if any.
151
+ */
152
+ readonly extension?: string;
153
+ /**
154
+ * Indicates whether the file is external, meaning it is not generated
155
+ * as part of the project but is referenced (e.g., a module from
156
+ * node_modules).
191
157
  *
192
- * @default false
158
+ * @example true
193
159
  */
194
- headless?: boolean;
160
+ readonly external?: boolean;
195
161
  /**
196
- * The desired name for the symbol within its file. If there are multiple symbols
162
+ * Unique file ID. If one is not provided, it will be auto-generated.
163
+ */
164
+ readonly id?: number;
165
+ /**
166
+ * The desired name for the file within the project. If there are multiple files
197
167
  * with the same desired name, this might not end up being the actual name.
198
168
  *
199
169
  * @example "UserModel"
200
170
  */
201
- readonly name: string;
171
+ readonly name?: string;
202
172
  /**
203
- * Selector array used to select this symbol. It doesn't have to be
173
+ * Absolute logical output path for the file.
174
+ *
175
+ * @example "/src/models/user.ts"
176
+ */
177
+ readonly path?: string;
178
+ /**
179
+ * Selector array used to select this file. It doesn't have to be
204
180
  * unique, but in practice it might be desirable.
205
181
  *
206
182
  * @example ["zod", "#/components/schemas/Foo"]
207
183
  */
208
- readonly selector?: ICodegenSymbolSelector;
184
+ readonly selector?: ISelector;
185
+ }
186
+ interface IFileOut extends IFileIn {
209
187
  /**
210
- * Internal representation of the symbol (e.g. AST node, IR object, raw code).
211
- * Used to generate output. If left undefined, this symbol becomes `headless`.
188
+ * Unique file ID.
212
189
  */
213
- readonly value?: unknown;
214
- }
215
-
216
- interface ICodegenSymbolOut extends ICodegenSymbolIn {
190
+ readonly id: number;
217
191
  /**
218
- * The file this symbol is located in.
192
+ * Map holding resolved names for symbols in this file.
219
193
  */
220
- readonly file: ICodegenFile;
194
+ readonly resolvedNames: IBiMap<number, string>;
221
195
  /**
222
- * Unique symbol ID.
196
+ * Symbols in this file, categorized by their role.
223
197
  */
224
- readonly id: number;
198
+ readonly symbols: {
199
+ /**
200
+ * Symbols declared in the body of this file.
201
+ */
202
+ body: Array<number>;
203
+ /**
204
+ * Symbols re-exported from other files.
205
+ */
206
+ exports: Array<number>;
207
+ /**
208
+ * Symbols imported from other files.
209
+ */
210
+ imports: Array<number>;
211
+ };
212
+ }
213
+ interface IFileRegistry {
225
214
  /**
226
- * Placeholder name for the symbol to be replaced later with the final value.
215
+ * Get a file by its ID.
227
216
  *
228
- * @example "_heyapi_31_"
217
+ * @param fileIdOrSelector File ID or selector to reference.
218
+ * @returns The file, or undefined if not found.
229
219
  */
230
- readonly placeholder: string;
220
+ get(fileIdOrSelector: number | ISelector): IFileOut | undefined;
231
221
  /**
232
- * Updates this symbol.
222
+ * Returns the current file ID and increments it.
233
223
  *
234
- * @param symbol The values to update.
235
- * @returns The updated symbol.
224
+ * @returns File ID before being incremented
236
225
  */
237
- readonly update: (symbol: Partial<ICodegenSymbolOut>) => ICodegenSymbolOut;
238
- }
239
-
240
- interface SelectorMethods {
226
+ readonly id: number;
241
227
  /**
242
- * Retrieves symbols matching the selector.
228
+ * Returns a file by ID or selector, registering it if it doesn't exist.
243
229
  *
244
- * @param selector The symbol selector to find.
245
- * @param file Find symbols only in this file.
246
- * @returns The array of all symbols matching the selector.
247
- * @example
248
- * const symbols = project.selectSymbolAll(["zod", "#/components/schemas/Foo"]);
230
+ * @param fileIdOrSelector File ID or selector to reference.
231
+ * @returns The referenced or newly registered file.
249
232
  */
250
- selectSymbolAll(
251
- selector: ICodegenSymbolSelector,
252
- file?: ICodegenFile,
253
- ): ReadonlyArray<ICodegenSymbolOut>;
233
+ reference(fileIdOrSelector: number | ISelector): IFileOut;
254
234
  /**
255
- * Retrieves the first symbol from all symbols matching the selector.
235
+ * Get all unregistered files in the order they were referenced.
256
236
  *
257
- * @param selector The symbol selector to find.
258
- * @param file Find symbols only in this file.
259
- * @returns The symbol if found, or undefined otherwise.
260
- * @example
261
- * const symbol = project.selectSymbolFirst(["zod", "#/components/schemas/Foo"]);
237
+ * @returns Array of all unregistered files, in reference order.
262
238
  */
263
- selectSymbolFirst(
264
- selector: ICodegenSymbolSelector,
265
- file?: ICodegenFile,
266
- ): ICodegenSymbolOut | undefined;
239
+ referenced(): IterableIterator<IFileOut>;
267
240
  /**
268
- * Retrieves the first symbol from all symbols matching the selector.
241
+ * Register a file globally.
269
242
  *
270
- * @param selector The symbol selector to find.
271
- * @param file Find symbols only in this file.
272
- * @returns The symbol if found, or throw otherwise.
273
- * @example
274
- * const symbol = project.selectSymbolFirstOrThrow(["zod", "#/components/schemas/Foo"]);
243
+ * Deduplicates identical files by ID.
244
+ *
245
+ * @param file File to register.
246
+ * @returns true if added, false if duplicate.
275
247
  */
276
- selectSymbolFirstOrThrow(
277
- selector: ICodegenSymbolSelector,
278
- file?: ICodegenFile,
279
- ): ICodegenSymbolOut;
248
+ register(file: IFileIn): IFileOut;
280
249
  /**
281
- * Retrieves the last symbol from all symbols matching the selector.
250
+ * Get all files in the order they were registered.
282
251
  *
283
- * @param selector The symbol selector to find.
284
- * @param file Find symbols only in this file.
285
- * @returns The symbol if found, or undefined otherwise.
286
- * @example
287
- * const symbol = project.selectSymbolLast(["zod", "#/components/schemas/Foo"]);
252
+ * @returns Array of all registered files, in insert order.
288
253
  */
289
- selectSymbolLast(
290
- selector: ICodegenSymbolSelector,
291
- file?: ICodegenFile,
292
- ): ICodegenSymbolOut | undefined;
254
+ registered(): IterableIterator<IFileOut>;
293
255
  }
294
-
256
+ //#endregion
257
+ //#region src/extensions/types.d.ts
295
258
  /**
296
- * Represents a code generation project consisting of multiple codegen files.
297
- * Manages imports, symbols, and output generation across the project.
259
+ * Arbitrary metadata passed to the project's render function.
260
+ *
261
+ * Implementers should extend this interface for their own needs.
262
+ */
263
+ interface IProjectRenderMeta {
264
+ [key: string]: unknown;
265
+ }
266
+ /**
267
+ * Additional metadata about the symbol.
268
+ *
269
+ * Implementers should extend this interface for their own needs.
298
270
  */
299
- interface ICodegenProject extends SelectorMethods {
271
+ interface ISymbolMeta {
272
+ [key: string]: unknown;
273
+ }
274
+ //#endregion
275
+ //#region src/symbols/types.d.ts
276
+ interface ISymbolIn {
300
277
  /**
301
- * Adds an export declaration to a specific file, creating the file if it doesn't exist.
278
+ * Array of file names (without extensions) from which this symbol is re-exported.
302
279
  *
303
- * @param fileOrPath - File instance or file path where to add the export.
304
- * @param imp - The export declaration to add.
305
- * @example
306
- * project.addExport("models/user.ts", { from: "lib", names: ["User"] });
280
+ * @default undefined
307
281
  */
308
- addExport(fileOrPath: ICodegenFile | string, imp: ICodegenImport): void;
282
+ readonly exportFrom?: ReadonlyArray<string>;
309
283
  /**
310
- * Adds an import declaration to a specific file, creating the file if it doesn't exist.
284
+ * Whether this symbol is exported from its own file.
311
285
  *
312
- * @param fileOrPath - File instance or file path where to add the import.
313
- * @param imp - The import declaration to add.
314
- * @example
315
- * project.addImport("models/user.ts", { from: "lib", names: ["User"] });
286
+ * @default false
316
287
  */
317
- addImport(fileOrPath: ICodegenFile | string, imp: ICodegenImport): void;
288
+ readonly exported?: boolean;
318
289
  /**
319
- * Adds a symbol to a specific file, creating the file if it doesn't exist.
290
+ * External module name if this symbol is imported from a module not managed
291
+ * by the project (e.g. "zod", "lodash").
320
292
  *
321
- * @param fileOrPath - File instance or file path where to add the symbol.
322
- * @param symbol - The symbol to add.
323
- * @returns The inserted symbol.
324
- * @example
325
- * project.addSymbol("models/user.ts", { name: "User", value: tsNode });
293
+ * @default undefined
326
294
  */
327
- addSymbol(
328
- fileOrPath: ICodegenFile | string,
329
- symbol: ICodegenSymbolIn,
330
- ): ICodegenSymbolOut;
295
+ readonly external?: string;
331
296
  /**
332
- * Creates a new codegen file with optional metadata and adds it to the project.
333
- *
334
- * If a file with the same path already exists, it is returned instead.
297
+ * Optional output strategy to override default behavior.
335
298
  *
336
- * @param path - The logical output path for the file (e.g. "models/user.ts").
337
- * @param meta - Optional renderer and metadata to attach to the file (e.g. { isInternal: true }).
338
- * @returns The newly created file instance.
339
- * @example
340
- * const file = project.createFile("models/user.ts", { isInternal: true });
299
+ * @returns The file path to output the symbol to, or undefined to fallback to default behavior.
341
300
  */
342
- createFile(
343
- path: string,
344
- meta?: ICodegenFile['meta'] & { renderer?: ICodegenRenderer },
345
- ): ICodegenFile;
301
+ readonly getFilePath?: (symbol: ISymbolOut) => string | undefined;
346
302
  /**
347
- * Ensures a codegen file exists and returns it.
348
- *
349
- * If a file does not exist yet, it is created with minimal information.
350
- * Later, it is expected `createFile()` will be called which will fill in
351
- * the missing information such as optional metadata.
352
- *
353
- * @param fileOrPath - The logical output path for the file or the file itself.
354
- * @returns The file instance.
355
- * @example
356
- * const file = project.ensureFile("models/user.ts");
303
+ * Unique symbol ID. If one is not provided, it will be auto-generated.
357
304
  */
358
- ensureFile(fileOrPath: ICodegenFile | string): ICodegenFile;
305
+ readonly id?: number;
359
306
  /**
360
- * Returns all files in the project in insertion order.
307
+ * Arbitrary metadata about the symbol.
361
308
  *
362
- * @example
363
- * project.files.forEach(file => console.log(file.path));
309
+ * @default undefined
364
310
  */
365
- readonly files: ReadonlyArray<ICodegenFile>;
311
+ readonly meta?: ISymbolMeta & {
312
+ /**
313
+ * Kind of import if this symbol represents an import.
314
+ */
315
+ importKind?: 'namespace' | 'default' | 'named';
316
+ /**
317
+ * Kind of symbol.
318
+ */
319
+ kind?: 'type';
320
+ };
366
321
  /**
367
- * Returns all symbols declared or imported across all files.
322
+ * The desired name for the symbol within its file. If there are multiple symbols
323
+ * with the same desired name, this might not end up being the actual name.
368
324
  *
369
- * @returns Flattened list of all codegen symbols.
370
- * @example
371
- * project.getAllSymbols().filter(s => s.name === "User");
325
+ * @example "UserModel"
372
326
  */
373
- getAllSymbols(): ReadonlyArray<Pick<ICodegenSymbolOut, 'name'>>;
327
+ readonly name?: string;
374
328
  /**
375
- * Retrieves a file by its logical output path.
329
+ * Placeholder name for the symbol to be replaced later with the final value.
376
330
  *
377
- * @param path - The file path to find.
378
- * @returns The file if found, or undefined otherwise.
379
- * @example
380
- * const file = project.getFileByPath("models/user.ts");
331
+ * @example "_heyapi_31_"
381
332
  */
382
- getFileByPath(path: string): ICodegenFile | undefined;
333
+ readonly placeholder?: string;
383
334
  /**
384
- * Retrieves a file from symbol ID included in the file.
335
+ * Selector array used to select this symbol. It doesn't have to be
336
+ * unique, but in practice it might be desirable.
385
337
  *
386
- * @param id The symbol ID to find.
387
- * @returns The file if found, undefined otherwise.
388
- * @example
389
- * const file = project.getFileBySymbolId(31);
338
+ * @example ["zod", "#/components/schemas/Foo"]
390
339
  */
391
- getFileBySymbolId(id: number): ICodegenFile | undefined;
340
+ readonly selector?: ISelector;
341
+ }
342
+ interface ISymbolOut extends ISymbolIn {
392
343
  /**
393
- * Retrieves a symbol from ID included in the project.
394
- *
395
- * @param id The symbol ID to find.
396
- * @returns The symbol if found, undefined otherwise.
397
- * @example
398
- * const symbol = project.getSymbolById(31);
344
+ * Array of file names (without extensions) from which this symbol is re-exported.
399
345
  */
400
- getSymbolById(id: number): ICodegenSymbolOut | undefined;
346
+ readonly exportFrom: ReadonlyArray<string>;
401
347
  /**
402
- * Returns the current file ID and increments it.
403
- *
404
- * @returns File ID before being incremented
348
+ * Unique symbol ID.
405
349
  */
406
- incrementFileId(): number;
350
+ readonly id: number;
407
351
  /**
408
- * Returns the current symbol ID and increments it.
352
+ * Placeholder name for the symbol to be replaced later with the final value.
409
353
  *
410
- * @returns Symbol ID before being incremented
354
+ * @example "_heyapi_31_"
411
355
  */
412
- incrementSymbolId(): number;
356
+ readonly placeholder: string;
357
+ }
358
+ interface ISymbolRegistry {
413
359
  /**
414
- * Tracks added symbol across the project.
360
+ * Get a symbol by its ID.
415
361
  *
416
- * @param symbol The symbol added to file.
417
- * @param file The file containing the added symbol.
362
+ * @param symbolIdOrSelector Symbol ID or selector to reference.
363
+ * @returns The symbol, or undefined if not found.
418
364
  */
419
- registerSymbol(symbol: ICodegenSymbolOut, file: ICodegenFile): void;
365
+ get(symbolIdOrSelector: number | ISelector): ISymbolOut | undefined;
420
366
  /**
421
- * Produces output representations for all files in the project.
367
+ * Returns the value associated with a symbol ID.
422
368
  *
423
- * @param meta Arbitrary metadata.
424
- * @returns Array of outputs ready for writing or further processing.
425
- * @example
426
- * project.render().forEach(output => writeFile(output));
369
+ * @param symbolId Symbol ID.
370
+ * @return The value associated with the symbol ID, or undefined if not found.
427
371
  */
428
- render(meta?: ICodegenMeta): ReadonlyArray<ICodegenOutput>;
429
- }
430
-
431
- interface ICodegenFile extends SelectorMethods {
372
+ getValue(symbolId: number): unknown;
432
373
  /**
433
- * Adds an export to this file.
434
- *
435
- * This is also known as a re-export.
374
+ * Checks if the registry has a value associated with a symbol ID.
436
375
  *
437
- * @param exp The export to add
376
+ * @param symbolId Symbol ID.
377
+ * @returns True if the registry has a value for symbol ID, false otherwise.
438
378
  */
439
- addExport(exp: ICodegenImport): void;
379
+ hasValue(symbolId: number): boolean;
440
380
  /**
441
- * Adds an import to this file.
381
+ * Returns the current symbol ID and increments it.
442
382
  *
443
- * @param imp The import to add
383
+ * @returns Symbol ID before being incremented.
444
384
  */
445
- addImport(imp: ICodegenImport): void;
385
+ readonly id: number;
446
386
  /**
447
- * Adds a symbol defined by this file.
387
+ * Returns a symbol by ID or selector, registering it if it doesn't exist.
448
388
  *
449
- * @param symbol The symbol to add
389
+ * @param symbolIdOrSelector Symbol ID or selector to reference.
390
+ * @returns The referenced or newly registered symbol.
450
391
  */
451
- addSymbol(symbol: ICodegenSymbolIn): ICodegenSymbolOut;
392
+ reference(symbolIdOrSelector: number | ISelector): ISymbolOut;
452
393
  /**
453
- * Ensures a symbol for the given selector exists, so it can be
454
- * safely used.
394
+ * Register a symbol globally.
455
395
  *
456
- * @param symbol The symbol to find. The required selector is used
457
- * to match a symbol. If there's no match, we create a headless
458
- * instance with the provided fields.
459
- * @returns The symbol if it exists, headless instance otherwise.
460
- */
461
- ensureSymbol(
462
- symbol: Partial<ICodegenSymbolIn> &
463
- Pick<Required<ICodegenSymbolIn>, 'selector'>,
464
- ): ICodegenSymbolOut;
465
- /**
466
- * Symbols exported from other files.
467
- **/
468
- exports: ReadonlyArray<ICodegenImport>;
469
- /**
470
- * Returns all symbols used in this file (declared + imported).
396
+ * Deduplicates identical symbols by ID.
471
397
  *
472
- * @returns List of all symbols used in this file
398
+ * @param symbol Symbol to register.
399
+ * @returns The registered symbol.
473
400
  */
474
- getAllSymbols(): ReadonlyArray<Pick<ICodegenSymbolOut, 'name'>>;
401
+ register(symbol: ISymbolIn): ISymbolOut;
475
402
  /**
476
- * Finds a symbol by symbol ID.
403
+ * Get all symbols in the order they were registered.
477
404
  *
478
- * @param id Symbol ID
479
- * @returns The symbol if it exists, undefined otherwise.
405
+ * @returns Array of all registered symbols, in insert order.
480
406
  */
481
- getSymbolById(id: number): ICodegenSymbolOut | undefined;
407
+ registered(): IterableIterator<ISymbolOut>;
482
408
  /**
483
- * Checks if this file contains any content.
409
+ * Sets a value for a symbol by its ID.
484
410
  *
485
- * This is used to determine whether we want to process the file further.
486
- * By default, we consider only symbols and exports as content.
487
- *
488
- * @returns True if the file contains content
411
+ * @param symbolId Symbol ID.
412
+ * @param value The value to set.
413
+ * @returns void
489
414
  */
490
- hasContent(): boolean;
415
+ setValue(symbolId: number, value: unknown): Map<number, unknown>;
416
+ }
417
+ //#endregion
418
+ //#region src/bindings/utils.d.ts
419
+ declare const createBinding: ({
420
+ file,
421
+ modulePath,
422
+ symbol,
423
+ symbolFile
424
+ }: {
425
+ file: IFileOut;
426
+ modulePath: string;
427
+ symbol: ISymbolOut;
428
+ symbolFile: IFileOut;
429
+ }) => IBinding;
430
+ declare const mergeBindings: (target: IBinding, source: IBinding) => void;
431
+ //#endregion
432
+ //#region src/output/types.d.ts
433
+ interface IOutput {
491
434
  /**
492
- * Checks if this file defines a symbol with the given name.
435
+ * The main content of the file to output.
493
436
  *
494
- * @param id Symbol ID to check
495
- * @returns True if the symbol is defined by this file
496
- */
497
- hasSymbol(id: number): boolean;
498
- /**
499
- * File ID within the project.
437
+ * A raw string representing source code.
438
+ *
439
+ * @example "function foo(): void {\n // implementation\n}\n"
500
440
  */
501
- id: number;
502
- /**
503
- * Symbols imported from other files.
504
- **/
505
- imports: ReadonlyArray<ICodegenImport>;
506
- /**
507
- * Optional metadata about the file.
508
- **/
509
- meta: {
510
- /**
511
- * Optional file extension.
512
- *
513
- * @example ".ts"
514
- */
515
- extension?: '.ts' | (string & {});
516
- /**
517
- * Optional logical module or package name.
518
- *
519
- * @example "models.user"
520
- */
521
- moduleName?: string;
522
- /**
523
- * Optional path transformer.
524
- *
525
- * @param path Original file path passed to the constructor.
526
- */
527
- path?: ((path: string) => string) | string;
528
- /**
529
- * Renderer ID.
530
- *
531
- * @example "typescript"
532
- */
533
- renderer?: ICodegenRenderer['id'];
534
- };
441
+ content: string;
535
442
  /**
536
443
  * Logical output path (used for writing the file).
537
444
  *
538
445
  * @example "models/user.ts"
539
446
  */
540
447
  path: string;
448
+ }
449
+ //#endregion
450
+ //#region src/files/registry.d.ts
451
+ declare class FileRegistry implements IFileRegistry {
452
+ private _id;
453
+ private referenceOrder;
454
+ private registerOrder;
455
+ private selectorToId;
456
+ private values;
457
+ get(fileIdOrSelector: number | ISelector): IFileOut | undefined;
458
+ get id(): number;
459
+ private idOrSelector;
460
+ reference(fileIdOrSelector: number | ISelector): IFileOut;
461
+ referenced(): IterableIterator<IFileOut>;
462
+ register(file: IFileIn): IFileOut;
463
+ registered(): IterableIterator<IFileOut>;
464
+ }
465
+ //#endregion
466
+ //#region src/project/types.d.ts
467
+ /**
468
+ * Represents a code generation project consisting of multiple codegen files.
469
+ * Manages imports, symbols, and output generation across the project.
470
+ */
471
+ interface IProject {
541
472
  /**
542
- * Parent project this file belongs to.
543
- */
544
- project: ICodegenProject;
545
- /**
546
- * Returns a relative path to this file from another file.
547
- *
548
- * @param file The file from which we want the relative path to this file.
549
- * @example "./this-file.ts"
550
- */
551
- relativePathFromFile(file: Pick<ICodegenFile, 'path'>): string;
552
- /**
553
- * Returns a relative path to file from this file.
473
+ * The default file to assign symbols without a specific file selector.
554
474
  *
555
- * @param file The file to which we want the relative path.
556
- * @example "./another-file.ts"
475
+ * @default 'main'
557
476
  */
558
- relativePathToFile(file: Pick<ICodegenFile, 'path'>): string;
559
- /**
560
- * Map holding resolved names for symbols in this file.
561
- */
562
- resolvedNames: ICodegenBiMap<number, string>;
563
- /**
564
- * Top-level symbols declared in this file.
565
- **/
566
- symbols: ReadonlyArray<ICodegenSymbolOut>;
477
+ readonly defaultFileName?: string;
567
478
  /**
568
- * Updates a symbol defined by this file.
479
+ * Optional function to transform file names before they are used.
569
480
  *
570
- * @param id ID of symbol to update.
571
- * @param symbol The values to update.
572
- * @returns The updated symbol.
481
+ * @param name The original file name.
482
+ * @returns The transformed file name.
573
483
  */
574
- updateSymbol(
575
- id: number,
576
- symbol: Partial<ICodegenSymbolOut>,
577
- ): ICodegenSymbolOut;
578
- }
579
-
580
- interface ICodegenImport {
484
+ readonly fileName?: (name: string) => string;
581
485
  /**
582
- * Optional aliasing map for imported symbols.
583
- *
584
- * Keys must be a subset of `names`, values are aliases.
585
- *
586
- * @example { User: "ImportedUser" }
486
+ * Centralized file registry for the project.
587
487
  */
588
- aliases?: Record<string, string>;
488
+ readonly files: IFileRegistry;
589
489
  /**
590
- * Name of the default import, if any.
490
+ * Produces output representations for all files in the project.
591
491
  *
592
- * @example "React"
492
+ * @param meta Arbitrary metadata.
493
+ * @returns Array of outputs ready for writing or further processing.
494
+ * @example
495
+ * project.render().forEach(output => writeFile(output));
593
496
  */
594
- defaultImport?: string;
497
+ render(meta?: IProjectRenderMeta): ReadonlyArray<IOutput>;
595
498
  /**
596
- * Source file or external module from which symbols are imported.
597
- *
598
- * For internal files, this should be a ICodegenFile instance to enable
599
- * dynamic path computation. For external or system modules, use a string.
499
+ * Map of available renderers by file extension.
600
500
  *
601
- * @example "./models/user"
602
- * @example "node:path"
501
+ * @example
502
+ * {
503
+ * ".ts": tsRenderer,
504
+ * ".js": jsRenderer,
505
+ * }
603
506
  */
604
- from: ICodegenFile | string;
507
+ readonly renderers: Record<string, IRenderer>;
605
508
  /**
606
- * Names of the symbols imported from the source.
607
- *
608
- * Must be non-empty unless `isNamespaceImport` is true.
609
- * All imported names, regardless of whether they are used as types or values.
610
- *
611
- * @example ["User", "UserDTO"]
509
+ * The absolute path to the root folder of the project.
612
510
  */
613
- names?: ReadonlyArray<string>;
511
+ readonly root: string;
614
512
  /**
615
- * If this import is a namespace import (e.g. `import * as ns from "..."`),
616
- * this should be the namespace alias. Set to `true` if no alias is needed.
513
+ * Retrieves files that include symbol ID. The first file is the one
514
+ * where the symbol is declared, the rest are files that re-export it.
617
515
  *
618
- * @example "utils"
619
- * @example true
516
+ * @param symbolId The symbol ID to find.
517
+ * @returns An array of files containing the symbol.
518
+ * @example
519
+ * const files = project.symbolIdToFiles(31);
520
+ * for (const file of files) {
521
+ * console.log(file.path);
522
+ * }
620
523
  */
621
- namespaceImport?: boolean | string;
524
+ symbolIdToFiles(symbolId: number): ReadonlyArray<IFileOut>;
622
525
  /**
623
- * Whether the default import is type-only.
624
- *
625
- * @example true
526
+ * Centralized symbol registry for the project.
626
527
  */
627
- typeDefaultImport?: boolean;
528
+ readonly symbols: ISymbolRegistry;
529
+ }
530
+ //#endregion
531
+ //#region src/renderer/types.d.ts
532
+ interface IRenderer {
628
533
  /**
629
- * Subset of `names` that are imported using the `type` modifier.
630
- * These symbols will be emitted as type-only imports in TypeScript.
534
+ * Renders content with replaced symbols.
631
535
  *
632
- * @example ["UserDTO"]
536
+ * @param content Content to render.
537
+ * @param file The file to render.
538
+ * @param project The parent project the file belongs to.
539
+ * @returns Rendered content.
633
540
  */
634
- typeNames?: ReadonlyArray<string>;
541
+ renderFile(content: string, file: IFile, project: IProject, meta?: IProjectRenderMeta): string;
635
542
  /**
636
- * Whether the namespace import is type-only.
543
+ * Returns printable data containing symbols and exports.
637
544
  *
638
- * @example true
545
+ * @param file The file to render.
546
+ * @param project The parent project the file belongs to.
547
+ * @param meta Arbitrary metadata.
548
+ * @returns Printable string containing symbols and exports.
639
549
  */
640
- typeNamespaceImport?: boolean;
550
+ renderSymbols(file: IFileOut, project: IProject, meta?: IProjectRenderMeta): string;
641
551
  }
642
-
643
- declare class CodegenFile implements ICodegenFile {
644
- path: string;
645
- project: ICodegenProject;
646
- meta: ICodegenFile['meta'];
647
- private cache;
648
- private renderSymbols;
649
- private state;
650
- id: number;
651
- resolvedNames: ICodegenBiMap<number, string>;
652
- constructor(path: string, project: ICodegenProject, meta?: ICodegenFile['meta']);
653
- addExport(exp: ICodegenImport): void;
654
- addImport(imp: ICodegenImport): void;
655
- private addImportExport;
656
- private addRenderSymbol;
657
- addSymbol(symbol: ICodegenSymbolIn): ICodegenSymbolOut;
658
- ensureSymbol(symbol: Partial<ICodegenSymbolIn> & Pick<Required<ICodegenSymbolIn>, 'selector'>): ICodegenSymbolOut;
659
- get exports(): ReadonlyArray<ICodegenImport>;
660
- getAllSymbols(): ReadonlyArray<Pick<ICodegenSymbolOut, 'name'>>;
661
- private getImportExportKey;
662
- getSymbolById(id: number): ICodegenSymbolOut | undefined;
663
- hasContent(): boolean;
664
- hasSymbol(id: number): boolean;
665
- get imports(): ReadonlyArray<ICodegenImport>;
666
- private mergeImportExportValues;
667
- static pathToFilePath(source: string): string;
668
- relativePathFromFile(file: Pick<ICodegenFile, 'path'>): string;
669
- relativePathToFile(file: Pick<ICodegenFile, 'path'>): string;
670
- selectSymbolAll(selector: ICodegenSymbolSelector): ReadonlyArray<ICodegenSymbolOut>;
671
- selectSymbolFirst(selector: ICodegenSymbolSelector): ICodegenSymbolOut | undefined;
672
- selectSymbolFirstOrThrow(selector: ICodegenSymbolSelector): ICodegenSymbolOut;
673
- selectSymbolLast(selector: ICodegenSymbolSelector): ICodegenSymbolOut | undefined;
674
- get symbols(): ReadonlyArray<ICodegenSymbolOut>;
675
- updateSymbol(id: number, symbol: Partial<ICodegenSymbolOut>): ICodegenSymbolOut;
552
+ //#endregion
553
+ //#region src/symbols/registry.d.ts
554
+ declare class SymbolRegistry implements ISymbolRegistry {
555
+ private _id;
556
+ private nodes;
557
+ private registerOrder;
558
+ private selectorToId;
559
+ private values;
560
+ get(symbolIdOrSelector: number | ISelector): ISymbolOut | undefined;
561
+ getValue(symbolId: number): unknown;
562
+ hasValue(symbolId: number): boolean;
563
+ get id(): number;
564
+ private idOrSelector;
565
+ reference(symbolIdOrSelector: number | ISelector): ISymbolOut;
566
+ register(symbol: ISymbolIn): ISymbolOut;
567
+ registered(): IterableIterator<ISymbolOut>;
568
+ setValue(symbolId: number, value: unknown): Map<number, unknown>;
676
569
  }
677
-
678
- declare class CodegenProject implements ICodegenProject {
679
- private fileId;
680
- private fileIdToFile;
681
- private fileOrder;
682
- private filePathToFileId;
683
- private renderers;
684
- private selectorToSymbolIds;
685
- private symbolId;
686
- private symbolIdToFileId;
687
- addExport(fileOrPath: ICodegenFile | string, imp: ICodegenImport): void;
688
- addImport(fileOrPath: ICodegenFile | string, imp: ICodegenImport): void;
689
- addSymbol(fileOrPath: ICodegenFile | string, symbol: ICodegenSymbolIn): ICodegenSymbolOut;
690
- createFile(path: string, meta?: Omit<ICodegenFile['meta'], 'renderer'> & {
691
- /**
692
- * Renderer to use to render this file.
693
- */
694
- renderer?: ICodegenRenderer;
695
- }): ICodegenFile;
696
- ensureFile(fileOrPath: ICodegenFile | string): ICodegenFile;
697
- private ensureRenderer;
698
- get files(): ReadonlyArray<ICodegenFile>;
699
- getAllSymbols(): ReadonlyArray<Pick<ICodegenSymbolOut, 'name'>>;
700
- getFileByPath(path: string): ICodegenFile | undefined;
701
- getFileBySymbolId(id: number): ICodegenFile | undefined;
702
- private getFileRenderer;
703
- getSymbolById(id: number): ICodegenSymbolOut | undefined;
704
- incrementFileId(): number;
705
- incrementSymbolId(): number;
706
- registerSymbol(symbol: ICodegenSymbolOut, file: ICodegenFile): void;
707
- render(meta?: ICodegenMeta): ReadonlyArray<ICodegenOutput>;
708
- selectSymbolAll(selector: ICodegenSymbolSelector, file?: ICodegenFile): ReadonlyArray<ICodegenSymbolOut>;
709
- selectSymbolFirst(selector: ICodegenSymbolSelector, file?: ICodegenFile): ICodegenSymbolOut | undefined;
710
- selectSymbolFirstOrThrow(selector: ICodegenSymbolSelector, file?: ICodegenFile): ICodegenSymbolOut;
711
- selectSymbolLast(selector: ICodegenSymbolSelector, file?: ICodegenFile): ICodegenSymbolOut | undefined;
570
+ //#endregion
571
+ //#region src/project/project.d.ts
572
+ declare class Project implements IProject {
573
+ private symbolIdToFileIds;
574
+ readonly defaultFileName: string;
575
+ readonly files: FileRegistry;
576
+ readonly fileName?: (name: string) => string;
577
+ readonly renderers: Record<string, IRenderer>;
578
+ readonly root: string;
579
+ readonly symbols: SymbolRegistry;
580
+ constructor({
581
+ defaultFileName,
582
+ fileName,
583
+ renderers,
584
+ root
585
+ }: Pick<IProject, 'defaultFileName' | 'fileName' | 'renderers' | 'root'>);
586
+ private getRenderer;
587
+ private prepareFiles;
588
+ render(meta?: IProjectRenderMeta): ReadonlyArray<IOutput>;
589
+ symbolIdToFiles(symbolId: number): ReadonlyArray<IFileOut>;
590
+ private symbolToFileSelector;
712
591
  }
713
-
592
+ //#endregion
593
+ //#region src/renderer/utils.d.ts
714
594
  /**
715
595
  *
716
596
  * @param source The source string to replace.
717
597
  * @param replacerFn Accepts a symbol ID, returns resolved symbol name.
718
598
  * @returns The replaced source string.
719
599
  */
720
- declare const replaceWrappedIds: (source: string, replacerFn: (symbolId: number) => string | undefined) => string;
721
-
722
- 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 };
600
+ declare const renderIds: (source: string, replacerFn: (symbolId: number) => string | undefined) => string;
601
+ //#endregion
602
+ 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 };
603
+ //# sourceMappingURL=index.d.ts.map