@kubb/ast 5.18.8 → 5.19.0

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.js CHANGED
@@ -31,14 +31,19 @@ function memo(store, key, compute) {
31
31
  store.set(key, value);
32
32
  return value;
33
33
  }
34
- function createStore({ type }) {
35
- if (type === "weak") return /* @__PURE__ */ new WeakMap();
34
+ function createStore({ weak, max } = {}) {
35
+ if (weak) return /* @__PURE__ */ new WeakMap();
36
36
  const entry = /* @__PURE__ */ new Map();
37
- if (type === "map") return entry;
37
+ if (max === void 0) return entry;
38
38
  const set = (key, value) => {
39
- entry.clear();
39
+ if (entry.size >= max) entry.clear();
40
40
  entry.set(key, value);
41
41
  };
42
+ if (max !== 1) return {
43
+ has: (key) => entry.has(key),
44
+ get: (key) => entry.get(key),
45
+ set
46
+ };
42
47
  const touch = (key) => {
43
48
  if (!entry.has(key)) return false;
44
49
  set(key, entry.get(key));
@@ -321,19 +326,17 @@ function walkSchema(node, through, visit) {
321
326
  * ```
322
327
  */
323
328
  function getSchemaLiteralValues$1(node) {
324
- const cached = literalValues.get(node);
325
- if (cached) return cached;
326
- const values = /* @__PURE__ */ new Set();
327
- walkSchema(node, "union", (current) => {
328
- if (current.type !== "enum") return;
329
- for (const { value } of current.namedEnumValues ?? current.enumValues?.map((value) => ({ value })) ?? []) values.add(value);
329
+ return memo(literalValues, node, () => {
330
+ const values = /* @__PURE__ */ new Set();
331
+ walkSchema(node, "union", (current) => {
332
+ if (current.type !== "enum") return;
333
+ for (const { value } of current.namedEnumValues ?? current.enumValues?.map((value) => ({ value })) ?? []) values.add(value);
334
+ });
335
+ return Object.freeze([...values]);
330
336
  });
331
- const result = Object.freeze([...values]);
332
- literalValues.set(node, result);
333
- return result;
334
337
  }
335
338
  __name(getSchemaLiteralValues$1, "getSchemaLiteralValues");
336
- const literalValues = /* @__PURE__ */ new WeakMap();
339
+ const literalValues = memo.create({ weak: true });
337
340
  function collectPropertyNames(node) {
338
341
  const names = /* @__PURE__ */ new Set();
339
342
  walkSchema(node, "intersection", (current) => {
@@ -760,21 +763,17 @@ function collectInto(node, visitor, recurse, parent, result) {
760
763
  * (RFC 6901), so `a~1b~0c` names the component `a/b~c`.
761
764
  */
762
765
  function refName$1(ref) {
763
- const cached = refNames.get(ref);
764
- if (cached !== void 0) return cached;
765
- const segment = ref.split("/").at(-1) ?? ref;
766
- let decoded = segment;
767
- try {
768
- decoded = decodeURIComponent(segment);
769
- } catch {}
770
- const name = decoded.replaceAll("~1", "/").replaceAll("~0", "~");
771
- if (refNames.size >= memoLimit) refNames.clear();
772
- refNames.set(ref, name);
773
- return name;
766
+ return memo(refNames, ref, () => {
767
+ const segment = ref.split("/").at(-1) ?? ref;
768
+ let decoded = segment;
769
+ try {
770
+ decoded = decodeURIComponent(segment);
771
+ } catch {}
772
+ return decoded.replaceAll("~1", "/").replaceAll("~0", "~");
773
+ });
774
774
  }
775
775
  __name(refName$1, "refName");
776
- const refNames = /* @__PURE__ */ new Map();
777
- const memoLimit = 1e5;
776
+ const refNames = memo.create({ max: 1e5 });
778
777
  /**
779
778
  * Resolves the emitted name of the schema a ref node points at. Prefers `targetName` (set when
780
779
  * the referenced schema was renamed, e.g. to break a collision), then the last segment of `ref`,
@@ -860,11 +859,11 @@ function isBareRef$1(node, keysToOmit) {
860
859
  }
861
860
  __name(isBareRef$1, "isBareRef");
862
861
  // src/utils/schemaGraph.ts
863
- const refEntries = /* @__PURE__ */ new WeakMap();
864
- const checkRefEntries = /* @__PURE__ */ new WeakMap();
865
- const schemaRefs = /* @__PURE__ */ new WeakMap();
866
- const importedRefNames = /* @__PURE__ */ new WeakMap();
867
- const importedCheckRefNames = /* @__PURE__ */ new WeakMap();
862
+ const refEntries = memo.create({ weak: true });
863
+ const checkRefEntries = memo.create({ weak: true });
864
+ const schemaRefs = memo.create({ weak: true });
865
+ const importedRefNames = memo.create({ weak: true });
866
+ const importedCheckRefNames = memo.create({ weak: true });
868
867
  /**
869
868
  * Walks a node's subtree once and lists every ref it holds, in traversal order, as the resolved
870
869
  * name plus whether the ref carries a `$ref` pointer. Memoized per node and per `checks` mode, so
@@ -1086,27 +1085,25 @@ function getBaseURL(server) {
1086
1085
  if (!server || server.variables || server.url.includes("{") || !server.url.includes("://")) return void 0;
1087
1086
  return URL.canParse(server.url) ? server.url : void 0;
1088
1087
  }
1089
- const parametersByNode = /* @__PURE__ */ new WeakMap();
1088
+ const parametersByNode = memo.create({ weak: true });
1090
1089
  /** The operation's parameters by location, with a `querystring` parameter in `query`. A repeated name keeps its first parameter. */
1091
1090
  function groupParameters(node) {
1092
- const cached = parametersByNode.get(node);
1093
- if (cached) return cached;
1094
- const group = (...locations) => {
1095
- const seen = /* @__PURE__ */ new Set();
1096
- return locations.flatMap((location) => node.parameters.filter((param) => {
1097
- if (param.in !== location || seen.has(param.name)) return false;
1098
- seen.add(param.name);
1099
- return true;
1100
- }));
1101
- };
1102
- const groups = {
1103
- path: group("path"),
1104
- query: group("query", "querystring"),
1105
- header: group("header"),
1106
- cookie: group("cookie")
1107
- };
1108
- parametersByNode.set(node, groups);
1109
- return groups;
1091
+ return memo(parametersByNode, node, () => {
1092
+ const group = (...locations) => {
1093
+ const seen = /* @__PURE__ */ new Set();
1094
+ return locations.flatMap((location) => node.parameters.filter((param) => {
1095
+ if (param.in !== location || seen.has(param.name)) return false;
1096
+ seen.add(param.name);
1097
+ return true;
1098
+ }));
1099
+ };
1100
+ return {
1101
+ path: group("path"),
1102
+ query: group("query", "querystring"),
1103
+ header: group("header"),
1104
+ cookie: group("cookie")
1105
+ };
1106
+ });
1110
1107
  }
1111
1108
  // src/nodes/schema.ts
1112
1109
  /**
@@ -1512,12 +1509,11 @@ function combineImports(imports, exports, source) {
1512
1509
  const exportedNames = new Set(exports.flatMap((e) => Array.isArray(e.name) ? e.name : e.name ? [e.name] : []));
1513
1510
  const identifiers = source && imports.length > INDEX_ABOVE_IMPORTS ? new Set(source.match(IDENTIFIER_RUN)) : null;
1514
1511
  const isUsed = (importName) => !source || identifiers?.has(importName) || source.includes(importName) || exportedNames.has(importName);
1515
- const importNameMemo = /* @__PURE__ */ new Map();
1512
+ const importNameMemo = memo.create();
1516
1513
  const canonicalizeName = (n) => {
1517
1514
  if (typeof n === "string") return n;
1518
1515
  const key = `${n.propertyName}:${n.name ?? ""}`;
1519
- if (!importNameMemo.has(key)) importNameMemo.set(key, n);
1520
- return importNameMemo.get(key);
1516
+ return memo(importNameMemo, key, () => n);
1521
1517
  };
1522
1518
  const pathsWithUsedNamedImport = /* @__PURE__ */ new Set();
1523
1519
  for (const node of imports) if (Array.isArray(node.name) && node.name.some((item) => isUsed(importLocalName(item)))) pathsWithUsedNamedImport.add(node.path);