@kubb/ast 5.0.0-beta.13 → 5.0.0-beta.15
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 +71 -50
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +3 -21
- package/dist/index.js +70 -51
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +2 -2
- package/src/transformers.ts +19 -14
- package/src/utils.ts +22 -11
- package/src/visitor.ts +31 -31
package/dist/index.d.ts
CHANGED
|
@@ -2901,6 +2901,7 @@ declare function setDiscriminatorEnum({
|
|
|
2901
2901
|
* ])
|
|
2902
2902
|
* ```
|
|
2903
2903
|
*/
|
|
2904
|
+
declare function mergeAdjacentObjectsLazy(members: Iterable<SchemaNode>): Generator<SchemaNode, void, undefined>;
|
|
2904
2905
|
declare function mergeAdjacentObjects(members: Array<SchemaNode>): Array<SchemaNode>;
|
|
2905
2906
|
/**
|
|
2906
2907
|
* Removes enum members that are covered by broader scalar primitives in the same union.
|
|
@@ -3170,6 +3171,7 @@ declare function transform(node: Node, options: TransformOptions): Node;
|
|
|
3170
3171
|
* const values = collect(root, { depth: 'shallow', root: () => 'root' })
|
|
3171
3172
|
* ```
|
|
3172
3173
|
*/
|
|
3174
|
+
declare function collectLazy<T>(node: Node, options: CollectOptions<T>): Generator<T, void, undefined>;
|
|
3173
3175
|
declare function collect<T>(node: Node, options: CollectOptions<T>): Array<T>;
|
|
3174
3176
|
//#endregion
|
|
3175
3177
|
//#region src/utils.d.ts
|
|
@@ -3371,26 +3373,6 @@ declare function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): st
|
|
|
3371
3373
|
* ```
|
|
3372
3374
|
*/
|
|
3373
3375
|
declare function resolveRefName(node: SchemaNode | undefined): string | undefined;
|
|
3374
|
-
/**
|
|
3375
|
-
* Collects every named schema referenced (transitively) from a node via ref edges.
|
|
3376
|
-
*
|
|
3377
|
-
* Refs are followed by name only — the resolved `node.schema` is not traversed inline.
|
|
3378
|
-
* Use this to determine schema dependencies, build reference graphs, or detect what schemas need to be emitted.
|
|
3379
|
-
*
|
|
3380
|
-
* @example Collect refs from a single schema
|
|
3381
|
-
* ```ts
|
|
3382
|
-
* const names = collectReferencedSchemaNames(petSchema)
|
|
3383
|
-
* // → Set { 'Category', 'Tag' }
|
|
3384
|
-
* ```
|
|
3385
|
-
*
|
|
3386
|
-
* @example Accumulate refs from multiple schemas into one set
|
|
3387
|
-
* ```ts
|
|
3388
|
-
* const out = new Set<string>()
|
|
3389
|
-
* for (const schema of schemas) {
|
|
3390
|
-
* collectReferencedSchemaNames(schema, out)
|
|
3391
|
-
* }
|
|
3392
|
-
* ```
|
|
3393
|
-
*/
|
|
3394
3376
|
declare function collectReferencedSchemaNames(node: SchemaNode | undefined, out?: Set<string>): Set<string>;
|
|
3395
3377
|
/**
|
|
3396
3378
|
* Collects the names of all top-level schemas transitively used by a set of operations.
|
|
@@ -3447,5 +3429,5 @@ declare function containsCircularRef(node: SchemaNode | undefined, {
|
|
|
3447
3429
|
excludeName?: string;
|
|
3448
3430
|
}): boolean;
|
|
3449
3431
|
//#endregion
|
|
3450
|
-
export { type ArraySchemaNode, type ArrowFunctionNode, AsyncVisitor, type BaseNode, type BreakNode, type CodeNode, CollectOptions, CollectVisitor, type ComplexSchemaType, type ConstNode, type DateSchemaNode, type DatetimeSchemaNode, DistributiveOmit, type EnumSchemaNode, type EnumValueNode, type ExportNode, type FileNode, type FormatStringSchemaNode, type FunctionNode, type FunctionNodeType, type FunctionParamNode, type FunctionParameterNode, type FunctionParametersNode, type HttpMethod, type HttpStatusCode, type ImportNode, InferSchema, InferSchemaNode, type InputMeta, type InputNode, type IntersectionSchemaNode, type Ipv4SchemaNode, type Ipv6SchemaNode, type JSDocNode, type JsxNode, type MediaType, Node, type NodeKind, type NumberSchemaNode, type ObjectSchemaNode, type OperationNode, OperationParamsResolver, type OutputNode, type ParameterGroupNode, type ParameterLocation, type ParameterNode, type ParamsTypeNode, ParentOf, ParserOptions, type PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, type PropertyNode, RefMap, type RefSchemaNode, type ResponseNode, ScalarPrimitive, type ScalarSchemaNode, type ScalarSchemaType, type SchemaNode, type SchemaNodeByType, type SchemaType, type SourceNode, type SpecialSchemaType, type StatusCode, type StringSchemaNode, type TextNode, type TimeSchemaNode, TransformOptions, type TypeDeclarationNode, type TypeNode, type UnionSchemaNode, type UrlSchemaNode, UserFileNode, Visitor, VisitorContext, VisitorDepth, WalkOptions, caseParams, childName, collect, collectImports, collectReferencedSchemaNames, collectUsedSchemaNames, containsCircularRef, createArrowFunction, createBreak, createConst, createDiscriminantNode, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createInput, createJsx, createOperation, createOperationParams, createOutput, createParameter, createParameterGroup, createParamsType, createPrinterFactory, createProperty, createResponse, createSchema, createSource, createText, createType, definePrinter, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, findDiscriminator, httpMethods, isInputNode, isOperationNode, isOutputNode, isScalarPrimitive, isSchemaNode, isStringType, mediaTypes, mergeAdjacentObjects, narrowSchema, nodeKinds, resolveRefName, schemaTypes, setDiscriminatorEnum, setEnumName, simplifyUnion, syncOptionality, syncSchemaRef, transform, walk };
|
|
3432
|
+
export { type ArraySchemaNode, type ArrowFunctionNode, AsyncVisitor, type BaseNode, type BreakNode, type CodeNode, CollectOptions, CollectVisitor, type ComplexSchemaType, type ConstNode, type DateSchemaNode, type DatetimeSchemaNode, DistributiveOmit, type EnumSchemaNode, type EnumValueNode, type ExportNode, type FileNode, type FormatStringSchemaNode, type FunctionNode, type FunctionNodeType, type FunctionParamNode, type FunctionParameterNode, type FunctionParametersNode, type HttpMethod, type HttpStatusCode, type ImportNode, InferSchema, InferSchemaNode, type InputMeta, type InputNode, type IntersectionSchemaNode, type Ipv4SchemaNode, type Ipv6SchemaNode, type JSDocNode, type JsxNode, type MediaType, Node, type NodeKind, type NumberSchemaNode, type ObjectSchemaNode, type OperationNode, OperationParamsResolver, type OutputNode, type ParameterGroupNode, type ParameterLocation, type ParameterNode, type ParamsTypeNode, ParentOf, ParserOptions, type PrimitiveSchemaType, Printer, PrinterFactoryOptions, PrinterPartial, type PropertyNode, RefMap, type RefSchemaNode, type ResponseNode, ScalarPrimitive, type ScalarSchemaNode, type ScalarSchemaType, type SchemaNode, type SchemaNodeByType, type SchemaType, type SourceNode, type SpecialSchemaType, type StatusCode, type StringSchemaNode, type TextNode, type TimeSchemaNode, TransformOptions, type TypeDeclarationNode, type TypeNode, type UnionSchemaNode, type UrlSchemaNode, UserFileNode, Visitor, VisitorContext, VisitorDepth, WalkOptions, caseParams, childName, collect, collectImports, collectLazy, collectReferencedSchemaNames, collectUsedSchemaNames, containsCircularRef, createArrowFunction, createBreak, createConst, createDiscriminantNode, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createInput, createJsx, createOperation, createOperationParams, createOutput, createParameter, createParameterGroup, createParamsType, createPrinterFactory, createProperty, createResponse, createSchema, createSource, createText, createType, definePrinter, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, findDiscriminator, httpMethods, isInputNode, isOperationNode, isOutputNode, isScalarPrimitive, isSchemaNode, isStringType, mediaTypes, mergeAdjacentObjects, mergeAdjacentObjectsLazy, narrowSchema, nodeKinds, resolveRefName, schemaTypes, setDiscriminatorEnum, setEnumName, simplifyUnion, syncOptionality, syncSchemaRef, transform, walk };
|
|
3451
3433
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -510,32 +510,36 @@ function createLimit(concurrency) {
|
|
|
510
510
|
* // returns parameters, requestBody schema (if present), and responses
|
|
511
511
|
* ```
|
|
512
512
|
*/
|
|
513
|
-
function getChildren(node, recurse) {
|
|
513
|
+
function* getChildren(node, recurse) {
|
|
514
514
|
switch (node.kind) {
|
|
515
|
-
case "Input":
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
if (
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
case "
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
case "
|
|
538
|
-
|
|
515
|
+
case "Input":
|
|
516
|
+
yield* node.schemas;
|
|
517
|
+
yield* node.operations;
|
|
518
|
+
break;
|
|
519
|
+
case "Output": break;
|
|
520
|
+
case "Operation":
|
|
521
|
+
yield* node.parameters;
|
|
522
|
+
if (node.requestBody?.content) {
|
|
523
|
+
for (const c of node.requestBody.content) if (c.schema) yield c.schema;
|
|
524
|
+
}
|
|
525
|
+
yield* node.responses;
|
|
526
|
+
break;
|
|
527
|
+
case "Schema":
|
|
528
|
+
if (!recurse) break;
|
|
529
|
+
if ("properties" in node && node.properties.length > 0) yield* node.properties;
|
|
530
|
+
if ("items" in node && node.items) yield* node.items;
|
|
531
|
+
if ("members" in node && node.members) yield* node.members;
|
|
532
|
+
if ("additionalProperties" in node && node.additionalProperties && node.additionalProperties !== true) yield node.additionalProperties;
|
|
533
|
+
break;
|
|
534
|
+
case "Property":
|
|
535
|
+
yield node.schema;
|
|
536
|
+
break;
|
|
537
|
+
case "Parameter":
|
|
538
|
+
yield node.schema;
|
|
539
|
+
break;
|
|
540
|
+
case "Response":
|
|
541
|
+
if (node.schema) yield node.schema;
|
|
542
|
+
break;
|
|
539
543
|
}
|
|
540
544
|
}
|
|
541
545
|
/**
|
|
@@ -722,10 +726,9 @@ function transform(node, options) {
|
|
|
722
726
|
* const values = collect(root, { depth: 'shallow', root: () => 'root' })
|
|
723
727
|
* ```
|
|
724
728
|
*/
|
|
725
|
-
function
|
|
729
|
+
function* collectLazy(node, options) {
|
|
726
730
|
const { depth, parent, ...visitor } = options;
|
|
727
731
|
const recurse = (depth ?? visitorDepths.deep) === visitorDepths.deep;
|
|
728
|
-
const results = [];
|
|
729
732
|
let v;
|
|
730
733
|
switch (node.kind) {
|
|
731
734
|
case "Input":
|
|
@@ -753,12 +756,14 @@ function collect(node, options) {
|
|
|
753
756
|
case "ParameterGroup":
|
|
754
757
|
case "FunctionParameters": break;
|
|
755
758
|
}
|
|
756
|
-
if (v !== void 0)
|
|
757
|
-
for (const child of getChildren(node, recurse))
|
|
759
|
+
if (v !== void 0) yield v;
|
|
760
|
+
for (const child of getChildren(node, recurse)) yield* collectLazy(child, {
|
|
758
761
|
...options,
|
|
759
762
|
parent: node
|
|
760
|
-
})
|
|
761
|
-
|
|
763
|
+
});
|
|
764
|
+
}
|
|
765
|
+
function collect(node, options) {
|
|
766
|
+
return Array.from(collectLazy(node, options));
|
|
762
767
|
}
|
|
763
768
|
//#endregion
|
|
764
769
|
//#region src/utils.ts
|
|
@@ -1258,14 +1263,23 @@ function resolveRefName(node) {
|
|
|
1258
1263
|
* }
|
|
1259
1264
|
* ```
|
|
1260
1265
|
*/
|
|
1261
|
-
|
|
1262
|
-
|
|
1266
|
+
const schemaRefCache = /* @__PURE__ */ new WeakMap();
|
|
1267
|
+
function collectSchemaRefs(node) {
|
|
1268
|
+
const cached = schemaRefCache.get(node);
|
|
1269
|
+
if (cached) return cached;
|
|
1270
|
+
const refs = /* @__PURE__ */ new Set();
|
|
1263
1271
|
collect(node, { schema(child) {
|
|
1264
1272
|
if (child.type === "ref") {
|
|
1265
1273
|
const name = resolveRefName(child);
|
|
1266
|
-
if (name)
|
|
1274
|
+
if (name) refs.add(name);
|
|
1267
1275
|
}
|
|
1268
1276
|
} });
|
|
1277
|
+
schemaRefCache.set(node, refs);
|
|
1278
|
+
return refs;
|
|
1279
|
+
}
|
|
1280
|
+
function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
|
|
1281
|
+
if (!node) return out;
|
|
1282
|
+
for (const name of collectSchemaRefs(node)) out.add(name);
|
|
1269
1283
|
return out;
|
|
1270
1284
|
}
|
|
1271
1285
|
/**
|
|
@@ -1308,7 +1322,7 @@ function collectUsedSchemaNames(operations, schemas) {
|
|
|
1308
1322
|
if (namedSchema) visitSchema(namedSchema);
|
|
1309
1323
|
}
|
|
1310
1324
|
}
|
|
1311
|
-
for (const op of operations) for (const schema of
|
|
1325
|
+
for (const op of operations) for (const schema of collectLazy(op, {
|
|
1312
1326
|
depth: "shallow",
|
|
1313
1327
|
schema: (node) => node
|
|
1314
1328
|
})) visitSchema(schema);
|
|
@@ -1357,11 +1371,12 @@ function findCircularSchemas(schemas) {
|
|
|
1357
1371
|
*/
|
|
1358
1372
|
function containsCircularRef(node, { circularSchemas, excludeName }) {
|
|
1359
1373
|
if (!node || circularSchemas.size === 0) return false;
|
|
1360
|
-
|
|
1374
|
+
for (const _ of collectLazy(node, { schema(child) {
|
|
1361
1375
|
if (child.type !== "ref") return void 0;
|
|
1362
1376
|
const name = resolveRefName(child);
|
|
1363
1377
|
return name && name !== excludeName && circularSchemas.has(name) ? true : void 0;
|
|
1364
|
-
} })
|
|
1378
|
+
} })) return true;
|
|
1379
|
+
return false;
|
|
1365
1380
|
}
|
|
1366
1381
|
//#endregion
|
|
1367
1382
|
//#region src/factory.ts
|
|
@@ -2135,23 +2150,27 @@ function setDiscriminatorEnum({ node, propertyName, values, enumName }) {
|
|
|
2135
2150
|
* ])
|
|
2136
2151
|
* ```
|
|
2137
2152
|
*/
|
|
2138
|
-
function
|
|
2139
|
-
|
|
2153
|
+
function* mergeAdjacentObjectsLazy(members) {
|
|
2154
|
+
let acc;
|
|
2155
|
+
for (const member of members) {
|
|
2140
2156
|
const objectMember = narrowSchema(member, "object");
|
|
2141
|
-
if (objectMember && !objectMember.name) {
|
|
2142
|
-
const
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
...
|
|
2147
|
-
properties: [...previousObject.properties ?? [], ...objectMember.properties ?? []]
|
|
2157
|
+
if (objectMember && !objectMember.name && acc !== void 0) {
|
|
2158
|
+
const accObject = narrowSchema(acc, "object");
|
|
2159
|
+
if (accObject && !accObject.name) {
|
|
2160
|
+
acc = createSchema({
|
|
2161
|
+
...accObject,
|
|
2162
|
+
properties: [...accObject.properties ?? [], ...objectMember.properties ?? []]
|
|
2148
2163
|
});
|
|
2149
|
-
|
|
2164
|
+
continue;
|
|
2150
2165
|
}
|
|
2151
2166
|
}
|
|
2152
|
-
acc
|
|
2153
|
-
|
|
2154
|
-
}
|
|
2167
|
+
if (acc !== void 0) yield acc;
|
|
2168
|
+
acc = member;
|
|
2169
|
+
}
|
|
2170
|
+
if (acc !== void 0) yield acc;
|
|
2171
|
+
}
|
|
2172
|
+
function mergeAdjacentObjects(members) {
|
|
2173
|
+
return [...mergeAdjacentObjectsLazy(members)];
|
|
2155
2174
|
}
|
|
2156
2175
|
/**
|
|
2157
2176
|
* Removes enum members that are covered by broader scalar primitives in the same union.
|
|
@@ -2192,6 +2211,6 @@ function setEnumName(propNode, parentName, propName, enumSuffix) {
|
|
|
2192
2211
|
return propNode;
|
|
2193
2212
|
}
|
|
2194
2213
|
//#endregion
|
|
2195
|
-
export { caseParams, childName, collect, collectImports, collectReferencedSchemaNames, collectUsedSchemaNames, containsCircularRef, createArrowFunction, createBreak, createConst, createDiscriminantNode, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createInput, createJsx, createOperation, createOperationParams, createOutput, createParameter, createParameterGroup, createParamsType, createPrinterFactory, createProperty, createResponse, createSchema, createSource, createText, createType, definePrinter, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, findDiscriminator, httpMethods, isInputNode, isOperationNode, isOutputNode, isScalarPrimitive, isSchemaNode, isStringType, mediaTypes, mergeAdjacentObjects, narrowSchema, nodeKinds, resolveRefName, schemaTypes, setDiscriminatorEnum, setEnumName, simplifyUnion, syncOptionality, syncSchemaRef, transform, walk };
|
|
2214
|
+
export { caseParams, childName, collect, collectImports, collectLazy, collectReferencedSchemaNames, collectUsedSchemaNames, containsCircularRef, createArrowFunction, createBreak, createConst, createDiscriminantNode, createExport, createFile, createFunction, createFunctionParameter, createFunctionParameters, createImport, createInput, createJsx, createOperation, createOperationParams, createOutput, createParameter, createParameterGroup, createParamsType, createPrinterFactory, createProperty, createResponse, createSchema, createSource, createText, createType, definePrinter, enumPropName, extractRefName, extractStringsFromNodes, findCircularSchemas, findDiscriminator, httpMethods, isInputNode, isOperationNode, isOutputNode, isScalarPrimitive, isSchemaNode, isStringType, mediaTypes, mergeAdjacentObjects, mergeAdjacentObjectsLazy, narrowSchema, nodeKinds, resolveRefName, schemaTypes, setDiscriminatorEnum, setEnumName, simplifyUnion, syncOptionality, syncSchemaRef, transform, walk };
|
|
2196
2215
|
|
|
2197
2216
|
//# sourceMappingURL=index.js.map
|