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

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.30",
3
+ "version": "5.0.0-beta.31",
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 ADDED
@@ -0,0 +1,202 @@
1
+ import { createSchema } from './factory.ts'
2
+ import type { Node, OperationNode, SchemaNode } from './nodes/index.ts'
3
+ import { signatureOf } from './signature.ts'
4
+ import { collectLazy, transform } from './visitor.ts'
5
+
6
+ /**
7
+ * A canonical destination for a deduplicated shape: the shared schema name and
8
+ * the synthetic `$ref` path that points at it.
9
+ */
10
+ export type DedupeCanonical = {
11
+ /**
12
+ * Canonical schema name every duplicate occurrence refers to.
13
+ */
14
+ name: string
15
+ /**
16
+ * `$ref` path stored on the generated `ref` nodes (for example `#/components/schemas/Status`).
17
+ */
18
+ ref: string
19
+ }
20
+
21
+ /**
22
+ * The result of {@link buildDedupePlan}: a lookup from structural signature to its
23
+ * canonical target, plus the freshly hoisted definitions that must be added to
24
+ * the schema list.
25
+ */
26
+ export type DedupePlan = {
27
+ /**
28
+ * Maps a structural signature to the canonical schema that represents it.
29
+ */
30
+ canonicalBySignature: Map<string, DedupeCanonical>
31
+ /**
32
+ * New top-level schema definitions created for inline shapes that had no existing
33
+ * named component. Nested duplicates inside each definition are already collapsed.
34
+ */
35
+ hoisted: Array<SchemaNode>
36
+ }
37
+
38
+ /**
39
+ * Options that inject the naming and candidate policy into {@link buildDedupePlan}.
40
+ * The mechanics (grouping, counting, rewriting) live here; the policy lives in the caller.
41
+ */
42
+ export type BuildDedupePlanOptions = {
43
+ /**
44
+ * Returns `true` when a node should be deduplicated. This is the only gate, so it must
45
+ * reject both ineligible kinds (return `false` for anything other than, say, enums and
46
+ * objects) and unsafe shapes (e.g. nodes that reference a circular schema).
47
+ */
48
+ isCandidate: (node: SchemaNode) => boolean
49
+ /**
50
+ * Produces the canonical name for an inline shape with no existing named component.
51
+ * Return `null` to leave the shape inline (for example when no contextual name exists).
52
+ */
53
+ nameFor: (node: SchemaNode, signature: string) => string | null
54
+ /**
55
+ * Builds the `$ref` path for a canonical name.
56
+ */
57
+ refFor: (name: string) => string
58
+ /**
59
+ * Minimum number of occurrences before a shape is deduplicated.
60
+ *
61
+ * @default 2
62
+ */
63
+ minOccurrences?: number
64
+ }
65
+
66
+ /**
67
+ * Builds the shared `ref` replacement for a duplicate occurrence, carrying the
68
+ * usage-slot and documentation fields that are not part of the canonical type.
69
+ */
70
+ function createRefNode(node: SchemaNode, canonical: DedupeCanonical): SchemaNode {
71
+ return createSchema({
72
+ type: 'ref',
73
+ name: canonical.name,
74
+ ref: canonical.ref,
75
+ optional: node.optional,
76
+ nullish: node.nullish,
77
+ readOnly: node.readOnly,
78
+ writeOnly: node.writeOnly,
79
+ deprecated: node.deprecated,
80
+ description: node.description,
81
+ default: node.default,
82
+ example: node.example,
83
+ })
84
+ }
85
+
86
+ /**
87
+ * Rewrites a node, replacing every candidate sub-schema whose signature has a canonical
88
+ * target with a `ref` to that target. Replacing a node with a `ref` prunes its subtree,
89
+ * so nested duplicates inside a replaced shape are not visited again.
90
+ *
91
+ * Pass `skipRootMatch` when rewriting a canonical definition so its own root is not
92
+ * turned into a reference to itself; nested duplicates are still collapsed.
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * const next = applyDedupe(operationNode, plan.canonicalBySignature)
97
+ * ```
98
+ */
99
+ export function applyDedupe(node: SchemaNode, canonicalBySignature: ReadonlyMap<string, DedupeCanonical>, skipRootMatch?: boolean): SchemaNode
100
+ export function applyDedupe(node: OperationNode, canonicalBySignature: ReadonlyMap<string, DedupeCanonical>, skipRootMatch?: boolean): OperationNode
101
+ export function applyDedupe(node: Node, canonicalBySignature: ReadonlyMap<string, DedupeCanonical>, skipRootMatch = false): Node {
102
+ if (canonicalBySignature.size === 0) return node
103
+
104
+ const signatures = new Map<SchemaNode, string>()
105
+ const root = node
106
+
107
+ return transform(node, {
108
+ schema(schemaNode) {
109
+ const signature = signatureOf(schemaNode, signatures)
110
+ if (skipRootMatch && schemaNode === root) return undefined
111
+
112
+ const canonical = canonicalBySignature.get(signature)
113
+ if (!canonical) return undefined
114
+
115
+ return createRefNode(schemaNode, canonical)
116
+ },
117
+ })
118
+ }
119
+
120
+ /**
121
+ * Strips usage-slot flags from a hoisted definition and applies its canonical name.
122
+ * A standalone definition is never optional, so `optional`/`nullish` are cleared.
123
+ */
124
+ function cleanDefinition(node: SchemaNode, name: string): SchemaNode {
125
+ return { ...node, name, optional: undefined, nullish: undefined }
126
+ }
127
+
128
+ /**
129
+ * Scans a forest of schema and operation nodes and produces a {@link DedupePlan}.
130
+ *
131
+ * A shape that occurs at least `minOccurrences` times is deduplicated: if any occurrence
132
+ * is a named top-level schema, that name becomes the canonical (so other top-level duplicates
133
+ * and inline copies turn into references to it); otherwise a new definition is hoisted using
134
+ * `nameFor`. The plan is then applied per node with {@link applyDedupe}.
135
+ *
136
+ * @example
137
+ * ```ts
138
+ * const plan = buildDedupePlan([...schemaNodes, ...operationNodes], {
139
+ * isCandidate: (node) => node.type === 'enum' || node.type === 'object',
140
+ * nameFor: (node) => node.name ?? null,
141
+ * refFor: (name) => `#/components/schemas/${name}`,
142
+ * })
143
+ * ```
144
+ */
145
+ export function buildDedupePlan(roots: ReadonlyArray<Node>, options: BuildDedupePlanOptions): DedupePlan {
146
+ const { isCandidate, nameFor, refFor, minOccurrences = 2 } = options
147
+
148
+ const signatures = new Map<SchemaNode, string>()
149
+ const topLevelNodes = new Set<SchemaNode>()
150
+
151
+ type Group = {
152
+ count: number
153
+ representative: SchemaNode
154
+ topLevelName?: string
155
+ }
156
+ const groups = new Map<string, Group>()
157
+
158
+ function record(schemaNode: SchemaNode): void {
159
+ const signature = signatureOf(schemaNode, signatures)
160
+ if (!isCandidate(schemaNode)) return
161
+
162
+ const isTopLevel = topLevelNodes.has(schemaNode) && !!schemaNode.name
163
+ const group = groups.get(signature)
164
+ if (group) {
165
+ group.count++
166
+ if (isTopLevel && !group.topLevelName) group.topLevelName = schemaNode.name!
167
+ } else {
168
+ groups.set(signature, { count: 1, representative: schemaNode, topLevelName: isTopLevel ? schemaNode.name! : undefined })
169
+ }
170
+ }
171
+
172
+ for (const root of roots) {
173
+ if (root.kind === 'Schema') topLevelNodes.add(root)
174
+ for (const schemaNode of collectLazy<SchemaNode>(root, { schema: (node) => node })) {
175
+ record(schemaNode)
176
+ }
177
+ }
178
+
179
+ const canonicalBySignature = new Map<string, DedupeCanonical>()
180
+ const pendingHoists: Array<{ name: string; representative: SchemaNode }> = []
181
+
182
+ for (const [signature, group] of groups) {
183
+ if (group.count < minOccurrences) continue
184
+
185
+ if (group.topLevelName) {
186
+ canonicalBySignature.set(signature, { name: group.topLevelName, ref: refFor(group.topLevelName) })
187
+ continue
188
+ }
189
+
190
+ const name = nameFor(group.representative, signature)
191
+ if (!name) continue
192
+
193
+ canonicalBySignature.set(signature, { name, ref: refFor(name) })
194
+ pendingHoists.push({ name, representative: group.representative })
195
+ }
196
+
197
+ // Build hoisted definitions only after every canonical name is known, so nested
198
+ // duplicates inside a definition also resolve to refs.
199
+ const hoisted = pendingHoists.map(({ name, representative }) => cleanDefinition(applyDedupe(representative, canonicalBySignature, true), name))
200
+
201
+ return { canonicalBySignature, hoisted }
202
+ }
package/src/index.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export { httpMethods, isScalarPrimitive, mediaTypes, nodeKinds, schemaTypes } from './constants.ts'
2
+ export { applyDedupe, buildDedupePlan } from './dedupe.ts'
2
3
  export { defineSchemaDialect } from './dialect.ts'
