@kubb/ast 5.0.0-beta.1 → 5.0.0-beta.3
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 +68 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +43 -2
- package/dist/index.js +68 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/utils.ts +84 -2
package/dist/index.d.ts
CHANGED
|
@@ -3377,9 +3377,50 @@ declare function resolveRefName(node: SchemaNode | undefined): string | undefine
|
|
|
3377
3377
|
* Refs are followed by name only — the resolved `node.schema` is not traversed inline.
|
|
3378
3378
|
* Use this to determine schema dependencies, build reference graphs, or detect what schemas need to be emitted.
|
|
3379
3379
|
*
|
|
3380
|
-
* @
|
|
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
|
+
* ```
|
|
3381
3393
|
*/
|
|
3382
3394
|
declare function collectReferencedSchemaNames(node: SchemaNode | undefined, out?: Set<string>): Set<string>;
|
|
3395
|
+
/**
|
|
3396
|
+
* Collects the names of all top-level schemas transitively used by a set of operations.
|
|
3397
|
+
*
|
|
3398
|
+
* An operation uses a schema when any of its parameters, request body content, or responses
|
|
3399
|
+
* reference it — directly or indirectly through other named schemas.
|
|
3400
|
+
* The walk is iterative and safe against reference cycles.
|
|
3401
|
+
*
|
|
3402
|
+
* Use this together with `include` filters to determine which schemas from `components/schemas`
|
|
3403
|
+
* are reachable from the allowed operations, so that schemas used only by excluded operations
|
|
3404
|
+
* are not generated.
|
|
3405
|
+
*
|
|
3406
|
+
* @example Only generate schemas referenced by included operations
|
|
3407
|
+
* ```ts
|
|
3408
|
+
* const includedOps = inputNode.operations.filter(op => resolver.resolveOptions(op, { options, include }) !== null)
|
|
3409
|
+
* const allowed = collectUsedSchemaNames(includedOps, inputNode.schemas)
|
|
3410
|
+
*
|
|
3411
|
+
* for (const schema of inputNode.schemas) {
|
|
3412
|
+
* if (schema.name && !allowed.has(schema.name)) continue
|
|
3413
|
+
* // … generate schema
|
|
3414
|
+
* }
|
|
3415
|
+
* ```
|
|
3416
|
+
*
|
|
3417
|
+
* @example Check whether a specific schema is needed
|
|
3418
|
+
* ```ts
|
|
3419
|
+
* const allowed = collectUsedSchemaNames(includedOps, inputNode.schemas)
|
|
3420
|
+
* allowed.has('OrderStatus') // false when no included operation references OrderStatus
|
|
3421
|
+
* ```
|
|
3422
|
+
*/
|
|
3423
|
+
declare function collectUsedSchemaNames(operations: ReadonlyArray<OperationNode>, schemas: ReadonlyArray<SchemaNode>): Set<string>;
|
|
3383
3424
|
/**
|
|
3384
3425
|
* Identifies all schemas that participate in circular dependency chains, including direct self-loops.
|
|
3385
3426
|
*
|
|
@@ -3406,5 +3447,5 @@ declare function containsCircularRef(node: SchemaNode | undefined, {
|
|
|
3406
3447
|
excludeName?: string;
|
|
3407
3448
|
}): boolean;
|
|
3408
3449
|
//#endregion
|
|
3409
|
-
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, 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 };
|
|
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 };
|
|
3410
3451
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1157,6 +1157,13 @@ function combineExports(exports) {
|
|
|
1157
1157
|
function combineImports(imports, exports, source) {
|
|
1158
1158
|
const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
|
|
1159
1159
|
const isUsed = (importName) => !source || source.includes(importName) || exportedNames.has(importName);
|
|
1160
|
+
const importNameMemo = /* @__PURE__ */ new Map();
|
|
1161
|
+
const canonicalizeName = (n) => {
|
|
1162
|
+
if (typeof n === "string") return n;
|
|
1163
|
+
const key = `${n.propertyName}:${n.name ?? ""}`;
|
|
1164
|
+
if (!importNameMemo.has(key)) importNameMemo.set(key, n);
|
|
1165
|
+
return importNameMemo.get(key);
|
|
1166
|
+
};
|
|
1160
1167
|
const result = [];
|
|
1161
1168
|
const namedByPath = /* @__PURE__ */ new Map();
|
|
1162
1169
|
const seen = /* @__PURE__ */ new Set();
|
|
@@ -1170,7 +1177,7 @@ function combineImports(imports, exports, source) {
|
|
|
1170
1177
|
const { path, isTypeOnly } = curr;
|
|
1171
1178
|
let { name } = curr;
|
|
1172
1179
|
if (Array.isArray(name)) {
|
|
1173
|
-
name = [...new Set(name)].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.propertyName));
|
|
1180
|
+
name = [...new Set(name.map(canonicalizeName))].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName));
|
|
1174
1181
|
if (!name.length) continue;
|
|
1175
1182
|
const key = pathTypeKey(path, isTypeOnly);
|
|
1176
1183
|
const existing = namedByPath.get(key);
|
|
@@ -1243,7 +1250,19 @@ function resolveRefName(node) {
|
|
|
1243
1250
|
* Refs are followed by name only — the resolved `node.schema` is not traversed inline.
|
|
1244
1251
|
* Use this to determine schema dependencies, build reference graphs, or detect what schemas need to be emitted.
|
|
1245
1252
|
*
|
|
1246
|
-
* @
|
|
1253
|
+
* @example Collect refs from a single schema
|
|
1254
|
+
* ```ts
|
|
1255
|
+
* const names = collectReferencedSchemaNames(petSchema)
|
|
1256
|
+
* // → Set { 'Category', 'Tag' }
|
|
1257
|
+
* ```
|
|
1258
|
+
*
|
|
1259
|
+
* @example Accumulate refs from multiple schemas into one set
|
|
1260
|
+
* ```ts
|
|
1261
|
+
* const out = new Set<string>()
|
|
1262
|
+
* for (const schema of schemas) {
|
|
1263
|
+
* collectReferencedSchemaNames(schema, out)
|
|
1264
|
+
* }
|
|
1265
|
+
* ```
|
|
1247
1266
|
*/
|
|
1248
1267
|
function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
|
|
1249
1268
|
if (!node) return out;
|
|
@@ -1256,6 +1275,52 @@ function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
|
|
|
1256
1275
|
return out;
|
|
1257
1276
|
}
|
|
1258
1277
|
/**
|
|
1278
|
+
* Collects the names of all top-level schemas transitively used by a set of operations.
|
|
1279
|
+
*
|
|
1280
|
+
* An operation uses a schema when any of its parameters, request body content, or responses
|
|
1281
|
+
* reference it — directly or indirectly through other named schemas.
|
|
1282
|
+
* The walk is iterative and safe against reference cycles.
|
|
1283
|
+
*
|
|
1284
|
+
* Use this together with `include` filters to determine which schemas from `components/schemas`
|
|
1285
|
+
* are reachable from the allowed operations, so that schemas used only by excluded operations
|
|
1286
|
+
* are not generated.
|
|
1287
|
+
*
|
|
1288
|
+
* @example Only generate schemas referenced by included operations
|
|
1289
|
+
* ```ts
|
|
1290
|
+
* const includedOps = inputNode.operations.filter(op => resolver.resolveOptions(op, { options, include }) !== null)
|
|
1291
|
+
* const allowed = collectUsedSchemaNames(includedOps, inputNode.schemas)
|
|
1292
|
+
*
|
|
1293
|
+
* for (const schema of inputNode.schemas) {
|
|
1294
|
+
* if (schema.name && !allowed.has(schema.name)) continue
|
|
1295
|
+
* // … generate schema
|
|
1296
|
+
* }
|
|
1297
|
+
* ```
|
|
1298
|
+
*
|
|
1299
|
+
* @example Check whether a specific schema is needed
|
|
1300
|
+
* ```ts
|
|
1301
|
+
* const allowed = collectUsedSchemaNames(includedOps, inputNode.schemas)
|
|
1302
|
+
* allowed.has('OrderStatus') // false when no included operation references OrderStatus
|
|
1303
|
+
* ```
|
|
1304
|
+
*/
|
|
1305
|
+
function collectUsedSchemaNames(operations, schemas) {
|
|
1306
|
+
const schemaMap = /* @__PURE__ */ new Map();
|
|
1307
|
+
for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema);
|
|
1308
|
+
const result = /* @__PURE__ */ new Set();
|
|
1309
|
+
function visitSchema(schema) {
|
|
1310
|
+
const directRefs = collectReferencedSchemaNames(schema);
|
|
1311
|
+
for (const name of directRefs) if (!result.has(name)) {
|
|
1312
|
+
result.add(name);
|
|
1313
|
+
const namedSchema = schemaMap.get(name);
|
|
1314
|
+
if (namedSchema) visitSchema(namedSchema);
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
for (const op of operations) for (const schema of collect(op, {
|
|
1318
|
+
depth: "shallow",
|
|
1319
|
+
schema: (node) => node
|
|
1320
|
+
})) visitSchema(schema);
|
|
1321
|
+
return result;
|
|
1322
|
+
}
|
|
1323
|
+
/**
|
|
1259
1324
|
* Identifies all schemas that participate in circular dependency chains, including direct self-loops.
|
|
1260
1325
|
*
|
|
1261
1326
|
* Returns a Set of schema names with circular dependencies. Use this to wrap recursive schema positions
|
|
@@ -2133,6 +2198,6 @@ function setEnumName(propNode, parentName, propName, enumSuffix) {
|
|
|
2133
2198
|
return propNode;
|
|
2134
2199
|
}
|
|
2135
2200
|
//#endregion
|
|
2136
|
-
export { caseParams, childName, collect, collectImports, collectReferencedSchemaNames, 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 };
|
|
2201
|
+
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 };
|
|
2137
2202
|
|
|
2138
2203
|
//# sourceMappingURL=index.js.map
|