@kubb/ast 5.0.0-beta.14 → 5.0.0-beta.16

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.14",
3
+ "version": "5.0.0-beta.16",
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/factory.ts CHANGED
@@ -12,7 +12,9 @@ import type {
12
12
  FunctionParameterNode,
13
13
  FunctionParametersNode,
14
14
  ImportNode,
15
+ InputMeta,
15
16
  InputNode,
17
+ InputStreamNode,
16
18
  JsxNode,
17
19
  ObjectSchemaNode,
18
20
  OperationNode,
@@ -89,6 +91,18 @@ export function createInput(overrides: Partial<Omit<InputNode, 'kind'>> = {}): I
89
91
  }
90
92
  }
91
93
 
94
+ /**
95
+ * Creates an `InputStreamNode` from pre-built `AsyncIterable` sources.
96
+ *
97
+ * @example
98
+ * ```ts
99
+ * const node = createStreamInput(schemasIterable, operationsIterable, { title: 'My API' })
100
+ * ```
101
+ */
102
+ export function createStreamInput(schemas: AsyncIterable<SchemaNode>, operations: AsyncIterable<OperationNode>, meta?: InputMeta): InputStreamNode {
103
+ return { kind: 'Input', schemas, operations, meta }
104
+ }
105
+
92
106
  /**
93
107
  * Creates an `OutputNode` with a stable default for `files`.
94
108
  *
package/src/index.ts CHANGED
@@ -10,6 +10,7 @@ export {
10
10
  createFunctionParameters,
11
11
  createImport,
12
12
  createInput,
13
+ createStreamInput,
13
14
  createJsx,
14
15
  createOperation,
15
16
  createOutput,
@@ -28,7 +29,7 @@ export { isInputNode, isOperationNode, isOutputNode, isSchemaNode, narrowSchema
28
29
  export { createPrinterFactory, definePrinter } from './printer.ts'
29
30
  export { extractRefName } from './refs.ts'
30
31
  export { childName, collectImports, enumPropName, findDiscriminator } from './resolvers.ts'
31
- export { mergeAdjacentObjects, setDiscriminatorEnum, setEnumName, simplifyUnion } from './transformers.ts'
32
+ export { mergeAdjacentObjects, mergeAdjacentObjectsLazy, setDiscriminatorEnum, setEnumName, simplifyUnion } from './transformers.ts'
32
33
  export type * from './types.ts'
33
34
  export {
34
35
  caseParams,
@@ -19,7 +19,7 @@ export type { OutputNode } from './output.ts'
19
19
  export type { ParameterLocation, ParameterNode } from './parameter.ts'
20
20
  export type { PropertyNode } from './property.ts'
21
21
  export type { ResponseNode } from './response.ts'
22
- export type { InputMeta, InputNode } from './root.ts'
22
+ export type { InputMeta, InputNode, InputStreamNode } from './root.ts'
23
23
  export type {
24
24
  ArraySchemaNode,
25
25
  ComplexSchemaType,
package/src/nodes/root.ts CHANGED
@@ -62,3 +62,27 @@ export type InputNode = BaseNode & {
62
62
  */
63
63
  meta?: InputMeta
64
64
  }
65
+
66
+ /**
67
+ * Streaming variant of `InputNode` for memory-efficient processing of large API specs.
68
+ *
69
+ * `schemas` and `operations` are `AsyncIterable` rather than arrays — each `for await`
70
+ * loop creates a fresh parse pass from the cached in-memory document, so multiple
71
+ * consumers (plugins) can iterate independently without keeping all nodes in memory.
72
+ *
73
+ * @example
74
+ * ```ts
75
+ * for await (const schema of inputStreamNode.schemas) {
76
+ * // only this one SchemaNode is live here; previous ones are GC-eligible
77
+ * }
78
+ * ```
79
+ */
80
+ export type InputStreamNode = {
81
+ kind: 'Input'
82
+ /** Lazily parsed schema nodes. Each `for await` creates a fresh parse pass. */
83
+ schemas: AsyncIterable<SchemaNode>
84
+ /** Lazily parsed operation nodes. Each `for await` creates a fresh parse pass. */
85
+ operations: AsyncIterable<OperationNode>
86
+ /** Document metadata — available immediately, before the first yield. */
87
+ meta?: InputMeta
88
+ }
@@ -73,25 +73,30 @@ export function setDiscriminatorEnum({
73
73
  * ])
