@kubb/ast 5.12.7 → 5.13.0

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.cts CHANGED
@@ -145,6 +145,12 @@ type BaseNode = {
145
145
  * Node discriminator.
146
146
  */
147
147
  kind: NodeKind;
148
+ /**
149
+ * Where the node comes from in the input, in the form the adapter's `locate` reads. Document
150
+ * adapters such as OpenAPI and AsyncAPI use an RFC 6901 JSON pointer, for example
151
+ * `#/components/schemas/Pet`. Plugins can copy it to the code nodes they emit.
152
+ */
153
+ pointer?: string;
148
154
  };
149
155
  /** `Omit` applied to each member of a union, so the union survives. */
150
156
  type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
@@ -858,6 +864,11 @@ type ParameterNode = BaseNode & {
858
864
  * percent-encoded. Only applies to query parameters. Absent when the source omits it.
859
865
  */
860
866
  allowReserved?: boolean;
867
+ /**
868
+ * Media type of a parameter described with `content` instead of `schema`, for example
869
+ * `application/json` when the value is sent as a JSON-encoded string. Absent for `schema` parameters.
870
+ */
871
+ contentType?: string;
861
872
  };
862
873
  type UserParameterNode = Pick<ParameterNode, 'name' | 'in' | 'schema'> & Partial<Omit<ParameterNode, 'kind' | 'name' | 'in' | 'schema'>>;
863
874
  /**
package/dist/index.d.ts CHANGED
@@ -145,6 +145,12 @@ type BaseNode = {
145
145
  * Node discriminator.
146
146
  */
147
147
  kind: NodeKind;
148
+ /**
149
+ * Where the node comes from in the input, in the form the adapter's `locate` reads. Document
150
+ * adapters such as OpenAPI and AsyncAPI use an RFC 6901 JSON pointer, for example
151
+ * `#/components/schemas/Pet`. Plugins can copy it to the code nodes they emit.
152
+ */
153
+ pointer?: string;
148
154
  };
149
155
  /** `Omit` applied to each member of a union, so the union survives. */
150
156
  type DistributiveOmit<T, K extends PropertyKey> = T extends unknown ? Omit<T, K> : never;
@@ -858,6 +864,11 @@ type ParameterNode = BaseNode & {
858
864
  * percent-encoded. Only applies to query parameters. Absent when the source omits it.
859
865
  */
860
866
  allowReserved?: boolean;
867
+ /**
868
+ * Media type of a parameter described with `content` instead of `schema`, for example
869
+ * `application/json` when the value is sent as a JSON-encoded string. Absent for `schema` parameters.
870
+ */
871
+ contentType?: string;
861
872
  };
862
873
  type UserParameterNode = Pick<ParameterNode, 'name' | 'in' | 'schema'> & Partial<Omit<ParameterNode, 'kind' | 'name' | 'in' | 'schema'>>;
863
874
  /**
package/dist/index.js CHANGED
@@ -220,24 +220,26 @@ function isNodeRecord(value) {
220
220
  * // returns parameters, the request body, and responses
221
221
  * ```
222
222
  */
223
- function* getChildren(node, recurse) {
223
+ function getChildren(node, recurse) {
224
+ const children = [];
224
225
  const keys = node.kind === "Schema" && !recurse ? void 0 : VISITOR_KEYS[node.kind];
225
- if (!keys) return;
226
+ if (!keys) return children;
226
227
  const record = node;
227
228
  for (const key of keys) {
228
229
  const value = record[key];
229
230
  if (Array.isArray(value)) {
230
- for (const item of value) if (isNode(item)) yield item;
231
+ for (const item of value) if (isNode(item)) children.push(item);
231
232
  continue;
232
233
  }
233
234
  if (isNode(value)) {
234
- yield value;
235
+ children.push(value);
235
236
  continue;
236
237
  }
237
238
  if (isNodeRecord(value)) {
238
- for (const item of Object.values(value)) if (isNode(item)) yield item;
239
+ for (const item of Object.values(value)) if (isNode(item)) children.push(item);
239
240
  }
240
241
  }
242
+ return children;
241
243
  }
242
244
  /**
243
245
  * Runs the visitor callback that matches `node.kind` with the traversal
@@ -374,7 +376,15 @@ function* collectNode(node, visitor, recurse, parent) {
374
376
  * ```
375
377
  */
376
378
  function collectSync(node, options) {
377
- return Array.from(collect(node, options));
379
+ const { depth, parent, ...visitor } = options;
380
+ const result = [];
381
+ collectInto(node, visitor, (depth ?? "deep") === "deep", parent, result);
382
+ return result;
383
+ }
384
+ function collectInto(node, visitor, recurse, parent, result) {
385
+ const v = applyVisitor(node, visitor, parent);
386
+ if (v != null) result.push(v);
387
+ for (const child of getChildren(node, recurse)) collectInto(child, visitor, recurse, node, result);
378
388
  }
379
389
  //#endregion
380
390
  //#region src/defineMacro.ts
@@ -613,6 +623,14 @@ function resolveRefName(node) {
613
623
  if (node.ref) return refName(node.ref);
614
624
  return node.name ?? node.schema?.name ?? null;
615
625
  }
626
+ /** Fields of a ref node that describe the ref itself, so they never override the resolved schema. */
627
+ const REF_OWN_KEYS = /* @__PURE__ */ new Set([
628
+ "kind",
629
+ "type",
630
+ "name",
631
+ "ref",
632
+ "schema"
633
+ ]);
616
634
  /**
617
635
  * Merges a ref node with its resolved schema, giving the fields set at the usage site precedence.
618
636
  *
@@ -629,12 +647,12 @@ function resolveRefName(node) {
629
647
  */
630
648
  function mergeRefWithSchema(node) {
631
649
  if (!node.schema) return node;
632
- const { kind: _kind, type: _type, name: _name, ref: _ref, schema: _schema, ...overrides } = node;
633
- const definedOverrides = Object.fromEntries(Object.entries(overrides).filter(([, value]) => value !== void 0));
634
- return {
635
- ...node.schema,
636
- ...definedOverrides
637
- };
650
+ const merged = { ...node.schema };
651
+ for (const key of Object.keys(node)) {
652
+ const value = node[key];
653
+ if (value !== void 0 && !REF_OWN_KEYS.has(key)) merged[key] = value;
654
+ }
655
+ return merged;
638
656
  }
639
657
  /**
640
658
  * Returns `true` when a schema is a bare ref without modifiers, metadata, or omit keys.
@@ -1088,16 +1106,23 @@ function mergeByPath(result) {
1088
1106
  return ({ node, names }) => {
1089
1107
  const key = `${node.path}:${node.isTypeOnly ?? false}`;
1090
1108
  const existing = byPath.get(key);
1091
- if (existing && Array.isArray(existing.name)) {
1092
- existing.name = [.../* @__PURE__ */ new Set([...existing.name, ...names])];
1109
+ if (existing) {
1110
+ for (const name of names) {
1111
+ if (existing.seen.has(name)) continue;
1112
+ existing.seen.add(name);
1113
+ existing.names.push(name);
1114
+ }
1093
1115
  return;
1094
1116
  }
1095
1117
  const item = {
1096
1118
  ...node,
1097
- name: names
1119
+ name: [...names]
1098
1120
  };
1099
1121
  result.push(item);
1100
- byPath.set(key, item);
1122
+ byPath.set(key, {
1123
+ names: item.name,
1124
+ seen: new Set(names)
1125
+ });
1101
1126
  };
1102
1127
  }
1103
1128
  /**