@kubb/ast 5.0.0-beta.1 → 5.0.0-beta.2

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.1",
3
+ "version": "5.0.0-beta.2",
4
4
  "description": "Spec-agnostic AST layer for Kubb. Defines nodes, visitor pattern, and factory functions used across codegen plugins.",
5
5
  "keywords": [
6
6
  "ast",
package/src/index.ts CHANGED
@@ -33,6 +33,7 @@ export type * from './types.ts'
33
33
  export {
34
34
  caseParams,
35
35
  collectReferencedSchemaNames,
36
+ collectUsedSchemaNames,
36
37
  containsCircularRef,
37
38
  createDiscriminantNode,
38
39
  createOperationParams,
package/src/utils.ts CHANGED
@@ -669,7 +669,7 @@ export function combineImports(imports: Array<ImportNode>, exports: Array<Export
669
669
  let { name } = curr
670
670
 
671
671
  if (Array.isArray(name)) {
672
- name = [...new Set(name)].filter((item) => (typeof item === 'string' ? isUsed(item) : isUsed(item.propertyName)))
672
+ name = [...new Set(name)].filter((item) => (typeof item === 'string' ? isUsed(item) : isUsed(item.name ?? item.propertyName)))
673
673
  if (!name.length) continue
674
674
 
675
675
  const key = pathTypeKey(path, isTypeOnly)
@@ -751,7 +751,19 @@ export function resolveRefName(node: SchemaNode | undefined): string | undefined
751
751
  * Refs are followed by name only — the resolved `node.schema` is not traversed inline.
752
752
  * Use this to determine schema dependencies, build reference graphs, or detect what schemas need to be emitted.
753
753
  *
754
- * @note Returns a Set of schema names for efficient membership testing.
754
+ * @example Collect refs from a single schema
755
+ * ```ts
756
+ * const names = collectReferencedSchemaNames(petSchema)
757
+ * // → Set { 'Category', 'Tag' }
758
+ * ```
759
+ *
760
+ * @example Accumulate refs from multiple schemas into one set
761
+ * ```ts
762
+ * const out = new Set<string>()
763
+ * for (const schema of schemas) {
764
+ * collectReferencedSchemaNames(schema, out)
765
+ * }
766
+ * ```
755
767
  */
756
768
  export function collectReferencedSchemaNames(node: SchemaNode | undefined, out: Set<string> = new Set()): Set<string> {
757
769
  if (!node) return out
@@ -768,6 +780,66 @@ export function collectReferencedSchemaNames(node: SchemaNode | undefined, out:
768
780
  return out
769
781
  }
770
782
 
783
+ /**
784
+ * Collects the names of all top-level schemas transitively used by a set of operations.
785
+ *
786
+ * An operation uses a schema when any of its parameters, request body content, or responses
787
+ * reference it — directly or indirectly through other named schemas.
788
+ * The walk is iterative and safe against reference cycles.
789
+ *
790
+ * Use this together with `include` filters to determine which schemas from `components/schemas`
791
+ * are reachable from the allowed operations, so that schemas used only by excluded operations
792
+ * are not generated.
793
+ *
794
+ * @example Only generate schemas referenced by included operations
795
+ * ```ts
796
+ * const includedOps = inputNode.operations.filter(op => resolver.resolveOptions(op, { options, include }) !== null)
797
+ * const allowed = collectUsedSchemaNames(includedOps, inputNode.schemas)
798
+ *
799
+ * for (const schema of inputNode.schemas) {
800
+ * if (schema.name && !allowed.has(schema.name)) continue
801
+ * // … generate schema
802
+ * }
803
+ * ```
804
+ *
805
+ * @example Check whether a specific schema is needed
806
+ * ```ts
807
+ * const allowed = collectUsedSchemaNames(includedOps, inputNode.schemas)
808
+ * allowed.has('OrderStatus') // false when no included operation references OrderStatus
809
+ * ```
810
+ */
811
+ export function collectUsedSchemaNames(operations: ReadonlyArray<OperationNode>, schemas: ReadonlyArray<SchemaNode>): Set<string> {
812
+ const schemaMap = new Map<string, SchemaNode>()
813
+ for (const schema of schemas) {
814
+ if (schema.name) {
815
+ schemaMap.set(schema.name, schema)
816
+ }
817
+ }
818
+
819
+ const result = new Set<string>()
820
+
821
+ function visitSchema(schema: SchemaNode): void {
822
+ const directRefs = collectReferencedSchemaNames(schema)
823
+ for (const name of directRefs) {
824
+ if (!result.has(name)) {
825
+ result.add(name)
826
+ const namedSchema = schemaMap.get(name)
827
+ if (namedSchema) {
828
+ visitSchema(namedSchema)
829
+ }
830
+ }
831
+ }
832
+ }
833
+
834
+ for (const op of operations) {
835
+ for (const schema of collect<SchemaNode>(op, { depth: 'shallow', schema: (node) => node })) {
836
+ visitSchema(schema)
837
+ }
838
+ }
839
+
840
+ return result
841
+ }
842
+
771
843
  /**
772
844
  * Identifies all schemas that participate in circular dependency chains, including direct self-loops.
773
845
  *