@kubb/ast 5.0.0-beta.94 → 5.0.0-beta.95

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 CHANGED
@@ -1410,16 +1410,21 @@ function extractRefName(ref) {
1410
1410
  return ref.split("/").at(-1) ?? ref;
1411
1411
  }
1412
1412
  /**
1413
- * Resolves the schema name of a ref node. Uses the last segment of `ref` when set, otherwise falls
1414
- * back to `name` then nested `schema.name`.
1413
+ * Resolves the emitted name of the schema a ref node points at. Prefers `targetName` (set when
1414
+ * the referenced schema was renamed, e.g. to break a collision), then the last segment of `ref`,
1415
+ * then `name`, then the nested `schema.name`.
1415
1416
  *
1416
1417
  * Returns `null` for non-ref nodes or when no name resolves.
1417
1418
  *
1418
1419
  * @example
1419
1420
  * `resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Pet' }) // 'Pet'`
1421
+ *
1422
+ * @example Collision-renamed target
1423
+ * `resolveRefName({ kind: 'Schema', type: 'ref', ref: '#/components/schemas/Order', targetName: 'OrderSchema' }) // 'OrderSchema'`
1420
1424
  */
1421
1425
  function resolveRefName(node) {
1422
1426
  if (!node || node.type !== "ref") return null;
1427
+ if (node.targetName) return node.targetName;
1423
1428
  if (node.ref) return extractRefName(node.ref);
1424
1429
  return node.name ?? node.schema?.name ?? null;
1425
1430
  }
@@ -1527,10 +1532,6 @@ function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
1527
1532
  for (const name of collectSchemaRefs(node)) out.add(name);
1528
1533
  return out;
1529
1534
  }
1530
- /**
1531
- * Memoized two-level cache keyed first on the operations array, then on the schemas array.
1532
- */
1533
- const collectUsedSchemaNamesMemo = memoize(/* @__PURE__ */ new WeakMap(), (ops) => memoize(/* @__PURE__ */ new WeakMap(), (schemas) => computeUsedSchemaNames(ops, schemas)));
1534
1535
  function computeUsedSchemaNames(operations, schemas) {
1535
1536
  const schemaMap = /* @__PURE__ */ new Map();
1536
1537
  for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema);
@@ -1570,7 +1571,7 @@ function computeUsedSchemaNames(operations, schemas) {
1570
1571
  * ```
1571
1572
  */
1572
1573
  function collectUsedSchemaNames(operations, schemas) {
1573
- return collectUsedSchemaNamesMemo(operations)(schemas);
1574
+ return computeUsedSchemaNames(operations, schemas);
1574
1575
  }
1575
1576
  const EMPTY_CIRCULAR_SET = /* @__PURE__ */ new Set();
1576
1577
  const findCircularSchemasMemo = memoize(/* @__PURE__ */ new WeakMap(), (schemas) => {
@@ -1734,6 +1735,36 @@ function macroEnumName({ parentName, propName, enumSuffix }) {
1734
1735
  });
1735
1736
  }
1736
1737
  //#endregion
1738
+ //#region src/macros/macroRenameSchema.ts
1739
+ /**
1740
+ * Builds a macro that renames a schema consistently: the declaration (`name`) and every ref
1741
+ * pointing at it (`targetName`) change together, so imports and printed references stay in
1742
+ * sync. Renaming only one side by hand produces imports for files that are never generated.
1743
+ *
1744
+ * @example
1745
+ * `const macro = macroRenameSchema({ from: 'Order', to: 'StoreOrder' })`
1746
+ */
1747
+ function macroRenameSchema({ from, to }) {
1748
+ return defineMacro({
1749
+ name: "rename-schema",
1750
+ schema(node) {
1751
+ const refNode = narrowSchema(node, "ref");
1752
+ if (!refNode) return node.name === from ? {
1753
+ ...node,
1754
+ name: to
1755
+ } : void 0;
1756
+ const renamesDeclaration = refNode.name === from;
1757
+ const renamesTarget = resolveRefName(refNode) === from;
1758
+ if (!renamesDeclaration && !renamesTarget) return void 0;
1759
+ return {
1760
+ ...refNode,
1761
+ ...renamesDeclaration ? { name: to } : {},
1762
+ ...renamesTarget ? { targetName: to } : {}
1763
+ };
1764
+ }
1765
+ });
1766
+ }
1767
+ //#endregion
1737
1768
  //#region src/macros/macroSimplifyUnion.ts
1738
1769
  /**
1739
1770
  * Scalar primitive schema types used for union simplification and type narrowing.
@@ -1869,6 +1900,7 @@ var exports_exports = /* @__PURE__ */ __exportAll({
1869
1900
  jsxDef: () => jsxDef,
1870
1901
  macroDiscriminatorEnum: () => macroDiscriminatorEnum,
1871
1902
  macroEnumName: () => macroEnumName,
1903
+ macroRenameSchema: () => macroRenameSchema,
1872
1904
  macroSimplifyUnion: () => macroSimplifyUnion,
1873
1905
  mapSchemaItems: () => mapSchemaItems,
1874
1906
  mapSchemaMembers: () => mapSchemaMembers,
@@ -1882,6 +1914,7 @@ var exports_exports = /* @__PURE__ */ __exportAll({
1882
1914
  parameterDef: () => parameterDef,
1883
1915
  propertyDef: () => propertyDef,
1884
1916
  requestBodyDef: () => requestBodyDef,
1917
+ resolveRefName: () => resolveRefName,
1885
1918
  responseDef: () => responseDef,
1886
1919
  schemaDef: () => schemaDef,
1887
1920
  schemaTypes: () => schemaTypes,
@@ -1934,6 +1967,7 @@ exports.isStringType = isStringType;
1934
1967
  exports.jsxDef = jsxDef;
1935
1968
  exports.macroDiscriminatorEnum = macroDiscriminatorEnum;
1936
1969
  exports.macroEnumName = macroEnumName;
1970
+ exports.macroRenameSchema = macroRenameSchema;
1937
1971
  exports.macroSimplifyUnion = macroSimplifyUnion;
1938
1972
  exports.mapSchemaItems = mapSchemaItems;
1939
1973
  exports.mapSchemaMembers = mapSchemaMembers;
@@ -1947,6 +1981,7 @@ exports.outputDef = outputDef;
1947
1981
  exports.parameterDef = parameterDef;
1948
1982
  exports.propertyDef = propertyDef;
1949
1983
  exports.requestBodyDef = requestBodyDef;
1984
+ exports.resolveRefName = resolveRefName;
1950
1985
  exports.responseDef = responseDef;
1951
1986
  exports.schemaDef = schemaDef;
1952
1987
  exports.schemaTypes = schemaTypes;