@kubb/ast 5.0.0-beta.20 → 5.0.0-beta.22

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
@@ -729,12 +729,14 @@ type FileNode<TMeta extends object = object> = BaseNode & {
729
729
  meta?: TMeta;
730
730
  /**
731
731
  * Optional banner prepended to the generated file content.
732
+ * Accepts `null` so `resolver.resolveBanner()` results can be passed directly.
732
733
  */
733
- banner?: string;
734
+ banner?: string | null;
734
735
  /**
735
736
  * Optional footer appended to the generated file content.
737
+ * Accepts `null` so `resolver.resolveFooter()` results can be passed directly.
736
738
  */
737
- footer?: string;
739
+ footer?: string | null;
738
740
  };
739
741
  //#endregion
740
742
  //#region src/nodes/function.d.ts
@@ -1326,8 +1328,9 @@ type RefSchemaNode = SchemaNodeBase & {
1326
1328
  type: 'ref';
1327
1329
  /**
1328
1330
  * Referenced schema name.
1331
+ * `null` means Kubb has processed this and determined there is no applicable name.
1329
1332
  */
1330
- name?: string;
1333
+ name?: string | null;
1331
1334
  /**
1332
1335
  * Original `$ref` path, for example, `#/components/schemas/Order`.
1333
1336
  * Used to resolve names later.
@@ -1340,12 +1343,13 @@ type RefSchemaNode = SchemaNodeBase & {
1340
1343
  /**
1341
1344
  * The fully-parsed schema that this ref resolves to.
1342
1345
  * Populated during OAS parsing when the referenced definition can be resolved.
1343
- * `undefined` when the ref cannot be resolved or is part of a circular chain.
1346
+ * `null` when the ref cannot be resolved or is part of a circular chain.
1347
+ * `undefined` when resolution has not been attempted.
1344
1348
  *
1345
1349
  * Useful for inspecting the referenced schema's structure (e.g. `primitive`, `properties`)
1346
1350
  * without following the reference manually.
1347
1351
  */
1348
- schema?: SchemaNode;
1352
+ schema?: SchemaNode | null;
1349
1353
  };
1350
1354
  /**
1351
1355
  * Datetime schema.
@@ -1693,7 +1697,7 @@ type ResponseNode = BaseNode & {
1693
1697
  * Property keys to exclude from the generated type via `Omit<Type, Keys>`.
1694
1698
  * Set when a referenced schema has `writeOnly` fields that should not appear in response types.
1695
1699
  */
1696
- keysToOmit?: Array<string>;
1700
+ keysToOmit?: Array<string> | null;
1697
1701
  };
1698
1702
  //#endregion
1699
1703
  //#region src/nodes/operation.d.ts
@@ -1794,7 +1798,7 @@ type OperationNode = BaseNode & {
1794
1798
  * Property keys to exclude from the generated request body type via `Omit<Type, Keys>`.
1795
1799
  * Set when a referenced schema has `readOnly` fields that should be omitted in request types.
1796
1800
  */
1797
- keysToOmit?: Array<string>;
1801
+ keysToOmit?: Array<string> | null;
1798
1802
  }>;
1799
1803
  };
1800
1804
  /**
@@ -1857,7 +1861,7 @@ type InputMeta = {
1857
1861
  /**
1858
1862
  * Resolved base URL from the first matching server entry in the source document.
1859
1863
  */
1860
- baseURL?: string;
1864
+ baseURL?: string | null;
1861
1865
  /**
1862
1866
  * Names of schemas that participate in a circular reference chain.
1863
1867
  * Computed once during the adapter pre-scan — use this instead of calling
@@ -2657,10 +2661,10 @@ declare function createJsx(value: string): JsxNode;
2657
2661
  * @example
2658
2662
  * ```ts
2659
2663
  * const schema = createSchema({ type: 'string' })
2660
- * const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | undefined
2664
+ * const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null
2661
2665
  * ```
2662
2666
  */
2663
- declare function narrowSchema<T extends SchemaNode['type']>(node: SchemaNode | undefined, type: T): SchemaNodeByType[T] | undefined;
2667
+ declare function narrowSchema<T extends SchemaNode['type']>(node: SchemaNode | undefined, type: T): SchemaNodeByType[T] | null;
2664
2668
  /**
2665
2669
  * Returns `true` when the input is an `InputNode`.
2666
2670
  *
@@ -2725,7 +2729,7 @@ type PrinterHandlerContext<TOutput, TOptions extends object> = {
2725
2729
  * Recursively transform a nested `SchemaNode` to `TOutput` using the node-level handlers.
2726
2730
  * Use `this.transform` inside `nodes` handlers and inside the `print` override.
2727
2731
  */
