@kubb/ast 5.0.0-beta.1 → 5.0.0-beta.100
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 +56 -36
- package/dist/index.cjs +1071 -1665
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +136 -3335
- package/dist/index.js +1022 -1620
- package/dist/index.js.map +1 -1
- package/dist/rolldown-runtime-CNktS9qV.js +17 -0
- package/dist/types-DK2c0yY4.d.ts +2764 -0
- package/dist/types.cjs +0 -0
- package/dist/types.d.ts +2 -0
- package/dist/types.js +1 -0
- package/package.json +9 -9
- package/dist/chunk--u3MIqq1.js +0 -8
- package/src/constants.ts +0 -228
- package/src/factory.ts +0 -742
- package/src/guards.ts +0 -110
- package/src/index.ts +0 -45
- package/src/infer.ts +0 -130
- package/src/mocks.ts +0 -176
- package/src/nodes/base.ts +0 -56
- package/src/nodes/code.ts +0 -304
- package/src/nodes/file.ts +0 -230
- package/src/nodes/function.ts +0 -223
- package/src/nodes/http.ts +0 -119
- package/src/nodes/index.ts +0 -86
- package/src/nodes/operation.ts +0 -111
- package/src/nodes/output.ts +0 -26
- package/src/nodes/parameter.ts +0 -41
- package/src/nodes/property.ts +0 -34
- package/src/nodes/response.ts +0 -43
- package/src/nodes/root.ts +0 -64
- package/src/nodes/schema.ts +0 -656
- package/src/printer.ts +0 -250
- package/src/refs.ts +0 -67
- package/src/resolvers.ts +0 -45
- package/src/transformers.ts +0 -159
- package/src/types.ts +0 -70
- package/src/utils.ts +0 -833
- package/src/visitor.ts +0 -592
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,24 @@
|
|
|
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
|
+
| `ast.factory` (via `@kubb/ast`) | Node constructors (`createSchema`, `createFile`, and friends), the `ts.factory` analogue |
|
|
35
|
+
| `@kubb/ast/types` | Types only: all node interfaces, type aliases, visitor types |
|
|
36
|
+
| `kubb/kit` | Re-exports the `ast` and `factory` namespaces, the way most Kubb code reaches the AST without a direct dependency |
|
|
37
|
+
|
|
38
|
+
`@kubb/ast` is an internal library. Inside the Kubb ecosystem the whole surface travels on the `ast` namespace from `kubb/kit`, so plugins and generators reach it there instead of depending on this package directly. The examples below import from `@kubb/ast` for clarity; through `kubb/kit` the same calls read as `ast.walk`, `ast.factory.createSchema`, and so on.
|
|
30
39
|
|
|
31
40
|
## Node tree
|
|
32
41
|
|
|
@@ -51,10 +60,14 @@ SchemaNode (discriminated union)
|
|
|
51
60
|
|
|
52
61
|
### Factory
|
|
53
62
|
|
|
63
|
+
Constructors are available as `ast.factory` from `@kubb/ast`, mirroring `ts.factory.createX`.
|
|
64
|
+
|
|
54
65
|
```ts
|
|
55
|
-
import {
|
|
66
|
+
import { ast } from '@kubb/ast'
|
|
67
|
+
|
|
68
|
+
const { createInput, createSchema, createProperty } = ast.factory
|
|
56
69
|
|
|
57
|
-
const root =
|
|
70
|
+
const root = createInput({
|
|
58
71
|
schemas: [
|
|
59
72
|
createSchema({
|
|
60
73
|
name: 'Pet',
|
|
@@ -79,14 +92,7 @@ const root = createRoot({
|
|
|
79
92
|
### Visitor
|
|
80
93
|
|
|
81
94
|
```ts
|
|
82
|
-
import {
|
|
83
|
-
|
|
84
|
-
// Side effects
|
|
85
|
-
await walk(root, {
|
|
86
|
-
schema(node) {
|
|
87
|
-
console.log(node.type)
|
|
88
|
-
},
|
|
89
|
-
})
|
|
95
|
+
import { collectSync, transform } from '@kubb/ast'
|
|
90
96
|
|
|
91
97
|
// Immutable transformation
|
|
92
98
|
const updated = transform(root, {
|
|
@@ -96,7 +102,7 @@ const updated = transform(root, {
|
|
|
96
102
|
})
|
|
97
103
|
|
|
98
104
|
// Extraction
|
|
99
|
-
const types =
|
|
105
|
+
const types = collectSync<string>(root, {
|
|
100
106
|
schema(node) {
|
|
101
107
|
return node.type
|
|
102
108
|
},
|
|
@@ -106,11 +112,11 @@ const types = collect<string>(root, {
|
|
|
106
112
|
### Guards
|
|
107
113
|
|
|
108
114
|
```ts
|
|
109
|
-
import {
|
|
115
|
+
import { narrowSchema, schemaDef } from '@kubb/ast'
|
|
110
116
|
import type { Node } from '@kubb/ast/types'
|
|
111
117
|
|
|
112
118
|
function process(node: Node) {
|
|
113
|
-
if (
|
|
119
|
+
if (schemaDef.is(node)) {
|
|
114
120
|
const obj = narrowSchema(node, 'object')
|
|
115
121
|
obj?.properties?.forEach((p) => console.log(p.name))
|
|
116
122
|
}
|
|
@@ -120,17 +126,27 @@ function process(node: Node) {
|
|
|
120
126
|
### Refs
|
|
121
127
|
|
|
122
128
|
```ts
|
|
123
|
-
import {
|
|
129
|
+
import { resolveRefName } from '@kubb/ast'
|
|
124
130
|
|
|
125
|
-
|
|
126
|
-
const pet = resolveRef(refMap, 'Pet')
|
|
131
|
+
resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' }) // 'Pet'
|
|
127
132
|
```
|
|
128
133
|
|
|
134
|
+
## Adding a node
|
|
135
|
+
|
|
136
|
+
Adding a node touches three files. The barrels and visitor tables derive the rest.
|
|
137
|
+
|
|
138
|
+
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.
|
|
139
|
+
2. Add `fooDef` to the `nodeDefs` array in `src/registry.ts`.
|
|
140
|
+
3. Re-export `createFoo` from `src/factory.ts`.
|
|
141
|
+
|
|
142
|
+
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.
|
|
143
|
+
|
|
129
144
|
## Supporting Kubb
|
|
130
145
|
|
|
131
|
-
Kubb
|
|
146
|
+
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
147
|
|
|
133
148
|
- [Become a Sponsor on GitHub](https://github.com/sponsors/stijnvanhulle)
|
|
149
|
+
- [See sponsorship tiers and our sponsors](https://kubb.dev/sponsors)
|
|
134
150
|
|
|
135
151
|
<p align="center">
|
|
136
152
|
<a href="https://github.com/sponsors/stijnvanhulle">
|
|
@@ -138,15 +154,19 @@ Kubb uses an MIT-licensed open source project with its ongoing development made
|
|
|
138
154
|
</a>
|
|
139
155
|
</p>
|
|
140
156
|
|
|
157
|
+
## License
|
|
158
|
+
|
|
159
|
+
[MIT](https://github.com/kubb-labs/kubb/blob/main/licenses/LICENSE-MIT)
|
|
160
|
+
|
|
141
161
|
<!-- Badges -->
|
|
142
162
|
|
|
143
|
-
[npm-version-src]: https://
|
|
144
|
-
[npm-version-href]: https://
|
|
145
|
-
[npm-downloads-src]: https://
|
|
146
|
-
[npm-downloads-href]: https://
|
|
147
|
-
[
|
|
163
|
+
[npm-version-src]: https://shieldcn.dev/npm/v/@kubb/ast.svg?variant=secondary&size=xs&theme=zinc&mode=dark
|
|
164
|
+
[npm-version-href]: https://npmx.dev/package/@kubb/ast
|
|
165
|
+
[npm-downloads-src]: https://shieldcn.dev/npm/dm/@kubb/ast.svg?variant=secondary&size=xs&theme=zinc&mode=dark
|
|
166
|
+
[npm-downloads-href]: https://npmx.dev/package/@kubb/ast
|
|
167
|
+
[stars-src]: https://shieldcn.dev/github/stars/kubb-labs/kubb.svg?variant=secondary&size=xs&theme=zinc&mode=dark
|
|
168
|
+
[stars-href]: https://github.com/kubb-labs/kubb
|
|
169
|
+
[license-src]: https://shieldcn.dev/npm/license/@kubb/ast.svg?variant=secondary&size=xs&theme=zinc
|
|
148
170
|
[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/
|
|
171
|
+
[node-src]: https://shieldcn.dev/npm/node/@kubb/ast.svg?variant=secondary&size=xs&theme=zinc&mode=dark
|
|
172
|
+
[node-href]: https://npmx.dev/package/@kubb/ast
|