@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/dist/index.cjs +61 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +43 -2
- package/dist/index.js +61 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/utils.ts +74 -2
package/package.json
CHANGED
package/src/index.ts
CHANGED
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
|
-
* @
|
|
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
|
*
|