@hey-api/codegen-core 0.0.1 → 0.1.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.cjs +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +468 -137
- package/dist/index.d.ts +468 -137
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -1,3 +1,95 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bi-directional map interface.
|
|
3
|
+
*
|
|
4
|
+
* Keys map to values and values map back to keys.
|
|
5
|
+
*
|
|
6
|
+
* @template Key Type of the map keys
|
|
7
|
+
* @template Value Type of the map values
|
|
8
|
+
*/
|
|
9
|
+
interface ICodegenBiMap<Key, Value> {
|
|
10
|
+
/**
|
|
11
|
+
* Deletes a key and its associated value from the map.
|
|
12
|
+
*
|
|
13
|
+
* @param key The key to delete.
|
|
14
|
+
*/
|
|
15
|
+
delete(key: Key): boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Deletes a value and its associated key from the map.
|
|
18
|
+
*
|
|
19
|
+
* @param value The value to delete.
|
|
20
|
+
*/
|
|
21
|
+
deleteValue(value: Value): boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Returns an iterator of [key, value] pairs.
|
|
24
|
+
*/
|
|
25
|
+
entries(): IterableIterator<[Key, Value]>;
|
|
26
|
+
/**
|
|
27
|
+
* Gets the value associated with a key.
|
|
28
|
+
*
|
|
29
|
+
* @param key The key to look up.
|
|
30
|
+
*/
|
|
31
|
+
get(key: Key): Value | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Gets the key associated with a value.
|
|
34
|
+
*
|
|
35
|
+
* @param value The value to look up.
|
|
36
|
+
*/
|
|
37
|
+
getKey(value: Value): Key | undefined;
|
|
38
|
+
/**
|
|
39
|
+
* Checks if a key exists in the map.
|
|
40
|
+
*
|
|
41
|
+
* @param key The key to check.
|
|
42
|
+
*/
|
|
43
|
+
hasKey(key: Key): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Checks if a value exists in the map.
|
|
46
|
+
*
|
|
47
|
+
* @param value The value to check.
|
|
48
|
+
*/
|
|
49
|
+
hasValue(value: Value): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Returns an iterator of keys.
|
|
52
|
+
*/
|
|
53
|
+
keys(): IterableIterator<Key>;
|
|
54
|
+
/**
|
|
55
|
+
* Sets a key-value pair in the map.
|
|
56
|
+
*
|
|
57
|
+
* @param key The key.
|
|
58
|
+
* @param value The value.
|
|
59
|
+
* @returns This instance for chaining.
|
|
60
|
+
*/
|
|
61
|
+
set(key: Key, value: Value): this;
|
|
62
|
+
/**
|
|
63
|
+
* Number of key-value pairs in the map.
|
|
64
|
+
*/
|
|
65
|
+
readonly size: number;
|
|
66
|
+
/**
|
|
67
|
+
* Returns an iterator of values.
|
|
68
|
+
*/
|
|
69
|
+
values(): IterableIterator<Value>;
|
|
70
|
+
/**
|
|
71
|
+
* Enables iteration with `for...of`.
|
|
72
|
+
*/
|
|
73
|
+
[Symbol.iterator](): IterableIterator<[Key, Value]>;
|
|
74
|
+
}
|
|
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
|
+
|
|
1
93
|
/**
|
|
2
94
|
* Arbitrary metadata passed to render functions.
|
|
3
95
|
*
|
|
@@ -43,42 +135,300 @@ interface ICodegenRenderer {
|
|
|
43
135
|
*/
|
|
44
136
|
id: string;
|
|
45
137
|
/**
|
|
46
|
-
* Returns printable data.
|
|
138
|
+
* Returns printable data containing header and imports.
|
|
139
|
+
*
|
|
140
|
+
* @param file The file to render.
|
|
141
|
+
* @param meta Arbitrary metadata.
|
|
142
|
+
* @returns Printable string containing header and imports.
|
|
143
|
+
*/
|
|
144
|
+
renderHeader(file: ICodegenFile, meta?: ICodegenMeta): string;
|
|
145
|
+
/**
|
|
146
|
+
* Returns printable data containing symbols and exports.
|
|
47
147
|
*
|
|
48
148
|
* @param file The file to render.
|
|
49
149
|
* @param meta Arbitrary metadata.
|
|
50
|
-
* @returns
|
|
150
|
+
* @returns Printable string containing symbols and exports.
|
|
51
151
|
*/
|
|
52
|
-
|
|
152
|
+
renderSymbols(file: ICodegenFile, meta?: ICodegenMeta): string;
|
|
153
|
+
/**
|
|
154
|
+
* Function replacing symbols with resolved names.
|
|
155
|
+
*
|
|
156
|
+
* @returns String with replaced symbols.
|
|
157
|
+
*/
|
|
158
|
+
replacerFn(args: {
|
|
159
|
+
file: ICodegenFile;
|
|
160
|
+
headless?: boolean;
|
|
161
|
+
scope?: 'file' | 'project';
|
|
162
|
+
symbolId: number;
|
|
163
|
+
}): string | undefined;
|
|
53
164
|
}
|
|
54
165
|
|
|
55
|
-
|
|
166
|
+
/**
|
|
167
|
+
* Selector array used to select symbols. It doesn't have to be
|
|
168
|
+
* unique, but in practice it might be desirable.
|
|
169
|
+
*
|
|
170
|
+
* @example ["zod", "#/components/schemas/Foo"]
|
|
171
|
+
*/
|
|
172
|
+
type ICodegenSymbolSelector = ReadonlyArray<string>;
|
|
173
|
+
|
|
174
|
+
interface ICodegenSymbolIn {
|
|
56
175
|
/**
|
|
57
|
-
*
|
|
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.
|
|
58
182
|
*
|
|
59
|
-
*
|
|
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.
|
|
191
|
+
*
|
|
192
|
+
* @default false
|
|
60
193
|
*/
|
|
61
|
-
|
|
194
|
+
headless?: boolean;
|
|
62
195
|
/**
|
|
63
|
-
*
|
|
196
|
+
* The desired name for the symbol within its file. If there are multiple symbols
|
|
197
|
+
* with the same desired name, this might not end up being the actual name.
|
|
64
198
|
*
|
|
65
|
-
* @example "
|
|
199
|
+
* @example "UserModel"
|
|
66
200
|
*/
|
|
67
|
-
|
|
201
|
+
readonly name: string;
|
|
68
202
|
/**
|
|
69
|
-
*
|
|
203
|
+
* Selector array used to select this symbol. It doesn't have to be
|
|
204
|
+
* unique, but in practice it might be desirable.
|
|
70
205
|
*
|
|
71
|
-
* @example "
|
|
206
|
+
* @example ["zod", "#/components/schemas/Foo"]
|
|
207
|
+
*/
|
|
208
|
+
readonly selector?: ICodegenSymbolSelector;
|
|
209
|
+
/**
|
|
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`.
|
|
212
|
+
*/
|
|
213
|
+
readonly value?: unknown;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
interface ICodegenSymbolOut extends ICodegenSymbolIn {
|
|
217
|
+
/**
|
|
218
|
+
* The file this symbol is located in.
|
|
219
|
+
*/
|
|
220
|
+
readonly file: ICodegenFile;
|
|
221
|
+
/**
|
|
222
|
+
* Unique symbol ID.
|
|
223
|
+
*/
|
|
224
|
+
readonly id: number;
|
|
225
|
+
/**
|
|
226
|
+
* Placeholder name for the symbol to be replaced later with the final value.
|
|
227
|
+
*
|
|
228
|
+
* @example "_heyapi_31_"
|
|
229
|
+
*/
|
|
230
|
+
readonly placeholder: string;
|
|
231
|
+
/**
|
|
232
|
+
* Updates this symbol.
|
|
233
|
+
*
|
|
234
|
+
* @param symbol The values to update.
|
|
235
|
+
* @returns The updated symbol.
|
|
236
|
+
*/
|
|
237
|
+
readonly update: (symbol: Partial<ICodegenSymbolOut>) => ICodegenSymbolOut;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
interface SelectorMethods {
|
|
241
|
+
/**
|
|
242
|
+
* Retrieves symbols matching the selector.
|
|
243
|
+
*
|
|
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"]);
|
|
249
|
+
*/
|
|
250
|
+
selectSymbolAll(
|
|
251
|
+
selector: ICodegenSymbolSelector,
|
|
252
|
+
file?: ICodegenFile,
|
|
253
|
+
): ReadonlyArray<ICodegenSymbolOut>;
|
|
254
|
+
/**
|
|
255
|
+
* Retrieves the first symbol from all symbols matching the selector.
|
|
256
|
+
*
|
|
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"]);
|
|
262
|
+
*/
|
|
263
|
+
selectSymbolFirst(
|
|
264
|
+
selector: ICodegenSymbolSelector,
|
|
265
|
+
file?: ICodegenFile,
|
|
266
|
+
): ICodegenSymbolOut | undefined;
|
|
267
|
+
/**
|
|
268
|
+
* Retrieves the first symbol from all symbols matching the selector.
|
|
269
|
+
*
|
|
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"]);
|
|
275
|
+
*/
|
|
276
|
+
selectSymbolFirstOrThrow(
|
|
277
|
+
selector: ICodegenSymbolSelector,
|
|
278
|
+
file?: ICodegenFile,
|
|
279
|
+
): ICodegenSymbolOut;
|
|
280
|
+
/**
|
|
281
|
+
* Retrieves the last symbol from all symbols matching the selector.
|
|
282
|
+
*
|
|
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"]);
|
|
288
|
+
*/
|
|
289
|
+
selectSymbolLast(
|
|
290
|
+
selector: ICodegenSymbolSelector,
|
|
291
|
+
file?: ICodegenFile,
|
|
292
|
+
): ICodegenSymbolOut | undefined;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* Represents a code generation project consisting of multiple codegen files.
|
|
297
|
+
* Manages imports, symbols, and output generation across the project.
|
|
298
|
+
*/
|
|
299
|
+
interface ICodegenProject extends SelectorMethods {
|
|
300
|
+
/**
|
|
301
|
+
* Adds an export declaration to a specific file, creating the file if it doesn't exist.
|
|
302
|
+
*
|
|
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"] });
|
|
307
|
+
*/
|
|
308
|
+
addExport(fileOrPath: ICodegenFile | string, imp: ICodegenImport): void;
|
|
309
|
+
/**
|
|
310
|
+
* Adds an import declaration to a specific file, creating the file if it doesn't exist.
|
|
311
|
+
*
|
|
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"] });
|
|
316
|
+
*/
|
|
317
|
+
addImport(fileOrPath: ICodegenFile | string, imp: ICodegenImport): void;
|
|
318
|
+
/**
|
|
319
|
+
* Adds a symbol to a specific file, creating the file if it doesn't exist.
|
|
320
|
+
*
|
|
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 });
|
|
326
|
+
*/
|
|
327
|
+
addSymbol(
|
|
328
|
+
fileOrPath: ICodegenFile | string,
|
|
329
|
+
symbol: ICodegenSymbolIn,
|
|
330
|
+
): ICodegenSymbolOut;
|
|
331
|
+
/**
|
|
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.
|
|
335
|
+
*
|
|
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 });
|
|
341
|
+
*/
|
|
342
|
+
createFile(
|
|
343
|
+
path: string,
|
|
344
|
+
meta?: ICodegenFile['meta'] & { renderer?: ICodegenRenderer },
|
|
345
|
+
): ICodegenFile;
|
|
346
|
+
/**
|
|
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");
|
|
357
|
+
*/
|
|
358
|
+
ensureFile(fileOrPath: ICodegenFile | string): ICodegenFile;
|
|
359
|
+
/**
|
|
360
|
+
* Returns all files in the project in insertion order.
|
|
361
|
+
*
|
|
362
|
+
* @example
|
|
363
|
+
* project.files.forEach(file => console.log(file.path));
|
|
364
|
+
*/
|
|
365
|
+
readonly files: ReadonlyArray<ICodegenFile>;
|
|
366
|
+
/**
|
|
367
|
+
* Returns all symbols declared or imported across all files.
|
|
368
|
+
*
|
|
369
|
+
* @returns Flattened list of all codegen symbols.
|
|
370
|
+
* @example
|
|
371
|
+
* project.getAllSymbols().filter(s => s.name === "User");
|
|
372
|
+
*/
|
|
373
|
+
getAllSymbols(): ReadonlyArray<Pick<ICodegenSymbolOut, 'name'>>;
|
|
374
|
+
/**
|
|
375
|
+
* Retrieves a file by its logical output path.
|
|
376
|
+
*
|
|
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");
|
|
381
|
+
*/
|
|
382
|
+
getFileByPath(path: string): ICodegenFile | undefined;
|
|
383
|
+
/**
|
|
384
|
+
* Retrieves a file from symbol ID included in the file.
|
|
385
|
+
*
|
|
386
|
+
* @param id The symbol ID to find.
|
|
387
|
+
* @returns The file if found, undefined otherwise.
|
|
388
|
+
* @example
|
|
389
|
+
* const file = project.getFileBySymbolId(31);
|
|
390
|
+
*/
|
|
391
|
+
getFileBySymbolId(id: number): ICodegenFile | undefined;
|
|
392
|
+
/**
|
|
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);
|
|
72
399
|
*/
|
|
73
|
-
|
|
400
|
+
getSymbolById(id: number): ICodegenSymbolOut | undefined;
|
|
74
401
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
402
|
+
* Returns the current file ID and increments it.
|
|
403
|
+
*
|
|
404
|
+
* @returns File ID before being incremented
|
|
77
405
|
*/
|
|
78
|
-
|
|
406
|
+
incrementFileId(): number;
|
|
407
|
+
/**
|
|
408
|
+
* Returns the current symbol ID and increments it.
|
|
409
|
+
*
|
|
410
|
+
* @returns Symbol ID before being incremented
|
|
411
|
+
*/
|
|
412
|
+
incrementSymbolId(): number;
|
|
413
|
+
/**
|
|
414
|
+
* Tracks added symbol across the project.
|
|
415
|
+
*
|
|
416
|
+
* @param symbol The symbol added to file.
|
|
417
|
+
* @param file The file containing the added symbol.
|
|
418
|
+
*/
|
|
419
|
+
registerSymbol(symbol: ICodegenSymbolOut, file: ICodegenFile): void;
|
|
420
|
+
/**
|
|
421
|
+
* Produces output representations for all files in the project.
|
|
422
|
+
*
|
|
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));
|
|
427
|
+
*/
|
|
428
|
+
render(meta?: ICodegenMeta): ReadonlyArray<ICodegenOutput>;
|
|
79
429
|
}
|
|
80
430
|
|
|
81
|
-
interface ICodegenFile {
|
|
431
|
+
interface ICodegenFile extends SelectorMethods {
|
|
82
432
|
/**
|
|
83
433
|
* Adds an export to this file.
|
|
84
434
|
*
|
|
@@ -98,7 +448,20 @@ interface ICodegenFile {
|
|
|
98
448
|
*
|
|
99
449
|
* @param symbol The symbol to add
|
|
100
450
|
*/
|
|
101
|
-
addSymbol(symbol:
|
|
451
|
+
addSymbol(symbol: ICodegenSymbolIn): ICodegenSymbolOut;
|
|
452
|
+
/**
|
|
453
|
+
* Ensures a symbol for the given selector exists, so it can be
|
|
454
|
+
* safely used.
|
|
455
|
+
*
|
|
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;
|
|
102
465
|
/**
|
|
103
466
|
* Symbols exported from other files.
|
|
104
467
|
**/
|
|
@@ -108,7 +471,14 @@ interface ICodegenFile {
|
|
|
108
471
|
*
|
|
109
472
|
* @returns List of all symbols used in this file
|
|
110
473
|
*/
|
|
111
|
-
getAllSymbols(): ReadonlyArray<
|
|
474
|
+
getAllSymbols(): ReadonlyArray<Pick<ICodegenSymbolOut, 'name'>>;
|
|
475
|
+
/**
|
|
476
|
+
* Finds a symbol by symbol ID.
|
|
477
|
+
*
|
|
478
|
+
* @param id Symbol ID
|
|
479
|
+
* @returns The symbol if it exists, undefined otherwise.
|
|
480
|
+
*/
|
|
481
|
+
getSymbolById(id: number): ICodegenSymbolOut | undefined;
|
|
112
482
|
/**
|
|
113
483
|
* Checks if this file contains any content.
|
|
114
484
|
*
|
|
@@ -121,10 +491,14 @@ interface ICodegenFile {
|
|
|
121
491
|
/**
|
|
122
492
|
* Checks if this file defines a symbol with the given name.
|
|
123
493
|
*
|
|
124
|
-
* @param
|
|
494
|
+
* @param id Symbol ID to check
|
|
125
495
|
* @returns True if the symbol is defined by this file
|
|
126
496
|
*/
|
|
127
|
-
hasSymbol(
|
|
497
|
+
hasSymbol(id: number): boolean;
|
|
498
|
+
/**
|
|
499
|
+
* File ID within the project.
|
|
500
|
+
*/
|
|
501
|
+
id: number;
|
|
128
502
|
/**
|
|
129
503
|
* Symbols imported from other files.
|
|
130
504
|
**/
|
|
@@ -164,6 +538,10 @@ interface ICodegenFile {
|
|
|
164
538
|
* @example "models/user.ts"
|
|
165
539
|
*/
|
|
166
540
|
path: string;
|
|
541
|
+
/**
|
|
542
|
+
* Parent project this file belongs to.
|
|
543
|
+
*/
|
|
544
|
+
project: ICodegenProject;
|
|
167
545
|
/**
|
|
168
546
|
* Returns a relative path to this file from another file.
|
|
169
547
|
*
|
|
@@ -178,10 +556,25 @@ interface ICodegenFile {
|
|
|
178
556
|
* @example "./another-file.ts"
|
|
179
557
|
*/
|
|
180
558
|
relativePathToFile(file: Pick<ICodegenFile, 'path'>): string;
|
|
559
|
+
/**
|
|
560
|
+
* Map holding resolved names for symbols in this file.
|
|
561
|
+
*/
|
|
562
|
+
resolvedNames: ICodegenBiMap<number, string>;
|
|
181
563
|
/**
|
|
182
564
|
* Top-level symbols declared in this file.
|
|
183
565
|
**/
|
|
184
|
-
symbols: ReadonlyArray<
|
|
566
|
+
symbols: ReadonlyArray<ICodegenSymbolOut>;
|
|
567
|
+
/**
|
|
568
|
+
* Updates a symbol defined by this file.
|
|
569
|
+
*
|
|
570
|
+
* @param id ID of symbol to update.
|
|
571
|
+
* @param symbol The values to update.
|
|
572
|
+
* @returns The updated symbol.
|
|
573
|
+
*/
|
|
574
|
+
updateSymbol(
|
|
575
|
+
id: number,
|
|
576
|
+
symbol: Partial<ICodegenSymbolOut>,
|
|
577
|
+
): ICodegenSymbolOut;
|
|
185
578
|
}
|
|
186
579
|
|
|
187
580
|
interface ICodegenImport {
|
|
@@ -249,143 +642,81 @@ interface ICodegenImport {
|
|
|
249
642
|
|
|
250
643
|
declare class CodegenFile implements ICodegenFile {
|
|
251
644
|
path: string;
|
|
645
|
+
project: ICodegenProject;
|
|
252
646
|
meta: ICodegenFile['meta'];
|
|
253
647
|
private cache;
|
|
648
|
+
private renderSymbols;
|
|
254
649
|
private state;
|
|
255
|
-
|
|
650
|
+
id: number;
|
|
651
|
+
resolvedNames: ICodegenBiMap<number, string>;
|
|
652
|
+
constructor(path: string, project: ICodegenProject, meta?: ICodegenFile['meta']);
|
|
256
653
|
addExport(exp: ICodegenImport): void;
|
|
257
654
|
addImport(imp: ICodegenImport): void;
|
|
258
655
|
private addImportExport;
|
|
259
|
-
|
|
656
|
+
private addRenderSymbol;
|
|
657
|
+
addSymbol(symbol: ICodegenSymbolIn): ICodegenSymbolOut;
|
|
658
|
+
ensureSymbol(symbol: Partial<ICodegenSymbolIn> & Pick<Required<ICodegenSymbolIn>, 'selector'>): ICodegenSymbolOut;
|
|
260
659
|
get exports(): ReadonlyArray<ICodegenImport>;
|
|
261
|
-
getAllSymbols(): ReadonlyArray<
|
|
660
|
+
getAllSymbols(): ReadonlyArray<Pick<ICodegenSymbolOut, 'name'>>;
|
|
661
|
+
private getImportExportKey;
|
|
662
|
+
getSymbolById(id: number): ICodegenSymbolOut | undefined;
|
|
262
663
|
hasContent(): boolean;
|
|
263
|
-
hasSymbol(
|
|
664
|
+
hasSymbol(id: number): boolean;
|
|
264
665
|
get imports(): ReadonlyArray<ICodegenImport>;
|
|
265
666
|
private mergeImportExportValues;
|
|
266
667
|
static pathToFilePath(source: string): string;
|
|
267
668
|
relativePathFromFile(file: Pick<ICodegenFile, 'path'>): string;
|
|
268
669
|
relativePathToFile(file: Pick<ICodegenFile, 'path'>): string;
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
*/
|
|
276
|
-
interface ICodegenProject {
|
|
277
|
-
/**
|
|
278
|
-
* Adds an export declaration to a specific file, creating the file if it doesn't exist.
|
|
279
|
-
*
|
|
280
|
-
* @param fileOrPath - File instance or file path where to add the export.
|
|
281
|
-
* @param imp - The export declaration to add.
|
|
282
|
-
* @example
|
|
283
|
-
* project.addExportToFile("models/user.ts", { from: "lib", names: ["User"] });
|
|
284
|
-
*/
|
|
285
|
-
addExportToFile(fileOrPath: ICodegenFile | string, imp: ICodegenImport): void;
|
|
286
|
-
/**
|
|
287
|
-
* Adds an import declaration to a specific file, creating the file if it doesn't exist.
|
|
288
|
-
*
|
|
289
|
-
* @param fileOrPath - File instance or file path where to add the import.
|
|
290
|
-
* @param imp - The import declaration to add.
|
|
291
|
-
* @example
|
|
292
|
-
* project.addImportToFile("models/user.ts", { from: "lib", names: ["User"] });
|
|
293
|
-
*/
|
|
294
|
-
addImportToFile(fileOrPath: ICodegenFile | string, imp: ICodegenImport): void;
|
|
295
|
-
/**
|
|
296
|
-
* Adds a symbol to a specific file, creating the file if it doesn't exist.
|
|
297
|
-
*
|
|
298
|
-
* @param fileOrPath - File instance or file path where to add the symbol.
|
|
299
|
-
* @param symbol - The symbol to add.
|
|
300
|
-
* @example
|
|
301
|
-
* project.addSymbolToFile("models/user.ts", { name: "User", value: tsNode });
|
|
302
|
-
*/
|
|
303
|
-
addSymbolToFile(
|
|
304
|
-
fileOrPath: ICodegenFile | string,
|
|
305
|
-
symbol: ICodegenSymbol,
|
|
306
|
-
): void;
|
|
307
|
-
/**
|
|
308
|
-
* Creates a new codegen file with optional metadata and adds it to the project.
|
|
309
|
-
*
|
|
310
|
-
* If a file with the same path already exists, it is returned instead.
|
|
311
|
-
*
|
|
312
|
-
* @param path - The logical output path for the file (e.g. "models/user.ts").
|
|
313
|
-
* @param meta - Optional renderer and metadata to attach to the file (e.g. { isInternal: true }).
|
|
314
|
-
* @returns The newly created file instance.
|
|
315
|
-
* @example
|
|
316
|
-
* const file = project.createFile("models/user.ts", { isInternal: true });
|
|
317
|
-
*/
|
|
318
|
-
createFile(
|
|
319
|
-
path: string,
|
|
320
|
-
meta?: ICodegenFile['meta'] & { renderer?: ICodegenRenderer },
|
|
321
|
-
): ICodegenFile;
|
|
322
|
-
/**
|
|
323
|
-
* Ensures a codegen file exists and returns it.
|
|
324
|
-
*
|
|
325
|
-
* If a file does not exist yet, it is created with minimal information.
|
|
326
|
-
* Later, it is expected `createFile()` will be called which will fill in
|
|
327
|
-
* the missing information such as optional metadata.
|
|
328
|
-
*
|
|
329
|
-
* @param fileOrPath - The logical output path for the file or the file itself.
|
|
330
|
-
* @returns The file instance.
|
|
331
|
-
* @example
|
|
332
|
-
* const file = project.ensureFile("models/user.ts");
|
|
333
|
-
*/
|
|
334
|
-
ensureFile(fileOrPath: ICodegenFile | string): ICodegenFile;
|
|
335
|
-
/**
|
|
336
|
-
* Returns all files in the project in insertion order.
|
|
337
|
-
*
|
|
338
|
-
* @example
|
|
339
|
-
* project.files.forEach(file => console.log(file.path));
|
|
340
|
-
*/
|
|
341
|
-
readonly files: ReadonlyArray<ICodegenFile>;
|
|
342
|
-
/**
|
|
343
|
-
* Returns all symbols declared or imported across all files.
|
|
344
|
-
*
|
|
345
|
-
* @returns Flattened list of all codegen symbols.
|
|
346
|
-
* @example
|
|
347
|
-
* project.getAllSymbols().filter(s => s.name === "User");
|
|
348
|
-
*/
|
|
349
|
-
getAllSymbols(): ReadonlyArray<ICodegenSymbol>;
|
|
350
|
-
/**
|
|
351
|
-
* Retrieves a file by its logical output path.
|
|
352
|
-
*
|
|
353
|
-
* @param path - The file path to find.
|
|
354
|
-
* @returns The file if found, or undefined otherwise.
|
|
355
|
-
* @example
|
|
356
|
-
* const file = project.getFileByPath("models/user.ts");
|
|
357
|
-
*/
|
|
358
|
-
getFileByPath(path: string): ICodegenFile | undefined;
|
|
359
|
-
/**
|
|
360
|
-
* Produces output representations for all files in the project.
|
|
361
|
-
*
|
|
362
|
-
* @param meta Arbitrary metadata.
|
|
363
|
-
* @returns Array of outputs ready for writing or further processing.
|
|
364
|
-
* @example
|
|
365
|
-
* project.render().forEach(output => writeFile(output));
|
|
366
|
-
*/
|
|
367
|
-
render(meta?: ICodegenMeta): ReadonlyArray<ICodegenOutput>;
|
|
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;
|
|
368
676
|
}
|
|
369
677
|
|
|
370
678
|
declare class CodegenProject implements ICodegenProject {
|
|
371
|
-
private
|
|
372
|
-
private
|
|
679
|
+
private fileId;
|
|
680
|
+
private fileIdToFile;
|
|
681
|
+
private fileOrder;
|
|
682
|
+
private filePathToFileId;
|
|
373
683
|
private renderers;
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
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'> & {
|
|
378
691
|
/**
|
|
379
692
|
* Renderer to use to render this file.
|
|
380
693
|
*/
|
|
381
694
|
renderer?: ICodegenRenderer;
|
|
382
|
-
}):
|
|
383
|
-
ensureFile(fileOrPath:
|
|
695
|
+
}): ICodegenFile;
|
|
696
|
+
ensureFile(fileOrPath: ICodegenFile | string): ICodegenFile;
|
|
384
697
|
private ensureRenderer;
|
|
385
|
-
get files(): ReadonlyArray<
|
|
386
|
-
getAllSymbols(): ReadonlyArray<
|
|
387
|
-
getFileByPath(path: string):
|
|
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;
|
|
388
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;
|
|
389
712
|
}
|
|
390
713
|
|
|
391
|
-
|
|
714
|
+
/**
|
|
715
|
+
*
|
|
716
|
+
* @param source The source string to replace.
|
|
717
|
+
* @param replacerFn Accepts a symbol ID, returns resolved symbol name.
|
|
718
|
+
* @returns The replaced source string.
|
|
719
|
+
*/
|
|
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 };
|