@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.cjs CHANGED
@@ -1193,7 +1193,7 @@ function combineImports(imports, exports, source) {
1193
1193
  const { path, isTypeOnly } = curr;
1194
1194
  let { name } = curr;
1195
1195
  if (Array.isArray(name)) {
1196
- name = [...new Set(name)].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.propertyName));
1196
+ name = [...new Set(name)].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName));
1197
1197
  if (!name.length) continue;
1198
1198
  const key = pathTypeKey(path, isTypeOnly);
1199
1199
  const existing = namedByPath.get(key);
@@ -1266,7 +1266,19 @@ function resolveRefName(node) {
1266
1266
  * Refs are followed by name only — the resolved `node.schema` is not traversed inline.
1267
1267
  * Use this to determine schema dependencies, build reference graphs, or detect what schemas need to be emitted.
1268
1268
  *
1269
- * @note Returns a Set of schema names for efficient membership testing.
1269
+ * @example Collect refs from a single schema
1270
+ * ```ts
1271
+ * const names = collectReferencedSchemaNames(petSchema)
1272
+ * // → Set { 'Category', 'Tag' }
1273
+ * ```
1274
+ *
1275
+ * @example Accumulate refs from multiple schemas into one set
1276
+ * ```ts
1277
+ * const out = new Set<string>()
1278
+ * for (const schema of schemas) {
1279
+ * collectReferencedSchemaNames(schema, out)
1280
+ * }
1281
+ * ```
1270
1282
  */
1271
1283
  function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
1272
1284
  if (!node) return out;
@@ -1279,6 +1291,52 @@ function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
1279
1291
  return out;
1280
1292
  }
1281
1293
  /**
1294
+ * Collects the names of all top-level schemas transitively used by a set of operations.
1295
+ *
1296
+ * An operation uses a schema when any of its parameters, request body content, or responses
1297
+ * reference it — directly or indirectly through other named schemas.
1298
+ * The walk is iterative and safe against reference cycles.
1299
+ *
1300
+ * Use this together with `include` filters to determine which schemas from `components/schemas`
1301
+ * are reachable from the allowed operations, so that schemas used only by excluded operations
1302
+ * are not generated.
1303
+ *
1304
+ * @example Only generate schemas referenced by included operations
1305
+ * ```ts
1306
+ * const includedOps = inputNode.operations.filter(op => resolver.resolveOptions(op, { options, include }) !== null)
1307
+ * const allowed = collectUsedSchemaNames(includedOps, inputNode.schemas)
1308
+ *
1309
+ * for (const schema of inputNode.schemas) {
1310
+ * if (schema.name && !allowed.has(schema.name)) continue
1311
+ * // … generate schema
1312
+ * }
1313
+ * ```
1314
+ *
1315
+ * @example Check whether a specific schema is needed
1316
+ * ```ts
1317
+ * const allowed = collectUsedSchemaNames(includedOps, inputNode.schemas)
1318
+ * allowed.has('OrderStatus') // false when no included operation references OrderStatus
1319
+ * ```
1320
+ */
1321
+ function collectUsedSchemaNames(operations, schemas) {
1322
+ const schemaMap = /* @__PURE__ */ new Map();
1323
+ for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema);
1324
+ const result = /* @__PURE__ */ new Set();
1325
+ function visitSchema(schema) {
1326
+ const directRefs = collectReferencedSchemaNames(schema);
1327
+ for (const name of directRefs) if (!result.has(name)) {
1328
+ result.add(name);
1329
+ const namedSchema = schemaMap.get(name);
1330
+ if (namedSchema) visitSchema(namedSchema);
1331
+ }
1332
+ }
1333
+ for (const op of operations) for (const schema of collect(op, {
1334
+ depth: "shallow",
1335
+ schema: (node) => node
1336
+ })) visitSchema(schema);
1337
+ return result;
1338
+ }
1339
+ /**
1282
1340
  * Identifies all schemas that participate in circular dependency chains, including direct self-loops.
1283
1341
  *
1284
1342
  * Returns a Set of schema names with circular dependencies. Use this to wrap recursive schema positions
@@ -2161,6 +2219,7 @@ exports.childName = childName;
2161
2219
  exports.collect = collect;
2162
2220
  exports.collectImports = collectImports;
2163
2221
  exports.collectReferencedSchemaNames = collectReferencedSchemaNames;
2222
+ exports.collectUsedSchemaNames = collectUsedSchemaNames;
2164
2223
  exports.containsCircularRef = containsCircularRef;
2165
2224
  exports.createArrowFunction = createArrowFunction;
2166
2225
  exports.createBreak = createBreak;