@kubb/ast 5.0.0-beta.1 → 5.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -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
- * @note Returns a Set of schema names for efficient membership testing.
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
@@ -1170,7 +1170,7 @@ function combineImports(imports, exports, source) {
1170
1170
  const { path, isTypeOnly } = curr;
1171
1171
  let { name } = curr;
1172
1172
  if (Array.isArray(name)) {
1173
- name = [...new Set(name)].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.propertyName));
1173
+ name = [...new Set(name)].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName));
1174
1174
  if (!name.length) continue;
1175
1175
  const key = pathTypeKey(path, isTypeOnly);
1176
1176
  const existing = namedByPath.get(key);
@@ -1243,7 +1243,19 @@ function resolveRefName(node) {
1243
1243
  * Refs are followed by name only — the resolved `node.schema` is not traversed inline.
1244
1244
  * Use this to determine schema dependencies, build reference graphs, or detect what schemas need to be emitted.
1245
1245
  *
1246
- * @note Returns a Set of schema names for efficient membership testing.
1246
+ * @example Collect refs from a single schema
1247
+ * ```ts
1248
+ * const names = collectReferencedSchemaNames(petSchema)
1249
+ * // → Set { 'Category', 'Tag' }
1250
+ * ```
1251
+ *
1252
+ * @example Accumulate refs from multiple schemas into one set
1253
+ * ```ts
1254
+ * const out = new Set<string>()
1255
+ * for (const schema of schemas) {
1256
+ * collectReferencedSchemaNames(schema, out)
1257
+ * }
1258
+ * ```
1247
1259
  */
1248
1260
  function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
1249
1261
  if (!node) return out;
@@ -1256,6 +1268,52 @@ function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
1256
1268
  return out;
1257
1269
  }
1258
1270
  /**
1271
+ * Collects the names of all top-level schemas transitively used by a set of operations.
1272
+ *
1273
+ * An operation uses a schema when any of its parameters, request body content, or responses
1274
+ * reference it — directly or indirectly through other named schemas.
1275
+ * The walk is iterative and safe against reference cycles.
1276
+ *
1277
+ * Use this together with `include` filters to determine which schemas from `components/schemas`
1278
+ * are reachable from the allowed operations, so that schemas used only by excluded operations
1279
+ * are not generated.
1280
+ *
1281
+ * @example Only generate schemas referenced by included operations
1282
+ * ```ts
1283
+ * const includedOps = inputNode.operations.filter(op => resolver.resolveOptions(op, { options, include }) !== null)
1284
+ * const allowed = collectUsedSchemaNames(includedOps, inputNode.schemas)
1285
+ *
1286
+ * for (const schema of inputNode.schemas) {
1287
+ * if (schema.name && !allowed.has(schema.name)) continue
1288
+ * // … generate schema
1289
+ * }
1290
+ * ```
1291
+ *
1292
+ * @example Check whether a specific schema is needed
1293
+ * ```ts
1294
+ * const allowed = collectUsedSchemaNames(includedOps, inputNode.schemas)
1295
+ * allowed.has('OrderStatus') // false when no included operation references OrderStatus
1296
+ * ```
1297
+ */
1298
+ function collectUsedSchemaNames(operations, schemas) {
1299
+ const schemaMap = /* @__PURE__ */ new Map();
1300
+ for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema);
1301
+ const result = /* @__PURE__ */ new Set();
1302
+ function visitSchema(schema) {
1303
+ const directRefs = collectReferencedSchemaNames(schema);
1304
+ for (const name of directRefs) if (!result.has(name)) {
1305
+ result.add(name);
1306
+ const namedSchema = schemaMap.get(name);
1307
+ if (namedSchema) visitSchema(namedSchema);
1308
+ }
1309
+ }
1310
+ for (const op of operations) for (const schema of collect(op, {
1311
+ depth: "shallow",
1312
+ schema: (node) => node
1313
+ })) visitSchema(schema);
1314
+ return result;
1315
+ }
1316
+ /**
1259
1317
  * Identifies all schemas that participate in circular dependency chains, including direct self-loops.
1260
1318
  *
1261
1319
  * Returns a Set of schema names with circular dependencies. Use this to wrap recursive schema positions
@@ -2133,6 +2191,6 @@ function setEnumName(propNode, parentName, propName, enumSuffix) {
2133
2191
  return propNode;
2134
2192
  }
2135
2193
  //#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 };
2194
+ 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
2195
 
2138
2196
  //# sourceMappingURL=index.js.map