@kubb/ast 5.0.0-beta.31 → 5.0.0-beta.32

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubb/ast",
3
- "version": "5.0.0-beta.31",
3
+ "version": "5.0.0-beta.32",
4
4
  "description": "Spec-agnostic AST layer for Kubb. Defines the node tree, visitor pattern, factory functions, and type guards used across all code generation plugins.",
5
5
  "keywords": [
6
6
  "ast",
package/src/dedupe.ts CHANGED
@@ -101,12 +101,11 @@ export function applyDedupe(node: OperationNode, canonicalBySignature: ReadonlyM
101
101
  export function applyDedupe(node: Node, canonicalBySignature: ReadonlyMap<string, DedupeCanonical>, skipRootMatch = false): Node {
102
102
  if (canonicalBySignature.size === 0) return node
103
103
 
104
- const signatures = new Map<SchemaNode, string>()
105
104
  const root = node
106
105
 
107
106
  return transform(node, {
108
107
  schema(schemaNode) {
109
- const signature = signatureOf(schemaNode, signatures)
108
+ const signature = signatureOf(schemaNode)
110
109
  if (skipRootMatch && schemaNode === root) return undefined
111
110
 
112
111
  const canonical = canonicalBySignature.get(signature)
@@ -145,7 +144,6 @@ function cleanDefinition(node: SchemaNode, name: string): SchemaNode {
145
144
  export function buildDedupePlan(roots: ReadonlyArray<Node>, options: BuildDedupePlanOptions): DedupePlan {
146
145
  const { isCandidate, nameFor, refFor, minOccurrences = 2 } = options
147
146
 
148
- const signatures = new Map<SchemaNode, string>()
149
147
  const topLevelNodes = new Set<SchemaNode>()
150
148
 
151
149
  type Group = {
@@ -156,7 +154,7 @@ export function buildDedupePlan(roots: ReadonlyArray<Node>, options: BuildDedupe
156
154
  const groups = new Map<string, Group>()
157
155
 
158
156
  function record(schemaNode: SchemaNode): void {
159
- const signature = signatureOf(schemaNode, signatures)
157
+ const signature = signatureOf(schemaNode)
160
158
  if (!isCandidate(schemaNode)) return
161
159
 
162
160
  const isTopLevel = topLevelNodes.has(schemaNode) && !!schemaNode.name
package/src/guards.ts CHANGED
@@ -1,19 +1,4 @@
1
- import type {
2
- FunctionParameterNode,
3
- FunctionParametersNode,
4
- HttpOperationNode,
5
- InputNode,
6
- Node,
7
- NodeKind,
8
- OperationNode,
9
- OutputNode,
10
- ParameterGroupNode,
11
- ParameterNode,
12
- PropertyNode,
13
- ResponseNode,
14
- SchemaNode,
15
- SchemaNodeByType,
16
- } from './nodes/index.ts'
1
+ import type { HttpOperationNode, InputNode, Node, NodeKind, OperationNode, OutputNode, SchemaNode, SchemaNodeByType } from './nodes/index.ts'
17
2
 
18
3
  /**
19
4
  * Narrows a `SchemaNode` to the variant that matches `type`.
@@ -93,33 +78,3 @@ export function isHttpOperationNode(node: OperationNode): node is HttpOperationN
93
78
  * ```
94
79
  */
95
80
  export const isSchemaNode = isKind<SchemaNode>('Schema')
96
-
97
- /**
98
- * Returns `true` when the input is a `PropertyNode`.
99
- */
100
- export const isPropertyNode = isKind<PropertyNode>('Property')
101
-
102
- /**
103
- * Returns `true` when the input is a `ParameterNode`.
104
- */
105
- export const isParameterNode = isKind<ParameterNode>('Parameter')
106
-
107
- /**
108
- * Returns `true` when the input is a `ResponseNode`.
109
- */
110
- export const isResponseNode = isKind<ResponseNode>('Response')
111
-
112
- /**
113
- * Returns `true` when the input is a `FunctionParameterNode`.
114
- */
115
- export const isFunctionParameterNode = isKind<FunctionParameterNode>('FunctionParameter')
116
-
117
- /**
118
- * Returns `true` when the input is a `ParameterGroupNode`.
119
- */
120
- export const isParameterGroupNode = isKind<ParameterGroupNode>('ParameterGroup')
121
-
122
- /**
123
- * Returns `true` when the input is a `FunctionParametersNode`.
124
- */
125
- export const isFunctionParametersNode = isKind<FunctionParametersNode>('FunctionParameters')
package/src/refs.ts CHANGED
@@ -1,4 +1,3 @@
1
- import type { InputNode } from './nodes/root.ts'
2
1
  import type { SchemaNode } from './nodes/schema.ts'
3
2
 
4
3
  /**
@@ -19,51 +18,3 @@ export type RefMap = Map<string, SchemaNode>
19
18
  export function extractRefName(ref: string): string {
20
19
  return ref.split('/').at(-1) ?? ref
21
20
  }
22
-
23
- /**
24
- * Builds a `RefMap` from `input.schemas` using each schema's `name`.
25
- *
26
- * Unnamed schemas are skipped.
27
- *
28
- * @example
29
- * ```ts
30
- * const refMap = buildRefMap(input)
31
- * const pet = refMap.get('Pet')
32
- * ```
33
- */
34
- export function buildRefMap(input: InputNode): RefMap {
35
- const map: RefMap = new Map()
36
-
37
- for (const schema of input.schemas) {
38
- if (schema.name) {
39
- map.set(schema.name, schema)
40
- }
41
- }
42
- return map
43
- }
44
-
45
- /**
46
- * Resolves a schema by name from a `RefMap`.
47
- *
48
- * Returns `null` when the ref is not found.
49
- *
50
- * @example
51
- * ```ts
52
- * const petSchema = resolveRef(refMap, 'Pet')
53
- * ```
54
- */
55
- export function resolveRef(refMap: RefMap, ref: string): SchemaNode | null {
56
- return refMap.get(ref) ?? null
57
- }
58
-
59
- /**
60
- * Converts a `RefMap` into a plain object.
61
- *
62
- * @example
63
- * ```ts
64
- * const refsObject = refMapToObject(refMap)
65
- * ```
66
- */
67
- export function refMapToObject(refMap: RefMap): Record<string, SchemaNode> {
68
- return Object.fromEntries(refMap)
69
- }
package/src/signature.ts CHANGED
@@ -16,90 +16,187 @@ function refTargetName(node: Extract<SchemaNode, { type: 'ref' }>): string {
16
16
  return node.name ?? ''
17
17
  }
18
18
 
19
+ type ScalarField = { kind: 'scalar'; key: string; prefix: string }
20
+ type BoolField = { kind: 'bool'; key: string; prefix: string }
21
+ type ChildField = { kind: 'child'; key: string; prefix: string }
22
+ type ChildrenField = { kind: 'children'; key: string; prefix: string }
23
+ type ObjectPropsField = { kind: 'objectProps' }
24
+ type AdditionalPropsField = { kind: 'additionalProps' }
25
+ type PatternPropsField = { kind: 'patternProps' }
26
+ type EnumValuesField = { kind: 'enumValues' }
27
+ type RefTargetField = { kind: 'refTarget' }
28
+
29
+ type ShapeField =
30
+ | ScalarField
31
+ | BoolField
32
+ | ChildField
33
+ | ChildrenField
34
+ | ObjectPropsField
35
+ | AdditionalPropsField
36
+ | PatternPropsField
37
+ | EnumValuesField
38
+ | RefTargetField
39
+
40
+ const arrayTupleFields: ReadonlyArray<ShapeField> = [
41
+ { kind: 'children', key: 'items', prefix: 'i' },
42
+ { kind: 'child', key: 'rest', prefix: 'r' },
43
+ { kind: 'scalar', key: 'min', prefix: 'mn' },
44
+ { kind: 'scalar', key: 'max', prefix: 'mx' },
45
+ { kind: 'bool', key: 'unique', prefix: 'u' },
46
+ ]
47
+
48
+ const numericFields: ReadonlyArray<ShapeField> = [
49
+ { kind: 'scalar', key: 'min', prefix: 'mn' },
50
+ { kind: 'scalar', key: 'max', prefix: 'mx' },
51
+ { kind: 'scalar', key: 'exclusiveMinimum', prefix: 'emn' },
52
+ { kind: 'scalar', key: 'exclusiveMaximum', prefix: 'emx' },
53
+ { kind: 'scalar', key: 'multipleOf', prefix: 'mo' },
54
+ ]
55
+
56
+ const rangeFields: ReadonlyArray<ShapeField> = [
57
+ { kind: 'scalar', key: 'min', prefix: 'mn' },
58
+ { kind: 'scalar', key: 'max', prefix: 'mx' },
59
+ ]
60
+
19
61
  /**
20
- * Builds the local, shape-only descriptor for a node: its kind, flags, constraints, and its
21
- * children's signatures. {@link signatureOf} hashes this string; children contribute their
22
- * fixed-length signature rather than their own full descriptor, which keeps the result bounded.
62
+ * Maps each schema node `type` to the ordered list of shape-contributing fields.
63
+ * Node types absent from this map (scalar types like boolean, null, any, etc.) fall
64
+ * back to `${type}|${flags}` with no additional fields.
23
65
  */
24
- function describeShape(node: SchemaNode, signatures: Map<SchemaNode, string>): string {
25
- const flags = flagsDescriptor(node)
66
+ const SHAPE_KEYS: Partial<Record<SchemaNode['type'], ReadonlyArray<ShapeField>>> = {
67
+ object: [
68
+ { kind: 'objectProps' },
69
+ { kind: 'additionalProps' },
70
+ { kind: 'patternProps' },
71
+ { kind: 'scalar', key: 'minProperties', prefix: 'mn' },
72
+ { kind: 'scalar', key: 'maxProperties', prefix: 'mx' },
73
+ ],
74
+ array: arrayTupleFields,
75
+ tuple: arrayTupleFields,
76
+ union: [
77
+ { kind: 'scalar', key: 'strategy', prefix: 's' },
78
+ { kind: 'scalar', key: 'discriminatorPropertyName', prefix: 'd' },
79
+ { kind: 'children', key: 'members', prefix: 'm' },
80
+ ],
81
+ intersection: [{ kind: 'children', key: 'members', prefix: 'm' }],
82
+ enum: [{ kind: 'enumValues' }],
83
+ ref: [{ kind: 'refTarget' }],
84
+ string: [
85
+ { kind: 'scalar', key: 'min', prefix: 'mn' },
86
+ { kind: 'scalar', key: 'max', prefix: 'mx' },
87
+ { kind: 'scalar', key: 'pattern', prefix: 'pt' },
88
+ ],
89
+ number: numericFields,
90
+ integer: numericFields,
91
+ bigint: numericFields,
92
+ url: [
93
+ { kind: 'scalar', key: 'path', prefix: 'path' },
94
+ { kind: 'scalar', key: 'min', prefix: 'mn' },
95
+ { kind: 'scalar', key: 'max', prefix: 'mx' },
96
+ ],
97
+ uuid: rangeFields,
98
+ email: rangeFields,
99
+ datetime: [
100
+ { kind: 'bool', key: 'offset', prefix: 'o' },
101
+ { kind: 'bool', key: 'local', prefix: 'l' },
102
+ ],
103
+ date: [{ kind: 'scalar', key: 'representation', prefix: 'rep' }],
104
+ time: [{ kind: 'scalar', key: 'representation', prefix: 'rep' }],
105
+ }
26
106
 
27
- switch (node.type) {
28
- case 'object': {
29
- const props = (node.properties ?? []).map((prop) => `${prop.name}${prop.required ? '!' : '?'}${signatureOf(prop.schema, signatures)}`).join(',')
30
- let additional = ''
31
- if (typeof node.additionalProperties === 'boolean') {
32
- additional = `ab:${node.additionalProperties}`
33
- } else if (node.additionalProperties) {
34
- additional = `as:${signatureOf(node.additionalProperties, signatures)}`
35
- }
36
- const pattern = node.patternProperties
37
- ? Object.keys(node.patternProperties)
38
- .sort()
39
- .map((key) => `${key}=${signatureOf(node.patternProperties![key]!, signatures)}`)
40
- .join(',')
41
- : ''
42
- return `object|${flags}|p[${props}]|${additional}|pp[${pattern}]|mn:${node.minProperties ?? ''}|mx:${node.maxProperties ?? ''}`
107
+ function serializeShapeField(field: ShapeField, node: SchemaNode, record: Record<string, unknown>): string {
108
+ switch (field.kind) {
109
+ case 'scalar':
110
+ return `${field.prefix}:${record[field.key] ?? ''}`
111
+ case 'bool':
112
+ return `${field.prefix}:${record[field.key] ? 1 : 0}`
113
+ case 'child': {
114
+ const child = record[field.key] as SchemaNode | undefined
115
+ return `${field.prefix}:${child ? signatureOf(child) : ''}`
43
116
  }
44
- case 'array':
45
- case 'tuple': {
46
- const items = (node.items ?? []).map((item) => signatureOf(item, signatures)).join(',')
47
- const rest = node.rest ? signatureOf(node.rest, signatures) : ''
48
- return `${node.type}|${flags}|i[${items}]|r:${rest}|mn:${node.min ?? ''}|mx:${node.max ?? ''}|u:${node.unique ? 1 : 0}`
117
+ case 'children': {
118
+ const children = (record[field.key] as Array<SchemaNode> | undefined) ?? []
119
+ return `${field.prefix}[${children.map((c) => signatureOf(c)).join(',')}]`
49
120
  }
50
- case 'union': {
51
- const members = (node.members ?? []).map((member) => signatureOf(member, signatures)).join(',')
52
- return `union|${flags}|s:${node.strategy ?? ''}|d:${node.discriminatorPropertyName ?? ''}|m[${members}]`
121
+ case 'objectProps': {
122
+ const obj = node as Extract<SchemaNode, { type: 'object' }>
123
+ const props = (obj.properties ?? []).map((prop) => `${prop.name}${prop.required ? '!' : '?'}${signatureOf(prop.schema)}`).join(',')
124
+ return `p[${props}]`
53
125
  }
54
- case 'intersection': {
55
- const members = (node.members ?? []).map((member) => signatureOf(member, signatures)).join(',')
56
- return `intersection|${flags}|m[${members}]`
126
+ case 'additionalProps': {
127
+ const obj = node as Extract<SchemaNode, { type: 'object' }>
128
+ if (typeof obj.additionalProperties === 'boolean') return `ab:${obj.additionalProperties}`
129
+ if (obj.additionalProperties) return `as:${signatureOf(obj.additionalProperties)}`
130
+ return ''
57
131
  }
58
- case 'enum': {
132
+ case 'patternProps': {
133
+ const obj = node as Extract<SchemaNode, { type: 'object' }>
134
+ const pattern = obj.patternProperties
135
+ ? Object.keys(obj.patternProperties)
136
+ .sort()
137
+ .map((key) => `${key}=${signatureOf(obj.patternProperties![key]!)}`)
138
+ .join(',')
139
+ : ''
140
+ return `pp[${pattern}]`
141
+ }
142
+ case 'enumValues': {
143
+ const en = node as Extract<SchemaNode, { type: 'enum' }>
59
144
  let values = ''
60
- if (node.namedEnumValues?.length) {
61
- values = node.namedEnumValues.map((entry) => `${entry.name}=${entry.primitive}:${String(entry.value)}`).join(',')
62
- } else if (node.enumValues?.length) {
63
- values = node.enumValues.map((value) => `${value === null ? 'null' : typeof value}:${String(value)}`).join(',')
145
+ if (en.namedEnumValues?.length) {
146
+ values = en.namedEnumValues.map((entry) => `${entry.name}=${entry.primitive}:${String(entry.value)}`).join(',')
147
+ } else if (en.enumValues?.length) {
148
+ values = en.enumValues.map((value) => `${value === null ? 'null' : typeof value}:${String(value)}`).join(',')
64
149
  }
65
- return `enum|${flags}|v[${values}]`
150
+ return `v[${values}]`
151
+ }
152
+ case 'refTarget': {
153
+ return `->${refTargetName(node as Extract<SchemaNode, { type: 'ref' }>)}`
66
154
  }
67
- case 'ref':
68
- return `ref|${flags}|->${refTargetName(node)}`
69
- case 'string':
70
- return `string|${flags}|mn:${node.min ?? ''}|mx:${node.max ?? ''}|pt:${node.pattern ?? ''}`
71
- case 'number':
72
- case 'integer':
73
- case 'bigint':
74
- return `${node.type}|${flags}|mn:${node.min ?? ''}|mx:${node.max ?? ''}|emn:${node.exclusiveMinimum ?? ''}|emx:${node.exclusiveMaximum ?? ''}|mo:${node.multipleOf ?? ''}`
75
- case 'url':
76
- return `url|${flags}|path:${node.path ?? ''}|mn:${node.min ?? ''}|mx:${node.max ?? ''}`
77
- case 'uuid':
78
- case 'email':
79
- return `${node.type}|${flags}|mn:${node.min ?? ''}|mx:${node.max ?? ''}`
80
- case 'datetime':
81
- return `datetime|${flags}|o:${node.offset ? 1 : 0}|l:${node.local ? 1 : 0}`
82
- case 'date':
83
- case 'time':
84
- return `${node.type}|${flags}|rep:${node.representation}`
85
- default:
86
- return `${node.type}|${flags}`
87
155
  }
88
156
  }
89
157
 
158
+ /**
159
+ * Builds the local, shape-only descriptor for a node: its kind, flags, constraints, and its
160
+ * children's signatures. {@link signatureOf} hashes this string; children contribute their
161
+ * fixed-length signature rather than their own full descriptor, which keeps the result bounded.
162
+ */
163
+ function describeShape(node: SchemaNode): string {
164
+ const flags = flagsDescriptor(node)
165
+ const fields = SHAPE_KEYS[node.type]
166
+ if (!fields) return `${node.type}|${flags}`
167
+
168
+ const record = node as unknown as Record<string, unknown>
169
+ const parts: Array<string> = [`${node.type}|${flags}`]
170
+ for (const field of fields) {
171
+ parts.push(serializeShapeField(field, node, record))
172
+ }
173
+ return parts.join('|')
174
+ }
175
+
176
+ /**
177
+ * Persistent hash-consing cache: `SchemaNode` → signature digest, keyed by node identity.
178
+ *
179
+ * A `WeakMap` so entries are released once the node is garbage-collected, and so a node hashed
180
+ * during dedupe planning is not re-hashed when the same tree is rewritten during streaming
181
+ * (where `schemaSignature` and `applyDedupe` would otherwise each walk it from scratch). Reuse
182
+ * across calls is sound because a signature depends only on a node's content, and schema nodes
183
+ * are immutable once created — transforms allocate new objects rather than mutating in place.
184
+ */
185
+ const signatureCache = new WeakMap<SchemaNode, string>()
186
+
90
187
  /**
91
188
  * Hash-consing: each node's signature is a fixed-length digest of its local shape plus its
92
189
  * children's digests (a Merkle hash). Children contribute their 64-char hash instead of their
93
190
  * full nested descriptor, so a signature stays bounded regardless of subtree depth, and the
94
191
  * digest is identical across calls because it depends only on content — never on traversal
95
192
  * order. This keeps the keys built during planning consistent with the ones recomputed later
96
- * during streaming. `signatures` memoizes node → digest within a single computation.
193
+ * during streaming. {@link signatureCache} memoizes node → digest across every computation.
97
194
  */
98
- export function signatureOf(node: SchemaNode, signatures: Map<SchemaNode, string>): string {
99
- const cached = signatures.get(node)
195
+ export function signatureOf(node: SchemaNode): string {
196
+ const cached = signatureCache.get(node)
100
197
  if (cached !== undefined) return cached
101
- const signature = createHash('sha256').update(describeShape(node, signatures)).digest('hex')
102
- signatures.set(node, signature)
198
+ const signature = createHash('sha256').update(describeShape(node)).digest('hex')
199
+ signatureCache.set(node, signature)
103
200
  return signature
104
201
  }
105
202
 
@@ -119,7 +216,7 @@ export function signatureOf(node: SchemaNode, signatures: Map<SchemaNode, string
119
216
  * ```
120
217
  */
121
218
  export function schemaSignature(node: SchemaNode): string {
122
- return signatureOf(node, new Map())
219
+ return signatureOf(node)
123
220
  }
124
221
 
125
222
  /**
File without changes