@kubb/ast 5.0.0-beta.61 → 5.0.0-beta.63

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.
Files changed (95) hide show
  1. package/dist/{defineMacro-BLIR6k-j.d.ts → defineMacro-BATi7xoC.d.ts} +11 -20
  2. package/dist/{defineMacro-Bv9R_9a2.cjs → defineMacro-CEZHaCXE.cjs} +13 -21
  3. package/dist/defineMacro-CEZHaCXE.cjs.map +1 -0
  4. package/dist/{defineMacro-BTXvS8nI.js → defineMacro-Shz8f6SG.js} +12 -19
  5. package/dist/defineMacro-Shz8f6SG.js.map +1 -0
  6. package/dist/factory.cjs +88 -28
  7. package/dist/factory.cjs.map +1 -0
  8. package/dist/factory.d.ts +6 -41
  9. package/dist/factory.js +58 -3
  10. package/dist/factory.js.map +1 -0
  11. package/dist/{index-BzjwdK2M.d.ts → index-B9cc8MBS.d.ts} +164 -194
  12. package/dist/index.cjs +144 -274
  13. package/dist/index.cjs.map +1 -1
  14. package/dist/index.d.ts +43 -37
  15. package/dist/index.js +109 -235
  16. package/dist/index.js.map +1 -1
  17. package/dist/macros.cjs +41 -28
  18. package/dist/macros.cjs.map +1 -1
  19. package/dist/macros.d.ts +14 -12
  20. package/dist/macros.js +34 -21
  21. package/dist/macros.js.map +1 -1
  22. package/dist/{operationParams-BZ07xDm0.d.ts → operationParams-k5CKwSWZ.d.ts} +9 -2
  23. package/dist/{visitor-DJ6ZEJvq.js → refs-BjNDuCBD.js} +116 -159
  24. package/dist/refs-BjNDuCBD.js.map +1 -0
  25. package/dist/{visitor-DpKZ9Tk0.cjs → refs-u5SDdyV7.cjs} +140 -195
  26. package/dist/refs-u5SDdyV7.cjs.map +1 -0
  27. package/dist/{response-hnSw2NKE.cjs → schema-BkvrrOAr.cjs} +496 -231
  28. package/dist/schema-BkvrrOAr.cjs.map +1 -0
  29. package/dist/{response-KUdWiDWw.js → schema-Cbnxmz4b.js} +472 -220
  30. package/dist/schema-Cbnxmz4b.js.map +1 -0
  31. package/dist/{types-DyDzizSf.d.ts → types-BB_xgRJ3.d.ts} +49 -123
  32. package/dist/types.d.ts +5 -6
  33. package/dist/{utils-BLJwyza-.cjs → utils-CEepwqmb.cjs} +76 -69
  34. package/dist/utils-CEepwqmb.cjs.map +1 -0
  35. package/dist/{utils-CF_-Pn_c.js → utils-DaXkewb1.js} +58 -51
  36. package/dist/utils-DaXkewb1.js.map +1 -0
  37. package/dist/utils.cjs +11 -11
  38. package/dist/utils.d.ts +53 -30
  39. package/dist/utils.js +3 -3
  40. package/package.json +1 -1
  41. package/src/constants.ts +3 -36
  42. package/src/defineMacro.ts +20 -13
  43. package/src/{node.ts → defineNode.ts} +12 -38
  44. package/src/{printer.ts → definePrinter.ts} +5 -3
  45. package/src/dialect.ts +52 -19
  46. package/src/factory.ts +7 -94
  47. package/src/index.ts +7 -9
  48. package/src/infer.ts +1 -1
  49. package/src/macros/macroDiscriminatorEnum.ts +7 -1
  50. package/src/macros/macroEnumName.ts +10 -2
  51. package/src/macros/macroSimplifyUnion.ts +29 -19
  52. package/src/nodes/code.ts +40 -80
  53. package/src/nodes/content.ts +2 -7
  54. package/src/nodes/file.ts +107 -18
  55. package/src/nodes/function.ts +46 -46
  56. package/src/nodes/index.ts +2 -3
  57. package/src/nodes/input.ts +13 -9
  58. package/src/nodes/operation.ts +8 -5
  59. package/src/nodes/output.ts +1 -1
  60. package/src/nodes/parameter.ts +5 -5
  61. package/src/nodes/property.ts +5 -5
  62. package/src/nodes/requestBody.ts +6 -13
  63. package/src/nodes/response.ts +93 -8
  64. package/src/nodes/schema.ts +5 -5
  65. package/src/optionality.ts +15 -0
  66. package/src/registry.ts +1 -1
  67. package/src/types.ts +4 -6
  68. package/src/utils/codegen.ts +6 -7
  69. package/src/utils/extractStringsFromNodes.ts +1 -0
  70. package/src/utils/fileMerge.ts +7 -8
  71. package/src/utils/operationParams.ts +9 -9
  72. package/src/utils/refs.ts +14 -18
  73. package/src/utils/schemaGraph.ts +41 -33
  74. package/src/utils/schemaMerge.ts +2 -2
  75. package/src/visitor.ts +3 -14
  76. package/dist/defineMacro-BTXvS8nI.js.map +0 -1
  77. package/dist/defineMacro-Bv9R_9a2.cjs.map +0 -1
  78. package/dist/extractStringsFromNodes-Cja-xxx5.js +0 -29
  79. package/dist/extractStringsFromNodes-Cja-xxx5.js.map +0 -1
  80. package/dist/extractStringsFromNodes-DKgDjFO0.cjs +0 -34
  81. package/dist/extractStringsFromNodes-DKgDjFO0.cjs.map +0 -1
  82. package/dist/extractStringsFromNodes-p4mX1TQD.d.ts +0 -14
  83. package/dist/factory-CZNOGI-N.js +0 -283
  84. package/dist/factory-CZNOGI-N.js.map +0 -1
  85. package/dist/factory-DG1CVkEb.cjs +0 -300
  86. package/dist/factory-DG1CVkEb.cjs.map +0 -1
  87. package/dist/response-KUdWiDWw.js.map +0 -1
  88. package/dist/response-hnSw2NKE.cjs.map +0 -1
  89. package/dist/utils-BLJwyza-.cjs.map +0 -1
  90. package/dist/utils-CF_-Pn_c.js.map +0 -1
  91. package/dist/visitor-DJ6ZEJvq.js.map +0 -1
  92. package/dist/visitor-DpKZ9Tk0.cjs.map +0 -1
  93. package/src/dedupe.ts +0 -239
  94. package/src/mocks.ts +0 -56
  95. package/src/nodes/http.ts +0 -85