74
74
  * ```
75
75
  */
76
- export function mergeAdjacentObjects(members: Array<SchemaNode>): Array<SchemaNode> {
77
- return members.reduce<Array<SchemaNode>>((acc, member) => {
76
+ export function* mergeAdjacentObjectsLazy(members: Iterable<SchemaNode>): Generator<SchemaNode, void, undefined> {
77
+ let acc: SchemaNode | undefined
78
+
79
+ for (const member of members) {
78
80
  const objectMember = narrowSchema(member, 'object')
79
- if (objectMember && !objectMember.name) {
80
- const previous = acc.at(-1)
81
- const previousObject = previous ? narrowSchema(previous, 'object') : undefined
82
-
83
- if (previousObject && !previousObject.name) {
84
- acc[acc.length - 1] = createSchema({
85
- ...previousObject,
86
- properties: [...(previousObject.properties ?? []), ...(objectMember.properties ?? [])],
81
+ if (objectMember && !objectMember.name && acc !== undefined) {
82
+ const accObject = narrowSchema(acc, 'object')
83
+ if (accObject && !accObject.name) {
84
+ acc = createSchema({
85
+ ...accObject,
86
+ properties: [...(accObject.properties ?? []), ...(objectMember.properties ?? [])],
87
87
  })
88
- return acc
88
+ continue
89
89
  }
90
90
  }
91
+ if (acc !== undefined) yield acc
92
+ acc = member
93
+ }
91
94
 
92
- acc.push(member)
93
- return acc
94
- }, [])
95
+ if (acc !== undefined) yield acc
96
+ }
97
+
98
+ export function mergeAdjacentObjects(members: Array<SchemaNode>): Array<SchemaNode> {
99
+ return [...mergeAdjacentObjectsLazy(members)]
95
100
  }
96
101
 
97
102
  /**
package/src/types.ts CHANGED
@@ -26,6 +26,7 @@ export type {
26
26
  ImportNode,
27
27
  InputMeta,
28
28
  InputNode,
29
+ InputStreamNode,
29
30
  IntersectionSchemaNode,
30
31
  Ipv4SchemaNode,
31
32
  Ipv6SchemaNode,
package/src/utils.ts CHANGED
@@ -775,18 +775,28 @@ export function resolveRefName(node: SchemaNode | undefined): string | undefined
775
775
  * }
776
776
  * ```
777
777
  */
778
- export function collectReferencedSchemaNames(node: SchemaNode | undefined, out: Set<string> = new Set()): Set<string> {
779
- if (!node) return out
778
+ const schemaRefCache = new WeakMap<SchemaNode, ReadonlySet<string>>()
779
+
780
+ function collectSchemaRefs(node: SchemaNode): ReadonlySet<string> {
781
+ const cached = schemaRefCache.get(node)
782
+ if (cached) return cached
783
+
784
+ const refs = new Set<string>()
780
785
  collect<void>(node, {
781
786
  schema(child) {
782
787
  if (child.type === 'ref') {
783
788
  const name = resolveRefName(child)
784
-
785
- if (name) out.add(name)
789
+ if (name) refs.add(name)
786
790
  }
787
- return undefined
788
791
  },
789
792
  })
793
+ schemaRefCache.set(node, refs)
794
+ return refs
795
+ }
796
+
797
+ export function collectReferencedSchemaNames(node: SchemaNode | undefined, out: Set<string> = new Set()): Set<string> {
798
+ if (!node) return out
799
+ for (const name of collectSchemaRefs(node)) out.add(name)
790
800
  return out
791
801
  }
792
802