@firstdraft.com/cli 0.1.0-alpha.2
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/LICENSE +21 -0
- package/README.md +267 -0
- package/bin/firstdraft.js +19 -0
- package/package.json +49 -0
- package/src/api-authentication.js +37 -0
- package/src/api-response.js +158 -0
- package/src/application-identity.js +149 -0
- package/src/cli.js +2056 -0
- package/src/commands/compilation.js +823 -0
- package/src/commands/plan-compile.js +150 -0
- package/src/commands/plan-init.js +79 -0
- package/src/commands/plan-publish.js +994 -0
- package/src/commands/plan-push.js +302 -0
- package/src/commands/plan-status.js +485 -0
- package/src/compilation-artifact.js +717 -0
- package/src/file-system.js +12 -0
- package/src/plan-state.js +214 -0
- package/src/uuid-v7.js +55 -0
- package/src/version.js +11 -0
|
@@ -0,0 +1,485 @@
|
|
|
1
|
+
import { lstatSync, readFileSync } from "node:fs";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
FirstDraftNetworkError,
|
|
5
|
+
FirstDraftProtocolError,
|
|
6
|
+
isProblemBody,
|
|
7
|
+
readResponseBody,
|
|
8
|
+
responseMediaType,
|
|
9
|
+
sendRequest,
|
|
10
|
+
} from "../api-response.js";
|
|
11
|
+
import { isUuidV7, readPlanState } from "../plan-state.js";
|
|
12
|
+
|
|
13
|
+
const REQUEST_TIMEOUT_MS = 30_000;
|
|
14
|
+
const WAIT_TIMEOUT_MS = 120_000;
|
|
15
|
+
const POLL_INTERVAL_MS = 1_000;
|
|
16
|
+
const TERMINAL_STATUSES = new Set([
|
|
17
|
+
"valid",
|
|
18
|
+
"issues_found",
|
|
19
|
+
"analysis_failed",
|
|
20
|
+
"superseded",
|
|
21
|
+
]);
|
|
22
|
+
const ALL_STATUSES = new Set(["processing", ...TERMINAL_STATUSES]);
|
|
23
|
+
const RESPONSE_KEYS = ["analysis", "project"];
|
|
24
|
+
const PROJECT_KEYS = ["graph_version", "id"];
|
|
25
|
+
const ANALYSIS_KEYS = [
|
|
26
|
+
"analyzer_release",
|
|
27
|
+
"completed_at",
|
|
28
|
+
"diagnostics",
|
|
29
|
+
"graph_version",
|
|
30
|
+
"id",
|
|
31
|
+
"started_at",
|
|
32
|
+
"status",
|
|
33
|
+
];
|
|
34
|
+
const DIAGNOSTIC_KEYS = [
|
|
35
|
+
"code",
|
|
36
|
+
"location",
|
|
37
|
+
"message",
|
|
38
|
+
"related_locations",
|
|
39
|
+
"severity",
|
|
40
|
+
"subject",
|
|
41
|
+
"suggestions",
|
|
42
|
+
];
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* @typedef {object} PlanStatusFileSystem
|
|
46
|
+
* @property {typeof lstatSync} lstatSync
|
|
47
|
+
* @property {typeof readFileSync} readFileSync
|
|
48
|
+
*/
|
|
49
|
+
|
|
50
|
+
/** @type {PlanStatusFileSystem} */
|
|
51
|
+
const DEFAULT_FILE_SYSTEM = { lstatSync, readFileSync };
|
|
52
|
+
|
|
53
|
+
export class PlanStatusNotPushedError extends Error {}
|
|
54
|
+
|
|
55
|
+
export class PlanStatusChangedError extends Error {
|
|
56
|
+
/** @param {AnalysisResponse} current */
|
|
57
|
+
constructor(current) {
|
|
58
|
+
super("The current analysis changed while it was being polled.");
|
|
59
|
+
this.current = current;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export class PlanStatusTimeoutError extends Error {
|
|
64
|
+
/** @param {AnalysisResponse} current */
|
|
65
|
+
constructor(current) {
|
|
66
|
+
super("The current analysis did not finish before the wait deadline.");
|
|
67
|
+
this.current = current;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* @typedef {object} PlanStatusOptions
|
|
73
|
+
* @property {string} cwd
|
|
74
|
+
* @property {boolean} [wait]
|
|
75
|
+
* @property {number} [expectedGraphVersion]
|
|
76
|
+
* @property {typeof globalThis.fetch} [fetchFunction]
|
|
77
|
+
* @property {PlanStatusFileSystem} [fileSystem]
|
|
78
|
+
* @property {(timeoutMs: number) => AbortSignal} [createRequestSignal]
|
|
79
|
+
* @property {(delayMs: number) => Promise<void>} [sleep]
|
|
80
|
+
* @property {() => number} [now]
|
|
81
|
+
*/
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* @typedef {object} AnalysisResponse
|
|
85
|
+
* @property {{id: string, graph_version: number}} project
|
|
86
|
+
* @property {{id: string, graph_version: number, analyzer_release: string, status: string, diagnostics: Record<string, unknown>[], started_at: string | null, completed_at: string | null}} analysis
|
|
87
|
+
*/
|
|
88
|
+
|
|
89
|
+
/** @typedef {{source_pointer: string} | {line: number, column: number}} SourceLocation */
|
|
90
|
+
/** @typedef {{kind: string, readable_path: string, subject_uuid?: string}} DiagnosticSubject */
|
|
91
|
+
/** @typedef {{code: string, severity: "error" | "warning", message: string, location: SourceLocation, subject: DiagnosticSubject | null, related_locations: SourceLocation[], suggestions: string[]}} StructuredDiagnostic */
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* @typedef {object} StatusSuccess
|
|
95
|
+
* @property {200} status
|
|
96
|
+
* @property {AnalysisResponse} body
|
|
97
|
+
*/
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* @typedef {object} StatusRejection
|
|
101
|
+
* @property {number} status
|
|
102
|
+
* @property {"problem" | null} responseKind
|
|
103
|
+
* @property {Record<string, unknown> | null} body
|
|
104
|
+
*/
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* @param {PlanStatusOptions} options
|
|
108
|
+
* @returns {Promise<StatusSuccess | StatusRejection>}
|
|
109
|
+
*/
|
|
110
|
+
export async function readPlanStatus({
|
|
111
|
+
cwd,
|
|
112
|
+
wait = false,
|
|
113
|
+
expectedGraphVersion,
|
|
114
|
+
fetchFunction = globalThis.fetch,
|
|
115
|
+
fileSystem = DEFAULT_FILE_SYSTEM,
|
|
116
|
+
createRequestSignal = (timeoutMs) => AbortSignal.timeout(timeoutMs),
|
|
117
|
+
sleep = sleepFor,
|
|
118
|
+
now = Date.now,
|
|
119
|
+
}) {
|
|
120
|
+
const state = readPlanState({ cwd, fileSystem });
|
|
121
|
+
if (state.api_url === undefined) {
|
|
122
|
+
throw new PlanStatusNotPushedError(
|
|
123
|
+
"The local Foundation Plan has not been pushed.",
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const endpoint = new URL(
|
|
128
|
+
`/v1/projects/${state.project_id}/analysis`,
|
|
129
|
+
state.api_url,
|
|
130
|
+
);
|
|
131
|
+
const deadline = wait ? now() + WAIT_TIMEOUT_MS : null;
|
|
132
|
+
/** @type {AnalysisResponse | null} */
|
|
133
|
+
let first = null;
|
|
134
|
+
/** @type {AnalysisResponse | null} */
|
|
135
|
+
let current = null;
|
|
136
|
+
|
|
137
|
+
while (true) {
|
|
138
|
+
if (deadline !== null && current !== null && now() >= deadline) {
|
|
139
|
+
throw new PlanStatusTimeoutError(current);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const requestTimeout =
|
|
143
|
+
deadline === null
|
|
144
|
+
? REQUEST_TIMEOUT_MS
|
|
145
|
+
: Math.max(1, Math.min(REQUEST_TIMEOUT_MS, deadline - now()));
|
|
146
|
+
let response;
|
|
147
|
+
let body;
|
|
148
|
+
try {
|
|
149
|
+
response = await sendRequest(fetchFunction, endpoint, {
|
|
150
|
+
method: "GET",
|
|
151
|
+
headers: { Accept: "application/json, application/problem+json" },
|
|
152
|
+
redirect: "error",
|
|
153
|
+
signal: createRequestSignal(requestTimeout),
|
|
154
|
+
});
|
|
155
|
+
body = await readResponseBody(response);
|
|
156
|
+
} catch (error) {
|
|
157
|
+
if (
|
|
158
|
+
error instanceof FirstDraftNetworkError &&
|
|
159
|
+
deadline !== null &&
|
|
160
|
+
current !== null &&
|
|
161
|
+
now() >= deadline
|
|
162
|
+
) {
|
|
163
|
+
throw new PlanStatusTimeoutError(current);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
throw error;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (response.status !== 200) {
|
|
170
|
+
if (response.ok) {
|
|
171
|
+
throw new FirstDraftProtocolError(
|
|
172
|
+
"First Draft returned an unexpected success status.",
|
|
173
|
+
{ status: response.status },
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (isProblemBody(response, body)) {
|
|
178
|
+
return {
|
|
179
|
+
status: response.status,
|
|
180
|
+
responseKind: "problem",
|
|
181
|
+
body,
|
|
182
|
+
};
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
return {
|
|
186
|
+
status: response.status,
|
|
187
|
+
responseKind: null,
|
|
188
|
+
body: null,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
const parsed = parseAnalysisResponse(body, state.project_id);
|
|
193
|
+
if (responseMediaType(response) !== "application/json" || parsed === null) {
|
|
194
|
+
throw new FirstDraftProtocolError(
|
|
195
|
+
"First Draft returned an invalid analysis response.",
|
|
196
|
+
{ status: response.status },
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
current = parsed;
|
|
201
|
+
if (
|
|
202
|
+
expectedGraphVersion !== undefined &&
|
|
203
|
+
current.analysis.graph_version !== expectedGraphVersion
|
|
204
|
+
) {
|
|
205
|
+
if (!wait || current.analysis.graph_version > expectedGraphVersion) {
|
|
206
|
+
throw new PlanStatusChangedError(current);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
const remaining = deadline === null ? 0 : deadline - now();
|
|
210
|
+
if (remaining <= 0) throw new PlanStatusTimeoutError(current);
|
|
211
|
+
await sleep(Math.min(POLL_INTERVAL_MS, remaining));
|
|
212
|
+
continue;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
first ??= parsed;
|
|
216
|
+
if (
|
|
217
|
+
current.analysis.id !== first.analysis.id ||
|
|
218
|
+
current.analysis.graph_version !== first.analysis.graph_version
|
|
219
|
+
) {
|
|
220
|
+
throw new PlanStatusChangedError(current);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
if (!wait || TERMINAL_STATUSES.has(current.analysis.status)) {
|
|
224
|
+
return { status: 200, body: current };
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const remaining = deadline === null ? 0 : deadline - now();
|
|
228
|
+
if (remaining <= 0) throw new PlanStatusTimeoutError(current);
|
|
229
|
+
await sleep(Math.min(POLL_INTERVAL_MS, remaining));
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** @param {number} delayMs */
|
|
234
|
+
function sleepFor(delayMs) {
|
|
235
|
+
return new Promise((resolve) => {
|
|
236
|
+
setTimeout(resolve, delayMs);
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* @param {unknown} value
|
|
242
|
+
* @param {string} projectId
|
|
243
|
+
* @returns {AnalysisResponse | null}
|
|
244
|
+
*/
|
|
245
|
+
function parseAnalysisResponse(value, projectId) {
|
|
246
|
+
if (!hasRequiredKeys(value, RESPONSE_KEYS)) return null;
|
|
247
|
+
|
|
248
|
+
const project = value.project;
|
|
249
|
+
const analysis = value.analysis;
|
|
250
|
+
if (
|
|
251
|
+
!hasRequiredKeys(project, PROJECT_KEYS) ||
|
|
252
|
+
!hasRequiredKeys(analysis, ANALYSIS_KEYS) ||
|
|
253
|
+
project.id !== projectId ||
|
|
254
|
+
!isGraphVersion(project.graph_version) ||
|
|
255
|
+
!isUuidV7(analysis.id) ||
|
|
256
|
+
!isGraphVersion(analysis.graph_version) ||
|
|
257
|
+
analysis.graph_version !== project.graph_version ||
|
|
258
|
+
typeof analysis.analyzer_release !== "string" ||
|
|
259
|
+
Buffer.byteLength(analysis.analyzer_release) === 0 ||
|
|
260
|
+
Buffer.byteLength(analysis.analyzer_release) > 256 ||
|
|
261
|
+
typeof analysis.status !== "string" ||
|
|
262
|
+
!ALL_STATUSES.has(analysis.status) ||
|
|
263
|
+
!Array.isArray(analysis.diagnostics) ||
|
|
264
|
+
!analysis.diagnostics.every(isStructuredDiagnostic) ||
|
|
265
|
+
!isNullableTimestamp(analysis.started_at) ||
|
|
266
|
+
!isNullableTimestamp(analysis.completed_at)
|
|
267
|
+
) {
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const terminal = TERMINAL_STATUSES.has(analysis.status);
|
|
272
|
+
if (
|
|
273
|
+
(terminal && analysis.completed_at === null) ||
|
|
274
|
+
(!terminal && analysis.completed_at !== null) ||
|
|
275
|
+
(analysis.started_at !== null &&
|
|
276
|
+
analysis.completed_at !== null &&
|
|
277
|
+
timestampMilliseconds(analysis.completed_at) <
|
|
278
|
+
timestampMilliseconds(analysis.started_at)) ||
|
|
279
|
+
(analysis.status === "valid" &&
|
|
280
|
+
analysis.diagnostics.some(
|
|
281
|
+
(diagnostic) => diagnostic.severity === "error",
|
|
282
|
+
)) ||
|
|
283
|
+
(analysis.status === "issues_found" &&
|
|
284
|
+
!analysis.diagnostics.some(
|
|
285
|
+
(diagnostic) => diagnostic.severity === "error",
|
|
286
|
+
))
|
|
287
|
+
) {
|
|
288
|
+
return null;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
return {
|
|
292
|
+
project: {
|
|
293
|
+
id: project.id,
|
|
294
|
+
graph_version: project.graph_version,
|
|
295
|
+
},
|
|
296
|
+
analysis: {
|
|
297
|
+
id: analysis.id,
|
|
298
|
+
graph_version: analysis.graph_version,
|
|
299
|
+
analyzer_release: analysis.analyzer_release,
|
|
300
|
+
status: analysis.status,
|
|
301
|
+
diagnostics: analysis.diagnostics.map(copyDiagnostic),
|
|
302
|
+
started_at: analysis.started_at,
|
|
303
|
+
completed_at: analysis.completed_at,
|
|
304
|
+
},
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** @param {unknown} value @returns {value is StructuredDiagnostic} */
|
|
309
|
+
function isStructuredDiagnostic(value) {
|
|
310
|
+
if (
|
|
311
|
+
!hasRequiredKeys(value, DIAGNOSTIC_KEYS) ||
|
|
312
|
+
typeof value.code !== "string" ||
|
|
313
|
+
(value.severity !== "error" && value.severity !== "warning") ||
|
|
314
|
+
typeof value.message !== "string" ||
|
|
315
|
+
!isSourceLocation(value.location) ||
|
|
316
|
+
!isDiagnosticSubject(value.subject) ||
|
|
317
|
+
!Array.isArray(value.related_locations) ||
|
|
318
|
+
!value.related_locations.every(isSourceLocation) ||
|
|
319
|
+
!Array.isArray(value.suggestions) ||
|
|
320
|
+
!value.suggestions.every((suggestion) => typeof suggestion === "string")
|
|
321
|
+
) {
|
|
322
|
+
return false;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
return true;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** @param {unknown} value @returns {value is SourceLocation} */
|
|
329
|
+
function isSourceLocation(value) {
|
|
330
|
+
if (!isRecord(value)) return false;
|
|
331
|
+
|
|
332
|
+
const pointerLocation = hasOwn(value, "source_pointer");
|
|
333
|
+
const coordinateLocation = hasOwn(value, "line") || hasOwn(value, "column");
|
|
334
|
+
if (pointerLocation === coordinateLocation) return false;
|
|
335
|
+
|
|
336
|
+
if (pointerLocation) {
|
|
337
|
+
return isJsonPointer(value.source_pointer);
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
return (
|
|
341
|
+
hasOwn(value, "line") &&
|
|
342
|
+
hasOwn(value, "column") &&
|
|
343
|
+
Number.isSafeInteger(value.line) &&
|
|
344
|
+
Number(value.line) > 0 &&
|
|
345
|
+
Number.isSafeInteger(value.column) &&
|
|
346
|
+
Number(value.column) > 0
|
|
347
|
+
);
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/** @param {unknown} value @returns {value is DiagnosticSubject | null} */
|
|
351
|
+
function isDiagnosticSubject(value) {
|
|
352
|
+
if (value === null) return true;
|
|
353
|
+
if (!isRecord(value)) return false;
|
|
354
|
+
|
|
355
|
+
return (
|
|
356
|
+
hasOwn(value, "kind") &&
|
|
357
|
+
hasOwn(value, "readable_path") &&
|
|
358
|
+
typeof value.kind === "string" &&
|
|
359
|
+
typeof value.readable_path === "string" &&
|
|
360
|
+
(value.subject_uuid === undefined || isUuidV7(value.subject_uuid))
|
|
361
|
+
);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** @param {unknown} value */
|
|
365
|
+
function isJsonPointer(value) {
|
|
366
|
+
return (
|
|
367
|
+
typeof value === "string" &&
|
|
368
|
+
(value === "" || value.startsWith("/")) &&
|
|
369
|
+
!/~(?:[^01]|$)/.test(value)
|
|
370
|
+
);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/** @param {unknown} value @returns {value is number} */
|
|
374
|
+
function isGraphVersion(value) {
|
|
375
|
+
return Number.isSafeInteger(value) && Number(value) >= 1;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/** @param {unknown} value @returns {value is string | null} */
|
|
379
|
+
function isNullableTimestamp(value) {
|
|
380
|
+
return value === null || timestampParts(value) !== null;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/** @param {string} value */
|
|
384
|
+
function timestampMilliseconds(value) {
|
|
385
|
+
const parts = timestampParts(value);
|
|
386
|
+
if (parts === null) return Number.NaN;
|
|
387
|
+
|
|
388
|
+
const parsed = Date.parse(value);
|
|
389
|
+
if (Number.isFinite(parsed)) return parsed;
|
|
390
|
+
|
|
391
|
+
return Date.parse(value.replace(/:60(?=[.,Zz+-])/, ":59")) + 1_000;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
/** @param {unknown} value */
|
|
395
|
+
function timestampParts(value) {
|
|
396
|
+
if (typeof value !== "string") return null;
|
|
397
|
+
|
|
398
|
+
const match =
|
|
399
|
+
/^(\d{4})-(\d{2})-(\d{2})[Tt](\d{2}):(\d{2}):(\d{2})(?:\.\d+)?(?:[Zz]|([+-])(\d{2}):(\d{2}))$/.exec(
|
|
400
|
+
value,
|
|
401
|
+
);
|
|
402
|
+
if (!match) return null;
|
|
403
|
+
|
|
404
|
+
const year = Number(match[1]);
|
|
405
|
+
const month = Number(match[2]);
|
|
406
|
+
const day = Number(match[3]);
|
|
407
|
+
const hour = Number(match[4]);
|
|
408
|
+
const minute = Number(match[5]);
|
|
409
|
+
const second = Number(match[6]);
|
|
410
|
+
const offsetHour = match[8] === undefined ? 0 : Number(match[8]);
|
|
411
|
+
const offsetMinute = match[9] === undefined ? 0 : Number(match[9]);
|
|
412
|
+
if (
|
|
413
|
+
month < 1 ||
|
|
414
|
+
month > 12 ||
|
|
415
|
+
day < 1 ||
|
|
416
|
+
day > daysInMonth(year, month) ||
|
|
417
|
+
hour > 23 ||
|
|
418
|
+
minute > 59 ||
|
|
419
|
+
second > 60 ||
|
|
420
|
+
(second === 60 && (hour !== 23 || minute !== 59)) ||
|
|
421
|
+
offsetHour > 23 ||
|
|
422
|
+
offsetMinute > 59
|
|
423
|
+
) {
|
|
424
|
+
return null;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
return { year, month, day, hour, minute, second };
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/** @param {number} year @param {number} month */
|
|
431
|
+
function daysInMonth(year, month) {
|
|
432
|
+
if (month === 2) {
|
|
433
|
+
return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0) ? 29 : 28;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
return [4, 6, 9, 11].includes(month) ? 30 : 31;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/** @param {StructuredDiagnostic} diagnostic */
|
|
440
|
+
function copyDiagnostic(diagnostic) {
|
|
441
|
+
return {
|
|
442
|
+
code: diagnostic.code,
|
|
443
|
+
severity: diagnostic.severity,
|
|
444
|
+
message: diagnostic.message,
|
|
445
|
+
location: copyLocation(diagnostic.location),
|
|
446
|
+
subject:
|
|
447
|
+
diagnostic.subject === null
|
|
448
|
+
? null
|
|
449
|
+
: {
|
|
450
|
+
kind: diagnostic.subject.kind,
|
|
451
|
+
readable_path: diagnostic.subject.readable_path,
|
|
452
|
+
...(diagnostic.subject.subject_uuid === undefined
|
|
453
|
+
? {}
|
|
454
|
+
: { subject_uuid: diagnostic.subject.subject_uuid }),
|
|
455
|
+
},
|
|
456
|
+
related_locations: diagnostic.related_locations.map(copyLocation),
|
|
457
|
+
suggestions: [...diagnostic.suggestions],
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/** @param {SourceLocation} location */
|
|
462
|
+
function copyLocation(location) {
|
|
463
|
+
return "source_pointer" in location
|
|
464
|
+
? { source_pointer: location.source_pointer }
|
|
465
|
+
: { line: location.line, column: location.column };
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* @param {unknown} value
|
|
470
|
+
* @param {string[]} keys
|
|
471
|
+
* @returns {value is Record<string, unknown>}
|
|
472
|
+
*/
|
|
473
|
+
function hasRequiredKeys(value, keys) {
|
|
474
|
+
return isRecord(value) && keys.every((key) => hasOwn(value, key));
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/** @param {Record<string, unknown>} value @param {string} key */
|
|
478
|
+
function hasOwn(value, key) {
|
|
479
|
+
return Object.prototype.hasOwnProperty.call(value, key);
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/** @param {unknown} value @returns {value is Record<string, unknown>} */
|
|
483
|
+
function isRecord(value) {
|
|
484
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
485
|
+
}
|