@scalar/cli 2.17.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 +77 -3
- package/chunks/{action-Y4L4OPPH.js → action-2WVJ2WEW.js} +15 -16
- package/chunks/{action-UOUS2G2B.js → action-4BDZ2CXP.js} +6 -7
- package/chunks/{action-52ZSECW2.js → action-5AXDB5VN.js} +14 -15
- package/chunks/{action-7PVWQJ7V.js → action-6I3TJLRO.js} +28 -26
- package/chunks/{action-MPBZUWQ2.js → action-6WZN63LT.js} +20 -19
- package/chunks/{action-5NYKJDBG.js → action-7GZGVTYI.js} +1067 -766
- package/chunks/{action-2Y6UTXAG.js → action-7ZCJAWI2.js} +5 -5
- package/chunks/{action-UWVZ6V7B.js → action-A6YOY24G.js} +5 -5
- package/chunks/{action-KJJ35X2T.js → action-AGGHY4N2.js} +21 -20
- package/chunks/{action-MKZYWAXH.js → action-BVZOA2G5.js} +9 -10
- package/chunks/{action-5TQHROY5.js → action-BYDJ4R4F.js} +14 -15
- package/chunks/{action-VJDK7ROI.js → action-CBAL5CYB.js} +16 -17
- package/chunks/action-COFGCCYS.js +89 -0
- package/chunks/action-CYU5Q5LT.js +254 -0
- package/chunks/{action-XMANK72Z.js → action-DTQH6CKR.js} +24 -23
- package/chunks/{action-AFZE62DU.js → action-ED5WLLIE.js} +6 -7
- package/chunks/action-F4CQ4DW3.js +654 -0
- package/chunks/{action-TA6VOPQ5.js → action-GVW5PSPT.js} +17 -18
- package/chunks/{action-TTDV5NIP.js → action-H5VRLFCY.js} +5 -5
- package/chunks/{action-2SZMLOYJ.js → action-IWEAWDT3.js} +9 -10
- package/chunks/{action-3EATJRIK.js → action-JN6JVQUV.js} +14 -15
- package/chunks/{action-WPUITVPF.js → action-KVM76ZKQ.js} +11 -11
- package/chunks/{action-PS4F4QWI.js → action-LLMFEBHH.js} +3 -4
- package/chunks/{action-WUMW2GH6.js → action-PEG7AUA6.js} +14 -13
- package/chunks/{action-UVWH2KZY.js → action-QC72LLYE.js} +7 -5
- package/chunks/{action-P5MGMJG3.js → action-QP6MRSQW.js} +15 -14
- package/chunks/{action-W47MFJSP.js → action-R53K22GE.js} +219 -45
- package/chunks/{action-RWITXLMW.js → action-SJPWL723.js} +5 -4
- package/chunks/{action-SBD4TFRS.js → action-T3R4SDAI.js} +5 -5
- package/chunks/{action-GOKWY3AQ.js → action-TT7ZJNST.js} +20 -19
- package/chunks/{action-LALZGEVR.js → action-UPVVJE26.js} +14 -15
- package/chunks/{action-RM4RZFZV.js → action-V35W4MRK.js} +10 -11
- package/chunks/{action-J3C4LAT5.js → action-W4KGRBRV.js} +5 -5
- package/chunks/{action-VWQNDXZT.js → action-ZIVYJCC2.js} +14 -15
- package/chunks/{action-LI6DZLDZ.js → action-ZU5X2KQX.js} +22 -21
- package/chunks/{actions-GWMW7V2B.js → actions-CERT7BL4.js} +16 -16
- package/chunks/{actions-4X5RTQCM.js → actions-RCIS37Y6.js} +14 -15
- package/chunks/{agent-LB5ZCVUN.js → agent-CT3RRYPJ.js} +7 -7
- package/chunks/{asyncapi-J6VAMIMO.js → asyncapi-DVBS23QW.js} +9 -10
- package/chunks/{build.action-YP46CGLS.js → build.action-7MKMMV6V.js} +16 -17
- package/chunks/{chunk-B3CMXTHQ.js → chunk-36UI2W7I.js} +1 -1
- package/chunks/{chunk-4TSZVWGM.js → chunk-4BMOCIYO.js} +19 -4
- package/chunks/{chunk-SKXMYIA2.js → chunk-5EO2CVAR.js} +4 -4
- package/chunks/{chunk-4YQNT5UH.js → chunk-5GZAGHKL.js} +1 -1
- package/chunks/{chunk-3BZ3VY6H.js → chunk-7LUXRNKV.js} +27 -2
- package/chunks/{chunk-KDEKIOIP.js → chunk-CWCRBWA6.js} +1 -1
- package/chunks/{chunk-YFUHTGGP.js → chunk-CYIGEQOJ.js} +2 -2
- package/chunks/{chunk-I4FQOD5L.js → chunk-DVA4NCCY.js} +3 -3
- package/chunks/{chunk-63AB5A5B.js → chunk-F6E7FJEC.js} +6 -6
- package/chunks/{chunk-2KMYC3ZI.js → chunk-FMPJSW6A.js} +2 -2
- package/chunks/{chunk-IZ3UMB7D.js → chunk-GCVB22TG.js} +2 -2
- package/chunks/{chunk-36AMOACU.js → chunk-GXKNYM54.js} +3 -3
- package/chunks/chunk-I25ZCHBJ.js +22 -0
- package/chunks/{chunk-KYCM5SL3.js → chunk-JBMHCTZD.js} +1 -0
- package/chunks/{chunk-57EMU765.js → chunk-JEOQ7LSA.js} +1 -1
- package/chunks/{chunk-DXEHHE6H.js → chunk-KI2AVDKM.js} +6 -4
- package/chunks/{chunk-QDT27U6O.js → chunk-KSY2GH53.js} +2 -2
- package/chunks/{chunk-LS6XOFVD.js → chunk-KWQVA66H.js} +11 -23
- package/chunks/{chunk-KJT4VYUO.js → chunk-LPWRG2MS.js} +1 -1
- package/chunks/{chunk-NKCM2UH5.js → chunk-LVUMNGVA.js} +1 -1
- package/chunks/{chunk-H6LOSAC7.js → chunk-M34HMQMV.js} +39 -83
- package/chunks/{chunk-XGKFAPON.js → chunk-MRZ5A7LB.js} +4 -4
- package/chunks/chunk-O3I3LTTJ.js +24 -0
- package/chunks/{chunk-XQPB4OGW.js → chunk-O4DJIA6Y.js} +2 -19
- package/chunks/{action-B3IDMFFO.js → chunk-OKLL5422.js} +5 -243
- package/chunks/{chunk-RVZZQKVE.js → chunk-PMDHYIB4.js} +4 -22
- package/chunks/{chunk-BIYCLKLZ.js → chunk-RELSIFIL.js} +1 -1
- package/chunks/{chunk-QLKBXVUV.js → chunk-RGJPENX7.js} +1 -1
- package/chunks/{chunk-3KK72EMO.js → chunk-RHAQ4XBA.js} +3 -3
- package/chunks/{action-S7DB6MBK.js → chunk-RQ7QBRJT.js} +1 -82
- package/chunks/chunk-S3LY7RKP.js +54 -0
- package/chunks/{chunk-X2NZINNR.js → chunk-S5OGPH2P.js} +3 -3
- package/chunks/{chunk-VNMVAJ6X.js → chunk-U36HEOGO.js} +17 -21
- package/chunks/{chunk-CAD672DX.js → chunk-Y3CXAIAG.js} +1 -1
- package/chunks/{chunk-HNWIIDGX.js → chunk-Z7EH7CJC.js} +3 -3
- package/chunks/{chunk-SVDXXGEO.js → chunk-ZMJW5UWU.js} +3 -3
- package/chunks/{chunk-UI5PWDNE.js → chunk-ZQDAPKWX.js} +9 -16
- package/chunks/{create.action-PRZKNQHM.js → create.action-VA2ISPIM.js} +16 -17
- package/chunks/{delete.action-G7OAQBR5.js → delete.action-AIWMDGXV.js} +16 -17
- package/chunks/{delete.action-2BLCOX6Y.js → delete.action-GEN6OD7A.js} +16 -17
- package/chunks/{diff.action-KDOJQVJ5.js → diff.action-LBK656RN.js} +15 -14
- package/chunks/{get.action-IKO22SCB.js → get.action-5SWRNQ3D.js} +15 -14
- package/chunks/{get.action-HSYV5TFY.js → get.action-UPPPGGI4.js} +17 -18
- package/chunks/{list.action-KYIFUM3C.js → list.action-YJZBC25S.js} +17 -18
- package/chunks/{list.action-IVJS7UFI.js → list.action-YOWWC4I4.js} +19 -20
- package/chunks/{publish.action-2QIOUMVY.js → publish.action-QMVDHDJZ.js} +22 -21
- package/chunks/{update.action-XZD6T5UR.js → update.action-GFDZIGEP.js} +17 -18
- package/chunks/{update.action-7QATFOGP.js → update.action-SNGKPS7A.js} +16 -17
- package/index.js +194 -154
- package/package.json +11 -11
- package/skills/scalar-cli/SKILL.md +3 -2
- package/template.md +76 -2
- package/chunks/chunk-YTJ67DWS.js +0 -32
|
@@ -1,733 +1,777 @@
|
|
|
1
1
|
import { createRequire as $$cR } from "node:module"; const require = $$cR(import.meta.url);
|
|
2
|
+
import {
|
|
3
|
+
isConfigFileName,
|
|
4
|
+
legacyTo2_0_0,
|
|
5
|
+
parseConfig
|
|
6
|
+
} from "./chunk-M34HMQMV.js";
|
|
2
7
|
import {
|
|
3
8
|
writeFileAtomic
|
|
4
9
|
} from "./chunk-CNJ4TLU7.js";
|
|
5
10
|
import {
|
|
6
11
|
isAsyncApiContent
|
|
7
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-CWCRBWA6.js";
|
|
8
13
|
import "./chunk-A2MPF6GE.js";
|
|
9
|
-
import "./chunk-QDT27U6O.js";
|
|
10
14
|
import {
|
|
15
|
+
registry
|
|
16
|
+
} from "./chunk-PMDHYIB4.js";
|
|
17
|
+
import "./chunk-O4DJIA6Y.js";
|
|
18
|
+
import "./chunk-KSY2GH53.js";
|
|
19
|
+
import {
|
|
20
|
+
config,
|
|
11
21
|
package_default
|
|
12
|
-
} from "./chunk-
|
|
22
|
+
} from "./chunk-7LUXRNKV.js";
|
|
13
23
|
import {
|
|
14
24
|
err,
|
|
15
25
|
ok
|
|
16
26
|
} from "./chunk-DUIMDHQQ.js";
|
|
17
|
-
import "./chunk-
|
|
18
|
-
import
|
|
19
|
-
|
|
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";
|
|
20
35
|
import "./chunk-3MZYUBJ6.js";
|
|
21
36
|
|
|
22
37
|
// src/domains/document/lint/action.ts
|
|
23
|
-
import { writeFile as
|
|
24
|
-
import { resolve as
|
|
38
|
+
import { writeFile as writeFile3 } from "node:fs/promises";
|
|
39
|
+
import { resolve as resolve7 } from "node:path";
|
|
25
40
|
|
|
26
|
-
//
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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";
|
|
31
47
|
|
|
32
|
-
//
|
|
33
|
-
|
|
34
|
-
|
|
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) {
|
|
35
76
|
try {
|
|
36
|
-
const
|
|
37
|
-
|
|
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
|
+
});
|
|
38
105
|
} catch {
|
|
39
106
|
return false;
|
|
40
107
|
}
|
|
41
108
|
}
|
|
42
|
-
function
|
|
43
|
-
return
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
// ../../packages/api-document-lint/dist/run-spectral.js
|
|
47
|
-
import spectralCore from "@stoplight/spectral-core";
|
|
48
|
-
import spectralParsers from "@stoplight/spectral-parsers";
|
|
49
|
-
import { bundleAndLoadRuleset } from "@stoplight/spectral-ruleset-bundler/with-loader";
|
|
50
|
-
import "@stoplight/spectral-rulesets";
|
|
51
|
-
var { Spectral, Document } = spectralCore;
|
|
52
|
-
var { Yaml } = spectralParsers;
|
|
53
|
-
async function runSpectral(input) {
|
|
54
|
-
const rulesetSource = input.ruleset && input.ruleset.trim().length > 0 ? input.ruleset : defaultRulesetFor(input.document);
|
|
55
|
-
const ruleset = await loadRuleset(rulesetSource);
|
|
56
|
-
if (!ruleset.ok) {
|
|
57
|
-
return ruleset;
|
|
58
|
-
}
|
|
59
|
-
const spectral = new Spectral();
|
|
60
|
-
spectral.setRuleset(ruleset.data);
|
|
61
|
-
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)));
|
|
62
|
-
if (diagnostics instanceof Error) {
|
|
63
|
-
return err(`Spectral failed to lint the document \u2014 ${diagnostics.message}`);
|
|
64
|
-
}
|
|
65
|
-
return ok(diagnostics);
|
|
66
|
-
}
|
|
67
|
-
async function loadRuleset(source) {
|
|
68
|
-
const ruleset = await bundleAndLoadRuleset("/.spectral.yaml", {
|
|
69
|
-
fs: {
|
|
70
|
-
promises: {
|
|
71
|
-
// @ts-expect-error The loader only ever reads our single virtual file.
|
|
72
|
-
async readFile() {
|
|
73
|
-
return source;
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
},
|
|
77
|
-
format: "esm"
|
|
78
|
-
}).catch((error) => error instanceof Error ? error : new Error(String(error)));
|
|
79
|
-
if (ruleset instanceof Error) {
|
|
80
|
-
return err(`Failed to load ruleset \u2014 ${ruleset.message}`);
|
|
81
|
-
}
|
|
82
|
-
return ok(ruleset);
|
|
109
|
+
function isRecord(value) {
|
|
110
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
83
111
|
}
|
|
84
112
|
|
|
85
|
-
//
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
import { fileURLToPath } from "node:url";
|
|
91
|
-
import { promisify } from "node:util";
|
|
92
|
-
|
|
93
|
-
// ../../packages/api-document-lint/dist/normalize-diagnostics.js
|
|
94
|
-
var EMPTY_RANGE = {
|
|
95
|
-
start: { line: 0, character: 0 },
|
|
96
|
-
end: { line: 0, character: 0 }
|
|
97
|
-
};
|
|
98
|
-
function normalizeDiagnostics(raw) {
|
|
99
|
-
if (!Array.isArray(raw)) {
|
|
100
|
-
return [];
|
|
101
|
-
}
|
|
102
|
-
return raw.filter(isObject).map((item) => {
|
|
103
|
-
const source = item.source;
|
|
104
|
-
return {
|
|
105
|
-
code: isCode(item.code) ? item.code : "",
|
|
106
|
-
message: typeof item.message === "string" ? item.message : "",
|
|
107
|
-
severity: normalizeSeverity(item.severity),
|
|
108
|
-
path: Array.isArray(item.path) ? item.path.filter(isSegment) : [],
|
|
109
|
-
range: normalizeRange(item.range),
|
|
110
|
-
...typeof source === "string" ? { source } : {},
|
|
111
|
-
...Array.isArray(item.locations) ? {
|
|
112
|
-
locations: item.locations.filter(isLocation).map((location) => ({
|
|
113
|
-
source: location.source,
|
|
114
|
-
path: location.path,
|
|
115
|
-
range: normalizeRange(location.range)
|
|
116
|
-
}))
|
|
117
|
-
} : {},
|
|
118
|
-
...typeof item.documentationUrl === "string" ? { documentationUrl: item.documentationUrl } : {}
|
|
119
|
-
};
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
function isObject(value) {
|
|
123
|
-
return typeof value === "object" && value !== null;
|
|
124
|
-
}
|
|
125
|
-
function isCode(value) {
|
|
126
|
-
return typeof value === "string" || typeof value === "number";
|
|
127
|
-
}
|
|
128
|
-
function isSegment(value) {
|
|
129
|
-
return typeof value === "string" || typeof value === "number";
|
|
130
|
-
}
|
|
131
|
-
function normalizeRange(range) {
|
|
132
|
-
if (!isObject(range)) {
|
|
133
|
-
return EMPTY_RANGE;
|
|
134
|
-
}
|
|
135
|
-
return {
|
|
136
|
-
start: normalizePosition(range.start),
|
|
137
|
-
end: normalizePosition(range.end)
|
|
138
|
-
};
|
|
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("/");
|
|
139
118
|
}
|
|
140
|
-
function
|
|
141
|
-
if (!isObject(position)) {
|
|
142
|
-
return { line: 0, character: 0 };
|
|
143
|
-
}
|
|
119
|
+
function findingIdentity(diagnostic, directory) {
|
|
144
120
|
return {
|
|
145
|
-
|
|
146
|
-
|
|
121
|
+
source: sourceIdentity(diagnostic.source ?? "", directory),
|
|
122
|
+
rule: String(diagnostic.code),
|
|
123
|
+
path: diagnostic.resolvedPath ?? diagnostic.path
|
|
147
124
|
};
|
|
148
125
|
}
|
|
149
|
-
function
|
|
150
|
-
return
|
|
126
|
+
function findingKey(finding) {
|
|
127
|
+
return JSON.stringify([finding.source, finding.rule, toPointer(finding.path)]);
|
|
151
128
|
}
|
|
152
|
-
function
|
|
153
|
-
return
|
|
129
|
+
function isRecord2(value) {
|
|
130
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
154
131
|
}
|
|
155
|
-
function
|
|
156
|
-
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
|
+
);
|
|
157
136
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
function resolveVacuumBinary(override) {
|
|
165
|
-
const explicit = override ?? process.env.VACUUM_BINARY;
|
|
166
|
-
if (explicit) {
|
|
167
|
-
return isFile(explicit) ? ok(explicit) : err(`vacuum binary not found at ${explicit}`);
|
|
168
|
-
}
|
|
169
|
-
const packaged = resolvePackagedBinary();
|
|
170
|
-
if (packaged) {
|
|
171
|
-
return ok(packaged);
|
|
172
|
-
}
|
|
173
|
-
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;
|
|
174
143
|
}
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
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
|
+
);
|
|
184
153
|
}
|
|
185
|
-
function
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
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);
|
|
190
174
|
}
|
|
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
|
-
|
|
220
|
-
|
|
221
|
-
}
|
|
222
|
-
if (child && typeof child === "object")
|
|
223
|
-
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
|
+
});
|
|
224
205
|
}
|
|
225
206
|
}
|
|
226
|
-
return
|
|
207
|
+
return { version: 1, documents: [...result.values()] };
|
|
227
208
|
}
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
return ok([]);
|
|
236
|
-
}
|
|
237
|
-
if (options.allowExternalReferences === false) {
|
|
238
|
-
const validation = validateInlineDocument(input.document);
|
|
239
|
-
if (!validation.ok)
|
|
240
|
-
return validation;
|
|
241
|
-
}
|
|
242
|
-
const binary = resolveVacuumBinary(options.binaryPath);
|
|
243
|
-
if (!binary.ok) {
|
|
244
|
-
return binary;
|
|
245
|
-
}
|
|
246
|
-
const directory = await mkdtemp(join2(tmpdir(), "scalar-lint-")).catch(() => null);
|
|
247
|
-
if (directory === null) {
|
|
248
|
-
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}`);
|
|
249
216
|
}
|
|
250
|
-
const documentPath = join2(directory, "document.yaml");
|
|
251
|
-
const reportPath = join2(directory, "report.json");
|
|
252
217
|
try {
|
|
253
|
-
const
|
|
254
|
-
if (
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
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
|
+
)
|
|
278
251
|
}
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
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);
|
|
291
287
|
}
|
|
292
|
-
return err(`vacuum failed to lint the document \u2014 ${describeFailure(failure)}`);
|
|
293
288
|
}
|
|
294
|
-
const
|
|
295
|
-
|
|
296
|
-
|
|
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;
|
|
297
295
|
}
|
|
298
|
-
const
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
...diagnostic,
|
|
306
|
-
source: !localSource || resolve(localSource) === documentPath ? input.source : /^https?:\/\//.test(localSource) ? localSource : resolve(localSource)
|
|
307
|
-
};
|
|
308
|
-
}));
|
|
309
|
-
} finally {
|
|
310
|
-
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}`);
|
|
311
303
|
}
|
|
312
304
|
}
|
|
313
|
-
function
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
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);
|
|
321
320
|
}
|
|
322
321
|
}
|
|
323
|
-
|
|
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
|
+
);
|
|
324
333
|
}
|
|
325
|
-
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);
|
|
326
373
|
try {
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
}
|
|
333
|
-
|
|
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);
|
|
334
388
|
}
|
|
335
389
|
}
|
|
336
390
|
|
|
337
|
-
// ../../packages/api-document-lint/dist/
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
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;
|
|
343
405
|
}
|
|
344
|
-
|
|
345
|
-
|
|
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;
|
|
346
423
|
}
|
|
347
|
-
const
|
|
348
|
-
|
|
349
|
-
|
|
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}`);
|
|
350
429
|
}
|
|
351
|
-
|
|
352
|
-
|
|
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}`);
|
|
353
446
|
}
|
|
354
|
-
return
|
|
447
|
+
return ok(ruleset);
|
|
355
448
|
}
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
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 [];
|
|
360
466
|
}
|
|
361
|
-
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
|
+
});
|
|
362
486
|
}
|
|
363
|
-
function
|
|
364
|
-
return
|
|
487
|
+
function isObject(value) {
|
|
488
|
+
return typeof value === "object" && value !== null;
|
|
365
489
|
}
|
|
366
|
-
function
|
|
367
|
-
return typeof value === "
|
|
490
|
+
function isCode(value) {
|
|
491
|
+
return typeof value === "string" || typeof value === "number";
|
|
368
492
|
}
|
|
369
|
-
function
|
|
370
|
-
|
|
371
|
-
const value = parseYaml2(source);
|
|
372
|
-
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
373
|
-
return value;
|
|
374
|
-
}
|
|
375
|
-
return null;
|
|
376
|
-
} catch {
|
|
377
|
-
return null;
|
|
378
|
-
}
|
|
493
|
+
function isSegment(value) {
|
|
494
|
+
return typeof value === "string" || typeof value === "number";
|
|
379
495
|
}
|
|
380
|
-
function
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
return true;
|
|
496
|
+
function normalizeRange(range) {
|
|
497
|
+
if (!isObject(range)) {
|
|
498
|
+
return EMPTY_RANGE;
|
|
384
499
|
}
|
|
385
|
-
return
|
|
500
|
+
return {
|
|
501
|
+
start: normalizePosition(range.start),
|
|
502
|
+
end: normalizePosition(range.end)
|
|
503
|
+
};
|
|
386
504
|
}
|
|
387
|
-
function
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
return false;
|
|
505
|
+
function normalizePosition(position) {
|
|
506
|
+
if (!isObject(position)) {
|
|
507
|
+
return { line: 0, character: 0 };
|
|
391
508
|
}
|
|
392
|
-
|
|
393
|
-
|
|
509
|
+
return {
|
|
510
|
+
line: toCoordinate(position.line),
|
|
511
|
+
character: toCoordinate(position.character)
|
|
512
|
+
};
|
|
394
513
|
}
|
|
395
|
-
function
|
|
396
|
-
|
|
397
|
-
|
|
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);
|
|
398
522
|
}
|
|
399
523
|
|
|
400
|
-
// ../../packages/api-document-lint/dist/
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
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}`);
|
|
408
533
|
}
|
|
409
|
-
|
|
534
|
+
const packaged = resolvePackagedBinary();
|
|
535
|
+
if (packaged) {
|
|
536
|
+
return ok(packaged);
|
|
537
|
+
}
|
|
538
|
+
return ok("vacuum");
|
|
410
539
|
}
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
function toPointer(path) {
|
|
421
|
-
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
|
+
}
|
|
422
549
|
}
|
|
423
|
-
|
|
424
|
-
// src/domains/document/lint/rule-mappings.ts
|
|
425
|
-
import { parse as parse2 } from "yaml";
|
|
426
|
-
var redoclyRuleMappings = {
|
|
427
|
-
"info-contact": { scalar: "info-contact", missingField: "contact" },
|
|
428
|
-
"info-license": { scalar: "info-license", missingField: "license" },
|
|
429
|
-
"operation-description": {
|
|
430
|
-
scalar: "operation-description",
|
|
431
|
-
missingField: "description"
|
|
432
|
-
},
|
|
433
|
-
"operation-operationId": {
|
|
434
|
-
scalar: "operation-operationId",
|
|
435
|
-
missingField: "operationId"
|
|
436
|
-
},
|
|
437
|
-
"operation-operationId-url-safe": {
|
|
438
|
-
scalar: "operation-operationId-valid-in-url"
|
|
439
|
-
},
|
|
440
|
-
"no-path-trailing-slash": { scalar: "path-keys-no-trailing-slash" },
|
|
441
|
-
"path-declaration-must-exist": { scalar: "path-declarations-must-exist" },
|
|
442
|
-
"path-not-include-query": { scalar: "path-not-include-query" },
|
|
443
|
-
"tag-description": { scalar: "tag-description", missingField: "description" },
|
|
444
|
-
"operation-singular-tag": { scalar: "operation-singular-tag" }
|
|
445
|
-
};
|
|
446
|
-
function supportsRedoclyMigration(ruleset) {
|
|
550
|
+
function isFile(path) {
|
|
447
551
|
try {
|
|
448
|
-
|
|
449
|
-
if (!isRecord2(parsed) || Object.keys(parsed).some((key) => !["extends", "rules"].includes(key)))
|
|
450
|
-
return false;
|
|
451
|
-
const extended = Array.isArray(parsed.extends) ? parsed.extends : [parsed.extends];
|
|
452
|
-
if (extended.length !== 1 || extended[0] !== "spectral:oas") return false;
|
|
453
|
-
if (parsed.rules === void 0) return true;
|
|
454
|
-
if (!isRecord2(parsed.rules)) return false;
|
|
455
|
-
const audited = new Set(
|
|
456
|
-
Object.values(redoclyRuleMappings).map((mapping) => mapping.scalar)
|
|
457
|
-
);
|
|
458
|
-
return Object.entries(parsed.rules).every(([rule, value]) => {
|
|
459
|
-
if (!audited.has(rule)) return false;
|
|
460
|
-
const severity = isRecord2(value) && Object.keys(value).length === 1 ? value.severity : value;
|
|
461
|
-
return [
|
|
462
|
-
"error",
|
|
463
|
-
"warn",
|
|
464
|
-
"info",
|
|
465
|
-
"hint",
|
|
466
|
-
"off",
|
|
467
|
-
0,
|
|
468
|
-
1,
|
|
469
|
-
2,
|
|
470
|
-
3,
|
|
471
|
-
-1,
|
|
472
|
-
true,
|
|
473
|
-
false
|
|
474
|
-
].some((allowed) => allowed === severity);
|
|
475
|
-
});
|
|
552
|
+
return statSync(path, { throwIfNoEntry: false })?.isFile() ?? false;
|
|
476
553
|
} catch {
|
|
477
554
|
return false;
|
|
478
555
|
}
|
|
479
556
|
}
|
|
480
|
-
function isRecord2(value) {
|
|
481
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
482
|
-
}
|
|
483
557
|
|
|
484
|
-
//
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
const
|
|
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
|
-
|
|
516
|
-
|
|
517
|
-
return
|
|
518
|
-
(rules) => isRecord3(rules) && Object.values(rules).every(
|
|
519
|
-
(pointers) => Array.isArray(pointers) && pointers.every(
|
|
520
|
-
(pointer) => typeof pointer === "string" && /^#(?:\/(?:[^~]|~[01])*)*$/.test(pointer)
|
|
521
|
-
)
|
|
522
|
-
)
|
|
523
|
-
);
|
|
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);
|
|
524
592
|
}
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
document.findings.push(
|
|
534
|
-
...Object.entries(rules).flatMap(
|
|
535
|
-
([rule, pointers]) => pointers.map((pointer) => ({
|
|
536
|
-
source,
|
|
537
|
-
rule,
|
|
538
|
-
path: pointer === "#" ? [] : pointer.slice(2).split("/").map(
|
|
539
|
-
(segment) => segment.replaceAll("~1", "/").replaceAll("~0", "~")
|
|
540
|
-
)
|
|
541
|
-
}))
|
|
542
|
-
)
|
|
543
|
-
);
|
|
544
|
-
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([]);
|
|
545
601
|
}
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
const
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
const
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
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;
|
|
576
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);
|
|
577
676
|
}
|
|
578
|
-
return { version: 1, documents: [...result.values()] };
|
|
579
677
|
}
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
if (
|
|
584
|
-
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;
|
|
585
686
|
}
|
|
586
|
-
return err(`Could not read ignore file: ${path}`);
|
|
587
687
|
}
|
|
688
|
+
return "no report was written";
|
|
689
|
+
}
|
|
690
|
+
function parseReport(report) {
|
|
588
691
|
try {
|
|
589
|
-
const parsed =
|
|
590
|
-
if (
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
);
|
|
595
|
-
const metadata = content.split("\n").find((line) => line.startsWith(OWNERS_PREFIX));
|
|
596
|
-
const owners = metadata ? JSON.parse(metadata.slice(OWNERS_PREFIX.length)) : void 0;
|
|
597
|
-
if (owners !== void 0 && !isBaseline(owners))
|
|
598
|
-
return err(`Invalid ownership metadata in ignore file: ${path}`);
|
|
599
|
-
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);
|
|
600
697
|
} catch {
|
|
601
|
-
return err(
|
|
698
|
+
return err("vacuum produced an unreadable report");
|
|
602
699
|
}
|
|
603
700
|
}
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
)
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
].sort((a, b) => a.source.localeCompare(b.source))
|
|
624
|
-
};
|
|
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";
|
|
625
720
|
}
|
|
626
|
-
function
|
|
627
|
-
const
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
);
|
|
633
|
-
return diagnostics.filter((diagnostic) => {
|
|
634
|
-
if ([
|
|
635
|
-
findingIdentity(diagnostic, dirname3(path)),
|
|
636
|
-
{
|
|
637
|
-
...findingIdentity(diagnostic, dirname3(path)),
|
|
638
|
-
path: diagnostic.path
|
|
639
|
-
}
|
|
640
|
-
].some((finding) => ignored.has(findingKey(finding))))
|
|
641
|
-
return false;
|
|
642
|
-
return !redoclyCompatibility || !ignoreCandidates(diagnostic, dirname3(path)).some(
|
|
643
|
-
(finding) => imported.has(findingKey(finding))
|
|
644
|
-
);
|
|
645
|
-
});
|
|
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)));
|
|
646
727
|
}
|
|
647
|
-
|
|
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) {
|
|
648
735
|
try {
|
|
649
|
-
const
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
mapping[finding.source] ??= /* @__PURE__ */ Object.create(null);
|
|
653
|
-
const rules = mapping[finding.source];
|
|
654
|
-
rules[finding.rule] ??= [];
|
|
655
|
-
const pointers = rules[finding.rule];
|
|
656
|
-
const pointer = toPointer(finding.path);
|
|
657
|
-
if (!pointers.includes(pointer)) pointers.push(pointer);
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
const sorted = /* @__PURE__ */ Object.create(null);
|
|
661
|
-
for (const source of Object.keys(mapping).sort()) {
|
|
662
|
-
const rules = /* @__PURE__ */ Object.create(null);
|
|
663
|
-
for (const rule of Object.keys(mapping[source] ?? {}).sort())
|
|
664
|
-
rules[rule] = [...mapping[source]?.[rule] ?? []].sort();
|
|
665
|
-
sorted[source] = rules;
|
|
736
|
+
const value = parseYaml2(source);
|
|
737
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
738
|
+
return value;
|
|
666
739
|
}
|
|
667
|
-
|
|
668
|
-
${OWNERS_PREFIX}${JSON.stringify({ version: baseline.version, documents: baseline.documents })}
|
|
669
|
-
${stringify(sorted)}`;
|
|
670
|
-
const written = await writeFileAtomic(path, content);
|
|
671
|
-
return written.ok ? written : err(`Could not write ignore file: ${path}`);
|
|
740
|
+
return null;
|
|
672
741
|
} catch {
|
|
673
|
-
return
|
|
742
|
+
return null;
|
|
674
743
|
}
|
|
675
744
|
}
|
|
676
|
-
|
|
677
|
-
const
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
try {
|
|
681
|
-
await mkdir(lock);
|
|
682
|
-
break;
|
|
683
|
-
} catch (error) {
|
|
684
|
-
if (!isRecord3(error) || error.code !== "EEXIST")
|
|
685
|
-
return err(`Could not lock ignore file: ${path}`);
|
|
686
|
-
if (Date.now() >= deadline)
|
|
687
|
-
return err(
|
|
688
|
-
`Ignore file is locked: ${path}. Remove ${lock} only if no generator is running.`
|
|
689
|
-
);
|
|
690
|
-
await setTimeout(25);
|
|
691
|
-
}
|
|
745
|
+
function hasCustomFunctions(ruleset) {
|
|
746
|
+
const functions = ruleset.functions;
|
|
747
|
+
if (Array.isArray(functions) && functions.length > 0) {
|
|
748
|
+
return true;
|
|
692
749
|
}
|
|
693
|
-
|
|
694
|
-
const loaded = await loadBaseline(path, false);
|
|
695
|
-
if (!loaded.ok) return loaded;
|
|
696
|
-
const baseline = updateBaseline(loaded.data, source, diagnostics, path);
|
|
697
|
-
const written = await writeBaseline(path, baseline);
|
|
698
|
-
return written.ok ? ok(baseline) : written;
|
|
699
|
-
})().catch(() => err(`Could not update ignore file: ${path}`));
|
|
700
|
-
const released = await rm2(lock, { recursive: true, force: true }).then(() => true).catch(() => false);
|
|
701
|
-
return released ? result : err(
|
|
702
|
-
`Could not release ignore file lock: ${lock}. Remove it only if no generator is running.`
|
|
703
|
-
);
|
|
750
|
+
return typeof ruleset.functionsDir === "string" && ruleset.functionsDir.length > 0;
|
|
704
751
|
}
|
|
705
|
-
function
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
].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));
|
|
713
759
|
}
|
|
714
|
-
function
|
|
715
|
-
const
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
path: [...primary.path, field]
|
|
728
|
-
});
|
|
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;
|
|
729
773
|
}
|
|
730
|
-
return
|
|
774
|
+
return ok({ diagnostics: diagnostics.data, engine });
|
|
731
775
|
}
|
|
732
776
|
|
|
733
777
|
// src/domains/document/lint/document-paths.ts
|
|
@@ -882,31 +926,6 @@ function resolveDiagnosticPaths(diagnostics, sources, engine) {
|
|
|
882
926
|
});
|
|
883
927
|
}
|
|
884
928
|
|
|
885
|
-
// src/domains/document/lint/ignore-path.ts
|
|
886
|
-
import { lstat } from "node:fs/promises";
|
|
887
|
-
import { resolve as resolve3 } from "node:path";
|
|
888
|
-
async function selectIgnorePath(explicit, generate) {
|
|
889
|
-
if (explicit) return ok(resolve3(explicit));
|
|
890
|
-
const scalar = resolve3(".scalar.lint-ignore.yaml");
|
|
891
|
-
if (generate) return ok(scalar);
|
|
892
|
-
try {
|
|
893
|
-
await lstat(scalar);
|
|
894
|
-
return ok(scalar);
|
|
895
|
-
} catch (error) {
|
|
896
|
-
if (!(typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT"))
|
|
897
|
-
return err("Could not inspect default ignore file");
|
|
898
|
-
}
|
|
899
|
-
const redocly = resolve3(".redocly.lint-ignore.yaml");
|
|
900
|
-
try {
|
|
901
|
-
await lstat(redocly);
|
|
902
|
-
return ok(redocly);
|
|
903
|
-
} catch (error) {
|
|
904
|
-
if (!(typeof error === "object" && error !== null && "code" in error && error.code === "ENOENT"))
|
|
905
|
-
return err("Could not inspect default ignore file");
|
|
906
|
-
return ok(scalar);
|
|
907
|
-
}
|
|
908
|
-
}
|
|
909
|
-
|
|
910
929
|
// src/domains/document/lint/locations.ts
|
|
911
930
|
import { isAlias as isAlias2, isMap as isMap2, LineCounter as LineCounter2, parseDocument as parseDocument2 } from "yaml";
|
|
912
931
|
var METHODS = /* @__PURE__ */ new Set([
|
|
@@ -1047,6 +1066,117 @@ function addRelatedLocations(diagnostics, sources, engine) {
|
|
|
1047
1066
|
});
|
|
1048
1067
|
}
|
|
1049
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
|
+
|
|
1050
1180
|
// src/domains/document/lint/paths.ts
|
|
1051
1181
|
import { realpath, stat } from "node:fs/promises";
|
|
1052
1182
|
import { basename, dirname as dirname4, join as join3, resolve as resolve4 } from "node:path";
|
|
@@ -1142,6 +1272,15 @@ function markdownCell(value) {
|
|
|
1142
1272
|
);
|
|
1143
1273
|
return [...result.parts, escapeText(value.slice(result.offset))].join("");
|
|
1144
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
|
+
}
|
|
1145
1284
|
|
|
1146
1285
|
// src/domains/document/lint/report.ts
|
|
1147
1286
|
function escapeCommand(value, property = false) {
|
|
@@ -1174,13 +1313,22 @@ function renderReport(diagnostics, engine, format, ignored = 0, color = true, op
|
|
|
1174
1313
|
warnings: diagnostics.filter((finding) => finding.severity === 1).length,
|
|
1175
1314
|
ignored
|
|
1176
1315
|
};
|
|
1177
|
-
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
|
+
];
|
|
1178
1321
|
const findings = selected.map((diagnostic) => {
|
|
1322
|
+
const coordinateEngine = diagnostic.engine ?? engine;
|
|
1179
1323
|
const position = (point) => ({
|
|
1180
|
-
line: Math.max(1, point.line + (
|
|
1181
|
-
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
|
+
)
|
|
1182
1329
|
});
|
|
1183
1330
|
return {
|
|
1331
|
+
...diagnostic.api ? { api: displaySource(diagnostic.api) } : {},
|
|
1184
1332
|
ruleId: String(diagnostic.code),
|
|
1185
1333
|
severity: ["error", "warn", "info", "hint"][diagnostic.severity],
|
|
1186
1334
|
message: diagnostic.message,
|
|
@@ -1200,8 +1348,15 @@ function renderReport(diagnostics, engine, format, ignored = 0, color = true, op
|
|
|
1200
1348
|
...diagnostic.documentationUrl ? { reference: diagnostic.documentationUrl } : {}
|
|
1201
1349
|
};
|
|
1202
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");
|
|
1203
1358
|
if (format === "json") {
|
|
1204
|
-
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)}
|
|
1205
1360
|
`;
|
|
1206
1361
|
}
|
|
1207
1362
|
if (format === "github-actions") {
|
|
@@ -1211,7 +1366,7 @@ function renderReport(diagnostics, engine, format, ignored = 0, color = true, op
|
|
|
1211
1366
|
const remote = /^https?:\/\//.test(source);
|
|
1212
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},`;
|
|
1213
1368
|
const level = finding.severity === "error" ? "error" : finding.severity === "warn" ? "warning" : "notice";
|
|
1214
|
-
const message = `${remote ? `${source}: ` : ""}${finding.message}${finding.reference ? `
|
|
1369
|
+
const message = `${finding.api ? `[${finding.api}] ` : ""}${remote ? `${source}: ` : ""}${finding.message}${finding.reference ? `
|
|
1215
1370
|
|
|
1216
1371
|
Reference: ${finding.reference}` : ""}`;
|
|
1217
1372
|
return `::${level} ${file}title=${escapeCommand(finding.ruleId, true)}::${escapeCommand(message)}
|
|
@@ -1222,8 +1377,10 @@ Reference: ${finding.reference}` : ""}`;
|
|
|
1222
1377
|
if (format === "junit") {
|
|
1223
1378
|
const groups = groupBy(
|
|
1224
1379
|
findings,
|
|
1225
|
-
(finding) => finding.location[0]?.source.ref ?? ""
|
|
1380
|
+
(finding) => finding.api ?? finding.location[0]?.source.ref ?? ""
|
|
1226
1381
|
);
|
|
1382
|
+
for (const api of apis ?? [])
|
|
1383
|
+
if (!groups.has(api.source)) groups.set(api.source, []);
|
|
1227
1384
|
const suites = [...groups].map(([source, problems]) => {
|
|
1228
1385
|
const errors = problems.filter(
|
|
1229
1386
|
(problem) => problem.severity === "error"
|
|
@@ -1241,9 +1398,9 @@ Pointer: ${point.pointer}`).join("\n")}`
|
|
|
1241
1398
|
);
|
|
1242
1399
|
const element = problem.severity === "error" ? "error" : problem.severity === "warn" ? "failure" : "system-out";
|
|
1243
1400
|
const result = element === "system-out" ? `<system-out>${details}</system-out>` : `<${element} message="${escapeXml(problem.message)}" type="${escapeXml(problem.ruleId)}">${details}</${element}>`;
|
|
1244
|
-
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>`;
|
|
1245
1402
|
}).join("\n");
|
|
1246
|
-
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>` : ""}
|
|
1247
1404
|
${cases}
|
|
1248
1405
|
</testsuite>`;
|
|
1249
1406
|
}).join("\n");
|
|
@@ -1260,7 +1417,9 @@ ${suites}
|
|
|
1260
1417
|
</testsuites>
|
|
1261
1418
|
`;
|
|
1262
1419
|
}
|
|
1263
|
-
const status = `${
|
|
1420
|
+
const status = `${apiStatus ? `${apiStatus}
|
|
1421
|
+
|
|
1422
|
+
` : ""}${totals.errors ? "Validation failed" : "Validation successful"}
|
|
1264
1423
|
Errors: ${totals.errors}
|
|
1265
1424
|
Warnings: ${totals.warnings}
|
|
1266
1425
|
Ignored: ${ignored}
|
|
@@ -1268,7 +1427,7 @@ Ignored: ${ignored}
|
|
|
1268
1427
|
if (format === "markdown") {
|
|
1269
1428
|
const groups = groupBy(
|
|
1270
1429
|
findings,
|
|
1271
|
-
(finding) => finding.location[0]?.source.ref ?? ""
|
|
1430
|
+
(finding) => finding.api ?? finding.location[0]?.source.ref ?? ""
|
|
1272
1431
|
);
|
|
1273
1432
|
return `${[...groups].map(
|
|
1274
1433
|
([source, problems]) => `## Lint: ${markdownCell(source)}
|
|
@@ -1288,14 +1447,16 @@ ${status}`;
|
|
|
1288
1447
|
if (format === "summary") {
|
|
1289
1448
|
const groups = groupBy(
|
|
1290
1449
|
findings,
|
|
1291
|
-
(finding) => `${finding.severity} ${finding.ruleId}`
|
|
1450
|
+
(finding) => `${finding.api ? `${finding.api}: ` : ""}${finding.severity} ${finding.ruleId}`
|
|
1292
1451
|
);
|
|
1293
1452
|
return `${[...groups].map(([key, problems]) => `${key}: ${problems.length}
|
|
1294
1453
|
`).join("")}
|
|
1295
1454
|
${status}`;
|
|
1296
1455
|
}
|
|
1297
1456
|
if (format === "codeframe") {
|
|
1298
|
-
const coloredStatus = `${
|
|
1457
|
+
const coloredStatus = `${apiStatus ? `${apiStatus}
|
|
1458
|
+
|
|
1459
|
+
` : ""}${totals.errors ? as2.red("Validation failed") : as2.green("Validation successful")}
|
|
1299
1460
|
Errors: ${as2.red(String(totals.errors))}
|
|
1300
1461
|
Warnings: ${as2.yellow(String(totals.warnings))}
|
|
1301
1462
|
Ignored: ${ignored}
|
|
@@ -1315,7 +1476,8 @@ Ignored: ${ignored}
|
|
|
1315
1476
|
location.start.col,
|
|
1316
1477
|
color ? highlight : void 0
|
|
1317
1478
|
);
|
|
1318
|
-
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}`)}
|
|
1319
1481
|
|
|
1320
1482
|
${highlight(finding.message)}
|
|
1321
1483
|
|
|
@@ -1330,10 +1492,12 @@ ${coloredStatus}`;
|
|
|
1330
1492
|
return color ? text2 : stripVTControlCharacters(text2);
|
|
1331
1493
|
}
|
|
1332
1494
|
const lines = [
|
|
1495
|
+
...apiStatus ? [apiStatus, ""] : [],
|
|
1333
1496
|
diagnostics.length ? (totals.errors ? as2.red : as2.yellow)("\u2716 Lint issues found:") : as2.green("\u2714 No lint issues found.")
|
|
1334
1497
|
];
|
|
1335
1498
|
for (const diagnostic of selected) {
|
|
1336
1499
|
lines.push(
|
|
1500
|
+
...diagnostic.api ? [`API: ${displaySource(diagnostic.api)}`] : [],
|
|
1337
1501
|
`${as2.yellow("Rule:")} ${as2.bold(String(diagnostic.code))}`,
|
|
1338
1502
|
`${as2.yellow("Message:")} ${diagnostic.message}`,
|
|
1339
1503
|
`${as2.yellow("Severity:")} ${severityToLabel(diagnostic.severity)}`,
|
|
@@ -1353,75 +1517,234 @@ ${coloredStatus}`;
|
|
|
1353
1517
|
return color ? text : stripVTControlCharacters(text);
|
|
1354
1518
|
}
|
|
1355
1519
|
|
|
1356
|
-
// src/domains/document/lint/
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
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;
|
|
1367
1546
|
}
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
);
|
|
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 });
|
|
1373
1551
|
}
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
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;
|
|
1388
1582
|
}
|
|
1583
|
+
if (results.some(({ result }) => result.ok))
|
|
1584
|
+
process.stderr.write(`Ignore baseline written to ${ignorePath}
|
|
1585
|
+
`);
|
|
1389
1586
|
}
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
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
|
+
}))
|
|
1397
1643
|
);
|
|
1398
|
-
url.hash = "";
|
|
1399
|
-
return ["file:", "https:", "http:"].includes(url.protocol) ? ok(url.protocol === "file:" ? fileURLToPath4(url) : url.href) : err("Unsupported reference protocol");
|
|
1400
|
-
} catch {
|
|
1401
|
-
return err("Invalid reference URI");
|
|
1402
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);
|
|
1403
1670
|
}
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1410
|
-
|
|
1411
|
-
|
|
1412
|
-
|
|
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
|
+
);
|
|
1413
1715
|
}
|
|
1414
|
-
})();
|
|
1415
|
-
for (const ref of refs) {
|
|
1416
|
-
const resolved = referenceSource(ref, current);
|
|
1417
|
-
if (!resolved.ok) continue;
|
|
1418
|
-
const target = resolved.data;
|
|
1419
|
-
if (sources.has(target)) continue;
|
|
1420
|
-
const loaded = await readSource(target);
|
|
1421
|
-
sources.set(target, loaded.ok ? loaded.data : void 0);
|
|
1422
1716
|
}
|
|
1423
|
-
}
|
|
1424
|
-
|
|
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));
|
|
1425
1748
|
}
|
|
1426
1749
|
|
|
1427
1750
|
// src/domains/document/lint/action.ts
|
|
@@ -1431,27 +1754,31 @@ function fail(message) {
|
|
|
1431
1754
|
process.exitCode = 1;
|
|
1432
1755
|
}
|
|
1433
1756
|
async function lintAction(inputArgument, options) {
|
|
1434
|
-
if (!inputArgument) {
|
|
1435
|
-
fail("Missing input\n\nProvide an API description file path or URL.\n");
|
|
1436
|
-
return;
|
|
1437
|
-
}
|
|
1438
1757
|
try {
|
|
1439
|
-
|
|
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
|
+
}
|
|
1440
1767
|
} catch (error) {
|
|
1441
1768
|
fail(error instanceof Error ? error.message : String(error));
|
|
1442
1769
|
}
|
|
1443
1770
|
}
|
|
1444
1771
|
async function runLint(inputArgument, options) {
|
|
1445
1772
|
const remote = (path) => /^https?:\/\//.test(path);
|
|
1446
|
-
const source = remote(inputArgument) ? new URL(inputArgument).href :
|
|
1773
|
+
const source = remote(inputArgument) ? new URL(inputArgument).href : resolve7(inputArgument);
|
|
1447
1774
|
const selectedIgnore = await selectIgnorePath(
|
|
1448
1775
|
options.ignoreFile,
|
|
1449
1776
|
Boolean(options.generateIgnoreFile)
|
|
1450
1777
|
);
|
|
1451
1778
|
if (!selectedIgnore.ok) return fail(selectedIgnore.error);
|
|
1452
1779
|
const ignorePath = selectedIgnore.data;
|
|
1453
|
-
const outputPath = options.output ?
|
|
1454
|
-
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));
|
|
1455
1782
|
const destinations = [
|
|
1456
1783
|
...outputPath ? [outputPath] : [],
|
|
1457
1784
|
...options.generateIgnoreFile ? [ignorePath] : []
|
|
@@ -1466,32 +1793,9 @@ async function runLint(inputArgument, options) {
|
|
|
1466
1793
|
Boolean(options.ignoreFile && !options.generateIgnoreFile)
|
|
1467
1794
|
);
|
|
1468
1795
|
if (!loaded.ok) return fail(loaded.error);
|
|
1469
|
-
const
|
|
1470
|
-
if (!
|
|
1471
|
-
const
|
|
1472
|
-
ok: true,
|
|
1473
|
-
data: isAsyncApiContent(specification.data) ? ASYNCAPI_RULESET : DEFAULT_RULESET
|
|
1474
|
-
};
|
|
1475
|
-
if (!ruleset.ok) return fail(ruleset.error);
|
|
1476
|
-
const sources = await collectSources(source, specification.data);
|
|
1477
|
-
const result = await lint({
|
|
1478
|
-
document: specification.data,
|
|
1479
|
-
ruleset: ruleset.data,
|
|
1480
|
-
source
|
|
1481
|
-
});
|
|
1482
|
-
if (!result.ok)
|
|
1483
|
-
return fail(`Could not lint the API description
|
|
1484
|
-
|
|
1485
|
-
${result.error}`);
|
|
1486
|
-
const diagnostics = addRelatedLocations(
|
|
1487
|
-
resolveDiagnosticPaths(
|
|
1488
|
-
result.data.diagnostics,
|
|
1489
|
-
sources,
|
|
1490
|
-
result.data.engine
|
|
1491
|
-
),
|
|
1492
|
-
sources,
|
|
1493
|
-
result.data.engine
|
|
1494
|
-
);
|
|
1796
|
+
const result = await lintDocument(source, options.rule);
|
|
1797
|
+
if (!result.ok) return fail(result.error);
|
|
1798
|
+
const { diagnostics, sources, compatibility } = result.data;
|
|
1495
1799
|
const referencedPaths = [
|
|
1496
1800
|
...sources.keys(),
|
|
1497
1801
|
...diagnostics.flatMap((diagnostic) => [
|
|
@@ -1516,9 +1820,6 @@ ${result.error}`);
|
|
|
1516
1820
|
process.stderr.write(`Ignore baseline written to ${ignorePath}
|
|
1517
1821
|
`);
|
|
1518
1822
|
}
|
|
1519
|
-
const compatibility = Boolean(
|
|
1520
|
-
!isAsyncApiContent(specification.data) && supportsRedoclyMigration(ruleset.data)
|
|
1521
|
-
);
|
|
1522
1823
|
if (compatibility) {
|
|
1523
1824
|
const unsupported = unsupportedRedoclyRules(baseline);
|
|
1524
1825
|
if (unsupported.length)
|
|
@@ -1543,7 +1844,7 @@ ${result.error}`);
|
|
|
1543
1844
|
);
|
|
1544
1845
|
}
|
|
1545
1846
|
if (outputPath) {
|
|
1546
|
-
await
|
|
1847
|
+
await writeFile3(outputPath, report, "utf8");
|
|
1547
1848
|
} else {
|
|
1548
1849
|
process.stdout.write(report);
|
|
1549
1850
|
}
|