@barqjs/cli 0.0.0-stage → 0.2.0-next.1
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 +128 -2
- package/dist/index.d.ts +104 -0
- package/dist/index.js +486 -0
- package/package.json +66 -4
- package/src/check.ts +518 -0
- package/src/index.ts +212 -0
package/src/check.ts
ADDED
|
@@ -0,0 +1,518 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `barq check`: every file of a project through the compiler's diagnostics,
|
|
3
|
+
* with no build and no dev server.
|
|
4
|
+
*
|
|
5
|
+
* The findings are the ones the Vite plugin reports, because they come from the
|
|
6
|
+
* same `transform`. What this adds is one answer for a whole project, a JSON
|
|
7
|
+
* form a program can read, and the fixes applied for the findings that carry
|
|
8
|
+
* one.
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import { existsSync, readdirSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
12
|
+
import { createRequire } from "node:module";
|
|
13
|
+
import { dirname, join, relative, resolve, sep } from "node:path";
|
|
14
|
+
|
|
15
|
+
export type Severity = "error" | "warning" | "note";
|
|
16
|
+
|
|
17
|
+
export interface Edit {
|
|
18
|
+
/** UTF-16 offsets into the file as it was read. Equal for an insertion. */
|
|
19
|
+
pos: number;
|
|
20
|
+
end: number;
|
|
21
|
+
text: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface Finding {
|
|
25
|
+
/** `BARQ001`, or absent for a syntax error, which is the parser's. */
|
|
26
|
+
code?: string;
|
|
27
|
+
severity: Severity;
|
|
28
|
+
message: string;
|
|
29
|
+
/** Relative to the root, with `/` separators. */
|
|
30
|
+
file: string;
|
|
31
|
+
line: number;
|
|
32
|
+
column: number;
|
|
33
|
+
endLine: number;
|
|
34
|
+
endColumn: number;
|
|
35
|
+
/** The page for the code. */
|
|
36
|
+
docs?: string;
|
|
37
|
+
/** The section of the installed repair guide for the code. */
|
|
38
|
+
skill?: string;
|
|
39
|
+
/** Present when `--fix` would repair it. */
|
|
40
|
+
fix?: Edit[];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface CheckResult {
|
|
44
|
+
version: 1;
|
|
45
|
+
root: string;
|
|
46
|
+
files: number;
|
|
47
|
+
summary: { errors: number; warnings: number; notes: number; fixable: number; fixed: number };
|
|
48
|
+
findings: Finding[];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface CheckOptions {
|
|
52
|
+
/** The project root: paths in the result are relative to it. */
|
|
53
|
+
root: string;
|
|
54
|
+
/** Files and directories to check, relative to the root. Defaults to the root. */
|
|
55
|
+
paths?: readonly string[];
|
|
56
|
+
/** Apply the fixes the findings carry, and report what is left. */
|
|
57
|
+
fix?: boolean;
|
|
58
|
+
/** Per-code level, as `[code, "suppress" | "note" | "warning" | "error"]`. */
|
|
59
|
+
levels?: readonly (readonly [string, string])[];
|
|
60
|
+
/**
|
|
61
|
+
* The directory of file routes, relative to the root. When it exists, every
|
|
62
|
+
* `<Link to>` is checked against the routes in it and a route file whose id
|
|
63
|
+
* disagrees with its filename is reported.
|
|
64
|
+
* @default "src/routes"
|
|
65
|
+
*/
|
|
66
|
+
routes?: string;
|
|
67
|
+
/**
|
|
68
|
+
* Route patterns a link may name beside the file routes, such as those of a
|
|
69
|
+
* second router the application builds by hand.
|
|
70
|
+
*/
|
|
71
|
+
extraRoutes?: readonly string[];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* What a project sets for every run, under `barq.check` in its `package.json`.
|
|
76
|
+
* A flag on the command line replaces the field of the same name.
|
|
77
|
+
*/
|
|
78
|
+
export function projectOptions(root: string): Partial<CheckOptions> {
|
|
79
|
+
let manifest: unknown;
|
|
80
|
+
try {
|
|
81
|
+
manifest = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
|
|
82
|
+
} catch {
|
|
83
|
+
return {};
|
|
84
|
+
}
|
|
85
|
+
const set = (manifest as { barq?: { check?: unknown } }).barq?.check;
|
|
86
|
+
if (typeof set !== "object" || set === null) return {};
|
|
87
|
+
const { paths, routes, extraRoutes, levels } = set as Record<string, unknown>;
|
|
88
|
+
const strings = (value: unknown): string[] | undefined =>
|
|
89
|
+
Array.isArray(value) && value.every((item) => typeof item === "string") ? value : undefined;
|
|
90
|
+
const out: Partial<CheckOptions> = {};
|
|
91
|
+
const listed = strings(paths);
|
|
92
|
+
if (listed !== undefined) out.paths = listed;
|
|
93
|
+
if (typeof routes === "string") out.routes = routes;
|
|
94
|
+
const extra = strings(extraRoutes);
|
|
95
|
+
if (extra !== undefined) out.extraRoutes = extra;
|
|
96
|
+
if (typeof levels === "object" && levels !== null) {
|
|
97
|
+
out.levels = Object.entries(levels).filter(
|
|
98
|
+
(entry): entry is [string, string] => typeof entry[1] === "string",
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
return out;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
interface NativeDiagnostic {
|
|
105
|
+
code?: string;
|
|
106
|
+
severity: string;
|
|
107
|
+
message: string;
|
|
108
|
+
line: number;
|
|
109
|
+
column: number;
|
|
110
|
+
endLine: number;
|
|
111
|
+
endColumn: number;
|
|
112
|
+
docs?: string;
|
|
113
|
+
fix?: Edit[];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
interface Native {
|
|
117
|
+
transform(
|
|
118
|
+
code: string,
|
|
119
|
+
options: {
|
|
120
|
+
filename: string;
|
|
121
|
+
root: string;
|
|
122
|
+
dev: boolean;
|
|
123
|
+
diagnostics: boolean;
|
|
124
|
+
sourceNames: boolean;
|
|
125
|
+
checks?: string[][];
|
|
126
|
+
routes?: string[];
|
|
127
|
+
cssImports?: string[][];
|
|
128
|
+
cssExports?: boolean;
|
|
129
|
+
},
|
|
130
|
+
): { diagnostics?: NativeDiagnostic[]; cssWanted?: string[][]; cssExports?: string[][] };
|
|
131
|
+
diagnosticCodes(): { code: string; level: string; summary: string; docs: string }[];
|
|
132
|
+
routeTree(
|
|
133
|
+
root: string,
|
|
134
|
+
dir: string,
|
|
135
|
+
): {
|
|
136
|
+
patterns: string[];
|
|
137
|
+
warnings: string[];
|
|
138
|
+
mismatches: { file: string; declared: string; expected: string }[];
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
let native: Native | undefined;
|
|
143
|
+
|
|
144
|
+
function compiler(): Native {
|
|
145
|
+
native ??= createRequire(import.meta.url)("@barqjs/compiler") as Native;
|
|
146
|
+
return native;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
const SKILL = "node_modules/@barqjs/compiler/skills/compiler-diagnostics/SKILL.md";
|
|
150
|
+
const JSX = /\.[jt]sx$/;
|
|
151
|
+
const SCRIPT = /\.[cm]?[jt]s$/;
|
|
152
|
+
const SKIPPED = new Set(["node_modules", "dist", "build", "coverage", ".git", ".vite", ".output"]);
|
|
153
|
+
/** A fix can uncover the next one, such as a call inside a call. */
|
|
154
|
+
const MAX_PASSES = 5;
|
|
155
|
+
|
|
156
|
+
const NAMED_IMPORT = /import\s*\{([^}]*)\}\s*from\s*["']([^"']+)["']/g;
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Whether `code` calls a binding it imports the way a `layer` binding is
|
|
160
|
+
* called, which makes it a module the build compiles though it never names
|
|
161
|
+
* barq. The same guess as `@barqjs/vite`'s, so both compile the same files.
|
|
162
|
+
*/
|
|
163
|
+
function callsThroughImport(code: string): boolean {
|
|
164
|
+
for (const [, names, from] of code.matchAll(NAMED_IMPORT)) {
|
|
165
|
+
if (from === "@barqjs/css" || names === undefined) continue;
|
|
166
|
+
for (const entry of names.split(",")) {
|
|
167
|
+
const local = entry
|
|
168
|
+
.trim()
|
|
169
|
+
.replace(/^type\s+/, "")
|
|
170
|
+
.split(/\s+as\s+/)
|
|
171
|
+
.pop()
|
|
172
|
+
?.trim();
|
|
173
|
+
if (local === undefined || !/^[A-Za-z_$][\w$]*$/.test(local)) continue;
|
|
174
|
+
const call = new RegExp(
|
|
175
|
+
`(?<![\\w$.])${local.replace(/\$/g, "\\$")}(?:\\.dynamic\\(|(?:\\.(?:create|props))?\\([^()]*\\{)`,
|
|
176
|
+
);
|
|
177
|
+
if (call.test(code)) return true;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
return false;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** A script with no JSX is compiled only when it names barq, as the build does. */
|
|
184
|
+
function wanted(path: string, text: () => string): boolean {
|
|
185
|
+
if (path.endsWith(".d.ts")) return false;
|
|
186
|
+
if (JSX.test(path)) return true;
|
|
187
|
+
return SCRIPT.test(path) && (text().includes("@barqjs/") || callsThroughImport(text()));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
const EXTENSIONS = [
|
|
191
|
+
"",
|
|
192
|
+
".ts",
|
|
193
|
+
".tsx",
|
|
194
|
+
".js",
|
|
195
|
+
".jsx",
|
|
196
|
+
".mts",
|
|
197
|
+
"/index.ts",
|
|
198
|
+
"/index.tsx",
|
|
199
|
+
"/index.js",
|
|
200
|
+
];
|
|
201
|
+
|
|
202
|
+
/** A relative import as the file it names, or `undefined` for a package. */
|
|
203
|
+
function resolveImport(from: string, specifier: string): string | undefined {
|
|
204
|
+
if (!specifier.startsWith(".")) return undefined;
|
|
205
|
+
const base = resolve(dirname(from), specifier);
|
|
206
|
+
for (const extension of EXTENSIONS) {
|
|
207
|
+
const candidate = base + extension;
|
|
208
|
+
if (existsSync(candidate) && statSync(candidate).isFile()) return candidate;
|
|
209
|
+
}
|
|
210
|
+
return undefined;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function collect(path: string, out: string[]): void {
|
|
214
|
+
if (statSync(path).isDirectory()) {
|
|
215
|
+
for (const entry of readdirSync(path).toSorted()) {
|
|
216
|
+
if (SKIPPED.has(entry) || entry.startsWith(".")) continue;
|
|
217
|
+
collect(join(path, entry), out);
|
|
218
|
+
}
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
if (JSX.test(path) || SCRIPT.test(path)) out.push(path);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function severityOf(level: string): Severity {
|
|
225
|
+
return level === "error" ? "error" : level === "note" ? "note" : "warning";
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
interface Project {
|
|
229
|
+
options: CheckOptions;
|
|
230
|
+
/** The route patterns, when the project has file routes. */
|
|
231
|
+
patterns: string[] | undefined;
|
|
232
|
+
/** What each file exports that a style call folds against, once per file. */
|
|
233
|
+
exports: Map<string, string[][]>;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** How many modules deep a folded value is followed, as `@barqjs/vite` does. */
|
|
237
|
+
const IMPORT_DEPTH = 6;
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* The transform the build runs for `file`: compiled again with the values it
|
|
241
|
+
* asked for from the files it imports, until a round learns nothing, so a call
|
|
242
|
+
* through an imported `layer` binding or token folds, and declines, as it does
|
|
243
|
+
* in the build.
|
|
244
|
+
*/
|
|
245
|
+
function transformWithImports(
|
|
246
|
+
file: string,
|
|
247
|
+
source: string,
|
|
248
|
+
options: Parameters<Native["transform"]>[1],
|
|
249
|
+
project: Project,
|
|
250
|
+
depth: number,
|
|
251
|
+
seen: Set<string>,
|
|
252
|
+
): ReturnType<Native["transform"]> {
|
|
253
|
+
let out = compiler().transform(source, options);
|
|
254
|
+
if (depth <= 0) return out;
|
|
255
|
+
const cssImports: string[][] = [];
|
|
256
|
+
const asked = new Set<string>();
|
|
257
|
+
for (let round = 0; round < 4; round++) {
|
|
258
|
+
const fresh = (out.cssWanted ?? []).filter(
|
|
259
|
+
([specifier, name]) => !asked.has(`${specifier}\u0000${name}`),
|
|
260
|
+
);
|
|
261
|
+
if (fresh.length === 0) break;
|
|
262
|
+
let learned = false;
|
|
263
|
+
for (const [specifier = "", name = ""] of fresh) {
|
|
264
|
+
asked.add(`${specifier}\u0000${name}`);
|
|
265
|
+
const dependency = resolveImport(file, specifier);
|
|
266
|
+
if (dependency === undefined) continue;
|
|
267
|
+
for (const row of exportsOf(dependency, project, depth - 1, new Set(seen))) {
|
|
268
|
+
if (row[0] !== name) continue;
|
|
269
|
+
cssImports.push([specifier, ...row]);
|
|
270
|
+
learned = true;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
if (!learned) break;
|
|
274
|
+
out = compiler().transform(source, { ...options, cssImports });
|
|
275
|
+
}
|
|
276
|
+
return out;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
function exportsOf(file: string, project: Project, depth: number, seen: Set<string>): string[][] {
|
|
280
|
+
if (depth <= 0 || seen.has(file)) return [];
|
|
281
|
+
seen.add(file);
|
|
282
|
+
const known = project.exports.get(file);
|
|
283
|
+
if (known !== undefined) return known;
|
|
284
|
+
let rows: string[][] = [];
|
|
285
|
+
try {
|
|
286
|
+
const source = readFileSync(file, "utf8");
|
|
287
|
+
rows =
|
|
288
|
+
transformWithImports(
|
|
289
|
+
file,
|
|
290
|
+
source,
|
|
291
|
+
{
|
|
292
|
+
filename: file,
|
|
293
|
+
root: dirname(file),
|
|
294
|
+
dev: true,
|
|
295
|
+
diagnostics: false,
|
|
296
|
+
sourceNames: false,
|
|
297
|
+
cssExports: true,
|
|
298
|
+
},
|
|
299
|
+
project,
|
|
300
|
+
depth,
|
|
301
|
+
seen,
|
|
302
|
+
).cssExports ?? [];
|
|
303
|
+
} catch {
|
|
304
|
+
rows = [];
|
|
305
|
+
}
|
|
306
|
+
project.exports.set(file, rows);
|
|
307
|
+
return rows;
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/** A finding about the project and no one position in a file. */
|
|
311
|
+
function about(file: string, severity: Severity, message: string): Finding {
|
|
312
|
+
return { severity, message, file, line: 1, column: 1, endLine: 1, endColumn: 1 };
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* The file routes, read the way the build reads them and written nowhere: the
|
|
317
|
+
* patterns a link may name, and what the scan itself found wrong.
|
|
318
|
+
*/
|
|
319
|
+
function routesOf(root: string, dir: string): { patterns?: string[]; findings: Finding[] } {
|
|
320
|
+
if (!existsSync(join(root, dir))) return { findings: [] };
|
|
321
|
+
const tree = compiler().routeTree(root, dir);
|
|
322
|
+
return {
|
|
323
|
+
patterns: tree.patterns,
|
|
324
|
+
findings: [
|
|
325
|
+
...tree.warnings.map((warning) => about(dir, "warning", warning)),
|
|
326
|
+
...tree.mismatches.map((mismatch) =>
|
|
327
|
+
about(
|
|
328
|
+
mismatch.file,
|
|
329
|
+
"error",
|
|
330
|
+
`\`createFileRoute("${mismatch.declared}")\` names an id that differs from the one this file's name gives, \`${mismatch.expected}\`, and a build refuses it. Write \`createFileRoute("${mismatch.expected}")\`.`,
|
|
331
|
+
),
|
|
332
|
+
),
|
|
333
|
+
],
|
|
334
|
+
};
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function findingsOf(file: string, root: string, source: string, project: Project): Finding[] {
|
|
338
|
+
const { options } = project;
|
|
339
|
+
const name = relative(root, file).split(sep).join("/");
|
|
340
|
+
let diagnostics: NativeDiagnostic[];
|
|
341
|
+
try {
|
|
342
|
+
diagnostics =
|
|
343
|
+
transformWithImports(
|
|
344
|
+
file,
|
|
345
|
+
source,
|
|
346
|
+
{
|
|
347
|
+
filename: file,
|
|
348
|
+
root,
|
|
349
|
+
dev: true,
|
|
350
|
+
diagnostics: true,
|
|
351
|
+
sourceNames: false,
|
|
352
|
+
checks: options.levels?.map(([code, level]) => [code, level]),
|
|
353
|
+
routes: project.patterns,
|
|
354
|
+
},
|
|
355
|
+
project,
|
|
356
|
+
IMPORT_DEPTH,
|
|
357
|
+
new Set([file]),
|
|
358
|
+
).diagnostics ?? [];
|
|
359
|
+
} catch (error) {
|
|
360
|
+
// The compiler refuses a file it cannot parse by throwing, and the message
|
|
361
|
+
// already carries the position.
|
|
362
|
+
return [about(name, "error", error instanceof Error ? error.message : String(error))];
|
|
363
|
+
}
|
|
364
|
+
return diagnostics.map((found) => {
|
|
365
|
+
const finding: Finding = {
|
|
366
|
+
severity: severityOf(found.severity),
|
|
367
|
+
message: found.message,
|
|
368
|
+
file: name,
|
|
369
|
+
line: found.line,
|
|
370
|
+
column: found.column,
|
|
371
|
+
endLine: found.endLine,
|
|
372
|
+
endColumn: found.endColumn,
|
|
373
|
+
};
|
|
374
|
+
if (found.code !== undefined) {
|
|
375
|
+
finding.code = found.code;
|
|
376
|
+
finding.skill = `${SKILL}#${found.code.toLowerCase()}`;
|
|
377
|
+
}
|
|
378
|
+
if (found.docs !== undefined) finding.docs = found.docs;
|
|
379
|
+
if (found.fix !== undefined && found.fix.length > 0) finding.fix = found.fix;
|
|
380
|
+
return finding;
|
|
381
|
+
});
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/**
|
|
385
|
+
* `source` with every fix applied that does not touch another's span, and how
|
|
386
|
+
* many findings that repaired. Last edit first, so an offset stays valid.
|
|
387
|
+
*/
|
|
388
|
+
export function applyFixes(source: string, findings: readonly Finding[]): [string, number] {
|
|
389
|
+
const chosen: Edit[] = [];
|
|
390
|
+
let repaired = 0;
|
|
391
|
+
for (const finding of findings) {
|
|
392
|
+
const edits = finding.fix;
|
|
393
|
+
if (edits === undefined) continue;
|
|
394
|
+
const clash = edits.some((edit) =>
|
|
395
|
+
chosen.some((taken) => edit.pos < taken.end && taken.pos < edit.end),
|
|
396
|
+
);
|
|
397
|
+
const repeated = edits.every((edit) =>
|
|
398
|
+
chosen.some(
|
|
399
|
+
(taken) => taken.pos === edit.pos && taken.end === edit.end && taken.text === edit.text,
|
|
400
|
+
),
|
|
401
|
+
);
|
|
402
|
+
if (clash || repeated) continue;
|
|
403
|
+
chosen.push(...edits);
|
|
404
|
+
repaired++;
|
|
405
|
+
}
|
|
406
|
+
let text = source;
|
|
407
|
+
for (const edit of chosen.toSorted((a, b) => b.pos - a.pos)) {
|
|
408
|
+
text = text.slice(0, edit.pos) + edit.text + text.slice(edit.end);
|
|
409
|
+
}
|
|
410
|
+
return [text, repaired];
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
export function check(options: CheckOptions): CheckResult {
|
|
414
|
+
const root = resolve(options.root);
|
|
415
|
+
const files: string[] = [];
|
|
416
|
+
for (const path of options.paths === undefined || options.paths.length === 0
|
|
417
|
+
? ["."]
|
|
418
|
+
: options.paths) {
|
|
419
|
+
collect(resolve(root, path), files);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
const routes = routesOf(root, options.routes ?? "src/routes");
|
|
423
|
+
const project: Project = {
|
|
424
|
+
options,
|
|
425
|
+
exports: new Map(),
|
|
426
|
+
patterns:
|
|
427
|
+
routes.patterns === undefined
|
|
428
|
+
? undefined
|
|
429
|
+
: [...routes.patterns, ...(options.extraRoutes ?? [])],
|
|
430
|
+
};
|
|
431
|
+
const findings: Finding[] = [...routes.findings];
|
|
432
|
+
let checked = 0;
|
|
433
|
+
let fixed = 0;
|
|
434
|
+
for (const file of files) {
|
|
435
|
+
let source: string | undefined;
|
|
436
|
+
const text = (): string => (source ??= readFileSync(file, "utf8"));
|
|
437
|
+
if (!wanted(file, text)) continue;
|
|
438
|
+
checked++;
|
|
439
|
+
let found = findingsOf(file, root, text(), project);
|
|
440
|
+
if (options.fix === true) {
|
|
441
|
+
let current = text();
|
|
442
|
+
for (let pass = 0; pass < MAX_PASSES; pass++) {
|
|
443
|
+
const [next, repaired] = applyFixes(current, found);
|
|
444
|
+
if (repaired === 0) break;
|
|
445
|
+
current = next;
|
|
446
|
+
fixed += repaired;
|
|
447
|
+
found = findingsOf(file, root, current, project);
|
|
448
|
+
}
|
|
449
|
+
if (current !== text()) writeFileSync(file, current);
|
|
450
|
+
}
|
|
451
|
+
findings.push(...found);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
const count = (severity: Severity): number =>
|
|
455
|
+
findings.filter((finding) => finding.severity === severity).length;
|
|
456
|
+
return {
|
|
457
|
+
version: 1,
|
|
458
|
+
root,
|
|
459
|
+
files: checked,
|
|
460
|
+
summary: {
|
|
461
|
+
errors: count("error"),
|
|
462
|
+
warnings: count("warning"),
|
|
463
|
+
notes: count("note"),
|
|
464
|
+
fixable: findings.filter((finding) => finding.fix !== undefined).length,
|
|
465
|
+
fixed,
|
|
466
|
+
},
|
|
467
|
+
findings,
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
export interface CodeEntry {
|
|
472
|
+
code: string;
|
|
473
|
+
level: string;
|
|
474
|
+
summary: string;
|
|
475
|
+
docs: string;
|
|
476
|
+
skill: string;
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/** Every code the installed compiler can raise, with its default level. */
|
|
480
|
+
export function codes(): CodeEntry[] {
|
|
481
|
+
return compiler()
|
|
482
|
+
.diagnosticCodes()
|
|
483
|
+
.map((entry) => ({ ...entry, skill: `${SKILL}#${entry.code.toLowerCase()}` }));
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
export interface Paint {
|
|
487
|
+
error(text: string): string;
|
|
488
|
+
warning(text: string): string;
|
|
489
|
+
note(text: string): string;
|
|
490
|
+
dim(text: string): string;
|
|
491
|
+
bold(text: string): string;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
const plural = (count: number, word: string): string => `${count} ${word}${count === 1 ? "" : "s"}`;
|
|
495
|
+
|
|
496
|
+
/** The result for a person: one finding per block, then the count. */
|
|
497
|
+
export function formatText(result: CheckResult, paint: Paint): string {
|
|
498
|
+
const lines: string[] = [];
|
|
499
|
+
for (const finding of result.findings) {
|
|
500
|
+
const at = `${finding.file}:${finding.line}:${finding.column}`;
|
|
501
|
+
const label = paint[finding.severity](finding.severity);
|
|
502
|
+
const code = finding.code === undefined ? "" : ` ${paint.bold(finding.code)}`;
|
|
503
|
+
lines.push(`${at} ${label}${code} ${finding.message}`);
|
|
504
|
+
if (finding.docs !== undefined) lines.push(paint.dim(` docs: ${finding.docs}`));
|
|
505
|
+
if (finding.fix !== undefined) lines.push(paint.dim(" fix: `barq check --fix` repairs this"));
|
|
506
|
+
lines.push("");
|
|
507
|
+
}
|
|
508
|
+
const { errors, warnings, notes, fixable, fixed } = result.summary;
|
|
509
|
+
const parts = [plural(errors, "error"), plural(warnings, "warning"), plural(notes, "note")];
|
|
510
|
+
let summary = `${parts.join(", ")} in ${plural(result.files, "file")}`;
|
|
511
|
+
if (fixed > 0) summary += `, ${fixed} fixed`;
|
|
512
|
+
if (fixable > 0) summary += `, ${fixable} more fixable with \`--fix\``;
|
|
513
|
+
lines.push(summary);
|
|
514
|
+
if (result.findings.some((finding) => finding.code !== undefined)) {
|
|
515
|
+
lines.push(paint.dim(`repair guide: ${SKILL}`));
|
|
516
|
+
}
|
|
517
|
+
return lines.join("\n");
|
|
518
|
+
}
|