@kubb/ast 5.0.0-beta.93 → 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
@@ -1149,10 +1149,11 @@ function transformChildren(node, visitor, recurse) {
1149
1149
  if (next !== value) (updates ??= {})[key] = next;
1150
1150
  }
1151
1151
  }
1152
- return updates ? {
1152
+ if (!updates) return node;
1153
+ return {
1153
1154
  ...node,
1154
1155
  ...updates
1155
- } : node;
1156
+ };
1156
1157
  }
1157
1158
  /**
1158
1159
  * Lazy depth-first collection pass. Yields every non-null value returned by
@@ -1409,17 +1410,22 @@ function extractRefName(ref) {
1409
1410
  return ref.split("/").at(-1) ?? ref;
1410
1411
  }
1411
1412
  /**
1412
- * Resolves the schema name of a ref node. Uses the last segment of `ref` when set, otherwise falls
1413
- * 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`.
1414
1416
  *
1415
1417
  * Returns `null` for non-ref nodes or when no name resolves.
1416
1418
  *
1417
1419
  * @example
1418
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'`
1419
1424
  */
1420
1425
  function resolveRefName(node) {
1421
1426
  if (!node || node.type !== "ref") return null;
1422
- if (node.ref) return extractRefName(node.ref) ?? node.name ?? node.schema?.name ?? null;
1427
+ if (node.targetName) return node.targetName;
1428
+ if (node.ref) return extractRefName(node.ref);
1423
1429
  return node.name ?? node.schema?.name ?? null;
1424
1430
  }
1425
1431
  /**
@@ -1475,7 +1481,7 @@ function syncSchemaRef(node) {
1475
1481
  });
1476
1482
  }
1477
1483
  /**
1478
- * Type guard that returns `true` when a schema emits as a plain `string` type.
1484
+ * Returns `true` when a schema emits as a plain `string` type.
1479
1485
  *
1480
1486
  * Covers `string`, `uuid`, `email`, `url`, and `datetime` types. For `date` and `time`
1481
1487
  * types, returns `true` only when `representation` is `'string'` rather than `'date'`.
@@ -1526,10 +1532,6 @@ function collectReferencedSchemaNames(node, out = /* @__PURE__ */ new Set()) {
1526
1532
  for (const name of collectSchemaRefs(node)) out.add(name);
1527
1533
  return out;
1528
1534
  }
1529
- /**
1530
- * Memoized two-level cache keyed first on the operations array, then on the schemas array.
1531
- */
1532
- const collectUsedSchemaNamesMemo = memoize(/* @__PURE__ */ new WeakMap(), (ops) => memoize(/* @__PURE__ */ new WeakMap(), (schemas) => computeUsedSchemaNames(ops, schemas)));
1533
1535
  function computeUsedSchemaNames(operations, schemas) {
1534
1536
  const schemaMap = /* @__PURE__ */ new Map();
1535
1537
  for (const schema of schemas) if (schema.name) schemaMap.set(schema.name, schema);
@@ -1569,7 +1571,7 @@ function computeUsedSchemaNames(operations, schemas) {
1569
1571
  * ```
1570
1572
  */
1571
1573
  function collectUsedSchemaNames(operations, schemas) {
1572
- return collectUsedSchemaNamesMemo(operations)(schemas);
1574
+ return computeUsedSchemaNames(operations, schemas);
1573
1575
  }
1574
1576
  const EMPTY_CIRCULAR_SET = /* @__PURE__ */ new Set();
1575
1577
  const findCircularSchemasMemo = memoize(/* @__PURE__ */ new WeakMap(), (schemas) => {
@@ -1733,6 +1735,36 @@ function macroEnumName({ parentName, propName, enumSuffix }) {
1733
1735
  });
1734
1736
  }
1735
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
1736
1768
  //#region src/macros/macroSimplifyUnion.ts
1737
1769
  /**
1738
1770
  * Scalar primitive schema types used for union simplification and type narrowing.
@@ -1868,6 +1900,7 @@ var exports_exports = /* @__PURE__ */ __exportAll({
1868
1900
  jsxDef: () => jsxDef,
1869
1901
  macroDiscriminatorEnum: () => macroDiscriminatorEnum,
1870
1902
  macroEnumName: () => macroEnumName,
1903
+ macroRenameSchema: () => macroRenameSchema,
1871
1904
  macroSimplifyUnion: () => macroSimplifyUnion,
1872
1905
  mapSchemaItems: () => mapSchemaItems,
1873
1906
  mapSchemaMembers: () => mapSchemaMembers,
@@ -1881,6 +1914,7 @@ var exports_exports = /* @__PURE__ */ __exportAll({
1881
1914
  parameterDef: () => parameterDef,
1882
1915
  propertyDef: () => propertyDef,
1883
1916
  requestBodyDef: () => requestBodyDef,
1917
+ resolveRefName: () => resolveRefName,
1884
1918
  responseDef: () => responseDef,
1885
1919
  schemaDef: () => schemaDef,
1886
1920
  schemaTypes: () => schemaTypes,
@@ -1933,6 +1967,7 @@ exports.isStringType = isStringType;
1933
1967
  exports.jsxDef = jsxDef;
1934
1968
  exports.macroDiscriminatorEnum = macroDiscriminatorEnum;
1935
1969
  exports.macroEnumName = macroEnumName;
1970
+ exports.macroRenameSchema = macroRenameSchema;
1936
1971
  exports.macroSimplifyUnion = macroSimplifyUnion;
1937
1972
  exports.mapSchemaItems = mapSchemaItems;
1938
1973
  exports.mapSchemaMembers = mapSchemaMembers;
@@ -1946,6 +1981,7 @@ exports.outputDef = outputDef;
1946
1981
  exports.parameterDef = parameterDef;
1947
1982
  exports.propertyDef = propertyDef;
1948
1983
  exports.requestBodyDef = requestBodyDef;
1984
+ exports.resolveRefName = resolveRefName;
1949
1985
  exports.responseDef = responseDef;
1950
1986
  exports.schemaDef = schemaDef;
1951
1987
  exports.schemaTypes = schemaTypes;