2728
- transform: (node: SchemaNode) => TOutput | null | undefined;
2732
+ transform: (node: SchemaNode) => TOutput | null;
2729
2733
  /**
2730
2734
  * Options for this printer instance.
2731
2735
  */
@@ -2743,7 +2747,7 @@ type PrinterHandlerContext<TOutput, TOptions extends object> = {
2743
2747
  * }
2744
2748
  * ```
2745
2749
  */
2746
- type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (this: PrinterHandlerContext<TOutput, TOptions>, node: SchemaNodeByType[T]) => TOutput | null | undefined;
2750
+ type PrinterHandler<TOutput, TOptions extends object, T extends SchemaType = SchemaType> = (this: PrinterHandlerContext<TOutput, TOptions>, node: SchemaNodeByType[T]) => TOutput | null;
2747
2751
  /**
2748
2752
  * Partial map of per-node-type handler overrides for a printer.
2749
2753
  *
@@ -2806,13 +2810,13 @@ type Printer<T extends PrinterFactoryOptions = PrinterFactoryOptions> = {
2806
2810
  * Always dispatches through the `nodes` map; never calls the `print` override.
2807
2811
  * Use this when you need the raw output (e.g. `ts.TypeNode`) without declaration wrapping.
2808
2812
  */
2809
- transform: (node: SchemaNode) => T['output'] | null | undefined;
2813
+ transform: (node: SchemaNode) => T['output'] | null;
2810
2814
  /**
2811
2815
  * Public printer. If the builder provides a root-level `print`, this calls that
2812
2816
  * higher-level function (which may produce full declarations).
2813
2817
  * Otherwise, falls back to the node-level dispatcher.
2814
2818
  */
2815
- print: (node: SchemaNode) => T['printOutput'] | null | undefined;
2819
+ print: (node: SchemaNode) => T['printOutput'] | null;
2816
2820
  };
2817
2821
  /**
2818
2822
  * Builder function passed to `definePrinter`.
@@ -2885,22 +2889,22 @@ declare function definePrinter<T extends PrinterFactoryOptions = PrinterFactoryO
2885
2889
  * )
2886
2890
  * ```
2887
2891
  */
2888
- declare function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | undefined): <T extends PrinterFactoryOptions>(build: (options: T["options"]) => {
2892
+ declare function createPrinterFactory<TNode, TKey extends string, TNodeByKey extends Partial<Record<TKey, TNode>>>(getKey: (node: TNode) => TKey | null): <T extends PrinterFactoryOptions>(build: (options: T["options"]) => {
2889
2893
  name: T["name"];
2890
2894
  options: T["options"];
2891
2895
  nodes: Partial<{ [K in TKey]: (this: {
2892
- transform: (node: TNode) => T["output"] | null | undefined;
2896
+ transform: (node: TNode) => T["output"] | null;
2893
2897
  options: T["options"];
2894
- }, node: TNodeByKey[K]) => T["output"] | null | undefined }>;
2898
+ }, node: TNodeByKey[K]) => T["output"] | null }>;
2895
2899
  print?: (this: {
2896
- transform: (node: TNode) => T["output"] | null | undefined;
2900
+ transform: (node: TNode) => T["output"] | null;
2897
2901
  options: T["options"];
2898
- }, node: TNode) => T["printOutput"] | null | undefined;
2902
+ }, node: TNode) => T["printOutput"] | null;
2899
2903
  }) => (options?: T["options"]) => {
2900
2904
  name: T["name"];
2901
2905
  options: T["options"];
2902
- transform: (node: TNode) => T["output"] | null | undefined;
2903
- print: (node: TNode) => T["printOutput"] | null | undefined;
2906
+ transform: (node: TNode) => T["output"] | null;
2907
+ print: (node: TNode) => T["printOutput"] | null;
2904
2908
  };
2905
2909
  //#endregion
2906
2910
  //#region src/refs.d.ts
@@ -2934,7 +2938,7 @@ declare function collectImports<TImport>({
2934
2938
  }: {
2935
2939
  node: SchemaNode;
2936
2940
  nameMapping: Map<string, string>;
2937
- resolve: (schemaName: string) => TImport | undefined;
2941
+ resolve: (schemaName: string) => TImport | null;
2938
2942
  }): Array<TImport>;
2939
2943
  //#endregion
2940
2944
  //#region src/transformers.d.ts