package/src/dedupe.ts DELETED
@@ -1,239 +0,0 @@
1
- /**
2
- * Schema-shape deduplication. `buildDedupePlan` finds top-level and inline schemas that share a
3
- * structural signature, picks one canonical definition, and `applyDedupe` repoints every duplicate
4
- * at it. This works on `SchemaNode` content, not on files.
5
- *
6
- * For merging a file's imports, exports, and source nodes, see `utils/fileMerge.ts`. Same idea of
7
- * collapsing duplicates, but a different domain.
8
- */
9
- import type { Node, OperationNode, SchemaNode } from './nodes/index.ts'
10
- import { createSchema } from './nodes/schema.ts'
11
- import { signatureOf } from './signature.ts'
12
- import { extractRefName } from './utils/index.ts'
13
- import { collectLazy, transform } from './visitor.ts'
14
-
15
- /**
16
- * A canonical destination for a deduplicated shape: the shared schema name and
17
- * the synthetic `$ref` path that points at it.
18
- */
19
- export type DedupeCanonical = {
20
- /**
21
- * Canonical schema name every duplicate occurrence refers to.
22
- */
23
- name: string
24
- /**
25
- * `$ref` path stored on the generated `ref` nodes (for example `#/components/schemas/Status`).
26
- */
27
- ref: string
28
- }
29
-
30
- /**
31
- * The result of {@link buildDedupePlan}: a lookup from structural signature to its
32
- * canonical target, plus the freshly hoisted definitions that must be added to
33
- * the schema list.
34
- */
35
- export type DedupePlan = {
36
- /**
37
- * Maps a structural signature to the canonical schema that represents it.
38
- */
39
- canonicalBySignature: Map<string, DedupeCanonical>
40
- /**
41
- * Maps the name of a top-level schema that duplicates a canonical one to that canonical, so
42
- * references to the duplicate can be repointed at the first schema with the same content.
43
- */
44
- aliasNames: Map<string, DedupeCanonical>
45
- /**
46
- * New top-level schema definitions created for inline shapes that had no existing
47
- * named component. Nested duplicates inside each definition are already collapsed.
48
- */
49
- hoisted: Array<SchemaNode>
50
- }
51
-
52
- /**
53
- * The lookups {@link applyDedupe} needs from a {@link DedupePlan}.
54
- */
55
- export type DedupeLookups = Pick<DedupePlan, 'canonicalBySignature' | 'aliasNames'>
56
-
57
- /**
58
- * Options that inject the naming and candidate policy into {@link buildDedupePlan}.
59
- * The mechanics (grouping, counting, rewriting) live here. The policy lives in the caller.
60
- */
61
- export type BuildDedupePlanOptions = {
62
- /**
63
- * Returns `true` when a node should be deduplicated. This is the only gate, so it must
64
- * reject both ineligible kinds (return `false` for anything other than, say, enums and
65
- * objects) and unsafe shapes (e.g. nodes that reference a circular schema).
66
- */
67
- isCandidate: (node: SchemaNode) => boolean
68
- /**
69
- * Produces the canonical name for an inline shape with no existing named component.
70
- * Return `null` to leave the shape inline (for example when no contextual name exists).
71
- */
72
- nameFor: (node: SchemaNode, signature: string) => string | null
73
- /**
74
- * Builds the `$ref` path for a canonical name.
75
- */
76
- refFor: (name: string) => string
77
- /**
78
- * Minimum number of occurrences before a shape is deduplicated.
79
- *
80
- * @default 2
81
- */
82
- minOccurrences?: number
83
- }
84
-
85
- /**
86
- * Builds the shared `ref` replacement for a duplicate occurrence, carrying the
87
- * usage-slot and documentation fields that are not part of the canonical type.
88
- */
89
- function createRefNode(node: SchemaNode, canonical: DedupeCanonical): SchemaNode {
90
- return createSchema({
91
- type: 'ref',
92
- name: canonical.name,
93
- ref: canonical.ref,
94
- optional: node.optional,
95
- nullish: node.nullish,
96
- readOnly: node.readOnly,
97
- writeOnly: node.writeOnly,
98
- deprecated: node.deprecated,
99
- description: node.description,
100
- default: node.default,
101
- example: node.example,
102
- })
103
- }
104
-
105
- /**
106
- * Rewrites a node, replacing every candidate sub-schema whose signature has a canonical
107
- * target with a `ref` to that target. Replacing a node with a `ref` prunes its subtree,
108
- * so nested duplicates inside a replaced shape are not visited again. A `ref` that points
109
- * at a duplicate top-level schema (see `aliasNames`) is repointed at the first schema with
110
- * the same content.
111
- *
112
- * Pass `skipRootMatch` when rewriting a canonical definition so its own root is not
113
- * turned into a reference to itself. Nested duplicates are still collapsed.
114
- *
115
- * @example
116
- * ```ts
117
- * const next = applyDedupe(operationNode, plan)
118
- * ```
119
- */
120
- export function applyDedupe(node: SchemaNode, plan: DedupeLookups, skipRootMatch?: boolean): SchemaNode
121
- export function applyDedupe(node: OperationNode, plan: DedupeLookups, skipRootMatch?: boolean): OperationNode
122
- export function applyDedupe(node: Node, plan: DedupeLookups, skipRootMatch = false): Node {
123
- const { canonicalBySignature, aliasNames } = plan
124
- if (canonicalBySignature.size === 0 && aliasNames.size === 0) return node
125
-
126
- const root = node
127
-
128
- return transform(node, {
129
- schema(schemaNode) {
130
- if (schemaNode.type === 'ref') {
131
- const target = schemaNode.ref ? extractRefName(schemaNode.ref) : schemaNode.name
132
- const canonical = target ? aliasNames.get(target) : undefined
133
-
134
- return canonical ? { ...schemaNode, name: canonical.name, ref: canonical.ref } : undefined
135
- }
136
-
137
- const signature = signatureOf(schemaNode)
138
- if (skipRootMatch && schemaNode === root) return undefined
139
-
140
- const canonical = canonicalBySignature.get(signature)
141
- if (!canonical) return undefined
142
-
143
- return createRefNode(schemaNode, canonical)
144
- },
145
- })
146
- }
147
-
148
- /**
149
- * Strips usage-slot flags from a hoisted definition and applies its canonical name.
150
- * A standalone definition is never optional, so `optional`/`nullish` are cleared.
151
- */
152
- function cleanDefinition(node: SchemaNode, name: string): SchemaNode {
153
- return { ...node, name, optional: undefined, nullish: undefined }
154
- }
155
-
156
- /**
157
- * Scans a forest of schema and operation nodes and produces a {@link DedupePlan}.
158
- *
159
- * A shape that occurs at least `minOccurrences` times is deduplicated: if any occurrence
160
- * is a named top-level schema, the first one becomes the canonical (so other top-level
161
- * duplicates and inline copies turn into references to it). Every other top-level name with
162
- * the same content is recorded in `aliasNames`, so refs to it can be repointed at the
163
- * canonical. Otherwise a new definition is hoisted using `nameFor`. The plan is then applied
164
- * per node with {@link applyDedupe}.
165
- *
166
- * @example
167
- * ```ts
168
- * const plan = buildDedupePlan([...schemaNodes, ...operationNodes], {
169
- * isCandidate: (node) => node.type === 'enum' || node.type === 'object',
170
- * nameFor: (node) => node.name ?? null,
171
- * refFor: (name) => `#/components/schemas/${name}`,
172
- * })
173
- * ```
174
- */
175
- export function buildDedupePlan(roots: ReadonlyArray<Node>, options: BuildDedupePlanOptions): DedupePlan {
176
- const { isCandidate, nameFor, refFor, minOccurrences = 2 } = options
177
-
178
- const topLevelNodes = new Set<SchemaNode>()
179
-
180
- type Group = {
181
- count: number
182
- representative: SchemaNode
183
- topLevelNames: Array<string>
184
- }
185
- const groups = new Map<string, Group>()
186
-
187
- function record(schemaNode: SchemaNode): void {
188
- const signature = signatureOf(schemaNode)
189
- if (!isCandidate(schemaNode)) return
190
-
191
- const isTopLevel = topLevelNodes.has(schemaNode) && !!schemaNode.name
192
- const group = groups.get(signature)
193
- if (group) {
194
- group.count++
195
- if (isTopLevel) group.topLevelNames.push(schemaNode.name!)
196
- } else {
197
- groups.set(signature, { count: 1, representative: schemaNode, topLevelNames: isTopLevel ? [schemaNode.name!] : [] })
198
- }
199
- }
200
-
201
- for (const root of roots) {
202
- if (root.kind === 'Schema') topLevelNodes.add(root)
203
- for (const schemaNode of collectLazy<SchemaNode>(root, { schema: (node) => node })) {
204
- record(schemaNode)
205
- }
206
- }
207
-
208
- const canonicalBySignature = new Map<string, DedupeCanonical>()
209
- const aliasNames = new Map<string, DedupeCanonical>()
210
- const pendingHoists: Array<{ name: string; representative: SchemaNode }> = []
211
-
212
- for (const [signature, group] of groups) {
213
- if (group.count < minOccurrences) continue
214
-
215
- const [firstName, ...duplicateNames] = group.topLevelNames
216
- if (firstName) {
217
- const canonical: DedupeCanonical = { name: firstName, ref: refFor(firstName) }
218
- canonicalBySignature.set(signature, canonical)
219
- for (const duplicate of duplicateNames) {
220
- aliasNames.set(duplicate, canonical)
221
- }
222
- continue
223
- }
224
-
225
- const name = nameFor(group.representative, signature)
226
- if (!name) continue
227
-
228
- canonicalBySignature.set(signature, { name, ref: refFor(name) })
229
- pendingHoists.push({ name, representative: group.representative })
230
- }
231
-
232
- // Build hoisted definitions only after every canonical name is known, so nested
233
- // duplicates inside a definition also resolve to refs.
234
- const hoisted = pendingHoists.map(({ name, representative }) =>
235
- cleanDefinition(applyDedupe(representative, { canonicalBySignature, aliasNames }, true), name),
236
- )
237
-
238
- return { canonicalBySignature, aliasNames, hoisted }
239
- }
package/src/mocks.ts DELETED
@@ -1,56 +0,0 @@
1
- import { createInput } from './nodes/input.ts'
2
- import type { InputNode } from './nodes/input.ts'
3
- import { createOperation } from './nodes/operation.ts'
4
- import { createParameter } from './nodes/parameter.ts'
5
- import { createProperty } from './nodes/property.ts'
6
- import { createResponse } from './nodes/response.ts'
7
- import { createSchema } from './nodes/schema.ts'
8
-
9
- /**
10
- * Builds a minimal sample AST with one `Pet` schema and one `getPetById` operation.
11
- */
12
- export function buildSampleTree(): InputNode {
13
- const petSchema = createSchema({
14
- type: 'object',
15
- name: 'Pet',
16
- properties: [
17
- createProperty({
18
- name: 'id',
19
- schema: createSchema({ type: 'integer' }),
20
- required: true,
21
- }),
22
- createProperty({
23
- name: 'name',
24
- schema: createSchema({ type: 'string' }),
25
- required: true,
26
- }),
27
- ],
28
- })
29
-
30
- const operation = createOperation({
31
- operationId: 'getPetById',
32
- method: 'GET',
33
- path: '/pets/{petId}',
34
- tags: ['pets'],
35
- parameters: [
36
- createParameter({
37
- name: 'petId',
38
- in: 'path',
39
- schema: createSchema({ type: 'integer' }),
40
- required: true,
41
- }),
42
- ],
43
- responses: [
44
- createResponse({
45
- statusCode: '200',
46
- schema: createSchema({ type: 'ref', name: 'Pet' }),
47
- }),
48
- createResponse({
49
- statusCode: '404',
50
- schema: createSchema({ type: 'ref', name: 'Error' }),
51
- }),
52
- ],
53
- })
54
-
55
- return createInput({ schemas: [petSchema], operations: [operation] })
56
- }
package/src/nodes/http.ts DELETED
@@ -1,85 +0,0 @@
1
- /**
2
- * All supported HTTP status code literals as strings, as used in API specs
3
- * (for example, `"200"` and `"404"`).
4
- */
5
- type HttpStatusCode =
6
- // 1xx Informational
7
- | '100'
8
- | '101'
9
- | '102'
10
- | '103'
11
- // 2xx Success
12
- | '200'
13
- | '201'
14
- | '202'
15
- | '203'
16
- | '204'
17
- | '205'
18
- | '206'
19
- | '207'
20
- | '208'
21
- | '226'
22
- // 3xx Redirection
23
- | '300'
24
- | '301'
25
- | '302'
26
- | '303'
27
- | '304'
28
- | '305'
29
- | '307'
30
- | '308'
31
- // 4xx Client Error
32
- | '400'
33
- | '401'
34
- | '402'
35
- | '403'
36
- | '404'
37
- | '405'
38
- | '406'
39
- | '407'
40
- | '408'
41
- | '409'
42
- | '410'
43
- | '411'
44
- | '412'
45
- | '413'
46
- | '414'
47
- | '415'
48
- | '416'
49
- | '417'
50
- | '418'
51
- | '421'
52
- | '422'
53
- | '423'
54
- | '424'
55
- | '425'
56
- | '426'
57
- | '428'
58
- | '429'
59
- | '431'
60
- | '451'
61
- // 5xx Server Error
62
- | '500'
63
- | '501'
64
- | '502'
65
- | '503'
66
- | '504'
67
- | '505'
68
- | '506'
69
- | '507'
70
- | '508'
71
- | '510'
72
- | '511'
73
-
74
- /**
75
- * Response status code literal used by operations.
76
- *
77
- * Includes specific HTTP status code strings and `"default"` for catch-all responses.
78
- *
79
- * @example
80
- * ```ts
81
- * const status: StatusCode = '200'
82
- * const fallback: StatusCode = 'default'
83
- * ```
84
- */
85
- export type StatusCode = HttpStatusCode | 'default'