@kubb/adapter-oas 5.11.4 → 5.11.6

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
@@ -3,6 +3,7 @@ import { Diagnostics, ast, childName, enumPropName, extractRefName, macroDiscrim
3
3
  import { access, readFile } from "node:fs/promises";
4
4
  import { upgrade } from "@scalar/openapi-upgrader";
5
5
  import { bundle } from "api-ref-bundler";
6
+ import * as yaml from "yaml";
6
7
  import { parse } from "yaml";
7
8
  //#endregion
8
9
  //#region kubb-extension.json
@@ -12,7 +13,7 @@ var compatibility = {
12
13
  };
13
14
  //#endregion
14
15
  //#region package.json
15
- var version = "5.11.4";
16
+ var version = "5.11.6";
16
17
  //#endregion
17
18
  //#region src/constants.ts
18
19
  /**
@@ -2294,6 +2295,58 @@ function visit(node, pointer) {
2294
2295
  if (node.type === "union" || node.type === "intersection" || node.type === "not") for (const [index, member] of (node.members ?? []).entries()) visit(member, `${pointer}/members/${index}`);
2295
2296
  }
2296
2297
  //#endregion
2298
+ //#region src/sourceLocator.ts
2299
+ const cache = /* @__PURE__ */ new Map();
2300
+ const toRange = ([start, end]) => ({
2301
+ start,
2302
+ end
2303
+ });
2304
+ const unescapeToken = (token) => token.replaceAll("~1", "/").replaceAll("~0", "~");
2305
+ /** Walks a JSON pointer through maps and sequences; the range is the last key or item it reached. */
2306
+ function findPointer(document, pointer) {
2307
+ return pointer.replace(/^#/, "").split("/").slice(1).map(unescapeToken).reduce((at, token) => {
2308
+ if (yaml.isMap(at?.node)) {
2309
+ const pair = at.node.items.find((item) => yaml.isScalar(item.key) && String(item.key.value) === token);
2310
+ return pair && yaml.isScalar(pair.key) && pair.key.range ? {
2311
+ node: pair.value,
2312
+ range: toRange(pair.key.range)
2313
+ } : void 0;
2314
+ }
2315
+ if (yaml.isSeq(at?.node)) {
2316
+ const item = at.node.items[Number(token)];
2317
+ return yaml.isNode(item) && item.range ? {
2318
+ node: item,
2319
+ range: toRange(item.range)
2320
+ } : void 0;
2321
+ }
2322
+ }, {
2323
+ node: document.contents,
2324
+ range: void 0
2325
+ })?.range;
2326
+ }
2327
+ /** The first `$ref` whose value is `ref`. */
2328
+ function findRefUsage(document, ref) {
2329
+ const found = [];
2330
+ yaml.visit(document, { Pair(_, { key, value }) {
2331
+ if (!yaml.isScalar(key) || key.value !== "$ref" || !yaml.isScalar(value) || value.value !== ref || !value.range) return;
2332
+ found.push(toRange(value.range));
2333
+ return yaml.visit.BREAK;
2334
+ } });
2335
+ return found[0];
2336
+ }
2337
+ /**
2338
+ * Maps OpenAPI JSON pointers and unresolved references to ranges in YAML or JSON source.
2339
+ */
2340
+ const locate = (source, location) => {
2341
+ if (!("pointer" in location)) return void 0;
2342
+ if (!cache.has(source)) {
2343
+ cache.clear();
2344
+ cache.set(source, yaml.parseDocument(source));
2345
+ }
2346
+ const document = cache.get(source);
2347
+ return findPointer(document, location.pointer) ?? ("ref" in location && location.ref ? findRefUsage(document, location.ref) : void 0);
2348
+ };
2349
+ //#endregion
2297
2350
  //#region src/adapter.ts
2298
2351
  /**
2299
2352
  * The `name` of `@kubb/adapter-oas`, used to identify this adapter in a Kubb config.
@@ -2448,6 +2501,7 @@ const adapterOas = defineExtension({
2448
2501
  get document() {
2449
2502
  return parsedDocument;
2450
2503
  },
2504
+ locate,
2451
2505
  async validate(input, options) {
2452
2506
  const document = await parseFromConfig({
2453
2507
  type: "path",