3
4
  export { dispatch } from './dispatch.ts'
4
5
  export {
@@ -34,6 +35,7 @@ export { isHttpOperationNode, isInputNode, isOperationNode, isOutputNode, isSche
34
35
  export { createPrinterFactory, definePrinter } from './printer.ts'
35
36
  export { extractRefName } from './refs.ts'
36
37
  export { childName, collectImports, enumPropName, findDiscriminator } from './resolvers.ts'
38
+ export { isSchemaEqual, schemaSignature } from './signature.ts'
37
39
  export { mergeAdjacentObjects, mergeAdjacentObjectsLazy, setDiscriminatorEnum, setEnumName, simplifyUnion } from './transformers.ts'
38
40
  export type * from './types.ts'
39
41
  export {
@@ -0,0 +1,135 @@
1
+ import { createHash } from 'node:crypto'
2
+ import type { SchemaNode } from './nodes/index.ts'
3
+ import { extractRefName } from './refs.ts'
4
+
5
+ /**
6
+ * The shape-affecting flags shared by every node kind: base primitive, format, and `nullable`.
7
+ * Documentation and usage-slot flags (`optional`/`nullish`/`readOnly`/`writeOnly`) are
8
+ * intentionally excluded — they describe the property slot, not the type.
9
+ */
10
+ function flagsDescriptor(node: SchemaNode): string {
11
+ return `${node.primitive ?? ''};${node.format ?? ''};${node.nullable ? 1 : 0}`
12
+ }
13
+
14
+ function refTargetName(node: Extract<SchemaNode, { type: 'ref' }>): string {
15
+ if (node.ref) return extractRefName(node.ref)
16
+ return node.name ?? ''
17
+ }
18
+
19
+ /**
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.
23
+ */
24
+ function describeShape(node: SchemaNode, signatures: Map<SchemaNode, string>): string {
25
+ const flags = flagsDescriptor(node)
26
+
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 ?? ''}`
43
+ }
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}`
49
+ }
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}]`
53
+ }
54
+ case 'intersection': {
55
+ const members = (node.members ?? []).map((member) => signatureOf(member, signatures)).join(',')
56
+ return `intersection|${flags}|m[${members}]`
57
+ }
58
+ case 'enum': {
59
+ 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(',')
64
+ }
65
+ return `enum|${flags}|v[${values}]`
66
+ }
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
+ }
88
+ }
89
+
90
+ /**
91
+ * Hash-consing: each node's signature is a fixed-length digest of its local shape plus its
92
+ * children's digests (a Merkle hash). Children contribute their 64-char hash instead of their
93
+ * full nested descriptor, so a signature stays bounded regardless of subtree depth, and the
94
+ * digest is identical across calls because it depends only on content — never on traversal
95
+ * 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.
97
+ */
98
+ export function signatureOf(node: SchemaNode, signatures: Map<SchemaNode, string>): string {
99
+ const cached = signatures.get(node)
100
+ if (cached !== undefined) return cached
101
+ const signature = createHash('sha256').update(describeShape(node, signatures)).digest('hex')
102
+ signatures.set(node, signature)
103
+ return signature
104
+ }
105
+
106
+ /**
107
+ * Computes a deterministic, shape-only signature (a fixed-length content hash) for a schema node.
108
+ *
109
+ * Two schemas share a signature when they are structurally identical, ignoring
110
+ * documentation (`name`, `title`, `description`, `example`, `default`, `deprecated`)
111
+ * and usage-slot flags (`optional`, `nullish`, `readOnly`, `writeOnly`). `nullable`
112
+ * is kept because it changes the produced type. `ref` nodes compare by target name,
113
+ * which also keeps the algorithm terminating on circular shapes.
114
+ *
115
+ * @example Two enums with different descriptions share a signature
116
+ * ```ts
117
+ * schemaSignature(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'], description: 'x' })) ===
118
+ * schemaSignature(createSchema({ type: 'enum', primitive: 'string', enumValues: ['a', 'b'] }))
119
+ * ```
120
+ */
121
+ export function schemaSignature(node: SchemaNode): string {
122
+ return signatureOf(node, new Map())
123
+ }
124
+
125
+ /**
126
+ * Returns `true` when two schema nodes are structurally identical under shape-only equality.
127
+ *
128
+ * @example
129
+ * ```ts
130
+ * isSchemaEqual(a, b) // a and b produce the same TypeScript type
131
+ * ```
132
+ */
133
+ export function isSchemaEqual(a: SchemaNode, b: SchemaNode): boolean {
134
+ return schemaSignature(a) === schemaSignature(b)
135
+ }
package/src/types.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export type { VisitorDepth } from './constants.ts'
2
+ export type { BuildDedupePlanOptions, DedupeCanonical, DedupePlan } from './dedupe.ts'
2
3
  export type { SchemaDialect } from './dialect.ts'
3
4
  export type { DispatchRule } from './dispatch.ts'
4
5
  export type { DistributiveOmit } from './factory.ts'