@kubb/ast 5.0.0-beta.75 → 5.0.0-beta.9

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 CHANGED
@@ -128,7 +128,7 @@ const pet = resolveRef(refMap, 'Pet')
128
128
 
129
129
  ## Supporting Kubb
130
130
 
131
- Kubb uses an MIT-licensed open source project with its ongoing development made possible entirely by the support of Sponsors. If you would like to become a sponsor, please consider:
131
+ Kubb is an MIT-licensed open source project with its ongoing development made possible entirely by the support of Sponsors. If you would like to become a sponsor, please consider:
132
132
 
133
133
  - [Become a Sponsor on GitHub](https://github.com/sponsors/stijnvanhulle)
134
134
 
package/dist/index.cjs CHANGED
@@ -468,12 +468,6 @@ const isOperationNode = isKind("Operation");
468
468
  * ```
469
469
  */
470
470
  const isSchemaNode = isKind("Schema");
471
- isKind("Property");
472
- isKind("Parameter");
473
- isKind("Response");
474
- isKind("FunctionParameter");
475
- isKind("ParameterGroup");
476
- isKind("FunctionParameters");
477
471
  //#endregion
478
472
  //#region src/refs.ts
479
473
  /**
@@ -1180,6 +1174,13 @@ function combineExports(exports) {
1180
1174
  function combineImports(imports, exports, source) {
1181
1175
  const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
1182
1176
  const isUsed = (importName) => !source || source.includes(importName) || exportedNames.has(importName);
1177
+ const importNameMemo = /* @__PURE__ */ new Map();
1178
+ const canonicalizeName = (n) => {
1179
+ if (typeof n === "string") return n;
1180
+ const key = `${n.propertyName}:${n.name ?? ""}`;
1181
+ if (!importNameMemo.has(key)) importNameMemo.set(key, n);
1182
+ return importNameMemo.get(key);
1183
+ };
1183
1184
  const result = [];
1184
1185
  const namedByPath = /* @__PURE__ */ new Map();
1185
1186
  const seen = /* @__PURE__ */ new Set();
@@ -1193,7 +1194,7 @@ function combineImports(imports, exports, source) {
1193
1194
  const { path, isTypeOnly } = curr;
1194
1195
  let { name } = curr;
1195
1196
  if (Array.isArray(name)) {
1196
- name = [...new Set(name)].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.propertyName));
1197
+ name = [...new Set(name.map(canonicalizeName))].filter((item) => typeof item === "string" ? isUsed(item) : isUsed(item.name ?? item.propertyName));
1197
1198
  if (!name.length) continue;
1198
1199
  const key = pathTypeKey(path, isTypeOnly);
1199
1200
  const existing = namedByPath.get(key);
@@ -1266,7 +1267,19 @@ function resolveRefName(node) {
1266
1267
  * Refs are followed by name only — the resolved `node.schema` is not traversed inline.
1267
1268
  * Use this to determine schema dependencies, build reference graphs, or detect what schemas need to be emitted.
1268
1269
  *
1269
- * @note Returns a Set of schema names for efficient membership testing.
1270
+ * @example Collect refs from a single schema
1271
+ * ```ts
1272
+ * const names = collectReferencedSchemaNames(petSchema)
1273
+ * // → Set { 'Category', 'Tag' }
1274
+ * ```
1275
+ *
1276
+ * @example Accumulate refs from multiple schemas into one set
1277
+ * ```ts
1278
+ * const out = new Set<string>()
1279
+ * for (const schema of schemas) {
1280
+ * collectReferencedSchemaNames(schema, out)
1281
+ * }
1282
+ * ```
1270
1283
  */
1271
1284
  function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
1272
1285
  if (!node) return out;
@@ -1279,6 +1292,52 @@ function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
1279
1292
  return out;
1280
1293
  }
1281
1294
  /**
1295
+ * Collects the names of all top-level schemas transitively used by a set of operations.
1296
+ *
1297
+ * An operation uses a schema when any of its parameters, request body content, or responses
1298
+ * reference it — directly or indirectly through other named schemas.
1299
+ * The walk is iterative and safe against reference cycles.
1300
+ *
1301
+ * Use this together with `include` filters to determine which schemas from `components/schemas`
1302
+ * are reachable from the allowed operations, so that schemas used only by excluded operations
1303
+ * are not generated.
1304
+ *
1305
+ * @example Only generate schemas referenced by included operations
1306
+ * ```ts
1307
+ * const includedOps = inputNode.operations.filter(op => resolver.resolveOptions(op, { options, include }) !== null)
1308
+ * const allowed = collectUsedSchemaNames(includedOps, inputNode.schemas)
1309
+ *
1310
+ * for (const schema of inputNode.schemas) {
1311
+ * if (schema.name && !allowed.has(schema.name)) continue
1312
+ * // … generate schema
1313
+ * }
1314
+ * ```
1315
+ *
1316
+ * @example Check whether a specific schema is needed
1317
+ * ```ts
1318
+ * const allowed = collectUsedSchemaNames(includedOps, inputNode.schemas)
1319
+ * allowed.has('OrderStatus') // false when no included operation references OrderStatus
1320
+ * ```
1321
+ */
1322
+ function collectUsedSchemaNames(operations, schemas) {
1323
+ const schemaMap = /* @__PURE__ */ new Map();
1324
+ for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema);
1325
+ const result = /* @__PURE__ */ new Set();
1326
+ function visitSchema(schema) {
1327
+ const directRefs = collectReferencedSchemaNames(schema);
1328
+ for (const name of directRefs) if (!result.has(name)) {
1329
+ result.add(name);
1330
+ const namedSchema = schemaMap.get(name);
1331
+ if (namedSchema) visitSchema(namedSchema);
1332
+ }
1333
+ }
1334
+ for (const op of operations) for (const schema of collect(op, {
1335
+ depth: "shallow",
1336
+ schema: (node) => node
1337
+ })) visitSchema(schema);
1338
+ return result;
1339
+ }
1340
+ /**
1282
1341
  * Identifies all schemas that participate in circular dependency chains, including direct self-loops.
1283
1342
  *
1284
1343
  * Returns a Set of schema names with circular dependencies. Use this to wrap recursive schema positions
@@ -2161,6 +2220,7 @@ exports.childName = childName;
2161
2220
  exports.collect = collect;
2162
2221
  exports.collectImports = collectImports;
2163
2222
  exports.collectReferencedSchemaNames = collectReferencedSchemaNames;
2223
+ exports.collectUsedSchemaNames = collectUsedSchemaNames;
2164
2224
  exports.containsCircularRef = containsCircularRef;
2165
2225
  exports.createArrowFunction = createArrowFunction;
2166
2226
  exports.createBreak = createBreak;