@scalar/cli 2.15.0 → 2.18.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/README.md +121 -25
- package/chunks/{action-3W4IC7QL.js → action-2WVJ2WEW.js} +26 -27
- package/chunks/{action-HOCCP353.js → action-4BDZ2CXP.js} +12 -13
- package/chunks/{action-3QQSKGM3.js → action-5AXDB5VN.js} +21 -23
- package/chunks/{action-LD5B5QQ4.js → action-6I3TJLRO.js} +34 -33
- package/chunks/{action-T7SD3ETN.js → action-6WZN63LT.js} +299 -75
- package/chunks/{action-IUAYC2SC.js → action-7GZGVTYI.js} +1071 -775
- package/chunks/{action-2BGAMGXU.js → action-7ZCJAWI2.js} +9 -9
- package/chunks/{action-6HFH7DAW.js → action-A6YOY24G.js} +14 -10
- package/chunks/{action-IU2AVNOW.js → action-AGGHY4N2.js} +26 -26
- package/chunks/{action-B2WWTOF6.js → action-BVZOA2G5.js} +15 -16
- package/chunks/{action-2UZWKMSJ.js → action-BYDJ4R4F.js} +18 -19
- package/chunks/{action-HDT7EO7W.js → action-CBAL5CYB.js} +19 -21
- package/chunks/action-COFGCCYS.js +89 -0
- package/chunks/action-CYU5Q5LT.js +254 -0
- package/chunks/{action-MJHLG7AE.js → action-DTQH6CKR.js} +33 -33
- package/chunks/{action-P2RPH5R2.js → action-ED5WLLIE.js} +11 -12
- package/chunks/action-F4CQ4DW3.js +654 -0
- package/chunks/{action-SRFBOLDL.js → action-GQDQU2WE.js} +1 -1
- package/chunks/{action-PDSSIW2A.js → action-GVW5PSPT.js} +26 -28
- package/chunks/{action-WOLNPEUQ.js → action-H5VRLFCY.js} +16 -12
- package/chunks/{action-G2WBTXOK.js → action-IWEAWDT3.js} +19 -19
- package/chunks/{action-NZJ4Z64U.js → action-JN6JVQUV.js} +16 -18
- package/chunks/{action-PNSWHIHC.js → action-KVM76ZKQ.js} +20 -20
- package/chunks/{action-JNH4DCZX.js → action-LLMFEBHH.js} +3 -4
- package/chunks/{action-QJMRAFOF.js → action-PEG7AUA6.js} +20 -19
- package/chunks/{action-2HPMP473.js → action-QC72LLYE.js} +13 -11
- package/chunks/{action-ZTLOQZTT.js → action-QP6MRSQW.js} +16 -15
- package/chunks/{action-4HXB3YQR.js → action-R53K22GE.js} +230 -52
- package/chunks/{action-U2RJ3ZAU.js → action-SJPWL723.js} +6 -5
- package/chunks/{action-57SV3ZBM.js → action-T3R4SDAI.js} +9 -9
- package/chunks/{action-777R7LOG.js → action-TT7ZJNST.js} +25 -25
- package/chunks/{action-TY4BZEMN.js → action-UPVVJE26.js} +18 -19
- package/chunks/{action-MBAAYXKK.js → action-V35W4MRK.js} +18 -19
- package/chunks/{action-TR2T2UYB.js → action-W4KGRBRV.js} +9 -9
- package/chunks/{action-ENDUKGWS.js → action-ZIVYJCC2.js} +16 -18
- package/chunks/{action-7LK4RUDP.js → action-ZU5X2KQX.js} +52 -49
- package/chunks/{actions-D5OHPBWC.js → actions-CERT7BL4.js} +25 -26
- package/chunks/{actions-RI42JY6H.js → actions-RCIS37Y6.js} +22 -24
- package/chunks/{agent-NA3QFKJX.js → agent-CT3RRYPJ.js} +7 -7
- package/chunks/{asyncapi-YBTWMTW6.js → asyncapi-DVBS23QW.js} +17 -18
- package/chunks/{build.action-4YDNDBXC.js → build.action-7MKMMV6V.js} +38 -32
- package/chunks/{chunk-MGGMCT77.js → chunk-36UI2W7I.js} +1 -1
- package/chunks/chunk-4BMOCIYO.js +536 -0
- package/chunks/{chunk-ADQ6B6QI.js → chunk-5DRLDTTU.js} +1 -1
- package/chunks/{chunk-G5WWW7VD.js → chunk-5EO2CVAR.js} +6 -6
- package/chunks/{chunk-Q26DFKTY.js → chunk-5GZAGHKL.js} +1 -1
- package/chunks/{chunk-DWJMPHLY.js → chunk-7LUXRNKV.js} +27 -2
- package/chunks/{chunk-BY4NGQ5D.js → chunk-A2MPF6GE.js} +7 -3
- package/chunks/{chunk-5D3T63G4.js → chunk-BIQFYZP6.js} +1 -1
- package/chunks/chunk-CNJ4TLU7.js +83 -0
- package/chunks/{chunk-UM3WZUGF.js → chunk-CWCRBWA6.js} +7 -3
- package/chunks/{chunk-Z6HOHGUJ.js → chunk-CYIGEQOJ.js} +2 -2
- package/chunks/{chunk-4S3DGMMZ.js → chunk-DUIMDHQQ.js} +1 -1
- package/chunks/{chunk-W7P6VTCG.js → chunk-DVA4NCCY.js} +6 -4
- package/chunks/chunk-DWWY3LPC.js +85 -0
- package/chunks/{chunk-USVU6AC2.js → chunk-F6E7FJEC.js} +18 -12
- package/chunks/{chunk-E24BNP6E.js → chunk-FDW4LC45.js} +1 -1
- package/chunks/{chunk-TW2UASEW.js → chunk-FMPJSW6A.js} +4 -4
- package/chunks/{chunk-IPWJEGPD.js → chunk-GCVB22TG.js} +2 -2
- package/chunks/{chunk-6BQHLHBE.js → chunk-GXKNYM54.js} +3 -3
- package/chunks/chunk-I25ZCHBJ.js +22 -0
- package/chunks/{chunk-I57ID5K7.js → chunk-JBMHCTZD.js} +8 -1
- package/chunks/{chunk-KOLEQNLG.js → chunk-JEOQ7LSA.js} +1 -1
- package/chunks/{chunk-QMERQSPM.js → chunk-KI2AVDKM.js} +8 -6
- package/chunks/{chunk-2KSOARE4.js → chunk-KSY2GH53.js} +3 -3
- package/chunks/{chunk-SHRUJFQI.js → chunk-KWQVA66H.js} +105 -18
- package/chunks/{chunk-DLYMDGFW.js → chunk-LPWRG2MS.js} +4 -4
- package/chunks/{chunk-Y6GKZ2FM.js → chunk-LVUMNGVA.js} +1 -1
- package/chunks/{chunk-PIDLENYN.js → chunk-M34HMQMV.js} +42 -83
- package/chunks/{chunk-7FS7BAUX.js → chunk-MRZ5A7LB.js} +133 -19
- package/chunks/chunk-O3I3LTTJ.js +24 -0
- package/chunks/{chunk-VBHPHAGS.js → chunk-O4DJIA6Y.js} +2 -18
- package/chunks/chunk-OKLL5422.js +263 -0
- package/chunks/{chunk-HIDIW2FN.js → chunk-OQE3WKI3.js} +1 -1
- package/chunks/{chunk-ERZ3HYB5.js → chunk-PMDHYIB4.js} +9 -21
- package/chunks/{chunk-IDGT5RAQ.js → chunk-RELSIFIL.js} +2 -2
- package/chunks/{chunk-BOCMOHQA.js → chunk-RGJPENX7.js} +1 -1
- package/chunks/{chunk-7FGJIUDN.js → chunk-RHAQ4XBA.js} +3 -3
- package/chunks/chunk-RQ7QBRJT.js +93 -0
- package/chunks/chunk-S3LY7RKP.js +54 -0
- package/chunks/{chunk-T76K7QNX.js → chunk-S5OGPH2P.js} +3 -3
- package/chunks/{chunk-ANP5XF26.js → chunk-U36HEOGO.js} +12625 -1369
- package/chunks/{chunk-IPUJEBMI.js → chunk-UWRJCJVG.js} +1 -1
- package/chunks/chunk-V7DBDXSJ.js +24 -0
- package/chunks/{chunk-RRYUCZYO.js → chunk-WGTZEX4Y.js} +7 -3
- package/chunks/{chunk-7PNOYE6B.js → chunk-Y3CXAIAG.js} +30 -3
- package/chunks/{chunk-GOKHFNB2.js → chunk-Z7EH7CJC.js} +3 -3
- package/chunks/{chunk-42MIV6WF.js → chunk-ZMJW5UWU.js} +6 -5
- package/chunks/chunk-ZQDAPKWX.js +111 -0
- package/chunks/{create.action-52K3XAYN.js → create.action-VA2ISPIM.js} +27 -29
- package/chunks/{delete.action-47LPLTOX.js → delete.action-AIWMDGXV.js} +26 -28
- package/chunks/{delete.action-3NMOQPIR.js → delete.action-GEN6OD7A.js} +26 -28
- package/chunks/{diff.action-5TLC2EHT.js → diff.action-LBK656RN.js} +32 -27
- package/chunks/{get.action-BCMLG7C7.js → get.action-5SWRNQ3D.js} +20 -19
- package/chunks/{get.action-TOZ474GO.js → get.action-UPPPGGI4.js} +29 -23
- package/chunks/{list.action-6ZWEENT6.js → list.action-YJZBC25S.js} +26 -28
- package/chunks/{list.action-CMAA5KTY.js → list.action-YOWWC4I4.js} +54 -25
- package/chunks/{publish.action-U3VIU5LZ.js → publish.action-QMVDHDJZ.js} +52 -49
- package/chunks/{update.action-UQHZYVTP.js → update.action-GFDZIGEP.js} +27 -29
- package/chunks/{update.action-QZOP25ME.js → update.action-SNGKPS7A.js} +26 -28
- package/index.js +688 -212
- package/package.json +11 -11
- package/skills/scalar-cli/SKILL.md +4 -3
- package/template.md +120 -24
- package/chunks/action-3KP3WVPZ.js +0 -112
- package/chunks/action-APBLB3IU.js +0 -235
- package/chunks/chunk-BLY2CF4S.js +0 -58
- package/chunks/chunk-BMQFEKHA.js +0 -3534
- package/chunks/chunk-EOTD7HAV.js +0 -318
- package/chunks/chunk-QXOUDBFK.js +0 -92
- package/chunks/chunk-TR5U7G7T.js +0 -32
|
@@ -1,738 +1,777 @@
|
|
|
1
1
|
import { createRequire as $$cR } from "node:module"; const require = $$cR(import.meta.url);
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
isConfigFileName,
|
|
4
|
+
legacyTo2_0_0,
|
|
5
|
+
parseConfig
|
|
6
|
+
} from "./chunk-M34HMQMV.js";
|
|
7
|
+
import {
|
|
8
|
+
writeFileAtomic
|
|
9
|
+
} from "./chunk-CNJ4TLU7.js";
|
|
5
10
|
import {
|
|
6
11
|
isAsyncApiContent
|
|
7
|
-
} from "./chunk-
|
|
8
|
-
import "./chunk-
|
|
9
|
-
import
|
|
12
|
+
} from "./chunk-CWCRBWA6.js";
|
|
13
|
+
import "./chunk-A2MPF6GE.js";
|
|
14
|
+
import {
|
|
15
|
+
registry
|
|
16
|
+
} from "./chunk-PMDHYIB4.js";
|
|
17
|
+
import "./chunk-O4DJIA6Y.js";
|
|
18
|
+
import "./chunk-KSY2GH53.js";
|
|
19
|
+
import {
|
|
20
|
+
config,
|
|
21
|
+
package_default
|
|
22
|
+
} from "./chunk-7LUXRNKV.js";
|
|
10
23
|
import {
|
|
11
24
|
err,
|
|
12
25
|
ok
|
|
13
|
-
} from "./chunk-
|
|
14
|
-
import "./chunk-
|
|
15
|
-
import
|
|
16
|
-
|
|
26
|
+
} from "./chunk-DUIMDHQQ.js";
|
|
27
|
+
import "./chunk-Y3CXAIAG.js";
|
|
28
|
+
import {
|
|
29
|
+
ConfigVersion
|
|
30
|
+
} from "./chunk-RGJPENX7.js";
|
|
31
|
+
import "./chunk-5GZAGHKL.js";
|
|
32
|
+
import {
|
|
33
|
+
prettifyError
|
|
34
|
+
} from "./chunk-JBMHCTZD.js";
|
|
17
35
|
import "./chunk-3MZYUBJ6.js";
|
|
18
36
|
|
|
19
37
|
// src/domains/document/lint/action.ts
|
|
20
38
|
import { writeFile as writeFile3 } from "node:fs/promises";
|
|
21
|
-
import { resolve as
|
|
39
|
+
import { resolve as resolve7 } from "node:path";
|
|
22
40
|
|
|
23
|
-
//
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
41
|
+
// src/domains/document/lint/baseline.ts
|
|
42
|
+
import { mkdir, readFile, rm } from "node:fs/promises";
|
|
43
|
+
import { dirname, relative, resolve, sep } from "node:path";
|
|
44
|
+
import { setTimeout } from "node:timers/promises";
|
|
45
|
+
import { fileURLToPath } from "node:url";
|
|
46
|
+
import { parse as parse2, stringify } from "yaml";
|
|
28
47
|
|
|
29
|
-
//
|
|
30
|
-
|
|
31
|
-
|
|
48
|
+
// src/domains/document/lint/pointer.ts
|
|
49
|
+
function toPointer(path) {
|
|
50
|
+
return `#${path.map((segment) => `/${String(segment).replaceAll("~", "~0").replaceAll("/", "~1")}`).join("")}`;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/domains/document/lint/rule-mappings.ts
|
|
54
|
+
import { parse } from "yaml";
|
|
55
|
+
var redoclyRuleMappings = {
|
|
56
|
+
"info-contact": { scalar: "info-contact", missingField: "contact" },
|
|
57
|
+
"info-license": { scalar: "info-license", missingField: "license" },
|
|
58
|
+
"operation-description": {
|
|
59
|
+
scalar: "operation-description",
|
|
60
|
+
missingField: "description"
|
|
61
|
+
},
|
|
62
|
+
"operation-operationId": {
|
|
63
|
+
scalar: "operation-operationId",
|
|
64
|
+
missingField: "operationId"
|
|
65
|
+
},
|
|
66
|
+
"operation-operationId-url-safe": {
|
|
67
|
+
scalar: "operation-operationId-valid-in-url"
|
|
68
|
+
},
|
|
69
|
+
"no-path-trailing-slash": { scalar: "path-keys-no-trailing-slash" },
|
|
70
|
+
"path-declaration-must-exist": { scalar: "path-declarations-must-exist" },
|
|
71
|
+
"path-not-include-query": { scalar: "path-not-include-query" },
|
|
72
|
+
"tag-description": { scalar: "tag-description", missingField: "description" },
|
|
73
|
+
"operation-singular-tag": { scalar: "operation-singular-tag" }
|
|
74
|
+
};
|
|
75
|
+
function supportsRedoclyMigration(ruleset) {
|
|
32
76
|
try {
|
|
33
|
-
const
|
|
34
|
-
|
|
77
|
+
const parsed = parse(ruleset);
|
|
78
|
+
if (!isRecord(parsed) || Object.keys(parsed).some((key) => !["extends", "rules"].includes(key)))
|
|
79
|
+
return false;
|
|
80
|
+
const extended = Array.isArray(parsed.extends) ? parsed.extends : [parsed.extends];
|
|
81
|
+
if (extended.length !== 1 || extended[0] !== "spectral:oas") return false;
|
|
82
|
+
if (parsed.rules === void 0) return true;
|
|
83
|
+
if (!isRecord(parsed.rules)) return false;
|
|
84
|
+
const audited = new Set(
|
|
85
|
+
Object.values(redoclyRuleMappings).map((mapping) => mapping.scalar)
|
|
86
|
+
);
|
|
87
|
+
return Object.entries(parsed.rules).every(([rule, value]) => {
|
|
88
|
+
if (!audited.has(rule)) return false;
|
|
89
|
+
const severity = isRecord(value) && Object.keys(value).length === 1 ? value.severity : value;
|
|
90
|
+
return [
|
|
91
|
+
"error",
|
|
92
|
+
"warn",
|
|
93
|
+
"info",
|
|
94
|
+
"hint",
|
|
95
|
+
"off",
|
|
96
|
+
0,
|
|
97
|
+
1,
|
|
98
|
+
2,
|
|
99
|
+
3,
|
|
100
|
+
-1,
|
|
101
|
+
true,
|
|
102
|
+
false
|
|
103
|
+
].some((allowed) => allowed === severity);
|
|
104
|
+
});
|
|
35
105
|
} catch {
|
|
36
106
|
return false;
|
|
37
107
|
}
|
|
38
108
|
}
|
|
39
|
-
function
|
|
40
|
-
return
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
// ../../packages/api-document-lint/dist/run-spectral.js
|
|
44
|
-
import spectralCore from "@stoplight/spectral-core";
|
|
45
|
-
import spectralParsers from "@stoplight/spectral-parsers";
|
|
46
|
-
import { bundleAndLoadRuleset } from "@stoplight/spectral-ruleset-bundler/with-loader";
|
|
47
|
-
import "@stoplight/spectral-rulesets";
|
|
48
|
-
var { Spectral, Document } = spectralCore;
|
|
49
|
-
var { Yaml } = spectralParsers;
|
|
50
|
-
async function runSpectral(input) {
|
|
51
|
-
const rulesetSource = input.ruleset && input.ruleset.trim().length > 0 ? input.ruleset : defaultRulesetFor(input.document);
|
|
52
|
-
const ruleset = await loadRuleset(rulesetSource);
|
|
53
|
-
if (!ruleset.ok) {
|
|
54
|
-
return ruleset;
|
|
55
|
-
}
|
|
56
|
-
const spectral = new Spectral();
|
|
57
|
-
spectral.setRuleset(ruleset.data);
|
|
58
|
-
const diagnostics = await spectral.run(input.source ? new Document(input.document, Yaml, input.source) : input.document).catch((error) => error instanceof Error ? error : new Error(String(error)));
|
|
59
|
-
if (diagnostics instanceof Error) {
|
|
60
|
-
return err(`Spectral failed to lint the document \u2014 ${diagnostics.message}`);
|
|
61
|
-
}
|
|
62
|
-
return ok(diagnostics);
|
|
63
|
-
}
|
|
64
|
-
async function loadRuleset(source) {
|
|
65
|
-
const ruleset = await bundleAndLoadRuleset("/.spectral.yaml", {
|
|
66
|
-
fs: {
|
|
67
|
-
promises: {
|
|
68
|
-
// @ts-expect-error The loader only ever reads our single virtual file.
|
|
69
|
-
async readFile() {
|
|
70
|
-
return source;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
},
|
|
74
|
-
format: "esm"
|
|
75
|
-
}).catch((error) => error instanceof Error ? error : new Error(String(error)));
|
|
76
|
-
if (ruleset instanceof Error) {
|
|
77
|
-
return err(`Failed to load ruleset \u2014 ${ruleset.message}`);
|
|
78
|
-
}
|
|
79
|
-
return ok(ruleset);
|
|
109
|
+
function isRecord(value) {
|
|
110
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
80
111
|
}
|
|
81
112
|
|
|
82
|
-
//
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
import { fileURLToPath } from "node:url";
|
|
88
|
-
import { promisify } from "node:util";
|
|
89
|
-
|
|
90
|
-
// ../../packages/api-document-lint/dist/normalize-diagnostics.js
|
|
91
|
-
var EMPTY_RANGE = {
|
|
92
|
-
start: { line: 0, character: 0 },
|
|
93
|
-
end: { line: 0, character: 0 }
|
|
94
|
-
};
|
|
95
|
-
function normalizeDiagnostics(raw) {
|
|
96
|
-
if (!Array.isArray(raw)) {
|
|
97
|
-
return [];
|
|
98
|
-
}
|
|
99
|
-
return raw.filter(isObject).map((item) => {
|
|
100
|
-
const source = item.source;
|
|
101
|
-
return {
|
|
102
|
-
code: isCode(item.code) ? item.code : "",
|
|
103
|
-
message: typeof item.message === "string" ? item.message : "",
|
|
104
|
-
severity: normalizeSeverity(item.severity),
|
|
105
|
-
path: Array.isArray(item.path) ? item.path.filter(isSegment) : [],
|
|
106
|
-
range: normalizeRange(item.range),
|
|
107
|
-
...typeof source === "string" ? { source } : {},
|
|
108
|
-
...Array.isArray(item.locations) ? {
|
|
109
|
-
locations: item.locations.filter(isLocation).map((location) => ({
|
|
110
|
-
source: location.source,
|
|
111
|
-
path: location.path,
|
|
112
|
-
range: normalizeRange(location.range)
|
|
113
|
-
}))
|
|
114
|
-
} : {},
|
|
115
|
-
...typeof item.documentationUrl === "string" ? { documentationUrl: item.documentationUrl } : {}
|
|
116
|
-
};
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
function isObject(value) {
|
|
120
|
-
return typeof value === "object" && value !== null;
|
|
121
|
-
}
|
|
122
|
-
function isCode(value) {
|
|
123
|
-
return typeof value === "string" || typeof value === "number";
|
|
124
|
-
}
|
|
125
|
-
function isSegment(value) {
|
|
126
|
-
return typeof value === "string" || typeof value === "number";
|
|
127
|
-
}
|
|
128
|
-
function normalizeRange(range) {
|
|
129
|
-
if (!isObject(range)) {
|
|
130
|
-
return EMPTY_RANGE;
|
|
131
|
-
}
|
|
132
|
-
return {
|
|
133
|
-
start: normalizePosition(range.start),
|
|
134
|
-
end: normalizePosition(range.end)
|
|
135
|
-
};
|
|
113
|
+
// src/domains/document/lint/baseline.ts
|
|
114
|
+
function sourceIdentity(source, directory) {
|
|
115
|
+
if (/^https?:\/\//.test(source)) return new URL(source).href;
|
|
116
|
+
const path = source.startsWith("file:") ? fileURLToPath(source) : source;
|
|
117
|
+
return relative(directory, resolve(path)).split(sep).join("/");
|
|
136
118
|
}
|
|
137
|
-
function
|
|
138
|
-
if (!isObject(position)) {
|
|
139
|
-
return { line: 0, character: 0 };
|
|
140
|
-
}
|
|
119
|
+
function findingIdentity(diagnostic, directory) {
|
|
141
120
|
return {
|
|
142
|
-
|
|
143
|
-
|
|
121
|
+
source: sourceIdentity(diagnostic.source ?? "", directory),
|
|
122
|
+
rule: String(diagnostic.code),
|
|
123
|
+
path: diagnostic.resolvedPath ?? diagnostic.path
|
|
144
124
|
};
|
|
145
125
|
}
|
|
146
|
-
function
|
|
147
|
-
return
|
|
126
|
+
function findingKey(finding) {
|
|
127
|
+
return JSON.stringify([finding.source, finding.rule, toPointer(finding.path)]);
|
|
148
128
|
}
|
|
149
|
-
function
|
|
150
|
-
return
|
|
129
|
+
function isRecord2(value) {
|
|
130
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
151
131
|
}
|
|
152
|
-
function
|
|
153
|
-
return
|
|
132
|
+
function isFinding(value) {
|
|
133
|
+
return isRecord2(value) && Object.keys(value).length === 3 && typeof value.source === "string" && typeof value.rule === "string" && Array.isArray(value.path) && value.path.every(
|
|
134
|
+
(segment) => typeof segment === "string" || typeof segment === "number" && Number.isInteger(segment) && segment >= 0
|
|
135
|
+
);
|
|
154
136
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
function resolveVacuumBinary(override) {
|
|
162
|
-
const explicit = override ?? process.env.VACUUM_BINARY;
|
|
163
|
-
if (explicit) {
|
|
164
|
-
return isFile(explicit) ? ok(explicit) : err(`vacuum binary not found at ${explicit}`);
|
|
165
|
-
}
|
|
166
|
-
const packaged = resolvePackagedBinary();
|
|
167
|
-
if (packaged) {
|
|
168
|
-
return ok(packaged);
|
|
169
|
-
}
|
|
170
|
-
return ok("vacuum");
|
|
137
|
+
function isBaseline(value) {
|
|
138
|
+
return isRecord2(value) && Object.keys(value).length === 2 && value.version === 1 && Array.isArray(value.documents) && value.documents.every(
|
|
139
|
+
(document) => isRecord2(document) && Object.keys(document).every(
|
|
140
|
+
(key) => ["source", "findings", "imported"].includes(key)
|
|
141
|
+
) && (document.imported === void 0 || typeof document.imported === "boolean") && typeof document.source === "string" && Array.isArray(document.findings) && document.findings.every(isFinding)
|
|
142
|
+
) && new Set(value.documents.map((document) => document.source)).size === value.documents.length;
|
|
171
143
|
}
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
144
|
+
var OWNERS_PREFIX = "# scalar-owners: ";
|
|
145
|
+
function isIgnoreMap(value) {
|
|
146
|
+
return isRecord2(value) && Object.values(value).every(
|
|
147
|
+
(rules) => isRecord2(rules) && Object.values(rules).every(
|
|
148
|
+
(pointers) => Array.isArray(pointers) && pointers.every(
|
|
149
|
+
(pointer) => typeof pointer === "string" && /^#(?:\/(?:[^~]|~[01])*)*$/.test(pointer)
|
|
150
|
+
)
|
|
151
|
+
)
|
|
152
|
+
);
|
|
181
153
|
}
|
|
182
|
-
function
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
154
|
+
function fromIgnoreMap(mapping, owners, directory) {
|
|
155
|
+
const normalized = /* @__PURE__ */ new Map();
|
|
156
|
+
for (const [input, rules] of Object.entries(mapping)) {
|
|
157
|
+
const source = sourceIdentity(
|
|
158
|
+
/^https?:\/\//.test(input) ? input : resolve(directory, input),
|
|
159
|
+
directory
|
|
160
|
+
);
|
|
161
|
+
const document = normalized.get(source) ?? { source, findings: [] };
|
|
162
|
+
document.findings.push(
|
|
163
|
+
...Object.entries(rules).flatMap(
|
|
164
|
+
([rule, pointers]) => pointers.map((pointer) => ({
|
|
165
|
+
source,
|
|
166
|
+
rule,
|
|
167
|
+
path: pointer === "#" ? [] : pointer.slice(2).split("/").map(
|
|
168
|
+
(segment) => segment.replaceAll("~1", "/").replaceAll("~0", "~")
|
|
169
|
+
)
|
|
170
|
+
}))
|
|
171
|
+
)
|
|
172
|
+
);
|
|
173
|
+
normalized.set(source, document);
|
|
187
174
|
}
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
}
|
|
219
|
-
if (child && typeof child === "object")
|
|
220
|
-
pending.push(child);
|
|
175
|
+
const documents = [...normalized.values()];
|
|
176
|
+
if (owners === void 0)
|
|
177
|
+
return {
|
|
178
|
+
version: 1,
|
|
179
|
+
documents: documents.map((document) => ({ ...document, imported: true }))
|
|
180
|
+
};
|
|
181
|
+
const available = new Set(
|
|
182
|
+
documents.flatMap((document) => document.findings.map(findingKey))
|
|
183
|
+
);
|
|
184
|
+
const owned = owners.documents.map((document) => ({
|
|
185
|
+
...document,
|
|
186
|
+
findings: document.findings.filter(
|
|
187
|
+
(finding) => available.has(findingKey(finding))
|
|
188
|
+
)
|
|
189
|
+
}));
|
|
190
|
+
const assigned = new Set(
|
|
191
|
+
owned.flatMap((document) => document.findings.map(findingKey))
|
|
192
|
+
);
|
|
193
|
+
const result = new Map(owned.map((document) => [document.source, document]));
|
|
194
|
+
for (const document of documents) {
|
|
195
|
+
const additions = document.findings.filter(
|
|
196
|
+
(finding) => !assigned.has(findingKey(finding))
|
|
197
|
+
);
|
|
198
|
+
if (additions.length) {
|
|
199
|
+
const existing = result.get(document.source);
|
|
200
|
+
result.set(document.source, {
|
|
201
|
+
...existing,
|
|
202
|
+
source: document.source,
|
|
203
|
+
findings: [...existing?.findings ?? [], ...additions]
|
|
204
|
+
});
|
|
221
205
|
}
|
|
222
206
|
}
|
|
223
|
-
return
|
|
207
|
+
return { version: 1, documents: [...result.values()] };
|
|
224
208
|
}
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
return ok([]);
|
|
233
|
-
}
|
|
234
|
-
if (options.allowExternalReferences === false) {
|
|
235
|
-
const validation = validateInlineDocument(input.document);
|
|
236
|
-
if (!validation.ok)
|
|
237
|
-
return validation;
|
|
238
|
-
}
|
|
239
|
-
const binary = resolveVacuumBinary(options.binaryPath);
|
|
240
|
-
if (!binary.ok) {
|
|
241
|
-
return binary;
|
|
242
|
-
}
|
|
243
|
-
const directory = await mkdtemp(join2(tmpdir(), "scalar-lint-")).catch(() => null);
|
|
244
|
-
if (directory === null) {
|
|
245
|
-
return err("vacuum lint failed \u2014 could not create a temporary directory");
|
|
209
|
+
async function loadBaseline(path, required) {
|
|
210
|
+
const content = await readFile(path, "utf8").catch((error) => error);
|
|
211
|
+
if (typeof content !== "string") {
|
|
212
|
+
if (!required && isRecord2(content) && content.code === "ENOENT") {
|
|
213
|
+
return ok({ version: 1, documents: [] });
|
|
214
|
+
}
|
|
215
|
+
return err(`Could not read ignore file: ${path}`);
|
|
246
216
|
}
|
|
247
|
-
const documentPath = join2(directory, "document.yaml");
|
|
248
|
-
const reportPath = join2(directory, "report.json");
|
|
249
217
|
try {
|
|
250
|
-
const
|
|
251
|
-
if (
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
218
|
+
const parsed = parse2(content);
|
|
219
|
+
if (isBaseline(parsed)) return ok(parsed);
|
|
220
|
+
if (!isIgnoreMap(parsed))
|
|
221
|
+
return err(
|
|
222
|
+
`Invalid ignore file: ${path}. Expected source, rule, and JSON pointer mappings.`
|
|
223
|
+
);
|
|
224
|
+
const metadata = content.split("\n").find((line) => line.startsWith(OWNERS_PREFIX));
|
|
225
|
+
const owners = metadata ? JSON.parse(metadata.slice(OWNERS_PREFIX.length)) : void 0;
|
|
226
|
+
if (owners !== void 0 && !isBaseline(owners))
|
|
227
|
+
return err(`Invalid ownership metadata in ignore file: ${path}`);
|
|
228
|
+
return ok(fromIgnoreMap(parsed, owners, dirname(path)));
|
|
229
|
+
} catch {
|
|
230
|
+
return err(`Invalid YAML or ownership metadata in ignore file: ${path}`);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
function updateBaseline(baseline, source, diagnostics, path) {
|
|
234
|
+
const directory = dirname(path);
|
|
235
|
+
const owner = sourceIdentity(source, directory);
|
|
236
|
+
const findings = new Map(
|
|
237
|
+
diagnostics.map((diagnostic) => {
|
|
238
|
+
const finding = findingIdentity(diagnostic, directory);
|
|
239
|
+
return [findingKey(finding), finding];
|
|
240
|
+
})
|
|
241
|
+
);
|
|
242
|
+
return {
|
|
243
|
+
version: 1,
|
|
244
|
+
documents: [
|
|
245
|
+
...baseline.documents.filter((document) => document.source !== owner),
|
|
246
|
+
{
|
|
247
|
+
source: owner,
|
|
248
|
+
findings: [...findings.values()].sort(
|
|
249
|
+
(a, b) => findingKey(a).localeCompare(findingKey(b))
|
|
250
|
+
)
|
|
275
251
|
}
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
252
|
+
].sort((a, b) => a.source.localeCompare(b.source))
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
function applyBaseline(baseline, diagnostics, path, redoclyCompatibility = false) {
|
|
256
|
+
const ignored = new Set(
|
|
257
|
+
baseline.documents.flatMap((document) => document.findings.map(findingKey))
|
|
258
|
+
);
|
|
259
|
+
const imported = new Set(
|
|
260
|
+
baseline.documents.filter((document) => document.imported).flatMap((document) => document.findings.map(findingKey))
|
|
261
|
+
);
|
|
262
|
+
return diagnostics.filter((diagnostic) => {
|
|
263
|
+
if ([
|
|
264
|
+
findingIdentity(diagnostic, dirname(path)),
|
|
265
|
+
{
|
|
266
|
+
...findingIdentity(diagnostic, dirname(path)),
|
|
267
|
+
path: diagnostic.path
|
|
268
|
+
}
|
|
269
|
+
].some((finding) => ignored.has(findingKey(finding))))
|
|
270
|
+
return false;
|
|
271
|
+
return !redoclyCompatibility || !ignoreCandidates(diagnostic, dirname(path)).some(
|
|
272
|
+
(finding) => imported.has(findingKey(finding))
|
|
273
|
+
);
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
async function writeBaseline(path, baseline) {
|
|
277
|
+
try {
|
|
278
|
+
const mapping = /* @__PURE__ */ Object.create(null);
|
|
279
|
+
for (const document of baseline.documents) {
|
|
280
|
+
for (const finding of document.findings) {
|
|
281
|
+
mapping[finding.source] ??= /* @__PURE__ */ Object.create(null);
|
|
282
|
+
const rules = mapping[finding.source];
|
|
283
|
+
rules[finding.rule] ??= [];
|
|
284
|
+
const pointers = rules[finding.rule];
|
|
285
|
+
const pointer = toPointer(finding.path);
|
|
286
|
+
if (!pointers.includes(pointer)) pointers.push(pointer);
|
|
288
287
|
}
|
|
289
|
-
return err(`vacuum failed to lint the document \u2014 ${describeFailure(failure)}`);
|
|
290
288
|
}
|
|
291
|
-
const
|
|
292
|
-
|
|
293
|
-
|
|
289
|
+
const sorted = /* @__PURE__ */ Object.create(null);
|
|
290
|
+
for (const source of Object.keys(mapping).sort()) {
|
|
291
|
+
const rules = /* @__PURE__ */ Object.create(null);
|
|
292
|
+
for (const rule of Object.keys(mapping[source] ?? {}).sort())
|
|
293
|
+
rules[rule] = [...mapping[source]?.[rule] ?? []].sort();
|
|
294
|
+
sorted[source] = rules;
|
|
294
295
|
}
|
|
295
|
-
const
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
...diagnostic,
|
|
303
|
-
source: !localSource || resolve(localSource) === documentPath ? input.source : /^https?:\/\//.test(localSource) ? localSource : resolve(localSource)
|
|
304
|
-
};
|
|
305
|
-
}));
|
|
306
|
-
} finally {
|
|
307
|
-
await rm(directory, { recursive: true, force: true }).catch(() => void 0);
|
|
296
|
+
const content = `# Ignore findings by source, rule, and JSON pointer.
|
|
297
|
+
${OWNERS_PREFIX}${JSON.stringify({ version: baseline.version, documents: baseline.documents })}
|
|
298
|
+
${stringify(sorted)}`;
|
|
299
|
+
const written = await writeFileAtomic(path, content);
|
|
300
|
+
return written.ok ? written : err(`Could not write ignore file: ${path}`);
|
|
301
|
+
} catch {
|
|
302
|
+
return err(`Could not write ignore file: ${path}`);
|
|
308
303
|
}
|
|
309
304
|
}
|
|
310
|
-
function
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
305
|
+
async function regenerateBaseline(path, source, diagnostics) {
|
|
306
|
+
const lock = `${path}.lock`;
|
|
307
|
+
const deadline = Date.now() + 1e4;
|
|
308
|
+
while (true) {
|
|
309
|
+
try {
|
|
310
|
+
await mkdir(lock);
|
|
311
|
+
break;
|
|
312
|
+
} catch (error) {
|
|
313
|
+
if (!isRecord2(error) || error.code !== "EEXIST")
|
|
314
|
+
return err(`Could not lock ignore file: ${path}`);
|
|
315
|
+
if (Date.now() >= deadline)
|
|
316
|
+
return err(
|
|
317
|
+
`Ignore file is locked: ${path}. Remove ${lock} only if no generator is running.`
|
|
318
|
+
);
|
|
319
|
+
await setTimeout(25);
|
|
318
320
|
}
|
|
319
321
|
}
|
|
320
|
-
|
|
322
|
+
const result = await (async () => {
|
|
323
|
+
const loaded = await loadBaseline(path, false);
|
|
324
|
+
if (!loaded.ok) return loaded;
|
|
325
|
+
const baseline = updateBaseline(loaded.data, source, diagnostics, path);
|
|
326
|
+
const written = await writeBaseline(path, baseline);
|
|
327
|
+
return written.ok ? ok(baseline) : written;
|
|
328
|
+
})().catch(() => err(`Could not update ignore file: ${path}`));
|
|
329
|
+
const released = await rm(lock, { recursive: true, force: true }).then(() => true).catch(() => false);
|
|
330
|
+
return released ? result : err(
|
|
331
|
+
`Could not release ignore file lock: ${lock}. Remove it only if no generator is running.`
|
|
332
|
+
);
|
|
321
333
|
}
|
|
322
|
-
function
|
|
334
|
+
function unsupportedRedoclyRules(baseline) {
|
|
335
|
+
return [
|
|
336
|
+
...new Set(
|
|
337
|
+
baseline.documents.filter((document) => document.imported).flatMap(
|
|
338
|
+
(document) => document.findings.map((finding) => finding.rule)
|
|
339
|
+
)
|
|
340
|
+
)
|
|
341
|
+
].filter((rule) => !Object.hasOwn(redoclyRuleMappings, rule)).sort();
|
|
342
|
+
}
|
|
343
|
+
function ignoreCandidates(diagnostic, directory) {
|
|
344
|
+
const primary = findingIdentity(diagnostic, directory);
|
|
345
|
+
const rules = Object.entries(redoclyRuleMappings).filter(
|
|
346
|
+
([, mapping]) => mapping.scalar === primary.rule
|
|
347
|
+
);
|
|
348
|
+
const candidates = [primary];
|
|
349
|
+
for (const [redocly, mapping] of rules) {
|
|
350
|
+
candidates.push({ ...primary, rule: redocly });
|
|
351
|
+
const field = mapping.missingField;
|
|
352
|
+
if (field && primary.path.at(-1) !== field)
|
|
353
|
+
candidates.push({
|
|
354
|
+
...primary,
|
|
355
|
+
rule: redocly,
|
|
356
|
+
path: [...primary.path, field]
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
return candidates;
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
// src/domains/document/lint/batch.ts
|
|
363
|
+
import { writeFile as writeFile2 } from "node:fs/promises";
|
|
364
|
+
import { resolve as resolve5 } from "node:path";
|
|
365
|
+
|
|
366
|
+
// src/domains/document/lint/ignore-path.ts
|
|
367
|
+
import { lstat } from "node:fs/promises";
|
|
368
|
+
import { resolve as resolve2 } from "node:path";
|
|
369
|
+
async function selectIgnorePath(explicit, generate) {
|
|
370
|
+
if (explicit) return ok(resolve2(explicit));
|
|
371
|
+
const scalar = resolve2(".scalar.lint-ignore.yaml");
|
|
372
|
+
if (generate) return ok(scalar);
|
|
323
373
|
try {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
}
|
|
330
|
-
|
|
374
|
+
await lstat(scalar);
|
|
375
|
+
return ok(scalar);
|
|
376
|
+
} catch (error) {
|
|
377
|
+
if (!(typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT"))
|
|
378
|
+
return err("Could not inspect default ignore file");
|
|
379
|
+
}
|
|
380
|
+
const redocly = resolve2(".redocly.lint-ignore.yaml");
|
|
381
|
+
try {
|
|
382
|
+
await lstat(redocly);
|
|
383
|
+
return ok(redocly);
|
|
384
|
+
} catch (error) {
|
|
385
|
+
if (!(typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT"))
|
|
386
|
+
return err("Could not inspect default ignore file");
|
|
387
|
+
return ok(scalar);
|
|
331
388
|
}
|
|
332
389
|
}
|
|
333
390
|
|
|
334
|
-
// ../../packages/api-document-lint/dist/
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
391
|
+
// ../../packages/api-document-lint/dist/constants.js
|
|
392
|
+
var DEFAULT_RULESET = `extends: spectral:oas
|
|
393
|
+
rules: {}`;
|
|
394
|
+
var ASYNCAPI_RULESET = `extends: spectral:asyncapi
|
|
395
|
+
rules: {}`;
|
|
396
|
+
|
|
397
|
+
// ../../packages/api-document-lint/dist/default-ruleset.js
|
|
398
|
+
import { parse as parseYaml } from "yaml";
|
|
399
|
+
function isAsyncApiContent2(content) {
|
|
400
|
+
try {
|
|
401
|
+
const document = parseYaml(content);
|
|
402
|
+
return typeof document === "object" && document !== null && !Array.isArray(document) && Object.hasOwn(document, "asyncapi");
|
|
403
|
+
} catch {
|
|
404
|
+
return false;
|
|
340
405
|
}
|
|
341
|
-
|
|
342
|
-
|
|
406
|
+
}
|
|
407
|
+
function defaultRulesetFor(content) {
|
|
408
|
+
return isAsyncApiContent2(content) ? ASYNCAPI_RULESET : DEFAULT_RULESET;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// ../../packages/api-document-lint/dist/run-spectral.js
|
|
412
|
+
import spectralCore from "@stoplight/spectral-core";
|
|
413
|
+
import spectralParsers from "@stoplight/spectral-parsers";
|
|
414
|
+
import { bundleAndLoadRuleset } from "@stoplight/spectral-ruleset-bundler/with-loader";
|
|
415
|
+
import "@stoplight/spectral-rulesets";
|
|
416
|
+
var { Spectral, Document } = spectralCore;
|
|
417
|
+
var { Yaml } = spectralParsers;
|
|
418
|
+
async function runSpectral(input) {
|
|
419
|
+
const rulesetSource = input.ruleset && input.ruleset.trim().length > 0 ? input.ruleset : defaultRulesetFor(input.document);
|
|
420
|
+
const ruleset = await loadRuleset(rulesetSource);
|
|
421
|
+
if (!ruleset.ok) {
|
|
422
|
+
return ruleset;
|
|
343
423
|
}
|
|
344
|
-
const
|
|
345
|
-
|
|
346
|
-
|
|
424
|
+
const spectral = new Spectral();
|
|
425
|
+
spectral.setRuleset(ruleset.data);
|
|
426
|
+
const diagnostics = await spectral.run(input.source ? new Document(input.document, Yaml, input.source) : input.document).catch((error) => error instanceof Error ? error : new Error(String(error)));
|
|
427
|
+
if (diagnostics instanceof Error) {
|
|
428
|
+
return err(`Spectral failed to lint the document \u2014 ${diagnostics.message}`);
|
|
347
429
|
}
|
|
348
|
-
|
|
349
|
-
|
|
430
|
+
return ok(diagnostics);
|
|
431
|
+
}
|
|
432
|
+
async function loadRuleset(source) {
|
|
433
|
+
const ruleset = await bundleAndLoadRuleset("/.spectral.yaml", {
|
|
434
|
+
fs: {
|
|
435
|
+
promises: {
|
|
436
|
+
// @ts-expect-error The loader only ever reads our single virtual file.
|
|
437
|
+
async readFile() {
|
|
438
|
+
return source;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
},
|
|
442
|
+
format: "esm"
|
|
443
|
+
}).catch((error) => error instanceof Error ? error : new Error(String(error)));
|
|
444
|
+
if (ruleset instanceof Error) {
|
|
445
|
+
return err(`Failed to load ruleset \u2014 ${ruleset.message}`);
|
|
350
446
|
}
|
|
351
|
-
return
|
|
447
|
+
return ok(ruleset);
|
|
352
448
|
}
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
449
|
+
|
|
450
|
+
// ../../packages/api-document-lint/dist/run-vacuum.js
|
|
451
|
+
import { execFile } from "node:child_process";
|
|
452
|
+
import { mkdtemp, readFile as readFile2, rm as rm2, writeFile } from "node:fs/promises";
|
|
453
|
+
import { tmpdir } from "node:os";
|
|
454
|
+
import { dirname as dirname3, join as join2, resolve as resolve3 } from "node:path";
|
|
455
|
+
import { fileURLToPath as fileURLToPath2 } from "node:url";
|
|
456
|
+
import { promisify } from "node:util";
|
|
457
|
+
|
|
458
|
+
// ../../packages/api-document-lint/dist/normalize-diagnostics.js
|
|
459
|
+
var EMPTY_RANGE = {
|
|
460
|
+
start: { line: 0, character: 0 },
|
|
461
|
+
end: { line: 0, character: 0 }
|
|
462
|
+
};
|
|
463
|
+
function normalizeDiagnostics(raw) {
|
|
464
|
+
if (!Array.isArray(raw)) {
|
|
465
|
+
return [];
|
|
357
466
|
}
|
|
358
|
-
return
|
|
467
|
+
return raw.filter(isObject).map((item) => {
|
|
468
|
+
const source = item.source;
|
|
469
|
+
return {
|
|
470
|
+
code: isCode(item.code) ? item.code : "",
|
|
471
|
+
message: typeof item.message === "string" ? item.message : "",
|
|
472
|
+
severity: normalizeSeverity(item.severity),
|
|
473
|
+
path: Array.isArray(item.path) ? item.path.filter(isSegment) : [],
|
|
474
|
+
range: normalizeRange(item.range),
|
|
475
|
+
...typeof source === "string" ? { source } : {},
|
|
476
|
+
...Array.isArray(item.locations) ? {
|
|
477
|
+
locations: item.locations.filter(isLocation).map((location) => ({
|
|
478
|
+
source: location.source,
|
|
479
|
+
path: location.path,
|
|
480
|
+
range: normalizeRange(location.range)
|
|
481
|
+
}))
|
|
482
|
+
} : {},
|
|
483
|
+
...typeof item.documentationUrl === "string" ? { documentationUrl: item.documentationUrl } : {}
|
|
484
|
+
};
|
|
485
|
+
});
|
|
359
486
|
}
|
|
360
|
-
function
|
|
361
|
-
return
|
|
487
|
+
function isObject(value) {
|
|
488
|
+
return typeof value === "object" && value !== null;
|
|
362
489
|
}
|
|
363
|
-
function
|
|
364
|
-
return typeof value === "
|
|
490
|
+
function isCode(value) {
|
|
491
|
+
return typeof value === "string" || typeof value === "number";
|
|
365
492
|
}
|
|
366
|
-
function
|
|
367
|
-
|
|
368
|
-
const value = parseYaml2(source);
|
|
369
|
-
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
370
|
-
return value;
|
|
371
|
-
}
|
|
372
|
-
return null;
|
|
373
|
-
} catch {
|
|
374
|
-
return null;
|
|
375
|
-
}
|
|
493
|
+
function isSegment(value) {
|
|
494
|
+
return typeof value === "string" || typeof value === "number";
|
|
376
495
|
}
|
|
377
|
-
function
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
return true;
|
|
496
|
+
function normalizeRange(range) {
|
|
497
|
+
if (!isObject(range)) {
|
|
498
|
+
return EMPTY_RANGE;
|
|
381
499
|
}
|
|
382
|
-
return
|
|
500
|
+
return {
|
|
501
|
+
start: normalizePosition(range.start),
|
|
502
|
+
end: normalizePosition(range.end)
|
|
503
|
+
};
|
|
383
504
|
}
|
|
384
|
-
function
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
return false;
|
|
505
|
+
function normalizePosition(position) {
|
|
506
|
+
if (!isObject(position)) {
|
|
507
|
+
return { line: 0, character: 0 };
|
|
388
508
|
}
|
|
389
|
-
|
|
390
|
-
|
|
509
|
+
return {
|
|
510
|
+
line: toCoordinate(position.line),
|
|
511
|
+
character: toCoordinate(position.character)
|
|
512
|
+
};
|
|
391
513
|
}
|
|
392
|
-
function
|
|
393
|
-
|
|
394
|
-
|
|
514
|
+
function toCoordinate(value) {
|
|
515
|
+
return typeof value === "number" && Number.isInteger(value) && value >= 0 ? value : 0;
|
|
516
|
+
}
|
|
517
|
+
function normalizeSeverity(severity) {
|
|
518
|
+
return severity === 1 || severity === 2 || severity === 3 ? severity : 0;
|
|
519
|
+
}
|
|
520
|
+
function isLocation(value) {
|
|
521
|
+
return isObject(value) && typeof value.source === "string" && Array.isArray(value.path) && value.path.every(isSegment) && isObject(value.range);
|
|
395
522
|
}
|
|
396
523
|
|
|
397
|
-
// ../../packages/api-document-lint/dist/
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
524
|
+
// ../../packages/api-document-lint/dist/resolve-binary.js
|
|
525
|
+
import { statSync } from "node:fs";
|
|
526
|
+
import { createRequire } from "node:module";
|
|
527
|
+
import { dirname as dirname2, join } from "node:path";
|
|
528
|
+
var binaryName = process.platform === "win32" ? "vacuum.exe" : "vacuum";
|
|
529
|
+
function resolveVacuumBinary(override) {
|
|
530
|
+
const explicit = override ?? process.env.VACUUM_BINARY;
|
|
531
|
+
if (explicit) {
|
|
532
|
+
return isFile(explicit) ? ok(explicit) : err(`vacuum binary not found at ${explicit}`);
|
|
405
533
|
}
|
|
406
|
-
|
|
534
|
+
const packaged = resolvePackagedBinary();
|
|
535
|
+
if (packaged) {
|
|
536
|
+
return ok(packaged);
|
|
537
|
+
}
|
|
538
|
+
return ok("vacuum");
|
|
407
539
|
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
// src/domains/document/lint/pointer.ts
|
|
418
|
-
function toPointer(path) {
|
|
419
|
-
return `#${path.map((segment) => `/${String(segment).replaceAll("~", "~0").replaceAll("/", "~1")}`).join("")}`;
|
|
540
|
+
function resolvePackagedBinary() {
|
|
541
|
+
try {
|
|
542
|
+
const require2 = createRequire(import.meta.url);
|
|
543
|
+
const packageJsonPath = require2.resolve("@quobix/vacuum/package.json");
|
|
544
|
+
const candidate = join(dirname2(packageJsonPath), "bin", binaryName);
|
|
545
|
+
return isFile(candidate) ? candidate : null;
|
|
546
|
+
} catch {
|
|
547
|
+
return null;
|
|
548
|
+
}
|
|
420
549
|
}
|
|
421
|
-
|
|
422
|
-
// src/domains/document/lint/rule-mappings.ts
|
|
423
|
-
import { parse as parse2 } from "yaml";
|
|
424
|
-
var redoclyRuleMappings = {
|
|
425
|
-
"info-contact": { scalar: "info-contact", missingField: "contact" },
|
|
426
|
-
"info-license": { scalar: "info-license", missingField: "license" },
|
|
427
|
-
"operation-description": {
|
|
428
|
-
scalar: "operation-description",
|
|
429
|
-
missingField: "description"
|
|
430
|
-
},
|
|
431
|
-
"operation-operationId": {
|
|
432
|
-
scalar: "operation-operationId",
|
|
433
|
-
missingField: "operationId"
|
|
434
|
-
},
|
|
435
|
-
"operation-operationId-url-safe": {
|
|
436
|
-
scalar: "operation-operationId-valid-in-url"
|
|
437
|
-
},
|
|
438
|
-
"no-path-trailing-slash": { scalar: "path-keys-no-trailing-slash" },
|
|
439
|
-
"path-declaration-must-exist": { scalar: "path-declarations-must-exist" },
|
|
440
|
-
"path-not-include-query": { scalar: "path-not-include-query" },
|
|
441
|
-
"tag-description": { scalar: "tag-description", missingField: "description" },
|
|
442
|
-
"operation-singular-tag": { scalar: "operation-singular-tag" }
|
|
443
|
-
};
|
|
444
|
-
function supportsRedoclyMigration(ruleset) {
|
|
550
|
+
function isFile(path) {
|
|
445
551
|
try {
|
|
446
|
-
|
|
447
|
-
if (!isRecord2(parsed) || Object.keys(parsed).some((key) => !["extends", "rules"].includes(key)))
|
|
448
|
-
return false;
|
|
449
|
-
const extended = Array.isArray(parsed.extends) ? parsed.extends : [parsed.extends];
|
|
450
|
-
if (extended.length !== 1 || extended[0] !== "spectral:oas") return false;
|
|
451
|
-
if (parsed.rules === void 0) return true;
|
|
452
|
-
if (!isRecord2(parsed.rules)) return false;
|
|
453
|
-
const audited = new Set(
|
|
454
|
-
Object.values(redoclyRuleMappings).map((mapping) => mapping.scalar)
|
|
455
|
-
);
|
|
456
|
-
return Object.entries(parsed.rules).every(([rule, value]) => {
|
|
457
|
-
if (!audited.has(rule)) return false;
|
|
458
|
-
const severity = isRecord2(value) && Object.keys(value).length === 1 ? value.severity : value;
|
|
459
|
-
return [
|
|
460
|
-
"error",
|
|
461
|
-
"warn",
|
|
462
|
-
"info",
|
|
463
|
-
"hint",
|
|
464
|
-
"off",
|
|
465
|
-
0,
|
|
466
|
-
1,
|
|
467
|
-
2,
|
|
468
|
-
3,
|
|
469
|
-
-1,
|
|
470
|
-
true,
|
|
471
|
-
false
|
|
472
|
-
].some((allowed) => allowed === severity);
|
|
473
|
-
});
|
|
552
|
+
return statSync(path, { throwIfNoEntry: false })?.isFile() ?? false;
|
|
474
553
|
} catch {
|
|
475
554
|
return false;
|
|
476
555
|
}
|
|
477
556
|
}
|
|
478
|
-
function isRecord2(value) {
|
|
479
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
480
|
-
}
|
|
481
557
|
|
|
482
|
-
//
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
const
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
return
|
|
516
|
-
(rules) => isRecord3(rules) && Object.values(rules).every(
|
|
517
|
-
(pointers) => Array.isArray(pointers) && pointers.every(
|
|
518
|
-
(pointer) => typeof pointer === "string" && /^#(?:\/(?:[^~]|~[01])*)*$/.test(pointer)
|
|
519
|
-
)
|
|
520
|
-
)
|
|
521
|
-
);
|
|
558
|
+
// ../../packages/api-document-lint/dist/validate-inline-document.js
|
|
559
|
+
import { parse as parse3 } from "yaml";
|
|
560
|
+
function validateInlineDocument(document) {
|
|
561
|
+
const parsed = (() => {
|
|
562
|
+
try {
|
|
563
|
+
return ok(parse3(document));
|
|
564
|
+
} catch {
|
|
565
|
+
return err("Document must be valid YAML or JSON before linting.");
|
|
566
|
+
}
|
|
567
|
+
})();
|
|
568
|
+
if (!parsed.ok)
|
|
569
|
+
return parsed;
|
|
570
|
+
const references2 = /* @__PURE__ */ new Set([
|
|
571
|
+
"$ref",
|
|
572
|
+
"$dynamicRef",
|
|
573
|
+
"$recursiveRef",
|
|
574
|
+
"operationRef"
|
|
575
|
+
]);
|
|
576
|
+
const seen = /* @__PURE__ */ new Set();
|
|
577
|
+
const pending = [parsed.data];
|
|
578
|
+
while (pending.length) {
|
|
579
|
+
const value = pending.pop();
|
|
580
|
+
if (!value || typeof value !== "object" || seen.has(value))
|
|
581
|
+
continue;
|
|
582
|
+
seen.add(value);
|
|
583
|
+
for (const [key, child] of Object.entries(value)) {
|
|
584
|
+
if (references2.has(key) && typeof child === "string" && child !== "" && !child.startsWith("#")) {
|
|
585
|
+
return err("External references are not supported by hosted linting. Bundle the document first.");
|
|
586
|
+
}
|
|
587
|
+
if (child && typeof child === "object")
|
|
588
|
+
pending.push(child);
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
return ok(void 0);
|
|
522
592
|
}
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
document.findings.push(
|
|
532
|
-
...Object.entries(rules).flatMap(
|
|
533
|
-
([rule, pointers]) => pointers.map((pointer) => ({
|
|
534
|
-
source,
|
|
535
|
-
rule,
|
|
536
|
-
path: pointer === "#" ? [] : pointer.slice(2).split("/").map(
|
|
537
|
-
(segment) => segment.replaceAll("~1", "/").replaceAll("~0", "~")
|
|
538
|
-
)
|
|
539
|
-
}))
|
|
540
|
-
)
|
|
541
|
-
);
|
|
542
|
-
normalized.set(source, document);
|
|
593
|
+
|
|
594
|
+
// ../../packages/api-document-lint/dist/run-vacuum.js
|
|
595
|
+
var execFileAsync = promisify(execFile);
|
|
596
|
+
var MAX_BUFFER = 64 * 1024 * 1024;
|
|
597
|
+
var TIMEOUT_MS = 6e4;
|
|
598
|
+
async function runVacuum(input, options = {}) {
|
|
599
|
+
if (input.document.trim().length === 0) {
|
|
600
|
+
return ok([]);
|
|
543
601
|
}
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
const
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
const
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
602
|
+
if (options.allowExternalReferences === false) {
|
|
603
|
+
const validation = validateInlineDocument(input.document);
|
|
604
|
+
if (!validation.ok)
|
|
605
|
+
return validation;
|
|
606
|
+
}
|
|
607
|
+
const binary = resolveVacuumBinary(options.binaryPath);
|
|
608
|
+
if (!binary.ok) {
|
|
609
|
+
return binary;
|
|
610
|
+
}
|
|
611
|
+
const directory = await mkdtemp(join2(tmpdir(), "scalar-lint-")).catch(() => null);
|
|
612
|
+
if (directory === null) {
|
|
613
|
+
return err("vacuum lint failed \u2014 could not create a temporary directory");
|
|
614
|
+
}
|
|
615
|
+
const documentPath = join2(directory, "document.yaml");
|
|
616
|
+
const reportPath = join2(directory, "report.json");
|
|
617
|
+
try {
|
|
618
|
+
const wroteDocument = await writeFile(documentPath, input.document, "utf8").then(() => true).catch(() => false);
|
|
619
|
+
if (!wroteDocument) {
|
|
620
|
+
return err("vacuum lint failed \u2014 could not write the document to a temporary file");
|
|
621
|
+
}
|
|
622
|
+
const args = [
|
|
623
|
+
"spectral-report",
|
|
624
|
+
"--no-style",
|
|
625
|
+
"--no-update-check",
|
|
626
|
+
"--skip-check",
|
|
627
|
+
// Match Spectral's working-directory base for document strings.
|
|
628
|
+
"--base",
|
|
629
|
+
options.allowExternalReferences === false ? directory : input.source ? /^https?:\/\//.test(input.source) ? new URL(".", input.source).href : dirname3(input.source) : process.cwd()
|
|
630
|
+
];
|
|
631
|
+
if (options.allowExternalReferences === false) {
|
|
632
|
+
const configPath = join2(directory, "vacuum.conf.yaml");
|
|
633
|
+
const configured = await writeFile(configPath, "{}").then(() => true).catch(() => false);
|
|
634
|
+
if (!configured)
|
|
635
|
+
return err("Could not create hosted lint configuration");
|
|
636
|
+
args.push("--remote=false", "--config", configPath);
|
|
637
|
+
}
|
|
638
|
+
if (input.ruleset && input.ruleset.trim().length > 0) {
|
|
639
|
+
const rulesetPath = join2(directory, "ruleset.yaml");
|
|
640
|
+
const wroteRuleset = await writeFile(rulesetPath, input.ruleset, "utf8").then(() => true).catch(() => false);
|
|
641
|
+
if (!wroteRuleset) {
|
|
642
|
+
return err("vacuum lint failed \u2014 could not write the ruleset to a temporary file");
|
|
643
|
+
}
|
|
644
|
+
args.push("--ruleset", rulesetPath);
|
|
645
|
+
}
|
|
646
|
+
args.push(documentPath, reportPath);
|
|
647
|
+
const failure = await execFileAsync(binary.data, args, {
|
|
648
|
+
maxBuffer: MAX_BUFFER,
|
|
649
|
+
timeout: options.timeoutMs ?? TIMEOUT_MS,
|
|
650
|
+
...options.allowExternalReferences === false ? { cwd: directory } : {}
|
|
651
|
+
}).then(() => null).catch((error) => error);
|
|
652
|
+
const report = await readFile2(reportPath, "utf8").catch(() => null);
|
|
653
|
+
if (report === null) {
|
|
654
|
+
if (failure === null) {
|
|
655
|
+
return err("vacuum ran but its report could not be read");
|
|
656
|
+
}
|
|
657
|
+
return err(`vacuum failed to lint the document \u2014 ${describeFailure(failure)}`);
|
|
658
|
+
}
|
|
659
|
+
const parsed = parseReport(report);
|
|
660
|
+
if (!parsed.ok) {
|
|
661
|
+
return parsed;
|
|
574
662
|
}
|
|
663
|
+
const diagnostics = normalizeDiagnostics(parsed.data);
|
|
664
|
+
if (!input.source)
|
|
665
|
+
return ok(diagnostics);
|
|
666
|
+
return ok(diagnostics.map((diagnostic) => {
|
|
667
|
+
const source = diagnostic.source;
|
|
668
|
+
const localSource = source?.startsWith("file:") ? fileURLToPath2(source) : source;
|
|
669
|
+
return {
|
|
670
|
+
...diagnostic,
|
|
671
|
+
source: !localSource || resolve3(localSource) === documentPath ? input.source : /^https?:\/\//.test(localSource) ? localSource : resolve3(localSource)
|
|
672
|
+
};
|
|
673
|
+
}));
|
|
674
|
+
} finally {
|
|
675
|
+
await rm2(directory, { recursive: true, force: true }).catch(() => void 0);
|
|
575
676
|
}
|
|
576
|
-
return { version: 1, documents: [...result.values()] };
|
|
577
677
|
}
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
if (
|
|
582
|
-
return
|
|
678
|
+
function describeFailure(failure) {
|
|
679
|
+
if (failure && typeof failure === "object") {
|
|
680
|
+
const stderr = failure.stderr;
|
|
681
|
+
if (typeof stderr === "string" && stderr.trim().length > 0) {
|
|
682
|
+
return stderr.trim();
|
|
683
|
+
}
|
|
684
|
+
if (failure instanceof Error) {
|
|
685
|
+
return failure.message;
|
|
583
686
|
}
|
|
584
|
-
return err(`Could not read ignore file: ${path}`);
|
|
585
687
|
}
|
|
688
|
+
return "no report was written";
|
|
689
|
+
}
|
|
690
|
+
function parseReport(report) {
|
|
586
691
|
try {
|
|
587
|
-
const parsed =
|
|
588
|
-
if (
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
);
|
|
593
|
-
const metadata = content.split("\n").find((line) => line.startsWith(OWNERS_PREFIX));
|
|
594
|
-
const owners = metadata ? JSON.parse(metadata.slice(OWNERS_PREFIX.length)) : void 0;
|
|
595
|
-
if (owners !== void 0 && !isBaseline(owners))
|
|
596
|
-
return err(`Invalid ownership metadata in ignore file: ${path}`);
|
|
597
|
-
return ok(fromIgnoreMap(parsed, owners, dirname3(path)));
|
|
692
|
+
const parsed = JSON.parse(report);
|
|
693
|
+
if (!Array.isArray(parsed)) {
|
|
694
|
+
return err("vacuum produced a report in an unexpected shape");
|
|
695
|
+
}
|
|
696
|
+
return ok(parsed);
|
|
598
697
|
} catch {
|
|
599
|
-
return err(
|
|
698
|
+
return err("vacuum produced an unreadable report");
|
|
600
699
|
}
|
|
601
700
|
}
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
)
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
].sort((a, b) => a.source.localeCompare(b.source))
|
|
622
|
-
};
|
|
701
|
+
|
|
702
|
+
// ../../packages/api-document-lint/dist/select-engine.js
|
|
703
|
+
import { parse as parseYaml2 } from "yaml";
|
|
704
|
+
function selectEngine(ruleset, document) {
|
|
705
|
+
const parsedDocument = document ? parseYamlObject(document) : null;
|
|
706
|
+
if (typeof parsedDocument?.asyncapi === "string" && parsedDocument.asyncapi.startsWith("2.")) {
|
|
707
|
+
return "spectral";
|
|
708
|
+
}
|
|
709
|
+
if (!ruleset || ruleset.trim().length === 0) {
|
|
710
|
+
return "vacuum";
|
|
711
|
+
}
|
|
712
|
+
const parsed = parseYamlObject(ruleset);
|
|
713
|
+
if (parsed === null) {
|
|
714
|
+
return "spectral";
|
|
715
|
+
}
|
|
716
|
+
if (hasCustomFunctions(parsed) || hasRuleCustomizations(parsed) || extendsUninspectableRuleset(parsed) || overridesRequireSpectral(parsed)) {
|
|
717
|
+
return "spectral";
|
|
718
|
+
}
|
|
719
|
+
return "vacuum";
|
|
623
720
|
}
|
|
624
|
-
function
|
|
625
|
-
const
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
);
|
|
631
|
-
return diagnostics.filter((diagnostic) => {
|
|
632
|
-
if ([
|
|
633
|
-
findingIdentity(diagnostic, dirname3(path)),
|
|
634
|
-
{
|
|
635
|
-
...findingIdentity(diagnostic, dirname3(path)),
|
|
636
|
-
path: diagnostic.path
|
|
637
|
-
}
|
|
638
|
-
].some((finding) => ignored.has(findingKey(finding))))
|
|
639
|
-
return false;
|
|
640
|
-
return !redoclyCompatibility || !ignoreCandidates(diagnostic, dirname3(path)).some(
|
|
641
|
-
(finding) => imported.has(findingKey(finding))
|
|
642
|
-
);
|
|
643
|
-
});
|
|
721
|
+
function overridesRequireSpectral(ruleset) {
|
|
722
|
+
const overrides = ruleset.overrides;
|
|
723
|
+
if (!Array.isArray(overrides)) {
|
|
724
|
+
return false;
|
|
725
|
+
}
|
|
726
|
+
return overrides.some((entry) => isRecord3(entry) && (hasCustomFunctions(entry) || hasRuleCustomizations(entry) || extendsUninspectableRuleset(entry)));
|
|
644
727
|
}
|
|
645
|
-
|
|
646
|
-
|
|
728
|
+
function hasRuleCustomizations(ruleset) {
|
|
729
|
+
return ruleset.rules !== void 0 && (!isRecord3(ruleset.rules) || Object.keys(ruleset.rules).length > 0);
|
|
730
|
+
}
|
|
731
|
+
function isRecord3(value) {
|
|
732
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
733
|
+
}
|
|
734
|
+
function parseYamlObject(source) {
|
|
647
735
|
try {
|
|
648
|
-
const
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
mapping[finding.source] ??= /* @__PURE__ */ Object.create(null);
|
|
652
|
-
const rules = mapping[finding.source];
|
|
653
|
-
rules[finding.rule] ??= [];
|
|
654
|
-
const pointers = rules[finding.rule];
|
|
655
|
-
const pointer = toPointer(finding.path);
|
|
656
|
-
if (!pointers.includes(pointer)) pointers.push(pointer);
|
|
657
|
-
}
|
|
658
|
-
}
|
|
659
|
-
const sorted = /* @__PURE__ */ Object.create(null);
|
|
660
|
-
for (const source of Object.keys(mapping).sort()) {
|
|
661
|
-
const rules = /* @__PURE__ */ Object.create(null);
|
|
662
|
-
for (const rule of Object.keys(mapping[source] ?? {}).sort())
|
|
663
|
-
rules[rule] = [...mapping[source]?.[rule] ?? []].sort();
|
|
664
|
-
sorted[source] = rules;
|
|
736
|
+
const value = parseYaml2(source);
|
|
737
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
738
|
+
return value;
|
|
665
739
|
}
|
|
666
|
-
|
|
667
|
-
${OWNERS_PREFIX}${JSON.stringify({ version: baseline.version, documents: baseline.documents })}
|
|
668
|
-
${stringify(sorted)}`;
|
|
669
|
-
await writeFile2(temporary, content, {
|
|
670
|
-
encoding: "utf8",
|
|
671
|
-
flag: "wx"
|
|
672
|
-
});
|
|
673
|
-
await rename(temporary, path);
|
|
674
|
-
return ok(void 0);
|
|
740
|
+
return null;
|
|
675
741
|
} catch {
|
|
676
|
-
return
|
|
677
|
-
} finally {
|
|
678
|
-
await rm2(temporary, { force: true }).catch(() => void 0);
|
|
742
|
+
return null;
|
|
679
743
|
}
|
|
680
744
|
}
|
|
681
|
-
|
|
682
|
-
const
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
try {
|
|
686
|
-
await mkdir(lock);
|
|
687
|
-
break;
|
|
688
|
-
} catch (error) {
|
|
689
|
-
if (!isRecord3(error) || error.code !== "EEXIST")
|
|
690
|
-
return err(`Could not lock ignore file: ${path}`);
|
|
691
|
-
if (Date.now() >= deadline)
|
|
692
|
-
return err(
|
|
693
|
-
`Ignore file is locked: ${path}. Remove ${lock} only if no generator is running.`
|
|
694
|
-
);
|
|
695
|
-
await setTimeout(25);
|
|
696
|
-
}
|
|
745
|
+
function hasCustomFunctions(ruleset) {
|
|
746
|
+
const functions = ruleset.functions;
|
|
747
|
+
if (Array.isArray(functions) && functions.length > 0) {
|
|
748
|
+
return true;
|
|
697
749
|
}
|
|
698
|
-
|
|
699
|
-
const loaded = await loadBaseline(path, false);
|
|
700
|
-
if (!loaded.ok) return loaded;
|
|
701
|
-
const baseline = updateBaseline(loaded.data, source, diagnostics, path);
|
|
702
|
-
const written = await writeBaseline(path, baseline);
|
|
703
|
-
return written.ok ? ok(baseline) : written;
|
|
704
|
-
})().catch(() => err(`Could not update ignore file: ${path}`));
|
|
705
|
-
const released = await rm2(lock, { recursive: true, force: true }).then(() => true).catch(() => false);
|
|
706
|
-
return released ? result : err(
|
|
707
|
-
`Could not release ignore file lock: ${lock}. Remove it only if no generator is running.`
|
|
708
|
-
);
|
|
750
|
+
return typeof ruleset.functionsDir === "string" && ruleset.functionsDir.length > 0;
|
|
709
751
|
}
|
|
710
|
-
function
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
].filter((rule) => !Object.hasOwn(redoclyRuleMappings, rule)).sort();
|
|
752
|
+
function extendsUninspectableRuleset(ruleset) {
|
|
753
|
+
const extended = ruleset.extends;
|
|
754
|
+
if (extended === void 0) {
|
|
755
|
+
return false;
|
|
756
|
+
}
|
|
757
|
+
const entries = Array.isArray(extended) ? extended : [extended];
|
|
758
|
+
return entries.some((entry) => !isBuiltInRuleset(entry));
|
|
718
759
|
}
|
|
719
|
-
function
|
|
720
|
-
const
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
path: [...primary.path, field]
|
|
733
|
-
});
|
|
760
|
+
function isBuiltInRuleset(entry) {
|
|
761
|
+
const source = Array.isArray(entry) ? entry[0] : entry;
|
|
762
|
+
return source === "spectral:oas" || source === "spectral:asyncapi";
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
// ../../packages/api-document-lint/dist/lint.js
|
|
766
|
+
async function lint(input, options = {}) {
|
|
767
|
+
const ruleset = input.ruleset && input.ruleset.trim().length > 0 ? input.ruleset : defaultRulesetFor(input.document);
|
|
768
|
+
const resolved = { ...input, ruleset };
|
|
769
|
+
const engine = selectEngine(ruleset, input.document);
|
|
770
|
+
const diagnostics = engine === "vacuum" ? await runVacuum(resolved, options) : await runSpectral(resolved);
|
|
771
|
+
if (!diagnostics.ok) {
|
|
772
|
+
return diagnostics;
|
|
734
773
|
}
|
|
735
|
-
return
|
|
774
|
+
return ok({ diagnostics: diagnostics.data, engine });
|
|
736
775
|
}
|
|
737
776
|
|
|
738
777
|
// src/domains/document/lint/document-paths.ts
|
|
@@ -887,31 +926,6 @@ function resolveDiagnosticPaths(diagnostics, sources, engine) {
|
|
|
887
926
|
});
|
|
888
927
|
}
|
|
889
928
|
|
|
890
|
-
// src/domains/document/lint/ignore-path.ts
|
|
891
|
-
import { lstat } from "node:fs/promises";
|
|
892
|
-
import { resolve as resolve3 } from "node:path";
|
|
893
|
-
async function selectIgnorePath(explicit, generate) {
|
|
894
|
-
if (explicit) return ok(resolve3(explicit));
|
|
895
|
-
const scalar = resolve3(".scalar.lint-ignore.yaml");
|
|
896
|
-
if (generate) return ok(scalar);
|
|
897
|
-
try {
|
|
898
|
-
await lstat(scalar);
|
|
899
|
-
return ok(scalar);
|
|
900
|
-
} catch (error) {
|
|
901
|
-
if (!(typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT"))
|
|
902
|
-
return err("Could not inspect default ignore file");
|
|
903
|
-
}
|
|
904
|
-
const redocly = resolve3(".redocly.lint-ignore.yaml");
|
|
905
|
-
try {
|
|
906
|
-
await lstat(redocly);
|
|
907
|
-
return ok(redocly);
|
|
908
|
-
} catch (error) {
|
|
909
|
-
if (!(typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT"))
|
|
910
|
-
return err("Could not inspect default ignore file");
|
|
911
|
-
return ok(scalar);
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
|
|
915
929
|
// src/domains/document/lint/locations.ts
|
|
916
930
|
import { isAlias as isAlias2, isMap as isMap2, LineCounter as LineCounter2, parseDocument as parseDocument2 } from "yaml";
|
|
917
931
|
var METHODS = /* @__PURE__ */ new Set([
|
|
@@ -1052,6 +1066,117 @@ function addRelatedLocations(diagnostics, sources, engine) {
|
|
|
1052
1066
|
});
|
|
1053
1067
|
}
|
|
1054
1068
|
|
|
1069
|
+
// src/domains/document/lint/sources.ts
|
|
1070
|
+
import { readFile as readFile3 } from "node:fs/promises";
|
|
1071
|
+
import { fileURLToPath as fileURLToPath4, pathToFileURL as pathToFileURL2 } from "node:url";
|
|
1072
|
+
import { parse as parse4 } from "yaml";
|
|
1073
|
+
async function readSource(source) {
|
|
1074
|
+
try {
|
|
1075
|
+
if (/^https?:\/\//.test(source)) {
|
|
1076
|
+
const response = await fetch(source, {
|
|
1077
|
+
signal: AbortSignal.timeout(1e4)
|
|
1078
|
+
});
|
|
1079
|
+
return response.ok ? ok(await response.text()) : err(`Could not read ${source}: HTTP ${response.status}`);
|
|
1080
|
+
}
|
|
1081
|
+
return ok(await readFile3(source, "utf8"));
|
|
1082
|
+
} catch (error) {
|
|
1083
|
+
return err(
|
|
1084
|
+
`Could not read ${source}: ${error instanceof Error ? error.message : String(error)}`
|
|
1085
|
+
);
|
|
1086
|
+
}
|
|
1087
|
+
}
|
|
1088
|
+
function references(content) {
|
|
1089
|
+
const root = parse4(content);
|
|
1090
|
+
const visited = /* @__PURE__ */ new WeakSet();
|
|
1091
|
+
const refs = /* @__PURE__ */ new Set();
|
|
1092
|
+
const pending = [root];
|
|
1093
|
+
while (pending.length) {
|
|
1094
|
+
const value = pending.pop();
|
|
1095
|
+
if (value === null || typeof value !== "object" || visited.has(value))
|
|
1096
|
+
continue;
|
|
1097
|
+
visited.add(value);
|
|
1098
|
+
for (const [key, child] of Object.entries(value).reverse()) {
|
|
1099
|
+
if (key === "$ref" && typeof child === "string") refs.add(child);
|
|
1100
|
+
else pending.push(child);
|
|
1101
|
+
}
|
|
1102
|
+
}
|
|
1103
|
+
return [...refs];
|
|
1104
|
+
}
|
|
1105
|
+
function referenceSource(ref, source) {
|
|
1106
|
+
try {
|
|
1107
|
+
const url = new URL(
|
|
1108
|
+
ref,
|
|
1109
|
+
/^https?:\/\//.test(source) ? source : pathToFileURL2(source)
|
|
1110
|
+
);
|
|
1111
|
+
url.hash = "";
|
|
1112
|
+
return ["file:", "https:", "http:"].includes(url.protocol) ? ok(url.protocol === "file:" ? fileURLToPath4(url) : url.href) : err("Unsupported reference protocol");
|
|
1113
|
+
} catch {
|
|
1114
|
+
return err("Invalid reference URI");
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
async function collectSources(source, content) {
|
|
1118
|
+
const sources = /* @__PURE__ */ new Map([[source, content]]);
|
|
1119
|
+
for (const [current, text] of sources) {
|
|
1120
|
+
if (text === void 0) continue;
|
|
1121
|
+
const refs = (() => {
|
|
1122
|
+
try {
|
|
1123
|
+
return references(text);
|
|
1124
|
+
} catch {
|
|
1125
|
+
return [];
|
|
1126
|
+
}
|
|
1127
|
+
})();
|
|
1128
|
+
for (const ref of refs) {
|
|
1129
|
+
const resolved = referenceSource(ref, current);
|
|
1130
|
+
if (!resolved.ok) continue;
|
|
1131
|
+
const target = resolved.data;
|
|
1132
|
+
if (sources.has(target)) continue;
|
|
1133
|
+
const loaded = await readSource(target);
|
|
1134
|
+
sources.set(target, loaded.ok ? loaded.data : void 0);
|
|
1135
|
+
}
|
|
1136
|
+
}
|
|
1137
|
+
return sources;
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
// src/domains/document/lint/lint-document.ts
|
|
1141
|
+
async function lintDocument(source, rule, collected) {
|
|
1142
|
+
try {
|
|
1143
|
+
const content = collected?.get(source);
|
|
1144
|
+
const specification = content === void 0 ? await readSource(source) : ok(content);
|
|
1145
|
+
if (!specification.ok) return specification;
|
|
1146
|
+
const asyncApi = isAsyncApiContent(specification.data);
|
|
1147
|
+
const ruleset = rule ? await readSource(rule) : ok(asyncApi ? ASYNCAPI_RULESET : DEFAULT_RULESET);
|
|
1148
|
+
if (!ruleset.ok) return ruleset;
|
|
1149
|
+
const sources = collected ?? await collectSources(source, specification.data);
|
|
1150
|
+
const result = await lint({
|
|
1151
|
+
document: specification.data,
|
|
1152
|
+
ruleset: ruleset.data,
|
|
1153
|
+
source
|
|
1154
|
+
});
|
|
1155
|
+
if (!result.ok)
|
|
1156
|
+
return err(`Could not lint the API description
|
|
1157
|
+
|
|
1158
|
+
${result.error}`);
|
|
1159
|
+
const diagnostics = addRelatedLocations(
|
|
1160
|
+
resolveDiagnosticPaths(
|
|
1161
|
+
result.data.diagnostics,
|
|
1162
|
+
sources,
|
|
1163
|
+
result.data.engine
|
|
1164
|
+
),
|
|
1165
|
+
sources,
|
|
1166
|
+
result.data.engine
|
|
1167
|
+
);
|
|
1168
|
+
return ok({
|
|
1169
|
+
source,
|
|
1170
|
+
diagnostics,
|
|
1171
|
+
engine: result.data.engine,
|
|
1172
|
+
sources,
|
|
1173
|
+
compatibility: !asyncApi && supportsRedoclyMigration(ruleset.data)
|
|
1174
|
+
});
|
|
1175
|
+
} catch (error) {
|
|
1176
|
+
return err(error instanceof Error ? error.message : String(error));
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1055
1180
|
// src/domains/document/lint/paths.ts
|
|
1056
1181
|
import { realpath, stat } from "node:fs/promises";
|
|
1057
1182
|
import { basename, dirname as dirname4, join as join3, resolve as resolve4 } from "node:path";
|
|
@@ -1147,6 +1272,15 @@ function markdownCell(value) {
|
|
|
1147
1272
|
);
|
|
1148
1273
|
return [...result.parts, escapeText(value.slice(result.offset))].join("");
|
|
1149
1274
|
}
|
|
1275
|
+
function markdownText(value) {
|
|
1276
|
+
const text = value.replace(/\r\n?|\n/g, " ");
|
|
1277
|
+
const longestDelimiter = Math.max(
|
|
1278
|
+
0,
|
|
1279
|
+
...[...text.matchAll(/`+/g)].map((match) => match[0].length)
|
|
1280
|
+
);
|
|
1281
|
+
const delimiter = "`".repeat(longestDelimiter + 1);
|
|
1282
|
+
return `${delimiter} ${text} ${delimiter}`;
|
|
1283
|
+
}
|
|
1150
1284
|
|
|
1151
1285
|
// src/domains/document/lint/report.ts
|
|
1152
1286
|
function escapeCommand(value, property = false) {
|
|
@@ -1179,13 +1313,22 @@ function renderReport(diagnostics, engine, format, ignored = 0, color = true, op
|
|
|
1179
1313
|
warnings: diagnostics.filter((finding) => finding.severity === 1).length,
|
|
1180
1314
|
ignored
|
|
1181
1315
|
};
|
|
1182
|
-
const
|
|
1316
|
+
const executionFailure = (diagnostic) => Boolean(options.apis && diagnostic.code === "scalar-lint-execution");
|
|
1317
|
+
const selected = [
|
|
1318
|
+
...diagnostics.filter(executionFailure),
|
|
1319
|
+
...diagnostics.filter((diagnostic) => !executionFailure(diagnostic)).sort((a, b) => a.severity - b.severity).slice(0, options.maxProblems ?? 100)
|
|
1320
|
+
];
|
|
1183
1321
|
const findings = selected.map((diagnostic) => {
|
|
1322
|
+
const coordinateEngine = diagnostic.engine ?? engine;
|
|
1184
1323
|
const position = (point) => ({
|
|
1185
|
-
line: Math.max(1, point.line + (
|
|
1186
|
-
col: Math.max(
|
|
1324
|
+
line: Math.max(1, point.line + (coordinateEngine === "spectral" ? 1 : 0)),
|
|
1325
|
+
col: Math.max(
|
|
1326
|
+
1,
|
|
1327
|
+
point.character + (coordinateEngine === "spectral" ? 1 : 0)
|
|
1328
|
+
)
|
|
1187
1329
|
});
|
|
1188
1330
|
return {
|
|
1331
|
+
...diagnostic.api ? { api: displaySource(diagnostic.api) } : {},
|
|
1189
1332
|
ruleId: String(diagnostic.code),
|
|
1190
1333
|
severity: ["error", "warn", "info", "hint"][diagnostic.severity],
|
|
1191
1334
|
message: diagnostic.message,
|
|
@@ -1205,8 +1348,15 @@ function renderReport(diagnostics, engine, format, ignored = 0, color = true, op
|
|
|
1205
1348
|
...diagnostic.documentationUrl ? { reference: diagnostic.documentationUrl } : {}
|
|
1206
1349
|
};
|
|
1207
1350
|
});
|
|
1351
|
+
const apis = options.apis?.map((api) => ({
|
|
1352
|
+
...api,
|
|
1353
|
+
source: displaySource(api.source)
|
|
1354
|
+
}));
|
|
1355
|
+
const apiStatus = apis?.map(
|
|
1356
|
+
(api) => `${api.source}: ${api.status} (${api.errors} errors, ${api.warnings} warnings, ${api.ignored} ignored)${api.error ? `: ${api.error}` : ""}`
|
|
1357
|
+
).map((line) => format === "markdown" ? markdownText(line) : line).join("\n");
|
|
1208
1358
|
if (format === "json") {
|
|
1209
|
-
return `${JSON.stringify({ totals, version: options.version ?? "1", problems: findings }, null, 2)}
|
|
1359
|
+
return `${JSON.stringify({ totals, version: options.version ?? "1", problems: findings, ...apis ? { apis } : {} }, null, 2)}
|
|
1210
1360
|
`;
|
|
1211
1361
|
}
|
|
1212
1362
|
if (format === "github-actions") {
|
|
@@ -1216,7 +1366,7 @@ function renderReport(diagnostics, engine, format, ignored = 0, color = true, op
|
|
|
1216
1366
|
const remote = /^https?:\/\//.test(source);
|
|
1217
1367
|
const file = remote ? "" : `file=${escapeCommand(displaySource(source, process.env.GITHUB_WORKSPACE ?? process.cwd()), true)},line=${location.start.line},col=${location.start.col},endLine=${location.end.line},endColumn=${location.end.col},`;
|
|
1218
1368
|
const level = finding.severity === "error" ? "error" : finding.severity === "warn" ? "warning" : "notice";
|
|
1219
|
-
const message = `${remote ? `${source}: ` : ""}${finding.message}${finding.reference ? `
|
|
1369
|
+
const message = `${finding.api ? `[${finding.api}] ` : ""}${remote ? `${source}: ` : ""}${finding.message}${finding.reference ? `
|
|
1220
1370
|
|
|
1221
1371
|
Reference: ${finding.reference}` : ""}`;
|
|
1222
1372
|
return `::${level} ${file}title=${escapeCommand(finding.ruleId, true)}::${escapeCommand(message)}
|
|
@@ -1227,8 +1377,10 @@ Reference: ${finding.reference}` : ""}`;
|
|
|
1227
1377
|
if (format === "junit") {
|
|
1228
1378
|
const groups = groupBy(
|
|
1229
1379
|
findings,
|
|
1230
|
-
(finding) => finding.location[0]?.source.ref ?? ""
|
|
1380
|
+
(finding) => finding.api ?? finding.location[0]?.source.ref ?? ""
|
|
1231
1381
|
);
|
|
1382
|
+
for (const api of apis ?? [])
|
|
1383
|
+
if (!groups.has(api.source)) groups.set(api.source, []);
|
|
1232
1384
|
const suites = [...groups].map(([source, problems]) => {
|
|
1233
1385
|
const errors = problems.filter(
|
|
1234
1386
|
(problem) => problem.severity === "error"
|
|
@@ -1246,9 +1398,9 @@ Pointer: ${point.pointer}`).join("\n")}`
|
|
|
1246
1398
|
);
|
|
1247
1399
|
const element = problem.severity === "error" ? "error" : problem.severity === "warn" ? "failure" : "system-out";
|
|
1248
1400
|
const result = element === "system-out" ? `<system-out>${details}</system-out>` : `<${element} message="${escapeXml(problem.message)}" type="${escapeXml(problem.ruleId)}">${details}</${element}>`;
|
|
1249
|
-
return ` <testcase classname="${escapeXml(problem.ruleId)}" name="${escapeXml(problem.ruleId)}" file="${escapeXml(source)}" line="${location.start.line}">${result}</testcase>`;
|
|
1401
|
+
return ` <testcase classname="${escapeXml(problem.ruleId)}" name="${escapeXml(problem.ruleId)}" file="${escapeXml(location.source.ref)}" line="${location.start.line}">${result}</testcase>`;
|
|
1250
1402
|
}).join("\n");
|
|
1251
|
-
return ` <testsuite name="${escapeXml(source)}" tests="${problems.length}" errors="${errors}" failures="${warnings}" skipped="0"
|
|
1403
|
+
return ` <testsuite name="${escapeXml(source)}" tests="${problems.length}" errors="${errors}" failures="${warnings}" skipped="0">${apis ? `<properties><property name="status" value="${escapeXml(apis.find((api) => api.source === source)?.status ?? "")}"/><property name="executionError" value="${escapeXml(apis.find((api) => api.source === source)?.error ?? "")}"/></properties>` : ""}
|
|
1252
1404
|
${cases}
|
|
1253
1405
|
</testsuite>`;
|
|
1254
1406
|
}).join("\n");
|
|
@@ -1265,7 +1417,9 @@ ${suites}
|
|
|
1265
1417
|
</testsuites>
|
|
1266
1418
|
`;
|
|
1267
1419
|
}
|
|
1268
|
-
const status = `${
|
|
1420
|
+
const status = `${apiStatus ? `${apiStatus}
|
|
1421
|
+
|
|
1422
|
+
` : ""}${totals.errors ? "Validation failed" : "Validation successful"}
|
|
1269
1423
|
Errors: ${totals.errors}
|
|
1270
1424
|
Warnings: ${totals.warnings}
|
|
1271
1425
|
Ignored: ${ignored}
|
|
@@ -1273,7 +1427,7 @@ Ignored: ${ignored}
|
|
|
1273
1427
|
if (format === "markdown") {
|
|
1274
1428
|
const groups = groupBy(
|
|
1275
1429
|
findings,
|
|
1276
|
-
(finding) => finding.location[0]?.source.ref ?? ""
|
|
1430
|
+
(finding) => finding.api ?? finding.location[0]?.source.ref ?? ""
|
|
1277
1431
|
);
|
|
1278
1432
|
return `${[...groups].map(
|
|
1279
1433
|
([source, problems]) => `## Lint: ${markdownCell(source)}
|
|
@@ -1293,14 +1447,16 @@ ${status}`;
|
|
|
1293
1447
|
if (format === "summary") {
|
|
1294
1448
|
const groups = groupBy(
|
|
1295
1449
|
findings,
|
|
1296
|
-
(finding) => `${finding.severity} ${finding.ruleId}`
|
|
1450
|
+
(finding) => `${finding.api ? `${finding.api}: ` : ""}${finding.severity} ${finding.ruleId}`
|
|
1297
1451
|
);
|
|
1298
1452
|
return `${[...groups].map(([key, problems]) => `${key}: ${problems.length}
|
|
1299
1453
|
`).join("")}
|
|
1300
1454
|
${status}`;
|
|
1301
1455
|
}
|
|
1302
1456
|
if (format === "codeframe") {
|
|
1303
|
-
const coloredStatus = `${
|
|
1457
|
+
const coloredStatus = `${apiStatus ? `${apiStatus}
|
|
1458
|
+
|
|
1459
|
+
` : ""}${totals.errors ? as2.red("Validation failed") : as2.green("Validation successful")}
|
|
1304
1460
|
Errors: ${as2.red(String(totals.errors))}
|
|
1305
1461
|
Warnings: ${as2.yellow(String(totals.warnings))}
|
|
1306
1462
|
Ignored: ${ignored}
|
|
@@ -1320,7 +1476,8 @@ Ignored: ${ignored}
|
|
|
1320
1476
|
location.start.col,
|
|
1321
1477
|
color ? highlight : void 0
|
|
1322
1478
|
);
|
|
1323
|
-
return
|
|
1479
|
+
return `${finding.api ? `API: ${finding.api}
|
|
1480
|
+
` : ""}[${index + 1}] ${as2.bold(`${location.source.ref}:${location.start.line}:${location.start.col}`)} ${as2.gray(`at ${location.pointer}`)}
|
|
1324
1481
|
|
|
1325
1482
|
${highlight(finding.message)}
|
|
1326
1483
|
|
|
@@ -1335,10 +1492,12 @@ ${coloredStatus}`;
|
|
|
1335
1492
|
return color ? text2 : stripVTControlCharacters(text2);
|
|
1336
1493
|
}
|
|
1337
1494
|
const lines = [
|
|
1495
|
+
...apiStatus ? [apiStatus, ""] : [],
|
|
1338
1496
|
diagnostics.length ? (totals.errors ? as2.red : as2.yellow)("\u2716 Lint issues found:") : as2.green("\u2714 No lint issues found.")
|
|
1339
1497
|
];
|
|
1340
1498
|
for (const diagnostic of selected) {
|
|
1341
1499
|
lines.push(
|
|
1500
|
+
...diagnostic.api ? [`API: ${displaySource(diagnostic.api)}`] : [],
|
|
1342
1501
|
`${as2.yellow("Rule:")} ${as2.bold(String(diagnostic.code))}`,
|
|
1343
1502
|
`${as2.yellow("Message:")} ${diagnostic.message}`,
|
|
1344
1503
|
`${as2.yellow("Severity:")} ${severityToLabel(diagnostic.severity)}`,
|
|
@@ -1358,75 +1517,234 @@ ${coloredStatus}`;
|
|
|
1358
1517
|
return color ? text : stripVTControlCharacters(text);
|
|
1359
1518
|
}
|
|
1360
1519
|
|
|
1361
|
-
// src/domains/document/lint/
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1520
|
+
// src/domains/document/lint/batch.ts
|
|
1521
|
+
async function runBatchLint(inputs, options) {
|
|
1522
|
+
const selectedIgnore = await selectIgnorePath(
|
|
1523
|
+
options.ignoreFile,
|
|
1524
|
+
Boolean(options.generateIgnoreFile)
|
|
1525
|
+
);
|
|
1526
|
+
if (!selectedIgnore.ok) return selectedIgnore;
|
|
1527
|
+
const ignorePath = selectedIgnore.data;
|
|
1528
|
+
const loaded = await loadBaseline(
|
|
1529
|
+
ignorePath,
|
|
1530
|
+
Boolean(options.ignoreFile && !options.generateIgnoreFile)
|
|
1531
|
+
);
|
|
1532
|
+
if (!loaded.ok) return loaded;
|
|
1533
|
+
const outputPath = options.output ? resolve5(options.output) : void 0;
|
|
1534
|
+
const destinations = [
|
|
1535
|
+
...outputPath ? [outputPath] : [],
|
|
1536
|
+
...options.generateIgnoreFile ? [ignorePath] : []
|
|
1537
|
+
];
|
|
1538
|
+
const sources = /* @__PURE__ */ new Map();
|
|
1539
|
+
const results = [];
|
|
1540
|
+
for (const source of inputs.sources) {
|
|
1541
|
+
const content = await readSource(source);
|
|
1542
|
+
if (!content.ok) {
|
|
1543
|
+
sources.set(source, void 0);
|
|
1544
|
+
results.push({ source, result: content });
|
|
1545
|
+
continue;
|
|
1372
1546
|
}
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
);
|
|
1547
|
+
const collected = await collectSources(source, content.data);
|
|
1548
|
+
for (const [path, text] of collected) sources.set(path, text);
|
|
1549
|
+
const result = await lintDocument(source, options.rule, collected);
|
|
1550
|
+
results.push({ source, result });
|
|
1378
1551
|
}
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1552
|
+
const referenced = results.flatMap(
|
|
1553
|
+
({ result }) => result.ok ? result.data.diagnostics.flatMap((diagnostic) => [
|
|
1554
|
+
...diagnostic.source ? [diagnostic.source] : [],
|
|
1555
|
+
...(diagnostic.locations ?? []).map((location) => location.source)
|
|
1556
|
+
]) : []
|
|
1557
|
+
);
|
|
1558
|
+
const protectedPaths = [
|
|
1559
|
+
...inputs.sources,
|
|
1560
|
+
...sources.keys(),
|
|
1561
|
+
...referenced,
|
|
1562
|
+
...inputs.configPath ? [inputs.configPath] : [],
|
|
1563
|
+
...options.rule && !/^https?:\/\//.test(options.rule) ? [resolve5(options.rule)] : [],
|
|
1564
|
+
...!options.generateIgnoreFile ? [ignorePath] : []
|
|
1565
|
+
].filter((source) => !/^https?:\/\//.test(source));
|
|
1566
|
+
const checked = await checkOutputPaths(protectedPaths, destinations);
|
|
1567
|
+
if (!checked.ok) return checked;
|
|
1568
|
+
let baseline = loaded.data;
|
|
1569
|
+
if (options.generateIgnoreFile) {
|
|
1570
|
+
for (const entry of results) {
|
|
1571
|
+
if (!entry.result.ok) continue;
|
|
1572
|
+
const written = await regenerateBaseline(
|
|
1573
|
+
ignorePath,
|
|
1574
|
+
entry.source,
|
|
1575
|
+
entry.result.data.diagnostics
|
|
1576
|
+
);
|
|
1577
|
+
if (!written.ok) {
|
|
1578
|
+
entry.result = err(written.error);
|
|
1579
|
+
continue;
|
|
1580
|
+
}
|
|
1581
|
+
baseline = written.data;
|
|
1393
1582
|
}
|
|
1583
|
+
if (results.some(({ result }) => result.ok))
|
|
1584
|
+
process.stderr.write(`Ignore baseline written to ${ignorePath}
|
|
1585
|
+
`);
|
|
1394
1586
|
}
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1587
|
+
if (results.some(({ result }) => result.ok && result.data.compatibility)) {
|
|
1588
|
+
const unsupported = unsupportedRedoclyRules(baseline);
|
|
1589
|
+
if (unsupported.length)
|
|
1590
|
+
process.stderr.write(
|
|
1591
|
+
`Ignore file contains rules outside the supported Redocly migration subset: ${unsupported.join(", ")}. Only exact Scalar rule/pointer matches apply to these entries.
|
|
1592
|
+
`
|
|
1593
|
+
);
|
|
1594
|
+
}
|
|
1595
|
+
const diagnostics = [];
|
|
1596
|
+
const apis = [];
|
|
1597
|
+
for (const { source, result } of results) {
|
|
1598
|
+
if (!result.ok) {
|
|
1599
|
+
apis.push({
|
|
1600
|
+
source,
|
|
1601
|
+
status: "failed",
|
|
1602
|
+
errors: 0,
|
|
1603
|
+
warnings: 0,
|
|
1604
|
+
ignored: 0,
|
|
1605
|
+
error: result.error
|
|
1606
|
+
});
|
|
1607
|
+
diagnostics.push({
|
|
1608
|
+
api: source,
|
|
1609
|
+
source,
|
|
1610
|
+
code: "scalar-lint-execution",
|
|
1611
|
+
severity: 0,
|
|
1612
|
+
message: result.error,
|
|
1613
|
+
path: [],
|
|
1614
|
+
range: {
|
|
1615
|
+
start: { line: 0, character: 0 },
|
|
1616
|
+
end: { line: 0, character: 0 }
|
|
1617
|
+
}
|
|
1618
|
+
});
|
|
1619
|
+
continue;
|
|
1620
|
+
}
|
|
1621
|
+
const active = applyBaseline(
|
|
1622
|
+
baseline,
|
|
1623
|
+
result.data.diagnostics,
|
|
1624
|
+
ignorePath,
|
|
1625
|
+
result.data.compatibility
|
|
1626
|
+
);
|
|
1627
|
+
const errors = active.filter(
|
|
1628
|
+
(diagnostic) => diagnostic.severity === 0
|
|
1629
|
+
).length;
|
|
1630
|
+
apis.push({
|
|
1631
|
+
source,
|
|
1632
|
+
status: errors ? "invalid" : "valid",
|
|
1633
|
+
errors,
|
|
1634
|
+
warnings: active.filter((diagnostic) => diagnostic.severity === 1).length,
|
|
1635
|
+
ignored: result.data.diagnostics.length - active.length
|
|
1636
|
+
});
|
|
1637
|
+
diagnostics.push(
|
|
1638
|
+
...active.map((diagnostic) => ({
|
|
1639
|
+
...diagnostic,
|
|
1640
|
+
engine: result.data.engine,
|
|
1641
|
+
api: source
|
|
1642
|
+
}))
|
|
1402
1643
|
);
|
|
1403
|
-
url.hash = "";
|
|
1404
|
-
return ["file:", "https:", "http:"].includes(url.protocol) ? ok(url.protocol === "file:" ? fileURLToPath4(url) : url.href) : err("Unsupported reference protocol");
|
|
1405
|
-
} catch {
|
|
1406
|
-
return err("Invalid reference URI");
|
|
1407
1644
|
}
|
|
1645
|
+
const report = renderReport(
|
|
1646
|
+
diagnostics,
|
|
1647
|
+
"spectral",
|
|
1648
|
+
options.format ?? "codeframe",
|
|
1649
|
+
apis.reduce((sum, api) => sum + api.ignored, 0),
|
|
1650
|
+
!outputPath,
|
|
1651
|
+
{
|
|
1652
|
+
maxProblems: options.maxProblems,
|
|
1653
|
+
sources,
|
|
1654
|
+
version: package_default.version,
|
|
1655
|
+
apis
|
|
1656
|
+
}
|
|
1657
|
+
);
|
|
1658
|
+
const findingCount = diagnostics.length - apis.filter((api) => api.status === "failed").length;
|
|
1659
|
+
if (findingCount > (options.maxProblems ?? 100))
|
|
1660
|
+
process.stderr.write(
|
|
1661
|
+
`${findingCount - (options.maxProblems ?? 100)} more problems hidden; increase with --max-problems N
|
|
1662
|
+
`
|
|
1663
|
+
);
|
|
1664
|
+
if (outputPath) {
|
|
1665
|
+
const written = await writeFile2(outputPath, report, "utf8").then(() => ok(void 0)).catch(() => err(`Could not write report: ${outputPath}`));
|
|
1666
|
+
if (!written.ok) return written;
|
|
1667
|
+
} else process.stdout.write(report);
|
|
1668
|
+
process.exitCode = apis.some((api) => api.status !== "valid") ? 1 : 0;
|
|
1669
|
+
return ok(void 0);
|
|
1408
1670
|
}
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1671
|
+
|
|
1672
|
+
// src/domains/document/lint/inputs.ts
|
|
1673
|
+
import { readdir, readFile as readFile4 } from "node:fs/promises";
|
|
1674
|
+
import { dirname as dirname5, join as join4, resolve as resolve6 } from "node:path";
|
|
1675
|
+
function normalizeInput(input) {
|
|
1676
|
+
return /^https?:\/\//.test(input) ? URL.parse(input)?.href ?? input : resolve6(input);
|
|
1677
|
+
}
|
|
1678
|
+
async function resolveLintInputs(inputs, explicitConfig) {
|
|
1679
|
+
if (inputs.length) {
|
|
1680
|
+
if (explicitConfig !== void 0)
|
|
1681
|
+
return err(
|
|
1682
|
+
"Use either API description inputs or --config <file>, not both."
|
|
1683
|
+
);
|
|
1684
|
+
return ok({ sources: [...new Set(inputs.map(normalizeInput))] });
|
|
1685
|
+
}
|
|
1686
|
+
const discovered = explicitConfig === void 0 ? await discoverConfig() : ok(resolve6(explicitConfig));
|
|
1687
|
+
if (!discovered.ok) return discovered;
|
|
1688
|
+
const configPath = discovered.data;
|
|
1689
|
+
const content = await readFile4(configPath, "utf8").catch(() => void 0);
|
|
1690
|
+
if (content === void 0)
|
|
1691
|
+
return err(`Could not read Scalar configuration file: ${configPath}`);
|
|
1692
|
+
const parsed = parseConfig(content);
|
|
1693
|
+
if (!parsed.success)
|
|
1694
|
+
return err(
|
|
1695
|
+
`Invalid Scalar configuration file ${configPath}: ${parsed.errorType === "validation" ? prettifyError(parsed.error) : String(parsed.error)}`
|
|
1696
|
+
);
|
|
1697
|
+
const configuration = parsed.config.version === ConfigVersion.V1 ? legacyTo2_0_0(parsed.config).upgradedConfig : parsed.config;
|
|
1698
|
+
const root = resolve6(dirname5(configPath), configuration.root ?? ".");
|
|
1699
|
+
const sources = /* @__PURE__ */ new Set();
|
|
1700
|
+
const visit = (entry) => {
|
|
1701
|
+
if (typeof entry === "string") return;
|
|
1702
|
+
if (entry.type === "group") {
|
|
1703
|
+
Object.values(entry.children).forEach(visit);
|
|
1704
|
+
} else if (entry.type === "openapi") {
|
|
1705
|
+
if ("filepath" in entry && entry.filepath)
|
|
1706
|
+
sources.add(join4(root, entry.filepath));
|
|
1707
|
+
else if ("url" in entry) sources.add(normalizeInput(entry.url));
|
|
1708
|
+
else if ("namespace" in entry && entry.namespace && entry.slug) {
|
|
1709
|
+
sources.add(
|
|
1710
|
+
new URL(
|
|
1711
|
+
registry(entry.namespace).apis(entry.slug).url({ version: entry.version }),
|
|
1712
|
+
`${config.projects.registry}/`
|
|
1713
|
+
).href
|
|
1714
|
+
);
|
|
1418
1715
|
}
|
|
1419
|
-
})();
|
|
1420
|
-
for (const ref of refs) {
|
|
1421
|
-
const resolved = referenceSource(ref, current);
|
|
1422
|
-
if (!resolved.ok) continue;
|
|
1423
|
-
const target = resolved.data;
|
|
1424
|
-
if (sources.has(target)) continue;
|
|
1425
|
-
const loaded = await readSource(target);
|
|
1426
|
-
sources.set(target, loaded.ok ? loaded.data : void 0);
|
|
1427
1716
|
}
|
|
1428
|
-
}
|
|
1429
|
-
|
|
1717
|
+
};
|
|
1718
|
+
const navigations = "navigation" in configuration ? [configuration.navigation] : Object.values(configuration.versions);
|
|
1719
|
+
for (const navigation of navigations)
|
|
1720
|
+
Object.values(navigation.routes).forEach(visit);
|
|
1721
|
+
if (!sources.size)
|
|
1722
|
+
return err(
|
|
1723
|
+
`No API descriptions found in ${configPath}. Add openapi or asyncapi navigation entries, or pass API description files or URLs.`
|
|
1724
|
+
);
|
|
1725
|
+
return ok({ sources: [...sources], configPath });
|
|
1726
|
+
}
|
|
1727
|
+
async function discoverConfig() {
|
|
1728
|
+
const entries = await readdir(process.cwd(), { withFileTypes: true }).catch(
|
|
1729
|
+
() => void 0
|
|
1730
|
+
);
|
|
1731
|
+
if (!entries)
|
|
1732
|
+
return err(
|
|
1733
|
+
"Could not search the current directory for a Scalar configuration file. Use --config <file>."
|
|
1734
|
+
);
|
|
1735
|
+
const candidates = entries.filter(
|
|
1736
|
+
(entry) => (entry.isFile() || entry.isSymbolicLink()) && isConfigFileName(entry.name)
|
|
1737
|
+
).map((entry) => entry.name).sort();
|
|
1738
|
+
const candidate = candidates[0];
|
|
1739
|
+
if (!candidate)
|
|
1740
|
+
return err(
|
|
1741
|
+
"No Scalar configuration file found in the current directory. Pass API description files or URLs, or use --config <file>."
|
|
1742
|
+
);
|
|
1743
|
+
if (candidates.length > 1)
|
|
1744
|
+
return err(
|
|
1745
|
+
`Multiple Scalar configuration files found: ${candidates.join(", ")}. Select one with --config <file>.`
|
|
1746
|
+
);
|
|
1747
|
+
return ok(resolve6(candidate));
|
|
1430
1748
|
}
|
|
1431
1749
|
|
|
1432
1750
|
// src/domains/document/lint/action.ts
|
|
@@ -1436,27 +1754,31 @@ function fail(message) {
|
|
|
1436
1754
|
process.exitCode = 1;
|
|
1437
1755
|
}
|
|
1438
1756
|
async function lintAction(inputArgument, options) {
|
|
1439
|
-
if (!inputArgument) {
|
|
1440
|
-
fail("Missing input\n\nProvide an API description file path or URL.\n");
|
|
1441
|
-
return;
|
|
1442
|
-
}
|
|
1443
1757
|
try {
|
|
1444
|
-
|
|
1758
|
+
const inputs = typeof inputArgument === "string" ? [inputArgument] : inputArgument ?? [];
|
|
1759
|
+
const selected = await resolveLintInputs(inputs, options.config);
|
|
1760
|
+
if (!selected.ok) return fail(selected.error);
|
|
1761
|
+
const source = selected.data.sources[0];
|
|
1762
|
+
if (inputs.length === 1 && source) await runLint(source, options);
|
|
1763
|
+
else {
|
|
1764
|
+
const result = await runBatchLint(selected.data, options);
|
|
1765
|
+
if (!result.ok) fail(result.error);
|
|
1766
|
+
}
|
|
1445
1767
|
} catch (error) {
|
|
1446
1768
|
fail(error instanceof Error ? error.message : String(error));
|
|
1447
1769
|
}
|
|
1448
1770
|
}
|
|
1449
1771
|
async function runLint(inputArgument, options) {
|
|
1450
1772
|
const remote = (path) => /^https?:\/\//.test(path);
|
|
1451
|
-
const source = remote(inputArgument) ? new URL(inputArgument).href :
|
|
1773
|
+
const source = remote(inputArgument) ? new URL(inputArgument).href : resolve7(inputArgument);
|
|
1452
1774
|
const selectedIgnore = await selectIgnorePath(
|
|
1453
1775
|
options.ignoreFile,
|
|
1454
1776
|
Boolean(options.generateIgnoreFile)
|
|
1455
1777
|
);
|
|
1456
1778
|
if (!selectedIgnore.ok) return fail(selectedIgnore.error);
|
|
1457
1779
|
const ignorePath = selectedIgnore.data;
|
|
1458
|
-
const outputPath = options.output ?
|
|
1459
|
-
const inputs = [source, ...options.rule ? [options.rule] : []].filter((path) => !remote(path)).map((path) =>
|
|
1780
|
+
const outputPath = options.output ? resolve7(options.output) : void 0;
|
|
1781
|
+
const inputs = [source, ...options.rule ? [options.rule] : []].filter((path) => !remote(path)).map((path) => resolve7(path));
|
|
1460
1782
|
const destinations = [
|
|
1461
1783
|
...outputPath ? [outputPath] : [],
|
|
1462
1784
|
...options.generateIgnoreFile ? [ignorePath] : []
|
|
@@ -1471,32 +1793,9 @@ async function runLint(inputArgument, options) {
|
|
|
1471
1793
|
Boolean(options.ignoreFile && !options.generateIgnoreFile)
|
|
1472
1794
|
);
|
|
1473
1795
|
if (!loaded.ok) return fail(loaded.error);
|
|
1474
|
-
const
|
|
1475
|
-
if (!
|
|
1476
|
-
const
|
|
1477
|
-
ok: true,
|
|
1478
|
-
data: isAsyncApiContent(specification.data) ? ASYNCAPI_RULESET : DEFAULT_RULESET
|
|
1479
|
-
};
|
|
1480
|
-
if (!ruleset.ok) return fail(ruleset.error);
|
|
1481
|
-
const sources = await collectSources(source, specification.data);
|
|
1482
|
-
const result = await lint({
|
|
1483
|
-
document: specification.data,
|
|
1484
|
-
ruleset: ruleset.data,
|
|
1485
|
-
source
|
|
1486
|
-
});
|
|
1487
|
-
if (!result.ok)
|
|
1488
|
-
return fail(`Could not lint the API description
|
|
1489
|
-
|
|
1490
|
-
${result.error}`);
|
|
1491
|
-
const diagnostics = addRelatedLocations(
|
|
1492
|
-
resolveDiagnosticPaths(
|
|
1493
|
-
result.data.diagnostics,
|
|
1494
|
-
sources,
|
|
1495
|
-
result.data.engine
|
|
1496
|
-
),
|
|
1497
|
-
sources,
|
|
1498
|
-
result.data.engine
|
|
1499
|
-
);
|
|
1796
|
+
const result = await lintDocument(source, options.rule);
|
|
1797
|
+
if (!result.ok) return fail(result.error);
|
|
1798
|
+
const { diagnostics, sources, compatibility } = result.data;
|
|
1500
1799
|
const referencedPaths = [
|
|
1501
1800
|
...sources.keys(),
|
|
1502
1801
|
...diagnostics.flatMap((diagnostic) => [
|
|
@@ -1521,9 +1820,6 @@ ${result.error}`);
|
|
|
1521
1820
|
process.stderr.write(`Ignore baseline written to ${ignorePath}
|
|
1522
1821
|
`);
|
|
1523
1822
|
}
|
|
1524
|
-
const compatibility = Boolean(
|
|
1525
|
-
!isAsyncApiContent(specification.data) && supportsRedoclyMigration(ruleset.data)
|
|
1526
|
-
);
|
|
1527
1823
|
if (compatibility) {
|
|
1528
1824
|
const unsupported = unsupportedRedoclyRules(baseline);
|
|
1529
1825
|
if (unsupported.length)
|