@@ -3103,13 +3107,13 @@ type AsyncVisitor = {
3103
3107
  * ```
3104
3108
  */
3105
3109
  type CollectVisitor<T> = {
3106
- input?(node: InputNode, context: VisitorContext<InputNode>): T | undefined;
3107
- output?(node: OutputNode, context: VisitorContext<OutputNode>): T | undefined;
3108
- operation?(node: OperationNode, context: VisitorContext<OperationNode>): T | undefined;
3109
- schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): T | undefined;
3110
- property?(node: PropertyNode, context: VisitorContext<PropertyNode>): T | undefined;
3111
- parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): T | undefined;
3112
- response?(node: ResponseNode, context: VisitorContext<ResponseNode>): T | undefined;
3110
+ input?(node: InputNode, context: VisitorContext<InputNode>): T | null | undefined;
3111
+ output?(node: OutputNode, context: VisitorContext<OutputNode>): T | null | undefined;
3112
+ operation?(node: OperationNode, context: VisitorContext<OperationNode>): T | null | undefined;
3113
+ schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): T | null | undefined;
3114
+ property?(node: PropertyNode, context: VisitorContext<PropertyNode>): T | null | undefined;
3115
+ parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): T | null | undefined;
3116
+ response?(node: ResponseNode, context: VisitorContext<ResponseNode>): T | null | undefined;
3113
3117
  };
3114
3118
  /**
3115
3119
  * Options for `transform`.
@@ -3228,7 +3232,7 @@ declare function transform(node: Node, options: TransformOptions): Node;
3228
3232
  /**
3229
3233
  * Runs a depth-first synchronous collection pass.
3230
3234
  *
3231
- * Non-`undefined` values returned by visitor callbacks are appended to the result.
3235
+ * Non-`null` values returned by visitor callbacks are appended to the result.
3232
3236
  *
3233
3237
  * @example
3234
3238
  * ```ts
@@ -3430,7 +3434,7 @@ declare function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): st
3430
3434
  /**
3431
3435
  * Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.
3432
3436
  *
3433
- * Returns `undefined` for non-ref nodes or when no name can be resolved. Use this to get a schema's
3437
+ * Returns `null` for non-ref nodes or when no name can be resolved. Use this to get a schema's
3434
3438
  * identifier for type definitions or error messages.
3435
3439
  *
3436
3440
  * @example
@@ -3439,7 +3443,7 @@ declare function extractStringsFromNodes(nodes: Array<CodeNode> | undefined): st
3439
3443
  * // => 'Pet'
3440
3444
  * ```
3441
3445
  */
3442
- declare function resolveRefName(node: SchemaNode | undefined): string | undefined;
3446
+ declare function resolveRefName(node: SchemaNode | undefined): string | null;
3443
3447
  declare function collectReferencedSchemaNames(node: SchemaNode | undefined, out?: Set<string>): Set<string>;
3444
3448
  declare function collectUsedSchemaNames(operations: ReadonlyArray<OperationNode>, schemas: ReadonlyArray<SchemaNode>): Set<string>;
3445
3449
  /**
package/dist/index.js CHANGED
@@ -432,11 +432,11 @@ function trimExtName(text) {
432
432
  * @example
433
433
  * ```ts
434
434
  * const schema = createSchema({ type: 'string' })
435
- * const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | undefined
435
+ * const stringNode = narrowSchema(schema, 'string') // StringSchemaNode | null
436
436
  * ```
437
437
  */
438
438
  function narrowSchema(node, type) {
439
- return node?.type === type ? node : void 0;
439
+ return node?.type === type ? node : null;
440
440
  }
