@kubb/ast 5.0.0-beta.22 → 5.0.0-beta.24
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 +55 -34
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +143 -93
- package/dist/index.js +55 -34
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/factory.ts +6 -3
- package/src/infer.ts +15 -4
- package/src/nodes/code.ts +21 -21
- package/src/nodes/file.ts +12 -12
- package/src/printer.ts +19 -12
- package/src/utils.ts +4 -4
- package/src/visitor.ts +72 -43
package/dist/index.cjs
CHANGED
|
@@ -610,11 +610,14 @@ function* getChildren(node, recurse) {
|
|
|
610
610
|
}
|
|
611
611
|
}
|
|
612
612
|
/**
|
|
613
|
-
*
|
|
614
|
-
*
|
|
615
|
-
* (default: `WALK_CONCURRENCY`).
|
|
613
|
+
* Async depth-first traversal for side effects. Visitor return values are
|
|
614
|
+
* ignored. Use `transform` when you want to rewrite nodes.
|
|
616
615
|
*
|
|
617
|
-
*
|
|
616
|
+
* Sibling nodes at each depth run concurrently up to `options.concurrency`
|
|
617
|
+
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
|
|
618
|
+
* work; lower values reduce memory pressure.
|
|
619
|
+
*
|
|
620
|
+
* @example Log every operation
|
|
618
621
|
* ```ts
|
|
619
622
|
* await walk(root, {
|
|
620
623
|
* operation(node) {
|
|
@@ -623,10 +626,9 @@ function* getChildren(node, recurse) {
|
|
|
623
626
|
* })
|
|
624
627
|
* ```
|
|
625
628
|
*
|
|
626
|
-
* @example
|
|
629
|
+
* @example Only visit the root node
|
|
627
630
|
* ```ts
|
|
628
|
-
*
|
|
629
|
-
* await walk(root, { depth: 'shallow', root: () => {} })
|
|
631
|
+
* await walk(root, { depth: 'shallow', input: () => {} })
|
|
630
632
|
* ```
|
|
631
633
|
*/
|
|
632
634
|
async function walk(node, options) {
|
|
@@ -748,23 +750,19 @@ function transform(node, options) {
|
|
|
748
750
|
return node;
|
|
749
751
|
}
|
|
750
752
|
/**
|
|
751
|
-
*
|
|
753
|
+
* Lazy depth-first collection pass. Yields every non-null value returned by
|
|
754
|
+
* the visitor callbacks. Use `collect` for the eager array form.
|
|
752
755
|
*
|
|
753
|
-
*
|
|
754
|
-
*
|
|
755
|
-
* @example
|
|
756
|
+
* @example Collect every operationId
|
|
756
757
|
* ```ts
|
|
757
|
-
* const ids =
|
|
758
|
+
* const ids: string[] = []
|
|
759
|
+
* for (const id of collectLazy<string>(root, {
|
|
758
760
|
* operation(node) {
|
|
759
761
|
* return node.operationId
|
|
760
762
|
* },
|
|
761
|
-
* })
|
|
762
|
-
*
|
|
763
|
-
*
|
|
764
|
-
* @example
|
|
765
|
-
* ```ts
|
|
766
|
-
* // Collect from only the current node
|
|
767
|
-
* const values = collect(root, { depth: 'shallow', root: () => 'root' })
|
|
763
|
+
* })) {
|
|
764
|
+
* ids.push(id)
|
|
765
|
+
* }
|
|
768
766
|
* ```
|
|
769
767
|
*/
|
|
770
768
|
function* collectLazy(node, options) {
|
|
@@ -800,6 +798,19 @@ function* collectLazy(node, options) {
|
|
|
800
798
|
parent: node
|
|
801
799
|
});
|
|
802
800
|
}
|
|
801
|
+
/**
|
|
802
|
+
* Eager depth-first collection pass. Returns an array of every non-null value
|
|
803
|
+
* the visitor callbacks return.
|
|
804
|
+
*
|
|
805
|
+
* @example Collect every operationId
|
|
806
|
+
* ```ts
|
|
807
|
+
* const ids = collect<string>(root, {
|
|
808
|
+
* operation(node) {
|
|
809
|
+
* return node.operationId
|
|
810
|
+
* },
|
|
811
|
+
* })
|
|
812
|
+
* ```
|
|
813
|
+
*/
|
|
803
814
|
function collect(node, options) {
|
|
804
815
|
return Array.from(collectLazy(node, options));
|
|
805
816
|
}
|
|
@@ -1425,10 +1436,13 @@ function containsCircularRef(node, { circularSchemas, excludeName }) {
|
|
|
1425
1436
|
//#endregion
|
|
1426
1437
|
//#region src/factory.ts
|
|
1427
1438
|
/**
|
|
1428
|
-
*
|
|
1439
|
+
* Updates a schema's `optional` and `nullish` flags from a parent's `required`
|
|
1440
|
+
* value and the schema's own `nullable`. Mirrors how OpenAPI parameters and
|
|
1441
|
+
* object properties combine "required" and "nullable" into a single AST.
|
|
1429
1442
|
*
|
|
1430
|
-
* -
|
|
1431
|
-
* -
|
|
1443
|
+
* - Non-required + non-nullable → `optional: true`.
|
|
1444
|
+
* - Non-required + nullable → `nullish: true`.
|
|
1445
|
+
* - Required → both flags cleared.
|
|
1432
1446
|
*/
|
|
1433
1447
|
function syncOptionality(schema, required) {
|
|
1434
1448
|
const nullable = schema.nullable ?? false;
|
|
@@ -2068,22 +2082,27 @@ function createJsx(value) {
|
|
|
2068
2082
|
//#endregion
|
|
2069
2083
|
//#region src/printer.ts
|
|
2070
2084
|
/**
|
|
2071
|
-
*
|
|
2072
|
-
*
|
|
2073
|
-
*
|
|
2085
|
+
* Defines a schema printer: a function that takes a `SchemaNode` and emits
|
|
2086
|
+
* code in your target language. Each plugin that produces code from schemas
|
|
2087
|
+
* (TypeScript types, Zod schemas, Faker factories) ships a printer built
|
|
2088
|
+
* with this helper.
|
|
2074
2089
|
*
|
|
2075
2090
|
* The builder receives resolved options and returns:
|
|
2076
|
-
* - `name` — a unique identifier for the printer
|
|
2077
|
-
* - `options` — options stored on the returned printer instance
|
|
2078
|
-
* - `nodes` — a map of `SchemaType` → handler functions that convert a `SchemaNode` to `TOutput`
|
|
2079
|
-
* - `print` _(optional)_ — top-level override exposed as `printer.print`
|
|
2080
|
-
* - Inside this function, use `this.transform(node)` to dispatch to the `nodes` map
|
|
2081
|
-
* - This keeps recursion safe and avoids self-calls
|
|
2082
2091
|
*
|
|
2083
|
-
*
|
|
2092
|
+
* - `name` — unique identifier for the printer.
|
|
2093
|
+
* - `options` — stored on the returned printer instance.
|
|
2094
|
+
* - `nodes` — map of `SchemaType` → handler. Handlers return the rendered
|
|
2095
|
+
* output (a string, a TypeScript AST node, ...) for that schema type.
|
|
2096
|
+
* - `print` (optional) — top-level override exposed as `printer.print`.
|
|
2097
|
+
* Use `this.transform(node)` inside it to dispatch to `nodes` recursively.
|
|
2084
2098
|
*
|
|
2085
|
-
*
|
|
2099
|
+
* Without a `print` override, `printer.print` falls back to `printer.transform`
|
|
2100
|
+
* (the node-level dispatcher).
|
|
2101
|
+
*
|
|
2102
|
+
* @example Tiny Zod printer
|
|
2086
2103
|
* ```ts
|
|
2104
|
+
* import { definePrinter, type PrinterFactoryOptions } from '@kubb/ast'
|
|
2105
|
+
*
|
|
2087
2106
|
* type PrinterZod = PrinterFactoryOptions<'zod', { strict?: boolean }, string>
|
|
2088
2107
|
*
|
|
2089
2108
|
* export const zodPrinter = definePrinter<PrinterZod>((options) => ({
|
|
@@ -2092,7 +2111,9 @@ function createJsx(value) {
|
|
|
2092
2111
|
* nodes: {
|
|
2093
2112
|
* string: () => 'z.string()',
|
|
2094
2113
|
* object(node) {
|
|
2095
|
-
* const props = node.properties
|
|
2114
|
+
* const props = node.properties
|
|
2115
|
+
* .map((p) => `${p.name}: ${this.transform(p.schema)}`)
|
|
2116
|
+
* .join(', ')
|
|
2096
2117
|
* return `z.object({ ${props} })`
|
|
2097
2118
|
* },
|
|
2098
2119
|
* },
|