@kubb/ast 5.0.0-beta.6 → 5.0.0-beta.61
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/LICENSE +17 -10
- package/README.md +51 -27
- package/dist/chunk-CNktS9qV.js +17 -0
- package/dist/defineMacro-BLIR6k-j.d.ts +475 -0
- package/dist/defineMacro-BTXvS8nI.js +106 -0
- package/dist/defineMacro-BTXvS8nI.js.map +1 -0
- package/dist/defineMacro-Bv9R_9a2.cjs +123 -0
- package/dist/defineMacro-Bv9R_9a2.cjs.map +1 -0
- package/dist/extractStringsFromNodes-Cja-xxx5.js +29 -0
- package/dist/extractStringsFromNodes-Cja-xxx5.js.map +1 -0
- package/dist/extractStringsFromNodes-DKgDjFO0.cjs +34 -0
- package/dist/extractStringsFromNodes-DKgDjFO0.cjs.map +1 -0
- package/dist/extractStringsFromNodes-p4mX1TQD.d.ts +14 -0
- package/dist/factory-CZNOGI-N.js +283 -0
- package/dist/factory-CZNOGI-N.js.map +1 -0
- package/dist/factory-DG1CVkEb.cjs +300 -0
- package/dist/factory-DG1CVkEb.cjs.map +1 -0
- package/dist/factory.cjs +29 -0
- package/dist/factory.d.ts +62 -0
- package/dist/factory.js +3 -0
- package/dist/index-BzjwdK2M.d.ts +2433 -0
- package/dist/index.cjs +444 -2180
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +94 -3408
- package/dist/index.js +395 -2101
- package/dist/index.js.map +1 -1
- package/dist/macros.cjs +117 -0
- package/dist/macros.cjs.map +1 -0
- package/dist/macros.d.ts +59 -0
- package/dist/macros.js +115 -0
- package/dist/macros.js.map +1 -0
- package/dist/operationParams-BZ07xDm0.d.ts +204 -0
- package/dist/response-KUdWiDWw.js +658 -0
- package/dist/response-KUdWiDWw.js.map +1 -0
- package/dist/response-hnSw2NKE.cjs +1027 -0
- package/dist/response-hnSw2NKE.cjs.map +1 -0
- package/dist/types-DyDzizSf.d.ts +364 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.ts +6 -0
- package/dist/types.js +1 -0
- package/dist/utils-BLJwyza-.cjs +912 -0
- package/dist/utils-BLJwyza-.cjs.map +1 -0
- package/dist/utils-CF_-Pn_c.js +770 -0
- package/dist/utils-CF_-Pn_c.js.map +1 -0
- package/dist/utils.cjs +36 -0
- package/dist/utils.d.ts +358 -0
- package/dist/utils.js +4 -0
- package/dist/visitor-DJ6ZEJvq.js +548 -0
- package/dist/visitor-DJ6ZEJvq.js.map +1 -0
- package/dist/visitor-DpKZ9Tk0.cjs +654 -0
- package/dist/visitor-DpKZ9Tk0.cjs.map +1 -0
- package/package.json +21 -6
- package/src/constants.ts +19 -64
- package/src/dedupe.ts +239 -0
- package/src/defineMacro.ts +132 -0
- package/src/dialect.ts +53 -0
- package/src/factory.ts +67 -678
- package/src/guards.ts +10 -92
- package/src/index.ts +13 -44
- package/src/infer.ts +16 -14
- package/src/macros/index.ts +3 -0
- package/src/macros/macroDiscriminatorEnum.ts +44 -0
- package/src/macros/macroEnumName.ts +25 -0
- package/src/macros/macroSimplifyUnion.ts +50 -0
- package/src/mocks.ts +7 -127
- package/src/node.ts +128 -0
- package/src/nodes/base.ts +5 -12
- package/src/nodes/code.ts +165 -74
- package/src/nodes/content.ts +56 -0
- package/src/nodes/file.ts +97 -36
- package/src/nodes/function.ts +216 -156
- package/src/nodes/http.ts +1 -35
- package/src/nodes/index.ts +23 -15
- package/src/nodes/input.ts +140 -0
- package/src/nodes/operation.ts +122 -68
- package/src/nodes/output.ts +23 -0
- package/src/nodes/parameter.ts +33 -3
- package/src/nodes/property.ts +36 -3
- package/src/nodes/requestBody.ts +61 -0
- package/src/nodes/response.ts +58 -13
- package/src/nodes/schema.ts +93 -17
- package/src/printer.ts +48 -42
- package/src/registry.ts +75 -0
- package/src/signature.ts +207 -0
- package/src/types.ts +8 -68
- package/src/utils/codegen.ts +104 -0
- package/src/utils/extractStringsFromNodes.ts +34 -0
- package/src/utils/fileMerge.ts +184 -0
- package/src/utils/index.ts +11 -0
- package/src/utils/operationParams.ts +353 -0
- package/src/utils/refs.ts +138 -0
- package/src/utils/schemaGraph.ts +169 -0
- package/src/utils/schemaMerge.ts +34 -0
- package/src/utils/schemaTraversal.ts +86 -0
- package/src/utils/strings.ts +139 -0
- package/src/visitor.ts +227 -289
- package/dist/chunk--u3MIqq1.js +0 -8
- package/src/nodes/root.ts +0 -64
- package/src/refs.ts +0 -67
- package/src/resolvers.ts +0 -45
- package/src/transformers.ts +0 -159
- package/src/utils.ts +0 -915
package/LICENSE
CHANGED
|
@@ -1,14 +1,21 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
This repository contains software under two licenses:
|
|
1
|
+
MIT License
|
|
4
2
|
|
|
5
|
-
|
|
6
|
-
MIT License — see licenses/LICENSE-MIT for the full license text.
|
|
3
|
+
Copyright (c) 2026 Stijn Van Hulle
|
|
7
4
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
13
14
|
|
|
14
|
-
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
<div align="center">
|
|
2
|
-
<h1>@kubb/ast</h1>
|
|
3
2
|
<a href="https://kubb.dev" target="_blank" rel="noopener noreferrer">
|
|
4
|
-
<img
|
|
3
|
+
<img src="https://kubb.dev/og.png" alt="Kubb banner">
|
|
5
4
|
</a>
|
|
6
5
|
|
|
7
6
|
[![npm version][npm-version-src]][npm-version-href]
|
|
8
7
|
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
9
|
-
[![
|
|
8
|
+
[![Stars][stars-src]][stars-href]
|
|
10
9
|
[![License][license-src]][license-href]
|
|
11
|
-
[![
|
|
10
|
+
[![Node][node-src]][node-href]
|
|
12
11
|
|
|
13
12
|
<h4>
|
|
14
|
-
<a href="https://kubb.dev
|
|
13
|
+
<a href="https://kubb.dev" target="_blank">Documentation</a>
|
|
15
14
|
<span> · </span>
|
|
16
15
|
<a href="https://github.com/kubb-labs/kubb/issues/" target="_blank">Report Bug</a>
|
|
17
16
|
<span> · </span>
|
|
@@ -19,14 +18,23 @@
|
|
|
19
18
|
</h4>
|
|
20
19
|
</div>
|
|
21
20
|
|
|
22
|
-
|
|
21
|
+
<br />
|
|
22
|
+
|
|
23
|
+
# @kubb/ast
|
|
24
|
+
|
|
25
|
+
### Spec-agnostic AST layer for Kubb
|
|
26
|
+
|
|
27
|
+
Defines the node tree, visitor pattern, factory functions, and type guards used across every Kubb code generation plugin.
|
|
23
28
|
|
|
24
29
|
## Imports
|
|
25
30
|
|
|
26
|
-
| Path
|
|
27
|
-
|
|
|
28
|
-
| `@kubb/ast`
|
|
29
|
-
| `@kubb/ast/
|
|
31
|
+
| Path | Contents |
|
|
32
|
+
| ------------------- | ---------------------------------------------------------------------------------------- |
|
|
33
|
+
| `@kubb/ast` | Runtime: node definitions, guards, visitor, macro engine, constants |
|
|
34
|
+
| `@kubb/ast/factory` | Node constructors (`createSchema`, `createFile`, and friends), the `ts.factory` analogue |
|
|
35
|
+
| `@kubb/ast/macros` | Built-in macro presets: `macroDiscriminatorEnum`, `macroSimplifyUnion`, `macroEnumName` |
|
|
36
|
+
| `@kubb/ast/types` | Types only: all node interfaces, type aliases, visitor types |
|
|
37
|
+
| `@kubb/ast/utils` | Spec-agnostic string and identifier helpers, ref helpers |
|
|
30
38
|
|
|
31
39
|
## Node tree
|
|
32
40
|
|
|
@@ -51,10 +59,12 @@ SchemaNode (discriminated union)
|
|
|
51
59
|
|
|
52
60
|
### Factory
|
|
53
61
|
|
|
62
|
+
Constructors live on the `@kubb/ast/factory` subpath, mirroring `ts.factory.createX`. Through `@kubb/core` the same set is reachable as `ast.factory.createSchema(...)`.
|
|
63
|
+
|
|
54
64
|
```ts
|
|
55
|
-
import {
|
|
65
|
+
import { createInput, createSchema, createProperty } from '@kubb/ast/factory'
|
|
56
66
|
|
|
57
|
-
const root =
|
|
67
|
+
const root = createInput({
|
|
58
68
|
schemas: [
|
|
59
69
|
createSchema({
|
|
60
70
|
name: 'Pet',
|
|
@@ -106,11 +116,11 @@ const types = collect<string>(root, {
|
|
|
106
116
|
### Guards
|
|
107
117
|
|
|
108
118
|
```ts
|
|
109
|
-
import {
|
|
119
|
+
import { narrowSchema, schemaDef } from '@kubb/ast'
|
|
110
120
|
import type { Node } from '@kubb/ast/types'
|
|
111
121
|
|
|
112
122
|
function process(node: Node) {
|
|
113
|
-
if (
|
|
123
|
+
if (schemaDef.is(node)) {
|
|
114
124
|
const obj = narrowSchema(node, 'object')
|
|
115
125
|
obj?.properties?.forEach((p) => console.log(p.name))
|
|
116
126
|
}
|
|
@@ -120,17 +130,27 @@ function process(node: Node) {
|
|
|
120
130
|
### Refs
|
|
121
131
|
|
|
122
132
|
```ts
|
|
123
|
-
import {
|
|
133
|
+
import { extractRefName } from '@kubb/ast/utils'
|
|
124
134
|
|
|
125
|
-
|
|
126
|
-
const pet = resolveRef(refMap, 'Pet')
|
|
135
|
+
extractRefName('#/components/schemas/Pet') // 'Pet'
|
|
127
136
|
```
|
|
128
137
|
|
|
138
|
+
## Adding a node
|
|
139
|
+
|
|
140
|
+
Adding a node touches three files. The barrels and visitor tables derive the rest.
|
|
141
|
+
|
|
142
|
+
1. Define the node in its own `src/nodes/*.ts` file. Call `defineNode` and export the resulting `fooDef`, the `createFoo` constructor, and the node's type.
|
|
143
|
+
2. Add `fooDef` to the `nodeDefs` array in `src/registry.ts`.
|
|
144
|
+
3. Re-export `createFoo` from `src/factory.ts`.
|
|
145
|
+
|
|
146
|
+
Everything else follows from there. `@kubb/ast/types` picks up the node type through `export type *`, the `@kubb/ast` barrel picks up `fooDef` through `export * from './registry.ts'`, and the visitor tables (`VISITOR_KEYS`, `VISITOR_KEY_BY_KIND`, `nodeRebuilders`) come from the def's `children`, `visitorKey`, and `rebuild` fields. `registry.test.ts` fails when a def has no matching `factory.create*`, so missing wiring is caught in CI.
|
|
147
|
+
|
|
129
148
|
## Supporting Kubb
|
|
130
149
|
|
|
131
|
-
Kubb
|
|
150
|
+
Kubb is an open source project, and its development is funded entirely by sponsors. If you would like to become a sponsor, please consider:
|
|
132
151
|
|
|
133
152
|
- [Become a Sponsor on GitHub](https://github.com/sponsors/stijnvanhulle)
|
|
153
|
+
- [See sponsorship tiers and our sponsors](https://kubb.dev/sponsors)
|
|
134
154
|
|
|
135
155
|
<p align="center">
|
|
136
156
|
<a href="https://github.com/sponsors/stijnvanhulle">
|
|
@@ -138,15 +158,19 @@ Kubb uses an MIT-licensed open source project with its ongoing development made
|
|
|
138
158
|
</a>
|
|
139
159
|
</p>
|
|
140
160
|
|
|
161
|
+
## License
|
|
162
|
+
|
|
163
|
+
[MIT](https://github.com/kubb-labs/kubb/blob/main/licenses/LICENSE-MIT)
|
|
164
|
+
|
|
141
165
|
<!-- Badges -->
|
|
142
166
|
|
|
143
|
-
[npm-version-src]: https://
|
|
144
|
-
[npm-version-href]: https://
|
|
145
|
-
[npm-downloads-src]: https://
|
|
146
|
-
[npm-downloads-href]: https://
|
|
147
|
-
[
|
|
167
|
+
[npm-version-src]: https://shieldcn.dev/npm/v/@kubb/ast.svg?variant=secondary&size=xs&theme=zinc&mode=dark
|
|
168
|
+
[npm-version-href]: https://npmx.dev/package/@kubb/ast
|
|
169
|
+
[npm-downloads-src]: https://shieldcn.dev/npm/dm/@kubb/ast.svg?variant=secondary&size=xs&theme=zinc&mode=dark
|
|
170
|
+
[npm-downloads-href]: https://npmx.dev/package/@kubb/ast
|
|
171
|
+
[stars-src]: https://shieldcn.dev/github/stars/kubb-labs/kubb.svg?variant=secondary&size=xs&theme=zinc&mode=dark
|
|
172
|
+
[stars-href]: https://github.com/kubb-labs/kubb
|
|
173
|
+
[license-src]: https://shieldcn.dev/npm/license/@kubb/ast.svg?variant=secondary&size=xs&theme=zinc
|
|
148
174
|
[license-href]: https://github.com/kubb-labs/kubb/blob/main/LICENSE
|
|
149
|
-
[
|
|
150
|
-
[
|
|
151
|
-
[sponsors-src]: https://img.shields.io/github/sponsors/stijnvanhulle?style=flat&colorA=18181B&colorB=f58517
|
|
152
|
-
[sponsors-href]: https://github.com/sponsors/stijnvanhulle/
|
|
175
|
+
[node-src]: https://shieldcn.dev/npm/node/@kubb/ast.svg?variant=secondary&size=xs&theme=zinc&mode=dark
|
|
176
|
+
[node-href]: https://npmx.dev/package/@kubb/ast
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
//#region \0rolldown/runtime.js
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __name = (target, value) => __defProp(target, "name", {
|
|
4
|
+
value,
|
|
5
|
+
configurable: true
|
|
6
|
+
});
|
|
7
|
+
var __exportAll = (all, no_symbols) => {
|
|
8
|
+
let target = {};
|
|
9
|
+
for (var name in all) __defProp(target, name, {
|
|
10
|
+
get: all[name],
|
|
11
|
+
enumerable: true
|
|
12
|
+
});
|
|
13
|
+
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
14
|
+
return target;
|
|
15
|
+
};
|
|
16
|
+
//#endregion
|
|
17
|
+
export { __name as n, __exportAll as t };
|
|
@@ -0,0 +1,475 @@
|
|
|
1
|
+
import { n as __name } from "./chunk-CNktS9qV.js";
|
|
2
|
+
import { Et as PropertyNode, _t as SchemaNode, f as OperationNode, h as ResponseNode, n as OutputNode, nt as ContentNode, o as InputNode, t as Node, w as ParameterNode, y as RequestBodyNode } from "./index-BzjwdK2M.js";
|
|
3
|
+
|
|
4
|
+
//#region src/constants.d.ts
|
|
5
|
+
/**
|
|
6
|
+
* Traversal depth for AST visitor utilities.
|
|
7
|
+
*
|
|
8
|
+
* - `'shallow'` visits only the immediate node, skipping children.
|
|
9
|
+
* - `'deep'` recursively visits all descendant nodes.
|
|
10
|
+
*/
|
|
11
|
+
type VisitorDepth = 'shallow' | 'deep';
|
|
12
|
+
/**
|
|
13
|
+
* Schema type discriminators used by all AST schema nodes.
|
|
14
|
+
*
|
|
15
|
+
* Each value is a stable discriminator across the AST (for example `schema.type === schemaTypes.object`).
|
|
16
|
+
* Call `isScalarPrimitive()` to check for the scalar types.
|
|
17
|
+
*/
|
|
18
|
+
declare const schemaTypes: {
|
|
19
|
+
/**
|
|
20
|
+
* Text value.
|
|
21
|
+
*/
|
|
22
|
+
readonly string: "string";
|
|
23
|
+
/**
|
|
24
|
+
* Floating-point number (`float`, `double`).
|
|
25
|
+
*/
|
|
26
|
+
readonly number: "number";
|
|
27
|
+
/**
|
|
28
|
+
* Whole number (`int32`). Use `bigint` for `int64`.
|
|
29
|
+
*/
|
|
30
|
+
readonly integer: "integer";
|
|
31
|
+
/**
|
|
32
|
+
* 64-bit integer (`int64`). Only used when `integerType` is set to `'bigint'`.
|
|
33
|
+
*/
|
|
34
|
+
readonly bigint: "bigint";
|
|
35
|
+
/**
|
|
36
|
+
* Boolean value.
|
|
37
|
+
*/
|
|
38
|
+
readonly boolean: "boolean";
|
|
39
|
+
/**
|
|
40
|
+
* Explicit null value.
|
|
41
|
+
*/
|
|
42
|
+
readonly null: "null";
|
|
43
|
+
/**
|
|
44
|
+
* Any value (no type restriction).
|
|
45
|
+
*/
|
|
46
|
+
readonly any: "any";
|
|
47
|
+
/**
|
|
48
|
+
* Unknown value (must be narrowed before usage).
|
|
49
|
+
*/
|
|
50
|
+
readonly unknown: "unknown";
|
|
51
|
+
/**
|
|
52
|
+
* No return value (`void`).
|
|
53
|
+
*/
|
|
54
|
+
readonly void: "void";
|
|
55
|
+
/**
|
|
56
|
+
* Object with named properties.
|
|
57
|
+
*/
|
|
58
|
+
readonly object: "object";
|
|
59
|
+
/**
|
|
60
|
+
* Sequential list of items.
|
|
61
|
+
*/
|
|
62
|
+
readonly array: "array";
|
|
63
|
+
/**
|
|
64
|
+
* Fixed-length list with position-specific items.
|
|
65
|
+
*/
|
|
66
|
+
readonly tuple: "tuple";
|
|
67
|
+
/**
|
|
68
|
+
* "One of" multiple schema members.
|
|
69
|
+
*/
|
|
70
|
+
readonly union: "union";
|
|
71
|
+
/**
|
|
72
|
+
* "All of" multiple schema members.
|
|
73
|
+
*/
|
|
74
|
+
readonly intersection: "intersection";
|
|
75
|
+
/**
|
|
76
|
+
* Enum schema.
|
|
77
|
+
*/
|
|
78
|
+
readonly enum: "enum";
|
|
79
|
+
/**
|
|
80
|
+
* Reference to another schema.
|
|
81
|
+
*/
|
|
82
|
+
readonly ref: "ref";
|
|
83
|
+
/**
|
|
84
|
+
* Calendar date (for example `2026-03-24`).
|
|
85
|
+
*/
|
|
86
|
+
readonly date: "date";
|
|
87
|
+
/**
|
|
88
|
+
* Date-time value (for example `2026-03-24T09:00:00Z`).
|
|
89
|
+
*/
|
|
90
|
+
readonly datetime: "datetime";
|
|
91
|
+
/**
|
|
92
|
+
* Time-only value (for example `09:00:00`).
|
|
93
|
+
*/
|
|
94
|
+
readonly time: "time";
|
|
95
|
+
/**
|
|
96
|
+
* UUID value.
|
|
97
|
+
*/
|
|
98
|
+
readonly uuid: "uuid";
|
|
99
|
+
/**
|
|
100
|
+
* Email address value.
|
|
101
|
+
*/
|
|
102
|
+
readonly email: "email";
|
|
103
|
+
/**
|
|
104
|
+
* URL value.
|
|
105
|
+
*/
|
|
106
|
+
readonly url: "url";
|
|
107
|
+
/**
|
|
108
|
+
* IPv4 address value.
|
|
109
|
+
*/
|
|
110
|
+
readonly ipv4: "ipv4";
|
|
111
|
+
/**
|
|
112
|
+
* IPv6 address value.
|
|
113
|
+
*/
|
|
114
|
+
readonly ipv6: "ipv6";
|
|
115
|
+
/**
|
|
116
|
+
* Binary/blob value.
|
|
117
|
+
*/
|
|
118
|
+
readonly blob: "blob";
|
|
119
|
+
/**
|
|
120
|
+
* Impossible value (`never`).
|
|
121
|
+
*/
|
|
122
|
+
readonly never: "never";
|
|
123
|
+
};
|
|
124
|
+
/**
|
|
125
|
+
* HTTP method identifiers used by operation nodes.
|
|
126
|
+
*/
|
|
127
|
+
declare const httpMethods: {
|
|
128
|
+
readonly get: "GET";
|
|
129
|
+
readonly post: "POST";
|
|
130
|
+
readonly put: "PUT";
|
|
131
|
+
readonly patch: "PATCH";
|
|
132
|
+
readonly delete: "DELETE";
|
|
133
|
+
readonly head: "HEAD";
|
|
134
|
+
readonly options: "OPTIONS";
|
|
135
|
+
readonly trace: "TRACE";
|
|
136
|
+
};
|
|
137
|
+
//#endregion
|
|
138
|
+
//#region src/visitor.d.ts
|
|
139
|
+
/**
|
|
140
|
+
* Ordered mapping of `[NodeType, ParentType]` pairs.
|
|
141
|
+
*
|
|
142
|
+
* `ParentOf` uses this map to find parent types.
|
|
143
|
+
*/
|
|
144
|
+
type ParentNodeMap = [[InputNode, undefined], [OutputNode, undefined], [OperationNode, InputNode], [RequestBodyNode, OperationNode], [ContentNode, RequestBodyNode | ResponseNode], [SchemaNode, InputNode | ContentNode | SchemaNode | PropertyNode | ParameterNode], [PropertyNode, SchemaNode], [ParameterNode, OperationNode], [ResponseNode, OperationNode]];
|
|
145
|
+
/**
|
|
146
|
+
* Resolves the parent node type for a given AST node type.
|
|
147
|
+
*
|
|
148
|
+
* Visitor context relies on this so `ctx.parent` is typed for each callback.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```ts
|
|
152
|
+
* type InputParent = ParentOf<InputNode>
|
|
153
|
+
* // undefined
|
|
154
|
+
* ```
|
|
155
|
+
*
|
|
156
|
+
* @example
|
|
157
|
+
* ```ts
|
|
158
|
+
* type PropertyParent = ParentOf<PropertyNode>
|
|
159
|
+
* // SchemaNode
|
|
160
|
+
* ```
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* ```ts
|
|
164
|
+
* type SchemaParent = ParentOf<SchemaNode>
|
|
165
|
+
* // InputNode | OperationNode | SchemaNode | PropertyNode | ParameterNode | ResponseNode
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
type ParentOf<T extends Node, TEntries extends ReadonlyArray<[Node, unknown]> = ParentNodeMap> = TEntries extends [infer TEntry extends [Node, unknown], ...infer TRest extends ReadonlyArray<[Node, unknown]>] ? T extends TEntry[0] ? TEntry[1] : ParentOf<T, TRest> : Node;
|
|
169
|
+
/**
|
|
170
|
+
* Traversal context passed as the second argument to every visitor callback.
|
|
171
|
+
* `parent` is typed from the current node type.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* ```ts
|
|
175
|
+
* const visitor: Visitor = {
|
|
176
|
+
* schema(node, { parent }) {
|
|
177
|
+
* // parent type is narrowed by node kind
|
|
178
|
+
* },
|
|
179
|
+
* }
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
type VisitorContext<T extends Node = Node> = {
|
|
183
|
+
/**
|
|
184
|
+
* Parent node of the currently visited node.
|
|
185
|
+
* For `InputNode`, this is `undefined`.
|
|
186
|
+
*/
|
|
187
|
+
parent?: ParentOf<T>;
|
|
188
|
+
};
|
|
189
|
+
/**
|
|
190
|
+
* Synchronous visitor consumed by `transform`. Each optional callback runs
|
|
191
|
+
* for the matching node type. Return a new node to replace it, or `undefined`
|
|
192
|
+
* to leave it untouched.
|
|
193
|
+
*
|
|
194
|
+
* Plugins typically expose `transformer` so users can supply a `Visitor` that
|
|
195
|
+
* rewrites the AST before printing.
|
|
196
|
+
*
|
|
197
|
+
* @example Prefix every operationId
|
|
198
|
+
* ```ts
|
|
199
|
+
* const visitor: Visitor = {
|
|
200
|
+
* operation(node) {
|
|
201
|
+
* return { ...node, operationId: `api_${node.operationId}` }
|
|
202
|
+
* },
|
|
203
|
+
* }
|
|
204
|
+
* ```
|
|
205
|
+
*
|
|
206
|
+
* @example Strip schema descriptions
|
|
207
|
+
* ```ts
|
|
208
|
+
* const visitor: Visitor = {
|
|
209
|
+
* schema(node) {
|
|
210
|
+
* return { ...node, description: undefined }
|
|
211
|
+
* },
|
|
212
|
+
* }
|
|
213
|
+
* ```
|
|
214
|
+
*/
|
|
215
|
+
type Visitor = {
|
|
216
|
+
input?(node: InputNode, context: VisitorContext<InputNode>): undefined | null | InputNode;
|
|
217
|
+
output?(node: OutputNode, context: VisitorContext<OutputNode>): undefined | null | OutputNode;
|
|
218
|
+
operation?(node: OperationNode, context: VisitorContext<OperationNode>): undefined | null | OperationNode;
|
|
219
|
+
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): undefined | null | SchemaNode;
|
|
220
|
+
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): undefined | null | PropertyNode;
|
|
221
|
+
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): undefined | null | ParameterNode;
|
|
222
|
+
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): undefined | null | ResponseNode;
|
|
223
|
+
};
|
|
224
|
+
/**
|
|
225
|
+
* A visitor callback result that may be sync or async.
|
|
226
|
+
*/
|
|
227
|
+
type MaybePromise<T> = T | Promise<T>;
|
|
228
|
+
/**
|
|
229
|
+
* Async visitor for `walk`. Synchronous `Visitor` objects are compatible.
|
|
230
|
+
*
|
|
231
|
+
* @example
|
|
232
|
+
* ```ts
|
|
233
|
+
* const visitor: AsyncVisitor = {
|
|
234
|
+
* async operation(node) {
|
|
235
|
+
* await Promise.resolve(node.operationId)
|
|
236
|
+
* },
|
|
237
|
+
* }
|
|
238
|
+
* ```
|
|
239
|
+
*/
|
|
240
|
+
type AsyncVisitor = {
|
|
241
|
+
input?(node: InputNode, context: VisitorContext<InputNode>): MaybePromise<undefined | null | InputNode>;
|
|
242
|
+
output?(node: OutputNode, context: VisitorContext<OutputNode>): MaybePromise<undefined | null | OutputNode>;
|
|
243
|
+
operation?(node: OperationNode, context: VisitorContext<OperationNode>): MaybePromise<undefined | null | OperationNode>;
|
|
244
|
+
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): MaybePromise<undefined | null | SchemaNode>;
|
|
245
|
+
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): MaybePromise<undefined | null | PropertyNode>;
|
|
246
|
+
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): MaybePromise<undefined | null | ParameterNode>;
|
|
247
|
+
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): MaybePromise<undefined | null | ResponseNode>;
|
|
248
|
+
};
|
|
249
|
+
/**
|
|
250
|
+
* Visitor used by `collect`.
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```ts
|
|
254
|
+
* const visitor: CollectVisitor<string> = {
|
|
255
|
+
* operation(node) {
|
|
256
|
+
* return node.operationId
|
|
257
|
+
* },
|
|
258
|
+
* }
|
|
259
|
+
* ```
|
|
260
|
+
*/
|
|
261
|
+
type CollectVisitor<T> = {
|
|
262
|
+
input?(node: InputNode, context: VisitorContext<InputNode>): T | null | undefined;
|
|
263
|
+
output?(node: OutputNode, context: VisitorContext<OutputNode>): T | null | undefined;
|
|
264
|
+
operation?(node: OperationNode, context: VisitorContext<OperationNode>): T | null | undefined;
|
|
265
|
+
schema?(node: SchemaNode, context: VisitorContext<SchemaNode>): T | null | undefined;
|
|
266
|
+
property?(node: PropertyNode, context: VisitorContext<PropertyNode>): T | null | undefined;
|
|
267
|
+
parameter?(node: ParameterNode, context: VisitorContext<ParameterNode>): T | null | undefined;
|
|
268
|
+
response?(node: ResponseNode, context: VisitorContext<ResponseNode>): T | null | undefined;
|
|
269
|
+
};
|
|
270
|
+
/**
|
|
271
|
+
* Options for `transform`.
|
|
272
|
+
*
|
|
273
|
+
* @example
|
|
274
|
+
* ```ts
|
|
275
|
+
* const options: TransformOptions = { depth: 'deep', schema: (node) => node }
|
|
276
|
+
* ```
|
|
277
|
+
*
|
|
278
|
+
* @example
|
|
279
|
+
* ```ts
|
|
280
|
+
* // Only transform the current node, not nested children
|
|
281
|
+
* const options: TransformOptions = { depth: 'shallow', schema: (node) => node }
|
|
282
|
+
* ```
|
|
283
|
+
*/
|
|
284
|
+
type TransformOptions = Visitor & {
|
|
285
|
+
/**
|
|
286
|
+
* Traversal depth.
|
|
287
|
+
* @default 'deep'
|
|
288
|
+
*/
|
|
289
|
+
depth?: VisitorDepth;
|
|
290
|
+
/**
|
|
291
|
+
* Internal parent override used during recursion.
|
|
292
|
+
*/
|
|
293
|
+
parent?: Node;
|
|
294
|
+
};
|
|
295
|
+
/**
|
|
296
|
+
* Options for `walk`.
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* ```ts
|
|
300
|
+
* const options: WalkOptions = { depth: 'deep', concurrency: 10, root: () => {} }
|
|
301
|
+
* ```
|
|
302
|
+
*/
|
|
303
|
+
type WalkOptions = AsyncVisitor & {
|
|
304
|
+
/**
|
|
305
|
+
* Traversal depth.
|
|
306
|
+
* @default 'deep'
|
|
307
|
+
*/
|
|
308
|
+
depth?: VisitorDepth;
|
|
309
|
+
/**
|
|
310
|
+
* Maximum number of sibling nodes visited concurrently.
|
|
311
|
+
* @default 30
|
|
312
|
+
*/
|
|
313
|
+
concurrency?: number;
|
|
314
|
+
};
|
|
315
|
+
/**
|
|
316
|
+
* Options for `collect`.
|
|
317
|
+
*
|
|
318
|
+
* @example
|
|
319
|
+
* ```ts
|
|
320
|
+
* const options: CollectOptions<string> = { depth: 'shallow', schema: () => undefined }
|
|
321
|
+
* ```
|
|
322
|
+
*/
|
|
323
|
+
type CollectOptions<T> = CollectVisitor<T> & {
|
|
324
|
+
/**
|
|
325
|
+
* Traversal depth.
|
|
326
|
+
* @default 'deep'
|
|
327
|
+
*/
|
|
328
|
+
depth?: VisitorDepth;
|
|
329
|
+
/**
|
|
330
|
+
* Internal parent override used during recursion.
|
|
331
|
+
*/
|
|
332
|
+
parent?: Node;
|
|
333
|
+
};
|
|
334
|
+
/**
|
|
335
|
+
* Async depth-first traversal for side effects. Visitor return values are
|
|
336
|
+
* ignored. Use `transform` when you want to rewrite nodes.
|
|
337
|
+
*
|
|
338
|
+
* Sibling nodes at each depth run concurrently up to `options.concurrency`
|
|
339
|
+
* (defaults to `WALK_CONCURRENCY`). Higher values overlap I/O-bound visitor
|
|
340
|
+
* work. Lower values reduce memory pressure.
|
|
341
|
+
*
|
|
342
|
+
* @example Log every operation
|
|
343
|
+
* ```ts
|
|
344
|
+
* await walk(root, {
|
|
345
|
+
* operation(node) {
|
|
346
|
+
* console.log(node.operationId)
|
|
347
|
+
* },
|
|
348
|
+
* })
|
|
349
|
+
* ```
|
|
350
|
+
*
|
|
351
|
+
* @example Only visit the root node
|
|
352
|
+
* ```ts
|
|
353
|
+
* await walk(root, { depth: 'shallow', input: () => {} })
|
|
354
|
+
* ```
|
|
355
|
+
*/
|
|
356
|
+
declare function walk(node: Node, options: WalkOptions): Promise<void>;
|
|
357
|
+
/**
|
|
358
|
+
* Synchronous depth-first transform. Each visitor callback can return a
|
|
359
|
+
* replacement node. Returning `undefined` keeps the original.
|
|
360
|
+
*
|
|
361
|
+
* The original tree is never mutated, a new tree is returned. Pass
|
|
362
|
+
* `depth: 'shallow'` to skip recursion into children.
|
|
363
|
+
*
|
|
364
|
+
* @example Prefix every operationId
|
|
365
|
+
* ```ts
|
|
366
|
+
* const next = transform(root, {
|
|
367
|
+
* operation(node) {
|
|
368
|
+
* return { ...node, operationId: `prefixed_${node.operationId}` }
|
|
369
|
+
* },
|
|
370
|
+
* })
|
|
371
|
+
* ```
|
|
372
|
+
*
|
|
373
|
+
* @example Replace only the root node
|
|
374
|
+
* ```ts
|
|
375
|
+
* const next = transform(root, {
|
|
376
|
+
* depth: 'shallow',
|
|
377
|
+
* input: (node) => ({ ...node, meta: { ...node.meta, title: 'Rewritten' } }),
|
|
378
|
+
* })
|
|
379
|
+
* ```
|
|
380
|
+
*/
|
|
381
|
+
declare function transform(node: InputNode, options: TransformOptions): InputNode;
|
|
382
|
+
declare function transform(node: OutputNode, options: TransformOptions): OutputNode;
|
|
383
|
+
declare function transform(node: OperationNode, options: TransformOptions): OperationNode;
|
|
384
|
+
declare function transform(node: SchemaNode, options: TransformOptions): SchemaNode;
|
|
385
|
+
declare function transform(node: PropertyNode, options: TransformOptions): PropertyNode;
|
|
386
|
+
declare function transform(node: ParameterNode, options: TransformOptions): ParameterNode;
|
|
387
|
+
declare function transform(node: ResponseNode, options: TransformOptions): ResponseNode;
|
|
388
|
+
declare function transform(node: Node, options: TransformOptions): Node;
|
|
389
|
+
/**
|
|
390
|
+
* Eager depth-first collection pass. Gathers every non-null value the visitor
|
|
391
|
+
* callbacks return into an array.
|
|
392
|
+
*
|
|
393
|
+
* @example Collect every operationId
|
|
394
|
+
* ```ts
|
|
395
|
+
* const ids = collect<string>(root, {
|
|
396
|
+
* operation(node) {
|
|
397
|
+
* return node.operationId
|
|
398
|
+
* },
|
|
399
|
+
* })
|
|
400
|
+
* ```
|
|
401
|
+
*/
|
|
402
|
+
declare function collect<T>(node: Node, options: CollectOptions<T>): Array<T>;
|
|
403
|
+
//#endregion
|
|
404
|
+
//#region src/defineMacro.d.ts
|
|
405
|
+
/**
|
|
406
|
+
* A named, composable transform over the Kubb AST. It carries the same per-kind callbacks as a
|
|
407
|
+
* {@link Visitor} (`schema`, `operation`, …), plus a `name`, an optional `enforce` order, and an
|
|
408
|
+
* optional `when` gate. Macros run on the shared AST, so the same macro works across every adapter
|
|
409
|
+
* and output target. Exports follow the `macro<Name>` convention, mirroring plugins (`pluginTs`).
|
|
410
|
+
*/
|
|
411
|
+
type Macro = Visitor & {
|
|
412
|
+
/**
|
|
413
|
+
* Macro identifier, surfaced in diagnostics. Follows the `macro<Name>` convention.
|
|
414
|
+
*/
|
|
415
|
+
name: string;
|
|
416
|
+
/**
|
|
417
|
+
* Ordering hint. `pre` macros run before unmarked macros, `post` macros run after.
|
|
418
|
+
* Ordering within a bucket follows list order.
|
|
419
|
+
*/
|
|
420
|
+
enforce?: 'pre' | 'post';
|
|
421
|
+
/**
|
|
422
|
+
* Gate checked against the current node before any callback runs. When it returns `false`
|
|
423
|
+
* the macro is skipped for that node.
|
|
424
|
+
*/
|
|
425
|
+
when?: (node: Node) => boolean;
|
|
426
|
+
};
|
|
427
|
+
/**
|
|
428
|
+
* Types a macro for inference and a single construction site, mirroring `definePlugin`.
|
|
429
|
+
* Adds no runtime behavior.
|
|
430
|
+
*
|
|
431
|
+
* @example
|
|
432
|
+
* ```ts
|
|
433
|
+
* const macroUntagged = defineMacro({
|
|
434
|
+
* name: 'untagged',
|
|
435
|
+
* operation(node) {
|
|
436
|
+
* return node.tags?.length ? undefined : { ...node, tags: ['untagged'] }
|
|
437
|
+
* },
|
|
438
|
+
* })
|
|
439
|
+
* ```
|
|
440
|
+
*/
|
|
441
|
+
declare function defineMacro(macro: Macro): Macro;
|
|
442
|
+
/**
|
|
443
|
+
* Folds an ordered list of macros into a single {@link Visitor} that `transform` (and the per-plugin
|
|
444
|
+
* transform layer in `@kubb/core`) can run. Macros are stable-sorted by `enforce`, then applied
|
|
445
|
+
* sequentially per node so later macros see earlier output. This differs from a plain visitor, which
|
|
446
|
+
* has no names, ordering, or composition.
|
|
447
|
+
*
|
|
448
|
+
* @example
|
|
449
|
+
* ```ts
|
|
450
|
+
* const visitor = composeMacros([macroSimplifyUnion, macroDiscriminatorEnum])
|
|
451
|
+
* const next = transform(root, visitor)
|
|
452
|
+
* ```
|
|
453
|
+
*/
|
|
454
|
+
declare function composeMacros(macros: ReadonlyArray<Macro>): Visitor;
|
|
455
|
+
/**
|
|
456
|
+
* Runs a list of macros over a node tree and returns the rewritten tree. Keeps `transform`'s
|
|
457
|
+
* structural sharing, so an empty or no-op macro list returns the same reference. Pass
|
|
458
|
+
* `depth: 'shallow'` to rewrite the root node only.
|
|
459
|
+
*
|
|
460
|
+
* @example
|
|
461
|
+
* ```ts
|
|
462
|
+
* const next = applyMacros(root, [macroIntegerToString])
|
|
463
|
+
* ```
|
|
464
|
+
*
|
|
465
|
+
* @example Apply to the root node only
|
|
466
|
+
* ```ts
|
|
467
|
+
* const named = applyMacros(node, [macroEnumName({ parentName, propName, enumSuffix })], { depth: 'shallow' })
|
|
468
|
+
* ```
|
|
469
|
+
*/
|
|
470
|
+
declare function applyMacros<TNode extends Node>(root: TNode, macros: ReadonlyArray<Macro>, options?: {
|
|
471
|
+
depth?: VisitorDepth;
|
|
472
|
+
}): TNode;
|
|
473
|
+
//#endregion
|
|
474
|
+
export { ParentOf as a, collect as c, httpMethods as d, schemaTypes as f, defineMacro as i, transform as l, applyMacros as n, Visitor as o, composeMacros as r, VisitorContext as s, Macro as t, walk as u };
|
|
475
|
+
//# sourceMappingURL=defineMacro-BLIR6k-j.d.ts.map
|