@kubb/ast 5.0.0-beta.58 → 5.0.0-beta.59
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/README.md +10 -0
- package/dist/casing-BE2R1RXg.cjs +88 -0
- package/dist/casing-BE2R1RXg.cjs.map +1 -0
- package/dist/extractStringsFromNodes-WMYJ8nQL.d.ts +82 -0
- package/dist/factory-BmcGBdeg.cjs +1251 -0
- package/dist/factory-BmcGBdeg.cjs.map +1 -0
- package/dist/factory-Du7nEP4B.js +282 -0
- package/dist/factory-Du7nEP4B.js.map +1 -0
- package/dist/factory.cjs +25 -28
- package/dist/factory.d.ts +3 -3
- package/dist/factory.js +3 -3
- package/dist/{ast-ClnJg9BN.d.ts → index-BzjwdK2M.d.ts} +74 -372
- package/dist/index.cjs +445 -46
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +94 -59
- package/dist/index.js +7 -113
- package/dist/index.js.map +1 -1
- package/dist/operationParams-BZ07xDm0.d.ts +204 -0
- package/dist/response-DKxTr522.js +683 -0
- package/dist/response-DKxTr522.js.map +1 -0
- package/dist/{types-CB2oY8Dw.d.ts → types-olVl9v5p.d.ts} +222 -227
- package/dist/types.d.ts +4 -3
- package/dist/utils-BCtRXfhI.cjs +275 -0
- package/dist/utils-BCtRXfhI.cjs.map +1 -0
- package/dist/{utils-DN4XLVqz.js → utils-SdZU0F3H.js} +472 -1235
- package/dist/utils-SdZU0F3H.js.map +1 -0
- package/dist/utils.cjs +127 -22
- package/dist/utils.d.ts +112 -80
- package/dist/utils.js +3 -2
- package/package.json +1 -1
- package/src/constants.ts +8 -14
- package/src/dedupe.ts +8 -0
- package/src/factory.ts +1 -2
- package/src/guards.ts +1 -1
- package/src/index.ts +4 -13
- package/src/nodes/code.ts +29 -47
- package/src/nodes/content.ts +2 -2
- package/src/nodes/file.ts +28 -23
- package/src/nodes/function.ts +0 -15
- package/src/nodes/input.ts +6 -6
- package/src/nodes/operation.ts +1 -1
- package/src/nodes/parameter.ts +0 -3
- package/src/nodes/property.ts +0 -3
- package/src/nodes/requestBody.ts +3 -6
- package/src/nodes/schema.ts +3 -2
- package/src/printer.ts +1 -1
- package/src/registry.ts +31 -26
- package/src/signature.ts +3 -3
- package/src/transformers.ts +28 -1
- package/src/types.ts +2 -53
- package/src/utils/codegen.ts +104 -0
- package/src/utils/fileMerge.ts +184 -0
- package/src/utils/index.ts +7 -339
- package/src/utils/operationParams.ts +353 -0
- package/src/utils/refs.ts +112 -0
- package/src/utils/schemaGraph.ts +169 -0
- package/src/utils/strings.ts +139 -0
- package/src/visitor.ts +43 -19
- package/dist/extractStringsFromNodes-Bn9cOos9.d.ts +0 -14
- package/dist/factory-C5gHvtLU.js +0 -138
- package/dist/factory-C5gHvtLU.js.map +0 -1
- package/dist/factory-JN-Ylfl6.cjs +0 -155
- package/dist/factory-JN-Ylfl6.cjs.map +0 -1
- package/dist/utils-C8bWAzhv.cjs +0 -2696
- package/dist/utils-C8bWAzhv.cjs.map +0 -1
- package/dist/utils-DN4XLVqz.js.map +0 -1
- package/src/utils/ast.ts +0 -816
|
@@ -0,0 +1,1251 @@
|
|
|
1
|
+
const require_casing = require("./casing-BE2R1RXg.cjs");
|
|
2
|
+
let node_crypto = require("node:crypto");
|
|
3
|
+
let node_path = require("node:path");
|
|
4
|
+
node_path = require_casing.__toESM(node_path, 1);
|
|
5
|
+
//#region src/node.ts
|
|
6
|
+
/**
|
|
7
|
+
* Builds a type guard that matches nodes of the given `kind`.
|
|
8
|
+
*/
|
|
9
|
+
function isKind(kind) {
|
|
10
|
+
return (node) => node.kind === kind;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Updates a schema's `optional` and `nullish` flags from a parent's `required`
|
|
14
|
+
* value and the schema's own `nullable`. Mirrors how OpenAPI parameters and
|
|
15
|
+
* object properties combine "required" and "nullable" into a single AST.
|
|
16
|
+
*
|
|
17
|
+
* - Non-required + non-nullable → `optional: true`.
|
|
18
|
+
* - Non-required + nullable → `nullish: true`.
|
|
19
|
+
* - Required → both flags cleared.
|
|
20
|
+
*/
|
|
21
|
+
function syncOptionality(schema, required) {
|
|
22
|
+
const nullable = schema.nullable ?? false;
|
|
23
|
+
return {
|
|
24
|
+
...schema,
|
|
25
|
+
optional: !required && !nullable ? true : void 0,
|
|
26
|
+
nullish: !required && nullable ? true : void 0
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Defines a node once and derives its `create` builder, `is` guard, and traversal
|
|
31
|
+
* metadata. `create` merges `defaults`, the `build` hook (or the raw input), and the
|
|
32
|
+
* `kind`, so node construction lives in one place without scattered `as` casts.
|
|
33
|
+
*
|
|
34
|
+
* Set `rebuild: true` when the `build` hook derives fields from children. After a
|
|
35
|
+
* transform rewrites those children, the registry reruns `create` so the derived
|
|
36
|
+
* fields stay correct.
|
|
37
|
+
*
|
|
38
|
+
* @example Simple node
|
|
39
|
+
* ```ts
|
|
40
|
+
* const importDef = defineNode<ImportNode>({ kind: 'Import' })
|
|
41
|
+
* const createImport = importDef.create
|
|
42
|
+
* ```
|
|
43
|
+
*
|
|
44
|
+
* @example Node with a build hook that is rerun on transform
|
|
45
|
+
* ```ts
|
|
46
|
+
* const propertyDef = defineNode<PropertyNode, UserPropertyNode>({
|
|
47
|
+
* kind: 'Property',
|
|
48
|
+
* build: (props) => ({ ...props, required: props.required ?? false }),
|
|
49
|
+
* children: ['schema'],
|
|
50
|
+
* visitorKey: 'property',
|
|
51
|
+
* rebuild: true,
|
|
52
|
+
* })
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
function defineNode(config) {
|
|
56
|
+
const { kind, defaults, build, children, visitorKey, rebuild } = config;
|
|
57
|
+
function create(input) {
|
|
58
|
+
const base = build ? build(input) : input;
|
|
59
|
+
return {
|
|
60
|
+
...defaults,
|
|
61
|
+
...base,
|
|
62
|
+
kind
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
kind,
|
|
67
|
+
create,
|
|
68
|
+
is: isKind(kind),
|
|
69
|
+
children,
|
|
70
|
+
visitorKey,
|
|
71
|
+
rebuild
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region src/nodes/schema.ts
|
|
76
|
+
/**
|
|
77
|
+
* Maps schema `type` to its underlying `primitive`.
|
|
78
|
+
* Primitive types map to themselves. Special string formats map to `'string'`.
|
|
79
|
+
* Complex types (`ref`, `enum`, `union`, `intersection`, `tuple`, `blob`) are left unset.
|
|
80
|
+
*/
|
|
81
|
+
const TYPE_TO_PRIMITIVE = {
|
|
82
|
+
string: "string",
|
|
83
|
+
number: "number",
|
|
84
|
+
integer: "integer",
|
|
85
|
+
bigint: "bigint",
|
|
86
|
+
boolean: "boolean",
|
|
87
|
+
null: "null",
|
|
88
|
+
any: "any",
|
|
89
|
+
unknown: "unknown",
|
|
90
|
+
void: "void",
|
|
91
|
+
never: "never",
|
|
92
|
+
object: "object",
|
|
93
|
+
array: "array",
|
|
94
|
+
date: "date",
|
|
95
|
+
uuid: "string",
|
|
96
|
+
email: "string",
|
|
97
|
+
url: "string",
|
|
98
|
+
datetime: "string",
|
|
99
|
+
time: "string"
|
|
100
|
+
};
|
|
101
|
+
/**
|
|
102
|
+
* Definition for the {@link SchemaNode}. Object schemas default `properties` to an
|
|
103
|
+
* empty array, and `primitive` is inferred from `type` when not explicitly provided.
|
|
104
|
+
*/
|
|
105
|
+
const schemaDef = defineNode({
|
|
106
|
+
kind: "Schema",
|
|
107
|
+
build: (props) => {
|
|
108
|
+
if (props.type === "object") return {
|
|
109
|
+
properties: [],
|
|
110
|
+
primitive: "object",
|
|
111
|
+
...props
|
|
112
|
+
};
|
|
113
|
+
return {
|
|
114
|
+
primitive: TYPE_TO_PRIMITIVE[props.type],
|
|
115
|
+
...props
|
|
116
|
+
};
|
|
117
|
+
},
|
|
118
|
+
children: [
|
|
119
|
+
"properties",
|
|
120
|
+
"items",
|
|
121
|
+
"members",
|
|
122
|
+
"additionalProperties"
|
|
123
|
+
],
|
|
124
|
+
visitorKey: "schema"
|
|
125
|
+
});
|
|
126
|
+
function createSchema(props) {
|
|
127
|
+
return schemaDef.create(props);
|
|
128
|
+
}
|
|
129
|
+
//#endregion
|
|
130
|
+
//#region ../../internals/utils/src/fs.ts
|
|
131
|
+
/**
|
|
132
|
+
* Strips the file extension from a path or file name.
|
|
133
|
+
* Only removes the last `.ext` segment when the dot is not part of a directory name.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* trimExtName('petStore.ts') // 'petStore'
|
|
137
|
+
* trimExtName('/src/models/pet.ts') // '/src/models/pet'
|
|
138
|
+
* trimExtName('/project.v2/gen/pet.ts') // '/project.v2/gen/pet'
|
|
139
|
+
* trimExtName('noExtension') // 'noExtension'
|
|
140
|
+
*/
|
|
141
|
+
function trimExtName(text) {
|
|
142
|
+
const dotIndex = text.lastIndexOf(".");
|
|
143
|
+
if (dotIndex > 0 && !text.includes("/", dotIndex)) return text.slice(0, dotIndex);
|
|
144
|
+
return text;
|
|
145
|
+
}
|
|
146
|
+
//#endregion
|
|
147
|
+
//#region src/nodes/code.ts
|
|
148
|
+
/**
|
|
149
|
+
* Definition for the {@link ConstNode}.
|
|
150
|
+
*/
|
|
151
|
+
const constDef = defineNode({ kind: "Const" });
|
|
152
|
+
/**
|
|
153
|
+
* Creates a `ConstNode` representing a TypeScript `const` declaration.
|
|
154
|
+
*
|
|
155
|
+
* @example Exported constant with type and `as const`
|
|
156
|
+
* ```ts
|
|
157
|
+
* createConst({ name: 'pets', export: true, type: 'Pet[]', asConst: true })
|
|
158
|
+
* // export const pets: Pet[] = ... as const
|
|
159
|
+
* ```
|
|
160
|
+
*/
|
|
161
|
+
const createConst = constDef.create;
|
|
162
|
+
/**
|
|
163
|
+
* Definition for the {@link TypeNode}.
|
|
164
|
+
*/
|
|
165
|
+
const typeDef = defineNode({ kind: "Type" });
|
|
166
|
+
/**
|
|
167
|
+
* Creates a `TypeNode` representing a TypeScript `type` alias declaration.
|
|
168
|
+
*
|
|
169
|
+
* @example
|
|
170
|
+
* ```ts
|
|
171
|
+
* createType({ name: 'Pet', export: true })
|
|
172
|
+
* // export type Pet = ...
|
|
173
|
+
* ```
|
|
174
|
+
*/
|
|
175
|
+
const createType = typeDef.create;
|
|
176
|
+
/**
|
|
177
|
+
* Definition for the {@link FunctionNode}.
|
|
178
|
+
*/
|
|
179
|
+
const functionDef = defineNode({ kind: "Function" });
|
|
180
|
+
/**
|
|
181
|
+
* Creates a `FunctionNode` representing a TypeScript `function` declaration.
|
|
182
|
+
*
|
|
183
|
+
* @example
|
|
184
|
+
* ```ts
|
|
185
|
+
* createFunction({ name: 'fetchPet', export: true, async: true, returnType: 'Pet' })
|
|
186
|
+
* // export async function fetchPet(): Promise<Pet> { ... }
|
|
187
|
+
* ```
|
|
188
|
+
*/
|
|
189
|
+
const createFunction = functionDef.create;
|
|
190
|
+
/**
|
|
191
|
+
* Definition for the {@link ArrowFunctionNode}.
|
|
192
|
+
*/
|
|
193
|
+
const arrowFunctionDef = defineNode({ kind: "ArrowFunction" });
|
|
194
|
+
/**
|
|
195
|
+
* Creates an `ArrowFunctionNode` representing a TypeScript arrow function.
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
* ```ts
|
|
199
|
+
* createArrowFunction({ name: 'double', export: true, params: 'n: number', singleLine: true })
|
|
200
|
+
* // export const double = (n: number) => ...
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
const createArrowFunction = arrowFunctionDef.create;
|
|
204
|
+
/**
|
|
205
|
+
* Definition for the {@link TextNode}.
|
|
206
|
+
*/
|
|
207
|
+
const textDef = defineNode({
|
|
208
|
+
kind: "Text",
|
|
209
|
+
build: (value) => ({ value })
|
|
210
|
+
});
|
|
211
|
+
/**
|
|
212
|
+
* Creates a {@link TextNode} representing a raw string fragment in the source output.
|
|
213
|
+
*
|
|
214
|
+
* @example
|
|
215
|
+
* ```ts
|
|
216
|
+
* createText('return fetch(id)')
|
|
217
|
+
* // { kind: 'Text', value: 'return fetch(id)' }
|
|
218
|
+
* ```
|
|
219
|
+
*/
|
|
220
|
+
const createText = textDef.create;
|
|
221
|
+
/**
|
|
222
|
+
* Definition for the {@link BreakNode}.
|
|
223
|
+
*/
|
|
224
|
+
const breakDef = defineNode({
|
|
225
|
+
kind: "Break",
|
|
226
|
+
build: () => ({})
|
|
227
|
+
});
|
|
228
|
+
/**
|
|
229
|
+
* Creates a {@link BreakNode} representing a line break in the source output.
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* ```ts
|
|
233
|
+
* createBreak()
|
|
234
|
+
* // { kind: 'Break' }
|
|
235
|
+
* ```
|
|
236
|
+
*/
|
|
237
|
+
function createBreak() {
|
|
238
|
+
return breakDef.create();
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Definition for the {@link JsxNode}.
|
|
242
|
+
*/
|
|
243
|
+
const jsxDef = defineNode({
|
|
244
|
+
kind: "Jsx",
|
|
245
|
+
build: (value) => ({ value })
|
|
246
|
+
});
|
|
247
|
+
/**
|
|
248
|
+
* Creates a {@link JsxNode} representing a raw JSX fragment in the source output.
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* ```ts
|
|
252
|
+
* createJsx('<>\n <a href={href}>Open</a>\n</>')
|
|
253
|
+
* // { kind: 'Jsx', value: '<>\n <a href={href}>Open</a>\n</>' }
|
|
254
|
+
* ```
|
|
255
|
+
*/
|
|
256
|
+
const createJsx = jsxDef.create;
|
|
257
|
+
//#endregion
|
|
258
|
+
//#region src/nodes/content.ts
|
|
259
|
+
/**
|
|
260
|
+
* Definition for the {@link ContentNode}.
|
|
261
|
+
*/
|
|
262
|
+
const contentDef = defineNode({
|
|
263
|
+
kind: "Content",
|
|
264
|
+
children: ["schema"]
|
|
265
|
+
});
|
|
266
|
+
/**
|
|
267
|
+
* Creates a `ContentNode` for a single request-body or response content type.
|
|
268
|
+
*/
|
|
269
|
+
const createContent = contentDef.create;
|
|
270
|
+
//#endregion
|
|
271
|
+
//#region src/nodes/file.ts
|
|
272
|
+
/**
|
|
273
|
+
* Definition for the {@link ImportNode}.
|
|
274
|
+
*/
|
|
275
|
+
const importDef = defineNode({ kind: "Import" });
|
|
276
|
+
/**
|
|
277
|
+
* Creates an `ImportNode` representing a language-agnostic import/dependency declaration.
|
|
278
|
+
*
|
|
279
|
+
* @example Named import
|
|
280
|
+
* ```ts
|
|
281
|
+
* createImport({ name: ['useState'], path: 'react' })
|
|
282
|
+
* // import { useState } from 'react'
|
|
283
|
+
* ```
|
|
284
|
+
*/
|
|
285
|
+
const createImport = importDef.create;
|
|
286
|
+
/**
|
|
287
|
+
* Definition for the {@link ExportNode}.
|
|
288
|
+
*/
|
|
289
|
+
const exportDef = defineNode({ kind: "Export" });
|
|
290
|
+
/**
|
|
291
|
+
* Creates an `ExportNode` representing a language-agnostic export/public API declaration.
|
|
292
|
+
*
|
|
293
|
+
* @example Named export
|
|
294
|
+
* ```ts
|
|
295
|
+
* createExport({ name: ['Pet'], path: './Pet' })
|
|
296
|
+
* // export { Pet } from './Pet'
|
|
297
|
+
* ```
|
|
298
|
+
*/
|
|
299
|
+
const createExport = exportDef.create;
|
|
300
|
+
/**
|
|
301
|
+
* Definition for the {@link SourceNode}.
|
|
302
|
+
*/
|
|
303
|
+
const sourceDef = defineNode({ kind: "Source" });
|
|
304
|
+
/**
|
|
305
|
+
* Creates a `SourceNode` representing a fragment of source code within a file.
|
|
306
|
+
*
|
|
307
|
+
* @example
|
|
308
|
+
* ```ts
|
|
309
|
+
* createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')], isExportable: true })
|
|
310
|
+
* ```
|
|
311
|
+
*/
|
|
312
|
+
const createSource = sourceDef.create;
|
|
313
|
+
/**
|
|
314
|
+
* Definition for the {@link FileNode}. The fully resolved builder lives in
|
|
315
|
+
* `createFile`, so this definition only supplies the guard.
|
|
316
|
+
*/
|
|
317
|
+
const fileDef = defineNode({ kind: "File" });
|
|
318
|
+
//#endregion
|
|
319
|
+
//#region src/nodes/function.ts
|
|
320
|
+
/**
|
|
321
|
+
* Definition for the {@link TypeLiteralNode}.
|
|
322
|
+
*/
|
|
323
|
+
const typeLiteralDef = defineNode({ kind: "TypeLiteral" });
|
|
324
|
+
/**
|
|
325
|
+
* Creates a {@link TypeLiteralNode} representing an inline anonymous object type.
|
|
326
|
+
*
|
|
327
|
+
* @example
|
|
328
|
+
* ```ts
|
|
329
|
+
* createTypeLiteral({ members: [{ name: 'petId', type: 'string', optional: false }] })
|
|
330
|
+
* // { petId: string }
|
|
331
|
+
* ```
|
|
332
|
+
*/
|
|
333
|
+
const createTypeLiteral = typeLiteralDef.create;
|
|
334
|
+
/**
|
|
335
|
+
* Definition for the {@link IndexedAccessTypeNode}.
|
|
336
|
+
*/
|
|
337
|
+
const indexedAccessTypeDef = defineNode({ kind: "IndexedAccessType" });
|
|
338
|
+
/**
|
|
339
|
+
* Creates an {@link IndexedAccessTypeNode} representing a single field accessed from a named type.
|
|
340
|
+
*
|
|
341
|
+
* @example
|
|
342
|
+
* ```ts
|
|
343
|
+
* createIndexedAccessType({ objectType: 'DeletePetPathParams', indexType: 'petId' })
|
|
344
|
+
* // DeletePetPathParams['petId']
|
|
345
|
+
* ```
|
|
346
|
+
*/
|
|
347
|
+
const createIndexedAccessType = indexedAccessTypeDef.create;
|
|
348
|
+
/**
|
|
349
|
+
* Definition for the {@link ObjectBindingPatternNode}.
|
|
350
|
+
*/
|
|
351
|
+
const objectBindingPatternDef = defineNode({ kind: "ObjectBindingPattern" });
|
|
352
|
+
/**
|
|
353
|
+
* Creates an {@link ObjectBindingPatternNode} for a destructured parameter binding.
|
|
354
|
+
*
|
|
355
|
+
* @example
|
|
356
|
+
* ```ts
|
|
357
|
+
* createObjectBindingPattern({ elements: [{ name: 'id' }, { name: 'name' }] })
|
|
358
|
+
* // { id, name }
|
|
359
|
+
* ```
|
|
360
|
+
*/
|
|
361
|
+
const createObjectBindingPattern = objectBindingPatternDef.create;
|
|
362
|
+
/**
|
|
363
|
+
* Definition for the {@link FunctionParameterNode}. `optional` defaults to `false`.
|
|
364
|
+
* Passing `properties` builds a destructured group: an {@link ObjectBindingPatternNode} name
|
|
365
|
+
* paired with a {@link TypeLiteralNode} type.
|
|
366
|
+
*/
|
|
367
|
+
const functionParameterDef = defineNode({
|
|
368
|
+
kind: "FunctionParameter",
|
|
369
|
+
build: (input) => {
|
|
370
|
+
if ("properties" in input) return {
|
|
371
|
+
name: createObjectBindingPattern({ elements: input.properties.map((p) => ({ name: p.name })) }),
|
|
372
|
+
type: createTypeLiteral({ members: input.properties.map((p) => ({
|
|
373
|
+
name: p.name,
|
|
374
|
+
type: p.type,
|
|
375
|
+
optional: p.optional ?? false
|
|
376
|
+
})) }),
|
|
377
|
+
optional: input.optional ?? false,
|
|
378
|
+
...input.default !== void 0 ? { default: input.default } : {}
|
|
379
|
+
};
|
|
380
|
+
return {
|
|
381
|
+
optional: false,
|
|
382
|
+
...input
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
/**
|
|
387
|
+
* Creates a `FunctionParameterNode`. `optional` defaults to `false`.
|
|
388
|
+
*
|
|
389
|
+
* @example Optional param
|
|
390
|
+
* ```ts
|
|
391
|
+
* createFunctionParameter({ name: 'params', type: 'QueryParams', optional: true })
|
|
392
|
+
* // → params?: QueryParams
|
|
393
|
+
* ```
|
|
394
|
+
*
|
|
395
|
+
* @example Destructured group
|
|
396
|
+
* ```ts
|
|
397
|
+
* createFunctionParameter({ properties: [{ name: 'id', type: 'string' }, { name: 'name', type: 'string', optional: true }], default: '{}' })
|
|
398
|
+
* // → { id, name }: { id: string; name?: string } = {}
|
|
399
|
+
* ```
|
|
400
|
+
*/
|
|
401
|
+
const createFunctionParameter = functionParameterDef.create;
|
|
402
|
+
/**
|
|
403
|
+
* Definition for the {@link FunctionParametersNode}.
|
|
404
|
+
*/
|
|
405
|
+
const functionParametersDef = defineNode({
|
|
406
|
+
kind: "FunctionParameters",
|
|
407
|
+
defaults: { params: [] }
|
|
408
|
+
});
|
|
409
|
+
/**
|
|
410
|
+
* Creates a `FunctionParametersNode` from an ordered list of parameters.
|
|
411
|
+
*
|
|
412
|
+
* @example
|
|
413
|
+
* ```ts
|
|
414
|
+
* const empty = createFunctionParameters()
|
|
415
|
+
* // { kind: 'FunctionParameters', params: [] }
|
|
416
|
+
* ```
|
|
417
|
+
*/
|
|
418
|
+
function createFunctionParameters(props = {}) {
|
|
419
|
+
return functionParametersDef.create(props);
|
|
420
|
+
}
|
|
421
|
+
//#endregion
|
|
422
|
+
//#region src/nodes/input.ts
|
|
423
|
+
/**
|
|
424
|
+
* Definition for the {@link InputNode}.
|
|
425
|
+
*/
|
|
426
|
+
const inputDef = defineNode({
|
|
427
|
+
kind: "Input",
|
|
428
|
+
defaults: {
|
|
429
|
+
schemas: [],
|
|
430
|
+
operations: [],
|
|
431
|
+
meta: {
|
|
432
|
+
circularNames: [],
|
|
433
|
+
enumNames: []
|
|
434
|
+
}
|
|
435
|
+
},
|
|
436
|
+
children: ["schemas", "operations"],
|
|
437
|
+
visitorKey: "input"
|
|
438
|
+
});
|
|
439
|
+
/**
|
|
440
|
+
* Creates an `InputNode`. Pass `stream: true` for the streaming variant whose `schemas` and
|
|
441
|
+
* `operations` are `AsyncIterable` sources and whose `meta` is optional. Otherwise it builds the
|
|
442
|
+
* eager variant with array `schemas`/`operations` and the defaulted `meta`.
|
|
443
|
+
*
|
|
444
|
+
* @example Eager
|
|
445
|
+
* ```ts
|
|
446
|
+
* const input = createInput()
|
|
447
|
+
* // { kind: 'Input', schemas: [], operations: [] }
|
|
448
|
+
* ```
|
|
449
|
+
*
|
|
450
|
+
* @example Streaming
|
|
451
|
+
* ```ts
|
|
452
|
+
* const node = createInput({ stream: true, schemas: schemasIterable, operations: operationsIterable, meta: { title: 'My API' } })
|
|
453
|
+
* ```
|
|
454
|
+
*/
|
|
455
|
+
function createInput(options = {}) {
|
|
456
|
+
const { stream, ...overrides } = options;
|
|
457
|
+
if (stream) return {
|
|
458
|
+
kind: "Input",
|
|
459
|
+
...overrides
|
|
460
|
+
};
|
|
461
|
+
return inputDef.create(overrides);
|
|
462
|
+
}
|
|
463
|
+
//#endregion
|
|
464
|
+
//#region src/nodes/requestBody.ts
|
|
465
|
+
/**
|
|
466
|
+
* Definition for the {@link RequestBodyNode}, normalizing each content entry into a `ContentNode`.
|
|
467
|
+
*/
|
|
468
|
+
const requestBodyDef = defineNode({
|
|
469
|
+
kind: "RequestBody",
|
|
470
|
+
build: (props) => ({
|
|
471
|
+
...props,
|
|
472
|
+
content: props.content?.map(createContent)
|
|
473
|
+
}),
|
|
474
|
+
children: ["content"]
|
|
475
|
+
});
|
|
476
|
+
/**
|
|
477
|
+
* Creates a `RequestBodyNode`, normalizing each content entry into a `ContentNode`.
|
|
478
|
+
*/
|
|
479
|
+
const createRequestBody = requestBodyDef.create;
|
|
480
|
+
//#endregion
|
|
481
|
+
//#region src/nodes/operation.ts
|
|
482
|
+
/**
|
|
483
|
+
* Definition for the {@link OperationNode}. HTTP operations (those carrying both
|
|
484
|
+
* `method` and `path`) are tagged with `protocol: 'http'`, and the request body is
|
|
485
|
+
* normalized into a `RequestBodyNode`.
|
|
486
|
+
*/
|
|
487
|
+
const operationDef = defineNode({
|
|
488
|
+
kind: "Operation",
|
|
489
|
+
build: (props) => {
|
|
490
|
+
const { requestBody, ...rest } = props;
|
|
491
|
+
const isHttp = rest.method !== void 0 && rest.path !== void 0;
|
|
492
|
+
return {
|
|
493
|
+
tags: [],
|
|
494
|
+
parameters: [],
|
|
495
|
+
responses: [],
|
|
496
|
+
...rest,
|
|
497
|
+
...isHttp ? { protocol: "http" } : {},
|
|
498
|
+
requestBody: requestBody ? createRequestBody(requestBody) : void 0
|
|
499
|
+
};
|
|
500
|
+
},
|
|
501
|
+
children: [
|
|
502
|
+
"parameters",
|
|
503
|
+
"requestBody",
|
|
504
|
+
"responses"
|
|
505
|
+
],
|
|
506
|
+
visitorKey: "operation"
|
|
507
|
+
});
|
|
508
|
+
function createOperation(props) {
|
|
509
|
+
return operationDef.create(props);
|
|
510
|
+
}
|
|
511
|
+
//#endregion
|
|
512
|
+
//#region src/nodes/output.ts
|
|
513
|
+
/**
|
|
514
|
+
* Definition for the {@link OutputNode}.
|
|
515
|
+
*/
|
|
516
|
+
const outputDef = defineNode({
|
|
517
|
+
kind: "Output",
|
|
518
|
+
defaults: { files: [] },
|
|
519
|
+
visitorKey: "output"
|
|
520
|
+
});
|
|
521
|
+
/**
|
|
522
|
+
* Creates an `OutputNode` with a stable default for `files`.
|
|
523
|
+
*
|
|
524
|
+
* @example
|
|
525
|
+
* ```ts
|
|
526
|
+
* const output = createOutput()
|
|
527
|
+
* // { kind: 'Output', files: [] }
|
|
528
|
+
* ```
|
|
529
|
+
*/
|
|
530
|
+
function createOutput(overrides = {}) {
|
|
531
|
+
return outputDef.create(overrides);
|
|
532
|
+
}
|
|
533
|
+
//#endregion
|
|
534
|
+
//#region src/nodes/parameter.ts
|
|
535
|
+
/**
|
|
536
|
+
* Definition for the {@link ParameterNode}. `required` defaults to `false` and the
|
|
537
|
+
* schema's `optional`/`nullish` flags are kept in sync with it.
|
|
538
|
+
*/
|
|
539
|
+
const parameterDef = defineNode({
|
|
540
|
+
kind: "Parameter",
|
|
541
|
+
build: (props) => {
|
|
542
|
+
const required = props.required ?? false;
|
|
543
|
+
return {
|
|
544
|
+
...props,
|
|
545
|
+
required,
|
|
546
|
+
schema: syncOptionality(props.schema, required)
|
|
547
|
+
};
|
|
548
|
+
},
|
|
549
|
+
children: ["schema"],
|
|
550
|
+
visitorKey: "parameter",
|
|
551
|
+
rebuild: true
|
|
552
|
+
});
|
|
553
|
+
/**
|
|
554
|
+
* Creates a `ParameterNode`.
|
|
555
|
+
*
|
|
556
|
+
* @example
|
|
557
|
+
* ```ts
|
|
558
|
+
* const param = createParameter({
|
|
559
|
+
* name: 'petId',
|
|
560
|
+
* in: 'path',
|
|
561
|
+
* required: true,
|
|
562
|
+
* schema: createSchema({ type: 'string' }),
|
|
563
|
+
* })
|
|
564
|
+
* ```
|
|
565
|
+
*/
|
|
566
|
+
const createParameter = parameterDef.create;
|
|
567
|
+
//#endregion
|
|
568
|
+
//#region src/nodes/property.ts
|
|
569
|
+
/**
|
|
570
|
+
* Definition for the {@link PropertyNode}. `required` defaults to `false` and the
|
|
571
|
+
* schema's `optional`/`nullish` flags are kept in sync with it.
|
|
572
|
+
*/
|
|
573
|
+
const propertyDef = defineNode({
|
|
574
|
+
kind: "Property",
|
|
575
|
+
build: (props) => {
|
|
576
|
+
const required = props.required ?? false;
|
|
577
|
+
return {
|
|
578
|
+
...props,
|
|
579
|
+
required,
|
|
580
|
+
schema: syncOptionality(props.schema, required)
|
|
581
|
+
};
|
|
582
|
+
},
|
|
583
|
+
children: ["schema"],
|
|
584
|
+
visitorKey: "property",
|
|
585
|
+
rebuild: true
|
|
586
|
+
});
|
|
587
|
+
/**
|
|
588
|
+
* Creates a `PropertyNode`.
|
|
589
|
+
*
|
|
590
|
+
* @example
|
|
591
|
+
* ```ts
|
|
592
|
+
* const property = createProperty({
|
|
593
|
+
* name: 'status',
|
|
594
|
+
* required: true,
|
|
595
|
+
* schema: createSchema({ type: 'string', nullable: true }),
|
|
596
|
+
* })
|
|
597
|
+
* // required=true, no optional/nullish
|
|
598
|
+
* ```
|
|
599
|
+
*/
|
|
600
|
+
const createProperty = propertyDef.create;
|
|
601
|
+
//#endregion
|
|
602
|
+
//#region src/nodes/response.ts
|
|
603
|
+
/**
|
|
604
|
+
* Definition for the {@link ResponseNode}. A single legacy `schema` (with optional
|
|
605
|
+
* `mediaType`/`keysToOmit`) is normalized into one `content` entry.
|
|
606
|
+
*/
|
|
607
|
+
const responseDef = defineNode({
|
|
608
|
+
kind: "Response",
|
|
609
|
+
build: (props) => {
|
|
610
|
+
const { schema, mediaType, keysToOmit, content, ...rest } = props;
|
|
611
|
+
const entries = content ?? (schema ? [{
|
|
612
|
+
contentType: mediaType ?? "application/json",
|
|
613
|
+
schema,
|
|
614
|
+
keysToOmit: keysToOmit ?? null
|
|
615
|
+
}] : void 0);
|
|
616
|
+
return {
|
|
617
|
+
...rest,
|
|
618
|
+
content: entries?.map(createContent)
|
|
619
|
+
};
|
|
620
|
+
},
|
|
621
|
+
children: ["content"],
|
|
622
|
+
visitorKey: "response"
|
|
623
|
+
});
|
|
624
|
+
/**
|
|
625
|
+
* Creates a `ResponseNode`.
|
|
626
|
+
*
|
|
627
|
+
* @example
|
|
628
|
+
* ```ts
|
|
629
|
+
* const response = createResponse({
|
|
630
|
+
* statusCode: '200',
|
|
631
|
+
* content: [{ contentType: 'application/json', schema: createSchema({ type: 'object', properties: [] }) }],
|
|
632
|
+
* })
|
|
633
|
+
* ```
|
|
634
|
+
*/
|
|
635
|
+
const createResponse = responseDef.create;
|
|
636
|
+
//#endregion
|
|
637
|
+
//#region src/utils/extractStringsFromNodes.ts
|
|
638
|
+
/**
|
|
639
|
+
* Extracts all string content from a `CodeNode` tree recursively.
|
|
640
|
+
*
|
|
641
|
+
* Collects text node values, identifier references in string fields (`params`, `generics`, `returnType`, `type`),
|
|
642
|
+
* and nested node content. Used to build the full source string for import filtering.
|
|
643
|
+
*/
|
|
644
|
+
function extractStringsFromNodes(nodes) {
|
|
645
|
+
if (!nodes?.length) return "";
|
|
646
|
+
return nodes.map((node) => {
|
|
647
|
+
if (typeof node === "string") return node;
|
|
648
|
+
if (node.kind === "Text") return node.value;
|
|
649
|
+
if (node.kind === "Break") return "";
|
|
650
|
+
if (node.kind === "Jsx") return node.value;
|
|
651
|
+
const parts = [];
|
|
652
|
+
if ("params" in node && node.params) parts.push(node.params);
|
|
653
|
+
if ("generics" in node && node.generics) parts.push(Array.isArray(node.generics) ? node.generics.join(", ") : node.generics);
|
|
654
|
+
if ("returnType" in node && node.returnType) parts.push(node.returnType);
|
|
655
|
+
if ("type" in node && typeof node.type === "string") parts.push(node.type);
|
|
656
|
+
const nested = extractStringsFromNodes(node.nodes);
|
|
657
|
+
if (nested) parts.push(nested);
|
|
658
|
+
return parts.join("\n");
|
|
659
|
+
}).filter(Boolean).join("\n");
|
|
660
|
+
}
|
|
661
|
+
//#endregion
|
|
662
|
+
//#region src/utils/fileMerge.ts
|
|
663
|
+
function sourceKey(source) {
|
|
664
|
+
return `${source.name ?? extractStringsFromNodes(source.nodes)}:${source.isExportable ?? false}:${source.isTypeOnly ?? false}`;
|
|
665
|
+
}
|
|
666
|
+
function pathTypeKey(path, isTypeOnly) {
|
|
667
|
+
return `${path}:${isTypeOnly ?? false}`;
|
|
668
|
+
}
|
|
669
|
+
function exportKey(path, name, isTypeOnly, asAlias) {
|
|
670
|
+
return `${path}:${name ?? ""}:${isTypeOnly ?? false}:${asAlias ?? ""}`;
|
|
671
|
+
}
|
|
672
|
+
function importKey(path, name, isTypeOnly) {
|
|
673
|
+
return `${path}:${name ?? ""}:${isTypeOnly ?? false}`;
|
|
674
|
+
}
|
|
675
|
+
/**
|
|
676
|
+
* Computes a multi-level sort key for exports and imports:
|
|
677
|
+
* non-array names first (wildcards/namespace aliases). Type-only before value. Alphabetical path. Unnamed before named.
|
|
678
|
+
*/
|
|
679
|
+
function sortKey(node) {
|
|
680
|
+
const isArray = Array.isArray(node.name) ? "1" : "0";
|
|
681
|
+
const typeOnly = node.isTypeOnly ? "0" : "1";
|
|
682
|
+
const hasName = node.name != null ? "1" : "0";
|
|
683
|
+
const name = Array.isArray(node.name) ? node.name.toSorted().join("\0") : node.name ?? "";
|
|
684
|
+
return `${isArray}:${typeOnly}:${node.path}:${hasName}:${name}`;
|
|
685
|
+
}
|
|
686
|
+
/**
|
|
687
|
+
* Deduplicates and merges `SourceNode` objects by `name + isExportable + isTypeOnly`.
|
|
688
|
+
*
|
|
689
|
+
* Unnamed sources are deduplicated by object reference. Returns a deduplicated array in original order.
|
|
690
|
+
*/
|
|
691
|
+
function combineSources(sources) {
|
|
692
|
+
const seen = /* @__PURE__ */ new Map();
|
|
693
|
+
for (const source of sources) {
|
|
694
|
+
const key = sourceKey(source);
|
|
695
|
+
if (!seen.has(key)) seen.set(key, source);
|
|
696
|
+
}
|
|
697
|
+
return [...seen.values()];
|
|
698
|
+
}
|
|
699
|
+
/**
|
|
700
|
+
* Merges `incoming` names into `existing`, preserving order and dropping duplicates.
|
|
701
|
+
*
|
|
702
|
+
* Shared by `combineExports` and `combineImports` for the same-path name-merge case.
|
|
703
|
+
*/
|
|
704
|
+
function mergeNameArrays(existing, incoming) {
|
|
705
|
+
const merged = new Set(existing);
|
|
706
|
+
for (const name of incoming) merged.add(name);
|
|
707
|
+
return [...merged];
|
|
708
|
+
}
|
|
709
|
+
/**
|
|
710
|
+
* Deduplicates and merges `ExportNode` objects by path and type.
|
|
711
|
+
*
|
|
712
|
+
* Named exports with the same path and `isTypeOnly` flag have their names merged into a single export.
|
|
713
|
+
* Non-array exports are deduplicated by exact identity. Returns a sorted, deduplicated array.
|
|
714
|
+
*/
|
|
715
|
+
function combineExports(exports) {
|
|
716
|
+
const result = [];
|
|
717
|
+
const namedByPath = /* @__PURE__ */ new Map();
|
|
718
|
+
const seen = /* @__PURE__ */ new Set();
|
|
719
|
+
const keyed = exports.map((node) => ({
|
|
720
|
+
node,
|
|
721
|
+
key: sortKey(node)
|
|
722
|
+
}));
|
|
723
|
+
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
|
|
724
|
+
for (const { node: curr } of keyed) {
|
|
725
|
+
const { name, path, isTypeOnly, asAlias } = curr;
|
|
726
|
+
if (Array.isArray(name)) {
|
|
727
|
+
if (!name.length) continue;
|
|
728
|
+
const key = pathTypeKey(path, isTypeOnly);
|
|
729
|
+
const existing = namedByPath.get(key);
|
|
730
|
+
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
|
|
731
|
+
else {
|
|
732
|
+
const newItem = {
|
|
733
|
+
...curr,
|
|
734
|
+
name: [...new Set(name)]
|
|
735
|
+
};
|
|
736
|
+
result.push(newItem);
|
|
737
|
+
namedByPath.set(key, newItem);
|
|
738
|
+
}
|
|
739
|
+
} else {
|
|
740
|
+
const key = exportKey(path, name, isTypeOnly, asAlias);
|
|
741
|
+
if (!seen.has(key)) {
|
|
742
|
+
result.push(curr);
|
|
743
|
+
seen.add(key);
|
|
744
|
+
}
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
return result;
|
|
748
|
+
}
|
|
749
|
+
/**
|
|
750
|
+
* Deduplicates and merges `ImportNode` objects, filtering out unused imports.
|
|
751
|
+
*
|
|
752
|
+
* Retains imports that are referenced in `source` or re-exported. Imports with the same path and
|
|
753
|
+
* `isTypeOnly` flag have their names merged. Returns a sorted, deduplicated, filtered array.
|
|
754
|
+
*/
|
|
755
|
+
function combineImports(imports, exports, source) {
|
|
756
|
+
const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
|
|
757
|
+
const isUsed = (importName) => !source || source.includes(importName) || exportedNames.has(importName);
|
|
758
|
+
const importNameMemo = /* @__PURE__ */ new Map();
|
|
759
|
+
const canonicalizeName = (n) => {
|
|
760
|
+
if (typeof n === "string") return n;
|
|
761
|
+
const key = `${n.propertyName}:${n.name ?? ""}`;
|
|
762
|
+
if (!importNameMemo.has(key)) importNameMemo.set(key, n);
|
|
763
|
+
return importNameMemo.get(key);
|
|
764
|
+
};
|
|
765
|
+
const pathsWithUsedNamedImport = /* @__PURE__ */ new Set();
|
|
766
|
+
for (const node of imports) {
|
|
767
|
+
if (!Array.isArray(node.name)) continue;
|
|
768
|
+
if (node.name.some((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName))) pathsWithUsedNamedImport.add(node.path);
|
|
769
|
+
}
|
|
770
|
+
const result = [];
|
|
771
|
+
const namedByPath = /* @__PURE__ */ new Map();
|
|
772
|
+
const seen = /* @__PURE__ */ new Set();
|
|
773
|
+
const keyed = imports.map((node) => ({
|
|
774
|
+
node,
|
|
775
|
+
key: sortKey(node)
|
|
776
|
+
}));
|
|
777
|
+
keyed.sort((a, b) => a.key < b.key ? -1 : a.key > b.key ? 1 : 0);
|
|
778
|
+
for (const { node: curr } of keyed) {
|
|
779
|
+
if (curr.path === curr.root) continue;
|
|
780
|
+
const { path, isTypeOnly } = curr;
|
|
781
|
+
let { name } = curr;
|
|
782
|
+
if (Array.isArray(name)) {
|
|
783
|
+
name = [...new Set(name.map(canonicalizeName))].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName));
|
|
784
|
+
if (!name.length) continue;
|
|
785
|
+
const key = pathTypeKey(path, isTypeOnly);
|
|
786
|
+
const existing = namedByPath.get(key);
|
|
787
|
+
if (existing && Array.isArray(existing.name)) existing.name = mergeNameArrays(existing.name, name);
|
|
788
|
+
else {
|
|
789
|
+
const newItem = {
|
|
790
|
+
...curr,
|
|
791
|
+
name
|
|
792
|
+
};
|
|
793
|
+
result.push(newItem);
|
|
794
|
+
namedByPath.set(key, newItem);
|
|
795
|
+
}
|
|
796
|
+
} else {
|
|
797
|
+
if (name && !isUsed(name) && !pathsWithUsedNamedImport.has(path)) continue;
|
|
798
|
+
const key = importKey(path, name, isTypeOnly);
|
|
799
|
+
if (!seen.has(key)) {
|
|
800
|
+
result.push(curr);
|
|
801
|
+
seen.add(key);
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
return result;
|
|
806
|
+
}
|
|
807
|
+
//#endregion
|
|
808
|
+
//#region src/factory.ts
|
|
809
|
+
var factory_exports = /* @__PURE__ */ require_casing.__exportAll({
|
|
810
|
+
createArrowFunction: () => createArrowFunction,
|
|
811
|
+
createBreak: () => createBreak,
|
|
812
|
+
createConst: () => createConst,
|
|
813
|
+
createContent: () => createContent,
|
|
814
|
+
createExport: () => createExport,
|
|
815
|
+
createFile: () => createFile,
|
|
816
|
+
createFunction: () => createFunction,
|
|
817
|
+
createFunctionParameter: () => createFunctionParameter,
|
|
818
|
+
createFunctionParameters: () => createFunctionParameters,
|
|
819
|
+
createImport: () => createImport,
|
|
820
|
+
createIndexedAccessType: () => createIndexedAccessType,
|
|
821
|
+
createInput: () => createInput,
|
|
822
|
+
createJsx: () => createJsx,
|
|
823
|
+
createObjectBindingPattern: () => createObjectBindingPattern,
|
|
824
|
+
createOperation: () => createOperation,
|
|
825
|
+
createOutput: () => createOutput,
|
|
826
|
+
createParameter: () => createParameter,
|
|
827
|
+
createProperty: () => createProperty,
|
|
828
|
+
createRequestBody: () => createRequestBody,
|
|
829
|
+
createResponse: () => createResponse,
|
|
830
|
+
createSchema: () => createSchema,
|
|
831
|
+
createSource: () => createSource,
|
|
832
|
+
createText: () => createText,
|
|
833
|
+
createType: () => createType,
|
|
834
|
+
createTypeLiteral: () => createTypeLiteral,
|
|
835
|
+
update: () => update
|
|
836
|
+
});
|
|
837
|
+
/**
|
|
838
|
+
* Identity-preserving node update: returns `node` unchanged when every field in
|
|
839
|
+
* `changes` already equals (by reference) the current value, otherwise a new node
|
|
840
|
+
* with the changes applied.
|
|
841
|
+
*
|
|
842
|
+
* Mirrors the TypeScript compiler's `factory.updateX` contract, pair it with the
|
|
843
|
+
* structural sharing in {@link transform} so a no-op rewrite doesn't allocate and
|
|
844
|
+
* downstream passes can detect "nothing changed" by identity. Comparison is
|
|
845
|
+
* shallow: a structurally-equal but newly-allocated array/object counts as a change.
|
|
846
|
+
*
|
|
847
|
+
* @example
|
|
848
|
+
* ```ts
|
|
849
|
+
* update(node, { name: node.name }) // -> same `node` reference
|
|
850
|
+
* update(node, { name: 'renamed' }) // -> new node, `name` replaced
|
|
851
|
+
* ```
|
|
852
|
+
*/
|
|
853
|
+
function update(node, changes) {
|
|
854
|
+
for (const key in changes) if (changes[key] !== node[key]) return {
|
|
855
|
+
...node,
|
|
856
|
+
...changes
|
|
857
|
+
};
|
|
858
|
+
return node;
|
|
859
|
+
}
|
|
860
|
+
/**
|
|
861
|
+
* Creates a fully resolved `FileNode` from a file input descriptor.
|
|
862
|
+
*
|
|
863
|
+
* Computes:
|
|
864
|
+
* - `id` SHA256 hash of the file path
|
|
865
|
+
* - `name` `baseName` without extension
|
|
866
|
+
* - `extname` extension extracted from `baseName`
|
|
867
|
+
*
|
|
868
|
+
* Deduplicates:
|
|
869
|
+
* - `sources` via `combineSources`
|
|
870
|
+
* - `exports` via `combineExports`
|
|
871
|
+
* - `imports` via `combineImports` (also filters unused imports)
|
|
872
|
+
*
|
|
873
|
+
* @throws {Error} when `baseName` has no extension.
|
|
874
|
+
*
|
|
875
|
+
* @example
|
|
876
|
+
* ```ts
|
|
877
|
+
* const file = createFile({
|
|
878
|
+
* baseName: 'petStore.ts',
|
|
879
|
+
* path: 'src/models/petStore.ts',
|
|
880
|
+
* sources: [createSource({ name: 'Pet', nodes: [createText('export type Pet = { id: number }')] })],
|
|
881
|
+
* imports: [createImport({ name: ['z'], path: 'zod' })],
|
|
882
|
+
* exports: [createExport({ name: ['Pet'], path: './petStore' })],
|
|
883
|
+
* })
|
|
884
|
+
* // file.id = SHA256 hash of 'src/models/petStore.ts'
|
|
885
|
+
* // file.name = 'petStore'
|
|
886
|
+
* // file.extname = '.ts'
|
|
887
|
+
* ```
|
|
888
|
+
*/
|
|
889
|
+
function createFile(input) {
|
|
890
|
+
const extname = node_path.default.extname(input.baseName) || (input.baseName.startsWith(".") ? input.baseName : "");
|
|
891
|
+
if (!extname) throw new Error(`No extname found for ${input.baseName}`);
|
|
892
|
+
const source = (input.sources ?? []).flatMap((item) => item.nodes ?? []).map((node) => extractStringsFromNodes([node])).filter(Boolean).join("\n\n");
|
|
893
|
+
const resolvedExports = input.exports?.length ? combineExports(input.exports) : [];
|
|
894
|
+
const combinedImports = input.imports?.length ? combineImports(input.imports, resolvedExports, source || void 0) : [];
|
|
895
|
+
const localNames = new Set((input.sources ?? []).map((item) => item.name).filter((name) => Boolean(name)));
|
|
896
|
+
const nameOf = (item) => typeof item === "string" ? item : item.name ?? item.propertyName;
|
|
897
|
+
const resolvedImports = combinedImports.filter((imp) => imp.path !== input.path).flatMap((imp) => {
|
|
898
|
+
if (!Array.isArray(imp.name)) return typeof imp.name === "string" && localNames.has(imp.name) ? [] : [imp];
|
|
899
|
+
const kept = imp.name.filter((item) => !localNames.has(nameOf(item)));
|
|
900
|
+
if (!kept.length) return [];
|
|
901
|
+
return [kept.length === imp.name.length ? imp : {
|
|
902
|
+
...imp,
|
|
903
|
+
name: kept
|
|
904
|
+
}];
|
|
905
|
+
});
|
|
906
|
+
const resolvedSources = input.sources?.length ? combineSources(input.sources) : [];
|
|
907
|
+
return {
|
|
908
|
+
kind: "File",
|
|
909
|
+
...input,
|
|
910
|
+
id: (0, node_crypto.hash)("sha256", input.path, "hex"),
|
|
911
|
+
name: trimExtName(input.baseName),
|
|
912
|
+
extname,
|
|
913
|
+
imports: resolvedImports,
|
|
914
|
+
exports: resolvedExports,
|
|
915
|
+
sources: resolvedSources,
|
|
916
|
+
meta: input.meta ?? {}
|
|
917
|
+
};
|
|
918
|
+
}
|
|
919
|
+
//#endregion
|
|
920
|
+
Object.defineProperty(exports, "arrowFunctionDef", {
|
|
921
|
+
enumerable: true,
|
|
922
|
+
get: function() {
|
|
923
|
+
return arrowFunctionDef;
|
|
924
|
+
}
|
|
925
|
+
});
|
|
926
|
+
Object.defineProperty(exports, "breakDef", {
|
|
927
|
+
enumerable: true,
|
|
928
|
+
get: function() {
|
|
929
|
+
return breakDef;
|
|
930
|
+
}
|
|
931
|
+
});
|
|
932
|
+
Object.defineProperty(exports, "constDef", {
|
|
933
|
+
enumerable: true,
|
|
934
|
+
get: function() {
|
|
935
|
+
return constDef;
|
|
936
|
+
}
|
|
937
|
+
});
|
|
938
|
+
Object.defineProperty(exports, "contentDef", {
|
|
939
|
+
enumerable: true,
|
|
940
|
+
get: function() {
|
|
941
|
+
return contentDef;
|
|
942
|
+
}
|
|
943
|
+
});
|
|
944
|
+
Object.defineProperty(exports, "createArrowFunction", {
|
|
945
|
+
enumerable: true,
|
|
946
|
+
get: function() {
|
|
947
|
+
return createArrowFunction;
|
|
948
|
+
}
|
|
949
|
+
});
|
|
950
|
+
Object.defineProperty(exports, "createBreak", {
|
|
951
|
+
enumerable: true,
|
|
952
|
+
get: function() {
|
|
953
|
+
return createBreak;
|
|
954
|
+
}
|
|
955
|
+
});
|
|
956
|
+
Object.defineProperty(exports, "createConst", {
|
|
957
|
+
enumerable: true,
|
|
958
|
+
get: function() {
|
|
959
|
+
return createConst;
|
|
960
|
+
}
|
|
961
|
+
});
|
|
962
|
+
Object.defineProperty(exports, "createContent", {
|
|
963
|
+
enumerable: true,
|
|
964
|
+
get: function() {
|
|
965
|
+
return createContent;
|
|
966
|
+
}
|
|
967
|
+
});
|
|
968
|
+
Object.defineProperty(exports, "createExport", {
|
|
969
|
+
enumerable: true,
|
|
970
|
+
get: function() {
|
|
971
|
+
return createExport;
|
|
972
|
+
}
|
|
973
|
+
});
|
|
974
|
+
Object.defineProperty(exports, "createFile", {
|
|
975
|
+
enumerable: true,
|
|
976
|
+
get: function() {
|
|
977
|
+
return createFile;
|
|
978
|
+
}
|
|
979
|
+
});
|
|
980
|
+
Object.defineProperty(exports, "createFunction", {
|
|
981
|
+
enumerable: true,
|
|
982
|
+
get: function() {
|
|
983
|
+
return createFunction;
|
|
984
|
+
}
|
|
985
|
+
});
|
|
986
|
+
Object.defineProperty(exports, "createFunctionParameter", {
|
|
987
|
+
enumerable: true,
|
|
988
|
+
get: function() {
|
|
989
|
+
return createFunctionParameter;
|
|
990
|
+
}
|
|
991
|
+
});
|
|
992
|
+
Object.defineProperty(exports, "createFunctionParameters", {
|
|
993
|
+
enumerable: true,
|
|
994
|
+
get: function() {
|
|
995
|
+
return createFunctionParameters;
|
|
996
|
+
}
|
|
997
|
+
});
|
|
998
|
+
Object.defineProperty(exports, "createImport", {
|
|
999
|
+
enumerable: true,
|
|
1000
|
+
get: function() {
|
|
1001
|
+
return createImport;
|
|
1002
|
+
}
|
|
1003
|
+
});
|
|
1004
|
+
Object.defineProperty(exports, "createIndexedAccessType", {
|
|
1005
|
+
enumerable: true,
|
|
1006
|
+
get: function() {
|
|
1007
|
+
return createIndexedAccessType;
|
|
1008
|
+
}
|
|
1009
|
+
});
|
|
1010
|
+
Object.defineProperty(exports, "createInput", {
|
|
1011
|
+
enumerable: true,
|
|
1012
|
+
get: function() {
|
|
1013
|
+
return createInput;
|
|
1014
|
+
}
|
|
1015
|
+
});
|
|
1016
|
+
Object.defineProperty(exports, "createJsx", {
|
|
1017
|
+
enumerable: true,
|
|
1018
|
+
get: function() {
|
|
1019
|
+
return createJsx;
|
|
1020
|
+
}
|
|
1021
|
+
});
|
|
1022
|
+
Object.defineProperty(exports, "createObjectBindingPattern", {
|
|
1023
|
+
enumerable: true,
|
|
1024
|
+
get: function() {
|
|
1025
|
+
return createObjectBindingPattern;
|
|
1026
|
+
}
|
|
1027
|
+
});
|
|
1028
|
+
Object.defineProperty(exports, "createOperation", {
|
|
1029
|
+
enumerable: true,
|
|
1030
|
+
get: function() {
|
|
1031
|
+
return createOperation;
|
|
1032
|
+
}
|
|
1033
|
+
});
|
|
1034
|
+
Object.defineProperty(exports, "createOutput", {
|
|
1035
|
+
enumerable: true,
|
|
1036
|
+
get: function() {
|
|
1037
|
+
return createOutput;
|
|
1038
|
+
}
|
|
1039
|
+
});
|
|
1040
|
+
Object.defineProperty(exports, "createParameter", {
|
|
1041
|
+
enumerable: true,
|
|
1042
|
+
get: function() {
|
|
1043
|
+
return createParameter;
|
|
1044
|
+
}
|
|
1045
|
+
});
|
|
1046
|
+
Object.defineProperty(exports, "createProperty", {
|
|
1047
|
+
enumerable: true,
|
|
1048
|
+
get: function() {
|
|
1049
|
+
return createProperty;
|
|
1050
|
+
}
|
|
1051
|
+
});
|
|
1052
|
+
Object.defineProperty(exports, "createRequestBody", {
|
|
1053
|
+
enumerable: true,
|
|
1054
|
+
get: function() {
|
|
1055
|
+
return createRequestBody;
|
|
1056
|
+
}
|
|
1057
|
+
});
|
|
1058
|
+
Object.defineProperty(exports, "createResponse", {
|
|
1059
|
+
enumerable: true,
|
|
1060
|
+
get: function() {
|
|
1061
|
+
return createResponse;
|
|
1062
|
+
}
|
|
1063
|
+
});
|
|
1064
|
+
Object.defineProperty(exports, "createSchema", {
|
|
1065
|
+
enumerable: true,
|
|
1066
|
+
get: function() {
|
|
1067
|
+
return createSchema;
|
|
1068
|
+
}
|
|
1069
|
+
});
|
|
1070
|
+
Object.defineProperty(exports, "createSource", {
|
|
1071
|
+
enumerable: true,
|
|
1072
|
+
get: function() {
|
|
1073
|
+
return createSource;
|
|
1074
|
+
}
|
|
1075
|
+
});
|
|
1076
|
+
Object.defineProperty(exports, "createText", {
|
|
1077
|
+
enumerable: true,
|
|
1078
|
+
get: function() {
|
|
1079
|
+
return createText;
|
|
1080
|
+
}
|
|
1081
|
+
});
|
|
1082
|
+
Object.defineProperty(exports, "createType", {
|
|
1083
|
+
enumerable: true,
|
|
1084
|
+
get: function() {
|
|
1085
|
+
return createType;
|
|
1086
|
+
}
|
|
1087
|
+
});
|
|
1088
|
+
Object.defineProperty(exports, "createTypeLiteral", {
|
|
1089
|
+
enumerable: true,
|
|
1090
|
+
get: function() {
|
|
1091
|
+
return createTypeLiteral;
|
|
1092
|
+
}
|
|
1093
|
+
});
|
|
1094
|
+
Object.defineProperty(exports, "defineNode", {
|
|
1095
|
+
enumerable: true,
|
|
1096
|
+
get: function() {
|
|
1097
|
+
return defineNode;
|
|
1098
|
+
}
|
|
1099
|
+
});
|
|
1100
|
+
Object.defineProperty(exports, "exportDef", {
|
|
1101
|
+
enumerable: true,
|
|
1102
|
+
get: function() {
|
|
1103
|
+
return exportDef;
|
|
1104
|
+
}
|
|
1105
|
+
});
|
|
1106
|
+
Object.defineProperty(exports, "extractStringsFromNodes", {
|
|
1107
|
+
enumerable: true,
|
|
1108
|
+
get: function() {
|
|
1109
|
+
return extractStringsFromNodes;
|
|
1110
|
+
}
|
|
1111
|
+
});
|
|
1112
|
+
Object.defineProperty(exports, "factory_exports", {
|
|
1113
|
+
enumerable: true,
|
|
1114
|
+
get: function() {
|
|
1115
|
+
return factory_exports;
|
|
1116
|
+
}
|
|
1117
|
+
});
|
|
1118
|
+
Object.defineProperty(exports, "fileDef", {
|
|
1119
|
+
enumerable: true,
|
|
1120
|
+
get: function() {
|
|
1121
|
+
return fileDef;
|
|
1122
|
+
}
|
|
1123
|
+
});
|
|
1124
|
+
Object.defineProperty(exports, "functionDef", {
|
|
1125
|
+
enumerable: true,
|
|
1126
|
+
get: function() {
|
|
1127
|
+
return functionDef;
|
|
1128
|
+
}
|
|
1129
|
+
});
|
|
1130
|
+
Object.defineProperty(exports, "functionParameterDef", {
|
|
1131
|
+
enumerable: true,
|
|
1132
|
+
get: function() {
|
|
1133
|
+
return functionParameterDef;
|
|
1134
|
+
}
|
|
1135
|
+
});
|
|
1136
|
+
Object.defineProperty(exports, "functionParametersDef", {
|
|
1137
|
+
enumerable: true,
|
|
1138
|
+
get: function() {
|
|
1139
|
+
return functionParametersDef;
|
|
1140
|
+
}
|
|
1141
|
+
});
|
|
1142
|
+
Object.defineProperty(exports, "importDef", {
|
|
1143
|
+
enumerable: true,
|
|
1144
|
+
get: function() {
|
|
1145
|
+
return importDef;
|
|
1146
|
+
}
|
|
1147
|
+
});
|
|
1148
|
+
Object.defineProperty(exports, "indexedAccessTypeDef", {
|
|
1149
|
+
enumerable: true,
|
|
1150
|
+
get: function() {
|
|
1151
|
+
return indexedAccessTypeDef;
|
|
1152
|
+
}
|
|
1153
|
+
});
|
|
1154
|
+
Object.defineProperty(exports, "inputDef", {
|
|
1155
|
+
enumerable: true,
|
|
1156
|
+
get: function() {
|
|
1157
|
+
return inputDef;
|
|
1158
|
+
}
|
|
1159
|
+
});
|
|
1160
|
+
Object.defineProperty(exports, "jsxDef", {
|
|
1161
|
+
enumerable: true,
|
|
1162
|
+
get: function() {
|
|
1163
|
+
return jsxDef;
|
|
1164
|
+
}
|
|
1165
|
+
});
|
|
1166
|
+
Object.defineProperty(exports, "objectBindingPatternDef", {
|
|
1167
|
+
enumerable: true,
|
|
1168
|
+
get: function() {
|
|
1169
|
+
return objectBindingPatternDef;
|
|
1170
|
+
}
|
|
1171
|
+
});
|
|
1172
|
+
Object.defineProperty(exports, "operationDef", {
|
|
1173
|
+
enumerable: true,
|
|
1174
|
+
get: function() {
|
|
1175
|
+
return operationDef;
|
|
1176
|
+
}
|
|
1177
|
+
});
|
|
1178
|
+
Object.defineProperty(exports, "outputDef", {
|
|
1179
|
+
enumerable: true,
|
|
1180
|
+
get: function() {
|
|
1181
|
+
return outputDef;
|
|
1182
|
+
}
|
|
1183
|
+
});
|
|
1184
|
+
Object.defineProperty(exports, "parameterDef", {
|
|
1185
|
+
enumerable: true,
|
|
1186
|
+
get: function() {
|
|
1187
|
+
return parameterDef;
|
|
1188
|
+
}
|
|
1189
|
+
});
|
|
1190
|
+
Object.defineProperty(exports, "propertyDef", {
|
|
1191
|
+
enumerable: true,
|
|
1192
|
+
get: function() {
|
|
1193
|
+
return propertyDef;
|
|
1194
|
+
}
|
|
1195
|
+
});
|
|
1196
|
+
Object.defineProperty(exports, "requestBodyDef", {
|
|
1197
|
+
enumerable: true,
|
|
1198
|
+
get: function() {
|
|
1199
|
+
return requestBodyDef;
|
|
1200
|
+
}
|
|
1201
|
+
});
|
|
1202
|
+
Object.defineProperty(exports, "responseDef", {
|
|
1203
|
+
enumerable: true,
|
|
1204
|
+
get: function() {
|
|
1205
|
+
return responseDef;
|
|
1206
|
+
}
|
|
1207
|
+
});
|
|
1208
|
+
Object.defineProperty(exports, "schemaDef", {
|
|
1209
|
+
enumerable: true,
|
|
1210
|
+
get: function() {
|
|
1211
|
+
return schemaDef;
|
|
1212
|
+
}
|
|
1213
|
+
});
|
|
1214
|
+
Object.defineProperty(exports, "sourceDef", {
|
|
1215
|
+
enumerable: true,
|
|
1216
|
+
get: function() {
|
|
1217
|
+
return sourceDef;
|
|
1218
|
+
}
|
|
1219
|
+
});
|
|
1220
|
+
Object.defineProperty(exports, "syncOptionality", {
|
|
1221
|
+
enumerable: true,
|
|
1222
|
+
get: function() {
|
|
1223
|
+
return syncOptionality;
|
|
1224
|
+
}
|
|
1225
|
+
});
|
|
1226
|
+
Object.defineProperty(exports, "textDef", {
|
|
1227
|
+
enumerable: true,
|
|
1228
|
+
get: function() {
|
|
1229
|
+
return textDef;
|
|
1230
|
+
}
|
|
1231
|
+
});
|
|
1232
|
+
Object.defineProperty(exports, "typeDef", {
|
|
1233
|
+
enumerable: true,
|
|
1234
|
+
get: function() {
|
|
1235
|
+
return typeDef;
|
|
1236
|
+
}
|
|
1237
|
+
});
|
|
1238
|
+
Object.defineProperty(exports, "typeLiteralDef", {
|
|
1239
|
+
enumerable: true,
|
|
1240
|
+
get: function() {
|
|
1241
|
+
return typeLiteralDef;
|
|
1242
|
+
}
|
|
1243
|
+
});
|
|
1244
|
+
Object.defineProperty(exports, "update", {
|
|
1245
|
+
enumerable: true,
|
|
1246
|
+
get: function() {
|
|
1247
|
+
return update;
|
|
1248
|
+
}
|
|
1249
|
+
});
|
|
1250
|
+
|
|
1251
|
+
//# sourceMappingURL=factory-BmcGBdeg.cjs.map
|