441
441
  function isKind(kind) {
442
442
  return (node) => node.kind === kind;
@@ -727,7 +727,7 @@ function transform(node, options) {
727
727
  /**
728
728
  * Runs a depth-first synchronous collection pass.
729
729
  *
730
- * Non-`undefined` values returned by visitor callbacks are appended to the result.
730
+ * Non-`null` values returned by visitor callbacks are appended to the result.
731
731
  *
732
732
  * @example
733
733
  * ```ts
@@ -771,7 +771,7 @@ function* collectLazy(node, options) {
771
771
  v = visitor.response?.(node, { parent });
772
772
  break;
773
773
  }
774
- if (v !== void 0) yield v;
774
+ if (v != null) yield v;
775
775
  for (const child of getChildren(node, recurse)) yield* collectLazy(child, {
776
776
  ...options,
777
777
  parent: node
@@ -970,7 +970,7 @@ function createOperationParams(node, options) {
970
970
  }));
971
971
  } else {
972
972
  if (pathParams.length) if (pathParamsType === "inlineSpread") {
973
- const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]) ?? void 0;
973
+ const spreadType = resolver?.resolvePathParamsName(node, pathParams[0]);
974
974
  params.push(createFunctionParameter({
975
975
  name: pathName,
976
976
  type: spreadType ? wrapType(spreadType) : void 0,
@@ -1046,13 +1046,13 @@ function buildGroupParam({ name, node, params, groupType, resolver, wrapType })
1046
1046
  }
1047
1047
  /**
1048
1048
  * Derives a {@link ParamGroupType} from the resolver's group method.
1049
- * Returns `undefined` when the group name equals the individual param name (no real group).
1049
+ * Returns `null` when the group name equals the individual param name (no real group).
1050
1050
  */
1051
1051
  function resolveGroupType({ node, params, groupMethod, resolver }) {
1052
- if (!params.length) return;
1052
+ if (!params.length) return null;
1053
1053
  const firstParam = params[0];
1054
1054
  const groupName = groupMethod.call(resolver, node, firstParam);
1055
- if (groupName === resolver.resolveParamName(node, firstParam)) return;
1055
+ if (groupName === resolver.resolveParamName(node, firstParam)) return null;
1056
1056
  const allOptional = params.every((p) => !p.required);
1057
1057
  return {
1058
1058
  type: createParamsType({
@@ -1245,7 +1245,7 @@ function extractStringsFromNodes(nodes) {
1245
1245
  /**
1246
1246
  * Resolves the schema name of a ref node, falling back through `ref` → `name` → nested `schema.name`.
1247
1247
  *
1248
- * Returns `undefined` for non-ref nodes or when no name can be resolved. Use this to get a schema's
1248
+ * Returns `null` for non-ref nodes or when no name can be resolved. Use this to get a schema's
1249
1249
  * identifier for type definitions or error messages.
1250
1250
  *
1251
1251
  * @example
@@ -1255,9 +1255,9 @@ function extractStringsFromNodes(nodes) {
1255
1255
  * ```
1256
1256
  */
1257
1257
  function resolveRefName(node) {
1258
- if (!node || node.type !== "ref") return void 0;
1259
- if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? void 0;
1260
- return node.name ?? node.schema?.name ?? void 0;
1258
+ if (!node || node.type !== "ref") return null;
1259
+ if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null;
1260
+ return node.name ?? node.schema?.name ?? null;
1261
1261
  }
1262
1262
  /**
1263
1263
  * Collects every named schema referenced (transitively) from a node via ref edges.
@@ -1393,9 +1393,9 @@ function findCircularSchemas(schemas) {
1393
1393
  function containsCircularRef(node, { circularSchemas, excludeName }) {
1394
1394
  if (!node || circularSchemas.size === 0) return false;
1395
1395
  for (const _ of collectLazy(node, { schema(child) {
1396
- if (child.type !== "ref") return void 0;
1396
+ if (child.type !== "ref") return null;
1397
1397
  const name = resolveRefName(child);
1398
- return name && name !== excludeName && circularSchemas.has(name) ? true : void 0;
1398
+ return name && name !== excludeName && circularSchemas.has(name) ? true : null;
1399
1399
  } })) return true;
1400
1400
  return false;
1401
1401
  }
@@ -2097,7 +2097,7 @@ function createPrinterFactory(getKey) {
2097
2097
  options: resolvedOptions,
2098
2098
  transform: (node) => {
2099
2099
  const key = getKey(node);
2100
- if (key === void 0) return null;
2100
+ if (key === null) return null;
2101
2101
  const handler = nodes[key];
2102
2102
  if (!handler) return null;
2103
2103
  return handler.call(context, node);
@@ -2134,10 +2134,10 @@ function enumPropName(parentName, propName, enumSuffix) {
2134
2134
  function collectImports({ node, nameMapping, resolve }) {
2135
2135
  return collect(node, { schema(schemaNode) {
2136
2136
  const schemaRef = narrowSchema(schemaNode, "ref");
2137
- if (!schemaRef?.ref) return;
2137
+ if (!schemaRef?.ref) return null;
2138
2138
  const rawName = extractRefName(schemaRef.ref);
2139
2139
  const result = resolve(nameMapping.get(rawName) ?? rawName);
2140
- if (!result) return;
2140
+ if (!result) return null;
2141
2141
  return result;
2142
2142
  } });
2143
2143
  }
@@ -2243,7 +2243,7 @@ function setEnumName(propNode, parentName, propName, enumSuffix) {
2243
2243
  const enumNode = narrowSchema(propNode, "enum");
2244
2244
  if (enumNode?.primitive === "boolean") return {
2245
2245
  ...propNode,
2246
- name: void 0
2246
+ name: null
2247
2247
  };
2248
2248
  if (enumNode) return {
2249
2249
  ...propNode,