@kubb/adapter-oas 5.18.7 → 5.18.8
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 +290 -199
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +287 -182
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.js
CHANGED
|
@@ -2,11 +2,11 @@ import { defineExtension } from "@kubb/core";
|
|
|
2
2
|
import { Diagnostics, ast, childName, enumPrimitive, enumPropName, macroDedupeUnion, macroDiscriminatorEnum, macroEnumName, mergeAdjacentObjectsLazy } from "@kubb/kit";
|
|
3
3
|
import { dirname, join, resolve } from "node:path";
|
|
4
4
|
import { fileURLToPath } from "node:url";
|
|
5
|
-
import {
|
|
5
|
+
import { readFile } from "node:fs";
|
|
6
|
+
import { access } from "node:fs/promises";
|
|
6
7
|
import { upgrade } from "@scalar/openapi-upgrader";
|
|
7
8
|
import { bundle } from "api-ref-bundler";
|
|
8
|
-
import { CORE_SCHEMA, load } from "js-yaml";
|
|
9
|
-
import * as yaml from "yaml";
|
|
9
|
+
import { COLLECTION_STYLE, CORE_SCHEMA, EVENT_ID, SCALAR_STYLE, constructFromEvents, load, parseEvents } from "js-yaml";
|
|
10
10
|
import { parse as parse$1 } from "yaml";
|
|
11
11
|
// kubb-extension.json
|
|
12
12
|
var compatibility = {
|
|
@@ -14,7 +14,7 @@ var compatibility = {
|
|
|
14
14
|
"node": ">=22.22"
|
|
15
15
|
};
|
|
16
16
|
// package.json
|
|
17
|
-
var version = "5.18.
|
|
17
|
+
var version = "5.18.8";
|
|
18
18
|
// src/constants.ts
|
|
19
19
|
/**
|
|
20
20
|
* Default parser options applied when no explicit options are provided.
|
|
@@ -361,8 +361,44 @@ async function exists(path) {
|
|
|
361
361
|
*/
|
|
362
362
|
async function read(path) {
|
|
363
363
|
if (runtime.isBun) return Bun.file(path).text();
|
|
364
|
-
return readFile(path, { encoding: "utf8" });
|
|
364
|
+
return new Promise((resolvePromise, reject) => readFile(path, { encoding: "utf8" }, (error, data) => error ? reject(error) : resolvePromise(data)));
|
|
365
365
|
}
|
|
366
|
+
// ../../internals/utils/src/promise.ts
|
|
367
|
+
/**
|
|
368
|
+
* Returns the value cached under `key` in `store`, computing and storing it on the first call.
|
|
369
|
+
* Pass a `WeakMap` for object keys, so an entry goes away with its key, or a `Map` for primitive
|
|
370
|
+
* keys. A cached `null` or `undefined` counts as a hit, so `compute` runs once per key.
|
|
371
|
+
*
|
|
372
|
+
* @example
|
|
373
|
+
* `const refs = memo(refCache, node, () => collectRefs(node))`
|
|
374
|
+
*/
|
|
375
|
+
function memo(store, key, compute) {
|
|
376
|
+
const cached = store.get(key);
|
|
377
|
+
if (cached !== void 0 || store.has(key)) return cached;
|
|
378
|
+
const value = compute();
|
|
379
|
+
store.set(key, value);
|
|
380
|
+
return value;
|
|
381
|
+
}
|
|
382
|
+
function createStore({ type }) {
|
|
383
|
+
if (type === "weak") return /* @__PURE__ */ new WeakMap();
|
|
384
|
+
const entry = /* @__PURE__ */ new Map();
|
|
385
|
+
if (type === "map") return entry;
|
|
386
|
+
const set = (key, value) => {
|
|
387
|
+
entry.clear();
|
|
388
|
+
entry.set(key, value);
|
|
389
|
+
};
|
|
390
|
+
const touch = (key) => {
|
|
391
|
+
if (!entry.has(key)) return false;
|
|
392
|
+
set(key, entry.get(key));
|
|
393
|
+
return true;
|
|
394
|
+
};
|
|
395
|
+
return {
|
|
396
|
+
has: touch,
|
|
397
|
+
get: (key) => touch(key) ? entry.get(key) : void 0,
|
|
398
|
+
set
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
memo.create = createStore;
|
|
366
402
|
// src/oas.ts
|
|
367
403
|
/**
|
|
368
404
|
* Returns `true` when a schema should be treated as nullable.
|
|
@@ -608,6 +644,11 @@ const NAMED_ENTRY_KEYS = /* @__PURE__ */ new Set([
|
|
|
608
644
|
"schemas",
|
|
609
645
|
"dependentSchemas"
|
|
610
646
|
]);
|
|
647
|
+
const TARGET_KEYS = [
|
|
648
|
+
"$id",
|
|
649
|
+
"$anchor",
|
|
650
|
+
"$dynamicAnchor"
|
|
651
|
+
];
|
|
611
652
|
function walk(node, pointer, visit, parentKey) {
|
|
612
653
|
if (!node || typeof node !== "object") return;
|
|
613
654
|
if (!Array.isArray(node)) visit(node, pointer);
|
|
@@ -617,6 +658,37 @@ function walk(node, pointer, visit, parentKey) {
|
|
|
617
658
|
walk(value, `${pointer}/${escapePointerToken(key)}`, visit, Array.isArray(node) ? parentKey : key);
|
|
618
659
|
}
|
|
619
660
|
}
|
|
661
|
+
/** Answers, in one walk and without building JSON pointers, what the loading steps after it need to know. */
|
|
662
|
+
function scanDocument(document) {
|
|
663
|
+
const scan = {
|
|
664
|
+
externalRef: false,
|
|
665
|
+
schemaIds: false,
|
|
666
|
+
pathRefs: false
|
|
667
|
+
};
|
|
668
|
+
const visit = ({ node, readsIds, parentKey }) => {
|
|
669
|
+
if (!node || typeof node !== "object") return;
|
|
670
|
+
const isArray = Array.isArray(node);
|
|
671
|
+
const record = node;
|
|
672
|
+
const ref = record["$ref"];
|
|
673
|
+
if (typeof ref === "string" && !ref.startsWith("#")) scan.externalRef = true;
|
|
674
|
+
if (typeof ref === "string" && ref.startsWith("#/paths/")) scan.pathRefs = true;
|
|
675
|
+
if (readsIds && !isArray && TARGET_KEYS.some((key) => typeof record[key] === "string")) scan.schemaIds = true;
|
|
676
|
+
const named = parentKey !== void 0 && NAMED_ENTRY_KEYS.has(parentKey);
|
|
677
|
+
for (const key in record) {
|
|
678
|
+
const childReadsIds = readsIds && (named || !(DATA_KEYS.has(key) || key.startsWith("x-")));
|
|
679
|
+
visit({
|
|
680
|
+
node: record[key],
|
|
681
|
+
readsIds: childReadsIds,
|
|
682
|
+
parentKey: isArray ? parentKey : key
|
|
683
|
+
});
|
|
684
|
+
}
|
|
685
|
+
};
|
|
686
|
+
visit({
|
|
687
|
+
node: document,
|
|
688
|
+
readsIds: true
|
|
689
|
+
});
|
|
690
|
+
return scan;
|
|
691
|
+
}
|
|
620
692
|
/**
|
|
621
693
|
* The declaration in the nearest enclosing schema, else the only one, else `undefined`.
|
|
622
694
|
*/
|
|
@@ -651,6 +723,182 @@ function resolveSchemaIds(document) {
|
|
|
651
723
|
}
|
|
652
724
|
});
|
|
653
725
|
}
|
|
726
|
+
// src/sourceLocator.ts
|
|
727
|
+
const flowGap = /(?:[\s,]|#[^\n]*)*/y;
|
|
728
|
+
const indexes = memo.create({ type: "last" });
|
|
729
|
+
function toKeyText(text) {
|
|
730
|
+
if (!/^[-+.~\d]|^(?:null|true|false)$/i.test(text)) return text;
|
|
731
|
+
try {
|
|
732
|
+
const value = load(text, { schema: CORE_SCHEMA });
|
|
733
|
+
return value === null || typeof value === "number" || typeof value === "boolean" ? String(value) : text;
|
|
734
|
+
} catch {
|
|
735
|
+
return text;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
function indexEvents(events) {
|
|
739
|
+
const types = new Uint8Array(events.length);
|
|
740
|
+
const styles = new Uint8Array(events.length);
|
|
741
|
+
const starts = new Int32Array(events.length);
|
|
742
|
+
const ends = new Int32Array(events.length);
|
|
743
|
+
for (let at = 0, last = 0; at < events.length; last = Math.max(last, starts[at], ends[at]), at++) {
|
|
744
|
+
const event = events[at];
|
|
745
|
+
types[at] = event.type;
|
|
746
|
+
if (event.type === EVENT_ID.SCALAR) {
|
|
747
|
+
const quote = event.style === SCALAR_STYLE.SINGLE_QUOTED || event.style === SCALAR_STYLE.DOUBLE_QUOTED ? 1 : 0;
|
|
748
|
+
const empty = event.valueStart < 0;
|
|
749
|
+
styles[at] = event.style;
|
|
750
|
+
starts[at] = empty ? last : event.valueStart - quote;
|
|
751
|
+
ends[at] = empty ? last : event.valueEnd + quote;
|
|
752
|
+
}
|
|
753
|
+
if (event.type === EVENT_ID.ALIAS) {
|
|
754
|
+
starts[at] = event.anchorStart - 1;
|
|
755
|
+
ends[at] = event.anchorEnd;
|
|
756
|
+
}
|
|
757
|
+
if (event.type === EVENT_ID.MAPPING || event.type === EVENT_ID.SEQUENCE) {
|
|
758
|
+
styles[at] = event.style;
|
|
759
|
+
starts[at] = event.start;
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
return {
|
|
763
|
+
types,
|
|
764
|
+
styles,
|
|
765
|
+
starts,
|
|
766
|
+
ends,
|
|
767
|
+
collections: /* @__PURE__ */ new Map()
|
|
768
|
+
};
|
|
769
|
+
}
|
|
770
|
+
function createLocator({ index, source }) {
|
|
771
|
+
const { types, styles, starts, ends, collections } = index;
|
|
772
|
+
const isCollection = (at) => types[at] === EVENT_ID.MAPPING || types[at] === EVENT_ID.SEQUENCE;
|
|
773
|
+
const readText = (at) => {
|
|
774
|
+
const text = source.slice(starts[at], ends[at]);
|
|
775
|
+
if (styles[at] === SCALAR_STYLE.SINGLE_QUOTED) return text.slice(1, -1).replaceAll("''", "'");
|
|
776
|
+
if (styles[at] !== SCALAR_STYLE.DOUBLE_QUOTED) return text;
|
|
777
|
+
try {
|
|
778
|
+
return JSON.parse(text);
|
|
779
|
+
} catch {
|
|
780
|
+
return String(load(text, { schema: CORE_SCHEMA }));
|
|
781
|
+
}
|
|
782
|
+
};
|
|
783
|
+
const readNode = (at) => {
|
|
784
|
+
if (!isCollection(at)) return {
|
|
785
|
+
start: starts[at],
|
|
786
|
+
end: ends[at],
|
|
787
|
+
next: at + 1
|
|
788
|
+
};
|
|
789
|
+
let next = at + 1;
|
|
790
|
+
let end = starts[at];
|
|
791
|
+
while (types[next] !== EVENT_ID.POP) ({end, next} = readNode(next));
|
|
792
|
+
if (styles[at] === COLLECTION_STYLE.FLOW) {
|
|
793
|
+
flowGap.lastIndex = end;
|
|
794
|
+
flowGap.exec(source);
|
|
795
|
+
end = flowGap.lastIndex + 1;
|
|
796
|
+
}
|
|
797
|
+
return {
|
|
798
|
+
start: starts[at],
|
|
799
|
+
end,
|
|
800
|
+
next: next + 1
|
|
801
|
+
};
|
|
802
|
+
};
|
|
803
|
+
const readChildren = (collection) => {
|
|
804
|
+
const keys = /* @__PURE__ */ new Map();
|
|
805
|
+
const items = [];
|
|
806
|
+
const isMapping = types[collection] === EVENT_ID.MAPPING;
|
|
807
|
+
for (let at = collection + 1; types[at] !== EVENT_ID.POP;) {
|
|
808
|
+
const key = isMapping ? at : void 0;
|
|
809
|
+
if (key !== void 0) at = readNode(at).next;
|
|
810
|
+
const { start, end, next } = readNode(at);
|
|
811
|
+
if (key === void 0) items.push({
|
|
812
|
+
index: at,
|
|
813
|
+
range: {
|
|
814
|
+
start,
|
|
815
|
+
end
|
|
816
|
+
}
|
|
817
|
+
});
|
|
818
|
+
const raw = key !== void 0 && types[key] === EVENT_ID.SCALAR ? readText(key) : void 0;
|
|
819
|
+
const text = raw !== void 0 && styles[key] === SCALAR_STYLE.PLAIN ? toKeyText(raw) : raw;
|
|
820
|
+
if (text !== void 0 && !keys.has(text)) keys.set(text, {
|
|
821
|
+
index: at,
|
|
822
|
+
range: {
|
|
823
|
+
start: starts[key],
|
|
824
|
+
end: ends[key]
|
|
825
|
+
}
|
|
826
|
+
});
|
|
827
|
+
at = next;
|
|
828
|
+
}
|
|
829
|
+
return {
|
|
830
|
+
keys,
|
|
831
|
+
items
|
|
832
|
+
};
|
|
833
|
+
};
|
|
834
|
+
const walk = ({ at, tokens: [token, ...rest], range }) => {
|
|
835
|
+
if (token === void 0) return range;
|
|
836
|
+
if (!isCollection(at)) return void 0;
|
|
837
|
+
const { keys, items } = memo(collections, at, () => readChildren(at));
|
|
838
|
+
const child = keys.get(token) ?? items[Number(token)];
|
|
839
|
+
return child ? walk({
|
|
840
|
+
at: child.index,
|
|
841
|
+
tokens: rest,
|
|
842
|
+
range: child.range
|
|
843
|
+
}) : void 0;
|
|
844
|
+
};
|
|
845
|
+
return {
|
|
846
|
+
find(pointer) {
|
|
847
|
+
return walk({
|
|
848
|
+
at: 1,
|
|
849
|
+
tokens: pointer.replace(/^#/, "").split("/").slice(1).map(unescapePointerToken),
|
|
850
|
+
range: void 0
|
|
851
|
+
});
|
|
852
|
+
},
|
|
853
|
+
findRef(ref) {
|
|
854
|
+
if (!index.refs) {
|
|
855
|
+
index.refs = /* @__PURE__ */ new Map();
|
|
856
|
+
const frames = [];
|
|
857
|
+
for (let at = 1; at + 1 < types.length; at++) {
|
|
858
|
+
if (types[at] === EVENT_ID.POP) {
|
|
859
|
+
frames.pop();
|
|
860
|
+
continue;
|
|
861
|
+
}
|
|
862
|
+
const top = frames.length - 1;
|
|
863
|
+
const isKey = frames[top] === 0;
|
|
864
|
+
if (frames[top] >= 0) frames[top] = 1 - frames[top];
|
|
865
|
+
if (isCollection(at)) frames.push(types[at] === EVENT_ID.MAPPING ? 0 : -1);
|
|
866
|
+
if (!isKey || types[at] !== EVENT_ID.SCALAR || types[at + 1] !== EVENT_ID.SCALAR || readText(at) !== "$ref") continue;
|
|
867
|
+
const text = readText(at + 1);
|
|
868
|
+
if (!index.refs.has(text)) index.refs.set(text, {
|
|
869
|
+
start: starts[at + 1],
|
|
870
|
+
end: ends[at + 1]
|
|
871
|
+
});
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
return index.refs.get(ref);
|
|
875
|
+
}
|
|
876
|
+
};
|
|
877
|
+
}
|
|
878
|
+
function parse(source) {
|
|
879
|
+
try {
|
|
880
|
+
return indexEvents(parseEvents(source, { maxDepth: Number.POSITIVE_INFINITY }));
|
|
881
|
+
} catch {
|
|
882
|
+
return;
|
|
883
|
+
}
|
|
884
|
+
}
|
|
885
|
+
function rememberEvents({ source, events }) {
|
|
886
|
+
indexes.set(source, indexEvents(events));
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* Maps OpenAPI JSON pointers and unresolved references to ranges in YAML or JSON source.
|
|
890
|
+
*/
|
|
891
|
+
const locate = (source, location) => {
|
|
892
|
+
if (!("pointer" in location)) return void 0;
|
|
893
|
+
const index = memo(indexes, source, () => parse(source));
|
|
894
|
+
if (!index) return void 0;
|
|
895
|
+
const { find, findRef } = createLocator({
|
|
896
|
+
index,
|
|
897
|
+
source
|
|
898
|
+
});
|
|
899
|
+
const ref = "ref" in location ? location.ref : void 0;
|
|
900
|
+
return find(location.pointer) ?? (ref ? findRef(ref) : void 0);
|
|
901
|
+
};
|
|
654
902
|
// src/load/source.ts
|
|
655
903
|
const urlRegExp = /^https?:\/+/i;
|
|
656
904
|
/**
|
|
@@ -705,14 +953,15 @@ async function readSource(sourcePath) {
|
|
|
705
953
|
* Reads and parses one source file or URL referenced during bundling: YAML/JSON is parsed into an
|
|
706
954
|
* object, Markdown is returned as-is (bundled inline rather than dereferenced).
|
|
707
955
|
*/
|
|
708
|
-
async function resolveSource(sourcePath) {
|
|
956
|
+
async function resolveSource(sourcePath, { remember } = {}) {
|
|
709
957
|
const data = await readSource(sourcePath);
|
|
710
958
|
if (sourcePath.toLowerCase().endsWith(".md")) return data;
|
|
711
959
|
const pathname = urlRegExp.test(sourcePath) ? new URL(sourcePath).pathname : sourcePath;
|
|
712
960
|
return parseYaml(data, {
|
|
713
961
|
origin: sourcePath,
|
|
714
962
|
json: !/\.ya?ml$/i.test(pathname),
|
|
715
|
-
help: "Fix the syntax at the reported line, or check that `input` points at the spec and not an HTML page."
|
|
963
|
+
help: "Fix the syntax at the reported line, or check that `input` points at the spec and not an HTML page.",
|
|
964
|
+
remember
|
|
716
965
|
});
|
|
717
966
|
}
|
|
718
967
|
/**
|
|
@@ -728,13 +977,23 @@ async function resolveSource(sourcePath) {
|
|
|
728
977
|
* keeping the last one. Its core schema keeps timestamps as strings and `<<` as a plain key, as
|
|
729
978
|
* `yaml` does for YAML 1.2.
|
|
730
979
|
*/
|
|
731
|
-
function parseYaml(data, { origin, help, json = true }) {
|
|
980
|
+
function parseYaml(data, { origin, help, json = true, remember = false }) {
|
|
732
981
|
if (json && /^\s*[[{]/.test(data)) try {
|
|
733
982
|
return JSON.parse(data);
|
|
734
983
|
} catch {}
|
|
735
984
|
try {
|
|
736
|
-
const
|
|
737
|
-
|
|
985
|
+
const events = parseEvents(data, {});
|
|
986
|
+
const documents = constructFromEvents(events, {
|
|
987
|
+
source: data,
|
|
988
|
+
schema: CORE_SCHEMA
|
|
989
|
+
});
|
|
990
|
+
if (documents.length === 1 && documents[0] !== void 0) {
|
|
991
|
+
if (remember) rememberEvents({
|
|
992
|
+
source: data,
|
|
993
|
+
events
|
|
994
|
+
});
|
|
995
|
+
return documents[0];
|
|
996
|
+
}
|
|
738
997
|
} catch {}
|
|
739
998
|
try {
|
|
740
999
|
return parse$1(data);
|
|
@@ -766,18 +1025,15 @@ async function assertInputExists(input) {
|
|
|
766
1025
|
});
|
|
767
1026
|
}
|
|
768
1027
|
// src/load/normalize.ts
|
|
1028
|
+
const scans = /* @__PURE__ */ new WeakMap();
|
|
769
1029
|
/**
|
|
770
|
-
*
|
|
771
|
-
* absolute path, or URL). An internal `#/...` fragment does not count.
|
|
772
|
-
*
|
|
773
|
-
* `Object.values` reads array elements and object property values alike, so the same recursion
|
|
774
|
-
* walks both without a separate array branch.
|
|
1030
|
+
* Rewrites `$id` and `$anchor` refs in place, then returns the scan of the result.
|
|
775
1031
|
*/
|
|
776
|
-
function
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
return
|
|
1032
|
+
function resolveTargets(document) {
|
|
1033
|
+
const scan = scanDocument(document);
|
|
1034
|
+
if (!scan.schemaIds) return scan;
|
|
1035
|
+
resolveSchemaIds(document);
|
|
1036
|
+
return scanDocument(document);
|
|
781
1037
|
}
|
|
782
1038
|
/**
|
|
783
1039
|
* Replaces each `$ref` into `#/paths/...` with a copy of its target, in place.
|
|
@@ -869,13 +1125,16 @@ async function bundleDocument(pathOrUrl, { document, sources = [] } = {}) {
|
|
|
869
1125
|
const cached = cache.get(key);
|
|
870
1126
|
if (cached) return cached;
|
|
871
1127
|
sources.push(key);
|
|
872
|
-
const result = resolveSource(sourcePath);
|
|
1128
|
+
const result = resolveSource(sourcePath, { remember: key === toKey(pathOrUrl) });
|
|
873
1129
|
cache.set(key, result);
|
|
874
1130
|
return result;
|
|
875
1131
|
};
|
|
876
1132
|
const root = await resolver(pathOrUrl);
|
|
877
|
-
|
|
878
|
-
if (typeof root === "object" && root !== null && !
|
|
1133
|
+
const scan = resolveTargets(root);
|
|
1134
|
+
if (typeof root === "object" && root !== null && !scan.externalRef) {
|
|
1135
|
+
scans.set(root, scan);
|
|
1136
|
+
return root;
|
|
1137
|
+
}
|
|
879
1138
|
entry = getBaseUri(pathOrUrl, root);
|
|
880
1139
|
entryDocument = root;
|
|
881
1140
|
return await bundle(entry, resolver, { hooks: { onError: (message, { path, state }) => {
|
|
@@ -918,11 +1177,13 @@ async function parseFromConfig(source, sources = []) {
|
|
|
918
1177
|
origin: "the inline `input` string",
|
|
919
1178
|
help: "Fix the syntax at the reported line. Kubb reads `input` as an inline spec, not a file path, because it starts with `{`, `[`, `openapi:`, or `swagger:`, or spans several lines."
|
|
920
1179
|
}) : structuredClone(source.data);
|
|
921
|
-
|
|
1180
|
+
const isObject = Boolean(data) && typeof data === "object";
|
|
1181
|
+
if (source.root && isObject) return bundleDocument(join(source.root, inlineInputName), {
|
|
922
1182
|
document: data,
|
|
923
1183
|
sources
|
|
924
1184
|
});
|
|
925
|
-
|
|
1185
|
+
const scan = resolveTargets(data);
|
|
1186
|
+
if (isObject) scans.set(data, scan);
|
|
926
1187
|
return data;
|
|
927
1188
|
}
|
|
928
1189
|
await assertInputExists(source.path);
|
|
@@ -935,7 +1196,7 @@ async function parseFromConfig(source, sources = []) {
|
|
|
935
1196
|
if (typeof version === "number") document[key] = Number.isInteger(version) ? version.toFixed(1) : String(version);
|
|
936
1197
|
}
|
|
937
1198
|
}
|
|
938
|
-
if (raw && typeof raw === "object" && "swagger" in raw) {
|
|
1199
|
+
if (raw && typeof raw === "object" && "swagger" in raw && (scans.get(raw)?.pathRefs ?? true)) {
|
|
939
1200
|
const refs = createRefs(raw);
|
|
940
1201
|
inlinePathRefs(raw, (ref) => refs.resolve(ref, { report: false }));
|
|
941
1202
|
}
|
|
@@ -3602,162 +3863,6 @@ function refPromotedEnums(node, promoted) {
|
|
|
3602
3863
|
});
|
|
3603
3864
|
} });
|
|
3604
3865
|
}
|
|
3605
|
-
// src/sourceLocator.ts
|
|
3606
|
-
let cached;
|
|
3607
|
-
const toRange = ([start, end]) => ({
|
|
3608
|
-
start,
|
|
3609
|
-
end
|
|
3610
|
-
});
|
|
3611
|
-
/** Walks a JSON pointer through maps and sequences; the range is the last key or item it reached. */
|
|
3612
|
-
function findPointer(document, pointer) {
|
|
3613
|
-
return pointer.replace(/^#/, "").split("/").slice(1).map(unescapePointerToken).reduce((at, token) => {
|
|
3614
|
-
if (yaml.isMap(at?.node)) {
|
|
3615
|
-
const pair = at.node.items.find((item) => yaml.isScalar(item.key) && String(item.key.value) === token);
|
|
3616
|
-
return pair && yaml.isScalar(pair.key) && pair.key.range ? {
|
|
3617
|
-
node: pair.value,
|
|
3618
|
-
range: toRange(pair.key.range)
|
|
3619
|
-
} : void 0;
|
|
3620
|
-
}
|
|
3621
|
-
if (yaml.isSeq(at?.node)) {
|
|
3622
|
-
const item = at.node.items[Number(token)];
|
|
3623
|
-
return yaml.isNode(item) && item.range ? {
|
|
3624
|
-
node: item,
|
|
3625
|
-
range: toRange(item.range)
|
|
3626
|
-
} : void 0;
|
|
3627
|
-
}
|
|
3628
|
-
}, {
|
|
3629
|
-
node: document.contents,
|
|
3630
|
-
range: void 0
|
|
3631
|
-
})?.range;
|
|
3632
|
-
}
|
|
3633
|
-
/** The first `$ref` whose value is `ref`. */
|
|
3634
|
-
function findRefUsage(document, ref) {
|
|
3635
|
-
const found = [];
|
|
3636
|
-
yaml.visit(document, { Pair(_, { key, value }) {
|
|
3637
|
-
if (!yaml.isScalar(key) || key.value !== "$ref" || !yaml.isScalar(value) || value.value !== ref || !value.range) return;
|
|
3638
|
-
found.push(toRange(value.range));
|
|
3639
|
-
return yaml.visit.BREAK;
|
|
3640
|
-
} });
|
|
3641
|
-
return found[0];
|
|
3642
|
-
}
|
|
3643
|
-
/**
|
|
3644
|
-
* One pass over JSON text, so a located diagnostic in a large JSON spec skips `yaml.parseDocument`.
|
|
3645
|
-
* Holds the range of every key and array item by pointer, and the first usage of every `$ref`, as
|
|
3646
|
-
* `findPointer` and `findRefUsage` return them. Throws where the text breaks JSON syntax, such as a
|
|
3647
|
-
* comment, an unquoted key, or an unquoted `$ref` value. Other bare values, such as `object`, pass.
|
|
3648
|
-
*/
|
|
3649
|
-
function indexJson(source) {
|
|
3650
|
-
const pointers = /* @__PURE__ */ new Map();
|
|
3651
|
-
const refs = /* @__PURE__ */ new Map();
|
|
3652
|
-
let at = source.indexOf("{");
|
|
3653
|
-
const fail = () => {
|
|
3654
|
-
throw new SyntaxError(`Unexpected character at ${at}`);
|
|
3655
|
-
};
|
|
3656
|
-
const skipSpace = () => {
|
|
3657
|
-
for (let code = source.charCodeAt(at); code === 32 || code === 9 || code === 10 || code === 13; code = source.charCodeAt(++at));
|
|
3658
|
-
};
|
|
3659
|
-
const readString = () => {
|
|
3660
|
-
if (source[at] !== "\"") fail();
|
|
3661
|
-
const start = at;
|
|
3662
|
-
for (at++; source.charCodeAt(at) !== 34; at++) {
|
|
3663
|
-
if (at >= source.length) fail();
|
|
3664
|
-
if (source.charCodeAt(at) === 92) at++;
|
|
3665
|
-
}
|
|
3666
|
-
const raw = source.slice(start + 1, at++);
|
|
3667
|
-
return {
|
|
3668
|
-
text: raw.includes("\\") ? JSON.parse(source.slice(start, at)) : raw,
|
|
3669
|
-
start,
|
|
3670
|
-
end: at
|
|
3671
|
-
};
|
|
3672
|
-
};
|
|
3673
|
-
const expect = (char) => {
|
|
3674
|
-
skipSpace();
|
|
3675
|
-
if (source[at++] !== char) fail();
|
|
3676
|
-
skipSpace();
|
|
3677
|
-
};
|
|
3678
|
-
const walk = (pointer) => {
|
|
3679
|
-
const open = source[at];
|
|
3680
|
-
if (open === "\"") {
|
|
3681
|
-
readString();
|
|
3682
|
-
return;
|
|
3683
|
-
}
|
|
3684
|
-
if (open !== "{" && open !== "[") {
|
|
3685
|
-
const start = at;
|
|
3686
|
-
while (at < source.length && !/[\s,\]}]/.test(source[at])) at++;
|
|
3687
|
-
if (at === start) fail();
|
|
3688
|
-
return;
|
|
3689
|
-
}
|
|
3690
|
-
const close = open === "{" ? "}" : "]";
|
|
3691
|
-
at++;
|
|
3692
|
-
for (let index = 0;; index++) {
|
|
3693
|
-
skipSpace();
|
|
3694
|
-
if (source[at] === close) break;
|
|
3695
|
-
if (index > 0) expect(",");
|
|
3696
|
-
if (open === "[") {
|
|
3697
|
-
const start = at;
|
|
3698
|
-
const next = `${pointer}/${index}`;
|
|
3699
|
-
walk(next);
|
|
3700
|
-
if (!pointers.has(next)) pointers.set(next, {
|
|
3701
|
-
start,
|
|
3702
|
-
end: at
|
|
3703
|
-
});
|
|
3704
|
-
continue;
|
|
3705
|
-
}
|
|
3706
|
-
const key = readString();
|
|
3707
|
-
const next = `${pointer}/${escapePointerToken(key.text)}`;
|
|
3708
|
-
if (!pointers.has(next)) pointers.set(next, {
|
|
3709
|
-
start: key.start,
|
|
3710
|
-
end: key.end
|
|
3711
|
-
});
|
|
3712
|
-
expect(":");
|
|
3713
|
-
if (key.text !== "$ref" || source[at] === "{" || source[at] === "[") {
|
|
3714
|
-
walk(next);
|
|
3715
|
-
continue;
|
|
3716
|
-
}
|
|
3717
|
-
const value = readString();
|
|
3718
|
-
if (!refs.has(value.text)) refs.set(value.text, {
|
|
3719
|
-
start: value.start,
|
|
3720
|
-
end: value.end
|
|
3721
|
-
});
|
|
3722
|
-
}
|
|
3723
|
-
at++;
|
|
3724
|
-
};
|
|
3725
|
-
walk("#");
|
|
3726
|
-
return {
|
|
3727
|
-
pointers,
|
|
3728
|
-
refs
|
|
3729
|
-
};
|
|
3730
|
-
}
|
|
3731
|
-
/** Indexes plain JSON text; anything else goes to the YAML parser. */
|
|
3732
|
-
function parse(source) {
|
|
3733
|
-
if (/^[ \t\n\r]*\{/.test(source)) try {
|
|
3734
|
-
return {
|
|
3735
|
-
source,
|
|
3736
|
-
json: indexJson(source)
|
|
3737
|
-
};
|
|
3738
|
-
} catch {}
|
|
3739
|
-
return {
|
|
3740
|
-
source,
|
|
3741
|
-
document: yaml.parseDocument(source)
|
|
3742
|
-
};
|
|
3743
|
-
}
|
|
3744
|
-
/**
|
|
3745
|
-
* Maps OpenAPI JSON pointers and unresolved references to ranges in YAML or JSON source.
|
|
3746
|
-
*/
|
|
3747
|
-
const locate = (source, location) => {
|
|
3748
|
-
if (!("pointer" in location)) return void 0;
|
|
3749
|
-
cached = cached?.source === source ? {
|
|
3750
|
-
...cached,
|
|
3751
|
-
source
|
|
3752
|
-
} : parse(source);
|
|
3753
|
-
const ref = "ref" in location ? location.ref : void 0;
|
|
3754
|
-
if ("json" in cached) {
|
|
3755
|
-
const { pointers, refs } = cached.json;
|
|
3756
|
-
return pointers.get(location.pointer) ?? (ref ? refs.get(ref) : void 0);
|
|
3757
|
-
}
|
|
3758
|
-
const { document } = cached;
|
|
3759
|
-
return findPointer(document, location.pointer) ?? (ref ? findRefUsage(document, ref) : void 0);
|
|
3760
|
-
};
|
|
3761
3866
|
// src/adapter.ts
|
|
3762
3867
|
/**
|
|
3763
3868
|
* The `name` of `@kubb/adapter-oas`, used to identify this adapter in a Kubb config.
|