@kubb/ast 5.18.8 → 5.19.1

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
@@ -52,14 +52,19 @@ function memo(store, key, compute) {
52
52
  store.set(key, value);
53
53
  return value;
54
54
  }
55
- function createStore({ type }) {
56
- if (type === "weak") return /* @__PURE__ */ new WeakMap();
55
+ function createStore({ weak, max } = {}) {
56
+ if (weak) return /* @__PURE__ */ new WeakMap();
57
57
  const entry = /* @__PURE__ */ new Map();
58
- if (type === "map") return entry;
58
+ if (max === void 0) return entry;
59
59
  const set = (key, value) => {
60
- entry.clear();
60
+ if (entry.size >= max) entry.clear();
61
61
  entry.set(key, value);
62
62
  };
63
+ if (max !== 1) return {
64
+ has: (key) => entry.has(key),
65
+ get: (key) => entry.get(key),
66
+ set
67
+ };
63
68
  const touch = (key) => {
64
69
  if (!entry.has(key)) return false;
65
70
  set(key, entry.get(key));
@@ -342,19 +347,17 @@ function walkSchema(node, through, visit) {
342
347
  * ```
343
348
  */
344
349
  function getSchemaLiteralValues$1(node) {
345
- const cached = literalValues.get(node);
346
- if (cached) return cached;
347
- const values = /* @__PURE__ */ new Set();
348
- walkSchema(node, "union", (current) => {
349
- if (current.type !== "enum") return;
350
- for (const { value } of current.namedEnumValues ?? current.enumValues?.map((value) => ({ value })) ?? []) values.add(value);
350
+ return memo(literalValues, node, () => {
351
+ const values = /* @__PURE__ */ new Set();
352
+ walkSchema(node, "union", (current) => {
353
+ if (current.type !== "enum") return;
354
+ for (const { value } of current.namedEnumValues ?? current.enumValues?.map((value) => ({ value })) ?? []) values.add(value);
355
+ });
356
+ return Object.freeze([...values]);
351
357
  });
352
- const result = Object.freeze([...values]);
353
- literalValues.set(node, result);
354
- return result;
355
358
  }
356
359
  __name(getSchemaLiteralValues$1, "getSchemaLiteralValues");
357
- const literalValues = /* @__PURE__ */ new WeakMap();
360
+ const literalValues = memo.create({ weak: true });
358
361
  function collectPropertyNames(node) {
359
362
  const names = /* @__PURE__ */ new Set();
360
363
  walkSchema(node, "intersection", (current) => {
@@ -781,21 +784,17 @@ function collectInto(node, visitor, recurse, parent, result) {
781
784
  * (RFC 6901), so `a~1b~0c` names the component `a/b~c`.
782
785
  */
783
786
  function refName$1(ref) {
784
- const cached = refNames.get(ref);
785
- if (cached !== void 0) return cached;
786
- const segment = ref.split("/").at(-1) ?? ref;
787
- let decoded = segment;
788
- try {
789
- decoded = decodeURIComponent(segment);
790
- } catch {}
791
- const name = decoded.replaceAll("~1", "/").replaceAll("~0", "~");
792
- if (refNames.size >= memoLimit) refNames.clear();
793
- refNames.set(ref, name);
794
- return name;
787
+ return memo(refNames, ref, () => {
788
+ const segment = ref.split("/").at(-1) ?? ref;
789
+ let decoded = segment;
790
+ try {
791
+ decoded = decodeURIComponent(segment);
792
+ } catch {}
793
+ return decoded.replaceAll("~1", "/").replaceAll("~0", "~");
794
+ });
795
795
  }
796
796
  __name(refName$1, "refName");
797
- const refNames = /* @__PURE__ */ new Map();
798
- const memoLimit = 1e5;
797
+ const refNames = memo.create({ max: 1e5 });
799
798
  /**
800
799
  * Resolves the emitted name of the schema a ref node points at. Prefers `targetName` (set when
801
800
  * the referenced schema was renamed, e.g. to break a collision), then the last segment of `ref`,
@@ -881,11 +880,11 @@ function isBareRef$1(node, keysToOmit) {
881
880
  }
882
881
  __name(isBareRef$1, "isBareRef");
883
882
  // src/utils/schemaGraph.ts
884
- const refEntries = /* @__PURE__ */ new WeakMap();
885
- const checkRefEntries = /* @__PURE__ */ new WeakMap();
886
- const schemaRefs = /* @__PURE__ */ new WeakMap();
887
- const importedRefNames = /* @__PURE__ */ new WeakMap();
888
- const importedCheckRefNames = /* @__PURE__ */ new WeakMap();
883
+ const refEntries = memo.create({ weak: true });
884
+ const checkRefEntries = memo.create({ weak: true });
885
+ const schemaRefs = memo.create({ weak: true });
886
+ const importedRefNames = memo.create({ weak: true });
887
+ const importedCheckRefNames = memo.create({ weak: true });
889
888
  /**
890
889
  * Walks a node's subtree once and lists every ref it holds, in traversal order, as the resolved
891
890
  * name plus whether the ref carries a `$ref` pointer. Memoized per node and per `checks` mode, so
@@ -1107,27 +1106,25 @@ function getBaseURL(server) {
1107
1106
  if (!server || server.variables || server.url.includes("{") || !server.url.includes("://")) return void 0;
1108
1107
  return URL.canParse(server.url) ? server.url : void 0;
1109
1108
  }
1110
- const parametersByNode = /* @__PURE__ */ new WeakMap();
1109
+ const parametersByNode = memo.create({ weak: true });
1111
1110
  /** The operation's parameters by location, with a `querystring` parameter in `query`. A repeated name keeps its first parameter. */
1112
1111
  function groupParameters(node) {
1113
- const cached = parametersByNode.get(node);
1114
- if (cached) return cached;
1115
- const group = (...locations) => {
1116
- const seen = /* @__PURE__ */ new Set();
1117
- return locations.flatMap((location) => node.parameters.filter((param) => {
1118
- if (param.in !== location || seen.has(param.name)) return false;
1119
- seen.add(param.name);
1120
- return true;
1121
- }));
1122
- };
1123
- const groups = {
1124
- path: group("path"),
1125
- query: group("query", "querystring"),
1126
- header: group("header"),
1127
- cookie: group("cookie")
1128
- };
1129
- parametersByNode.set(node, groups);
1130
- return groups;
1112
+ return memo(parametersByNode, node, () => {
1113
+ const group = (...locations) => {
1114
+ const seen = /* @__PURE__ */ new Set();
1115
+ return locations.flatMap((location) => node.parameters.filter((param) => {
1116
+ if (param.in !== location || seen.has(param.name)) return false;
1117
+ seen.add(param.name);
1118
+ return true;
1119
+ }));
1120
+ };
1121
+ return {
1122
+ path: group("path"),
1123
+ query: group("query", "querystring"),
1124
+ header: group("header"),
1125
+ cookie: group("cookie")
1126
+ };
1127
+ });
1131
1128
  }
1132
1129
  // src/nodes/schema.ts
1133
1130
  /**
@@ -1533,12 +1530,11 @@ function combineImports(imports, exports, source) {
1533
1530
  const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
1534
1531
  const identifiers = source && imports.length > INDEX_ABOVE_IMPORTS ? new Set(source.match(IDENTIFIER_RUN)) : null;
1535
1532
  const isUsed = (importName) => !source || identifiers?.has(importName) || source.includes(importName) || exportedNames.has(importName);
1536
- const importNameMemo = /* @__PURE__ */ new Map();
1533
+ const importNameMemo = memo.create();
1537
1534
  const canonicalizeName = (n) => {
1538
1535
  if (typeof n === "string") return n;
1539
1536
  const key = `${n.propertyName}:${n.name ?? ""}`;
1540
- if (!importNameMemo.has(key)) importNameMemo.set(key, n);
1541
- return importNameMemo.get(key);
1537
+ return memo(importNameMemo, key, () => n);
1542
1538
  };
1543
1539
  const pathsWithUsedNamedImport = /* @__PURE__ */ new Set();
1544
1540
  for (const node of imports) if (Array.isArray(node.name) && node.name.some((item) => isUsed(importLocalName(item)))) pathsWithUsedNamedImport.add(node.path);