flashtrace 0.7.1 → 0.8.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 +5 -0
- package/dist/flashtrace.mjs +308 -276
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -33,6 +33,11 @@ You can then execute `npx flashtrace` to run the tracing.
|
|
|
33
33
|
Read our [Usage Guide](docs/USAGE.md) for more details.
|
|
34
34
|
Node >= 18 is required for flashtrace to work.
|
|
35
35
|
|
|
36
|
+
Looking for a working setup to start from? The [examples/](examples/) folder
|
|
37
|
+
holds four self-contained example projects, from a minimal clean trace to a
|
|
38
|
+
deliberately broken one showing every defect flashtrace reports. Each has a
|
|
39
|
+
README, and the end-to-end tests verify their exact report output on every run.
|
|
40
|
+
|
|
36
41
|
## About flashtrace
|
|
37
42
|
|
|
38
43
|
Let's be honest: This project started when I needed it myself.
|
package/dist/flashtrace.mjs
CHANGED
|
@@ -187,11 +187,11 @@ async function walk(dir, out) {
|
|
|
187
187
|
} catch {
|
|
188
188
|
return out;
|
|
189
189
|
}
|
|
190
|
-
for (const
|
|
191
|
-
if (
|
|
192
|
-
const
|
|
193
|
-
if (
|
|
194
|
-
else if (
|
|
190
|
+
for (const entry of entries) {
|
|
191
|
+
if (entry.name === ".git" || entry.name === "node_modules") continue;
|
|
192
|
+
const entryPath = path.join(dir, entry.name);
|
|
193
|
+
if (entry.isDirectory()) await walk(entryPath, out);
|
|
194
|
+
else if (entry.isFile()) out.push(entryPath);
|
|
195
195
|
}
|
|
196
196
|
return out;
|
|
197
197
|
}
|
|
@@ -199,9 +199,9 @@ async function collectFiles(dirs) {
|
|
|
199
199
|
const files = /* @__PURE__ */ new Set();
|
|
200
200
|
for (const dir of dirs) {
|
|
201
201
|
const abs = path.resolve(dir);
|
|
202
|
-
const
|
|
203
|
-
if (!
|
|
204
|
-
if (
|
|
202
|
+
const stats = await fs.stat(abs).catch(() => null);
|
|
203
|
+
if (!stats) throw new UsageError(`input path does not exist: ${dir}`);
|
|
204
|
+
if (stats.isFile()) {
|
|
205
205
|
files.add(abs);
|
|
206
206
|
continue;
|
|
207
207
|
}
|
|
@@ -218,52 +218,52 @@ async function collectFiles(dirs) {
|
|
|
218
218
|
} catch {
|
|
219
219
|
list = await walk(abs, []);
|
|
220
220
|
}
|
|
221
|
-
for (const
|
|
221
|
+
for (const file of list) files.add(file);
|
|
222
222
|
}
|
|
223
|
-
return [...files].filter((
|
|
224
|
-
const ext = path.extname(
|
|
223
|
+
return [...files].filter((file) => {
|
|
224
|
+
const ext = path.extname(file).toLowerCase();
|
|
225
225
|
return MD_EXT.has(ext) || CODE_EXT.has(ext);
|
|
226
226
|
}).sort(compareStrings);
|
|
227
227
|
}
|
|
228
228
|
|
|
229
229
|
// src/ids.mjs
|
|
230
|
-
var
|
|
230
|
+
var SEGMENT_SRC = "[A-Za-z][A-Za-z0-9_.-]*";
|
|
231
231
|
var REV_SRC = String.raw`\d+(?:\.\d+){0,2}`;
|
|
232
|
-
var
|
|
233
|
-
var
|
|
234
|
-
var ID_SRC = String.raw`([A-Za-z]+):(?:((?:${
|
|
232
|
+
var WILDCARD_SRC = String.raw`(?:\d+\.\d+\.x|\d+\.x\.y|x\.y\.z|\d+\.x|x\.y|x)`;
|
|
233
|
+
var REV_REF_SRC = `(?:${WILDCARD_SRC}|${REV_SRC})`;
|
|
234
|
+
var ID_SRC = String.raw`([A-Za-z]+):(?:((?:${SEGMENT_SRC}\/)*${SEGMENT_SRC})\/)?(${SEGMENT_SRC})#(${REV_SRC})`;
|
|
235
235
|
var ID_RE = new RegExp(`^${ID_SRC}$`);
|
|
236
|
-
var NEED_ID_SRC = String.raw`([A-Za-z]+):(?:((?:${
|
|
236
|
+
var NEED_ID_SRC = String.raw`([A-Za-z]+):(?:((?:${SEGMENT_SRC}\/)*${SEGMENT_SRC})\/)?(${SEGMENT_SRC})#(${REV_REF_SRC})`;
|
|
237
237
|
var NEED_ID_RE = new RegExp(`^${NEED_ID_SRC}$`);
|
|
238
238
|
var FORWARD_SRC = String.raw`\[\s*${ID_SRC}\s*-->\s*${ID_SRC}\s*\]`;
|
|
239
|
-
var
|
|
240
|
-
var
|
|
241
|
-
from:
|
|
242
|
-
to:
|
|
239
|
+
var makeId = (type, group, name, rev) => `${type}:${group ? group + "/" : ""}${name}#${rev}`;
|
|
240
|
+
var makeForward = (m, base, file, line) => ({
|
|
241
|
+
from: makeId(m[base], m[base + 1], m[base + 2], m[base + 3]),
|
|
242
|
+
to: makeId(m[base + 4], m[base + 5], m[base + 6], m[base + 7]),
|
|
243
243
|
file,
|
|
244
244
|
line
|
|
245
245
|
});
|
|
246
246
|
var keyOf = (id) => id.slice(0, id.lastIndexOf("#"));
|
|
247
247
|
var revOf = (id) => id.slice(id.lastIndexOf("#") + 1);
|
|
248
248
|
function compareRev(a, b) {
|
|
249
|
-
const
|
|
250
|
-
const
|
|
251
|
-
for (let i = 0; i < Math.max(
|
|
252
|
-
if (i >=
|
|
253
|
-
if (i >=
|
|
254
|
-
if (
|
|
249
|
+
const partsA = a.split(".");
|
|
250
|
+
const partsB = b.split(".");
|
|
251
|
+
for (let i = 0; i < Math.max(partsA.length, partsB.length); i++) {
|
|
252
|
+
if (i >= partsA.length) return -1;
|
|
253
|
+
if (i >= partsB.length) return 1;
|
|
254
|
+
if (partsA[i] !== partsB[i]) return Number(partsA[i]) - Number(partsB[i]);
|
|
255
255
|
}
|
|
256
256
|
return 0;
|
|
257
257
|
}
|
|
258
|
-
var
|
|
258
|
+
var WILDCARD_LAYERS = /* @__PURE__ */ new Set(["x", "y", "z"]);
|
|
259
259
|
var isWildcardRev = (rev) => /[xyz]/.test(rev);
|
|
260
260
|
function revMatches(pattern, concrete) {
|
|
261
|
-
const
|
|
262
|
-
const
|
|
263
|
-
if (
|
|
264
|
-
for (let i = 0; i <
|
|
265
|
-
if (
|
|
266
|
-
if (
|
|
261
|
+
const patternParts = pattern.split(".");
|
|
262
|
+
const concreteParts = concrete.split(".");
|
|
263
|
+
if (patternParts.length !== concreteParts.length) return false;
|
|
264
|
+
for (let i = 0; i < patternParts.length; i++) {
|
|
265
|
+
if (WILDCARD_LAYERS.has(patternParts[i])) continue;
|
|
266
|
+
if (patternParts[i] !== concreteParts[i]) return false;
|
|
267
267
|
}
|
|
268
268
|
return true;
|
|
269
269
|
}
|
|
@@ -271,12 +271,12 @@ var idMatches = (need, id) => keyOf(need) === keyOf(id) && revMatches(revOf(need
|
|
|
271
271
|
function parseIdEntry(raw) {
|
|
272
272
|
const cleaned = raw.replaceAll("`", "").trim();
|
|
273
273
|
const m = cleaned.match(ID_RE);
|
|
274
|
-
return m ?
|
|
274
|
+
return m ? makeId(m[1], m[2], m[3], m[4]) : null;
|
|
275
275
|
}
|
|
276
276
|
function parseNeedEntry(raw) {
|
|
277
277
|
const cleaned = raw.replaceAll("`", "").trim();
|
|
278
278
|
const m = cleaned.match(NEED_ID_RE);
|
|
279
|
-
return m ?
|
|
279
|
+
return m ? makeId(m[1], m[2], m[3], m[4]) : null;
|
|
280
280
|
}
|
|
281
281
|
function newItem(id, origin, file, line) {
|
|
282
282
|
return {
|
|
@@ -299,24 +299,32 @@ function newItem(id, origin, file, line) {
|
|
|
299
299
|
}
|
|
300
300
|
|
|
301
301
|
// src/parse-markdown.mjs
|
|
302
|
-
var
|
|
302
|
+
var DEFINITION_RE = new RegExp(String.raw`^\s*\`${ID_SRC}\`\s*$`);
|
|
303
303
|
var HEADING_RE = /^(#{1,6})\s+(\S(?:.*\S)?)\s*$/;
|
|
304
|
+
var SETEXT_UNDERLINE_RE = /^ {0,3}(?:=+|-+)[ \t]*$/;
|
|
304
305
|
var KEYWORD_RE = /^(Needs|Covers|Tags):\s*((?:\S.*)?)$/;
|
|
305
306
|
var BULLET_RE = /^\s*[-*+]\s+(\S(?:.*\S)?)\s*$/;
|
|
306
|
-
var
|
|
307
|
+
var DELIMITER_CELL_RE = /^:?-+:?$/;
|
|
307
308
|
var FORWARD_LINE_RE = new RegExp(String.raw`^\s*(\`?)${FORWARD_SRC}\1\s*$`);
|
|
308
|
-
var isBoundary = (
|
|
309
|
-
function takeForward(line, file,
|
|
310
|
-
const
|
|
311
|
-
if (
|
|
312
|
-
return !!
|
|
313
|
-
}
|
|
314
|
-
function
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
309
|
+
var isBoundary = (line) => DEFINITION_RE.test(line) || HEADING_RE.test(line);
|
|
310
|
+
function takeForward(line, file, lineIndex, forwards) {
|
|
311
|
+
const forward = line.match(FORWARD_LINE_RE);
|
|
312
|
+
if (forward) forwards.push(makeForward(forward, 2, file, lineIndex + 1));
|
|
313
|
+
return !!forward;
|
|
314
|
+
}
|
|
315
|
+
function isParagraphLine(line) {
|
|
316
|
+
return line.trim() !== "" && !HEADING_RE.test(line) && !DEFINITION_RE.test(line) && !BULLET_RE.test(line) && !SETEXT_UNDERLINE_RE.test(line);
|
|
317
|
+
}
|
|
318
|
+
function titleAbove(lines, definitionIndex) {
|
|
319
|
+
for (let k = definitionIndex - 1; k >= 0; k--) {
|
|
320
|
+
const line = lines[k];
|
|
321
|
+
if (line.trim() === "") continue;
|
|
322
|
+
const heading = line.match(HEADING_RE);
|
|
323
|
+
if (heading) return heading[2];
|
|
324
|
+
if (SETEXT_UNDERLINE_RE.test(line) && k > 0 && isParagraphLine(lines[k - 1])) {
|
|
325
|
+
return lines[k - 1].trim();
|
|
326
|
+
}
|
|
327
|
+
return null;
|
|
320
328
|
}
|
|
321
329
|
return null;
|
|
322
330
|
}
|
|
@@ -326,19 +334,19 @@ function keywordEntries(lines, j, inline) {
|
|
|
326
334
|
}
|
|
327
335
|
const entries = [];
|
|
328
336
|
while (j + 1 < lines.length) {
|
|
329
|
-
const
|
|
330
|
-
if (!
|
|
331
|
-
entries.push(
|
|
337
|
+
const bullet = lines[j + 1].match(BULLET_RE);
|
|
338
|
+
if (!bullet) break;
|
|
339
|
+
entries.push(bullet[1].trim());
|
|
332
340
|
j++;
|
|
333
341
|
}
|
|
334
342
|
return { entries, j };
|
|
335
343
|
}
|
|
336
344
|
function rowCells(line) {
|
|
337
|
-
let
|
|
338
|
-
if (!
|
|
339
|
-
if (
|
|
340
|
-
if (
|
|
341
|
-
return
|
|
345
|
+
let row = line.trim();
|
|
346
|
+
if (!row.includes("|")) return null;
|
|
347
|
+
if (row.startsWith("|")) row = row.slice(1);
|
|
348
|
+
if (row.endsWith("|")) row = row.slice(0, -1);
|
|
349
|
+
return row.split("|").map((cell) => cell.trim());
|
|
342
350
|
}
|
|
343
351
|
function takeKeywordTable(lines, j, item, file, problems) {
|
|
344
352
|
const header = rowCells(lines[j]);
|
|
@@ -348,8 +356,8 @@ function takeKeywordTable(lines, j, item, file, problems) {
|
|
|
348
356
|
if (cell === "Needs" || cell === "Covers" || cell === "Tags") columns.push([col, cell]);
|
|
349
357
|
});
|
|
350
358
|
if (columns.length === 0) return null;
|
|
351
|
-
const
|
|
352
|
-
if (
|
|
359
|
+
const delimiter = j + 1 < lines.length ? rowCells(lines[j + 1]) : null;
|
|
360
|
+
if (delimiter?.length !== header.length || !delimiter.every((cell) => DELIMITER_CELL_RE.test(cell))) return null;
|
|
353
361
|
j++;
|
|
354
362
|
while (j + 1 < lines.length && !isBoundary(lines[j + 1])) {
|
|
355
363
|
const cells = rowCells(lines[j + 1]);
|
|
@@ -361,46 +369,47 @@ function takeKeywordTable(lines, j, item, file, problems) {
|
|
|
361
369
|
}
|
|
362
370
|
return j;
|
|
363
371
|
}
|
|
364
|
-
function applyKeyword(item, keyword, entries, file,
|
|
372
|
+
function applyKeyword(item, keyword, entries, file, keywordLine, problems) {
|
|
365
373
|
if (keyword === "Tags") {
|
|
366
374
|
item.tags.push(...entries);
|
|
367
375
|
return;
|
|
368
376
|
}
|
|
369
377
|
const target = keyword === "Needs" ? "needs" : "covers";
|
|
370
378
|
const parse = keyword === "Needs" ? parseNeedEntry : parseIdEntry;
|
|
371
|
-
for (const
|
|
372
|
-
const id = parse(
|
|
379
|
+
for (const entry of entries) {
|
|
380
|
+
const id = parse(entry);
|
|
373
381
|
if (id) item[target].push(id);
|
|
374
382
|
else
|
|
375
383
|
problems.push({
|
|
384
|
+
severity: "error",
|
|
376
385
|
file,
|
|
377
|
-
line:
|
|
378
|
-
message: `invalid ID "${
|
|
386
|
+
line: keywordLine,
|
|
387
|
+
message: `invalid ID "${entry}" in ${keyword}: list of ${item.id}`
|
|
379
388
|
});
|
|
380
389
|
}
|
|
381
390
|
}
|
|
382
391
|
function parseItemBody(lines, start, item, file, problems, forwards) {
|
|
383
392
|
let j = start;
|
|
384
|
-
let
|
|
393
|
+
let descriptionDone = false;
|
|
385
394
|
while (j < lines.length && !isBoundary(lines[j])) {
|
|
386
395
|
const line = lines[j];
|
|
387
396
|
if (takeForward(line, file, j, forwards)) {
|
|
388
397
|
j++;
|
|
389
398
|
continue;
|
|
390
399
|
}
|
|
391
|
-
const
|
|
392
|
-
const tableEnd =
|
|
393
|
-
if (
|
|
394
|
-
|
|
395
|
-
const collected = keywordEntries(lines, j,
|
|
396
|
-
applyKeyword(item,
|
|
400
|
+
const keywordMatch = line.match(KEYWORD_RE);
|
|
401
|
+
const tableEnd = keywordMatch ? null : takeKeywordTable(lines, j, item, file, problems);
|
|
402
|
+
if (keywordMatch) {
|
|
403
|
+
descriptionDone = true;
|
|
404
|
+
const collected = keywordEntries(lines, j, keywordMatch[2]);
|
|
405
|
+
applyKeyword(item, keywordMatch[1], collected.entries, file, j + 1, problems);
|
|
397
406
|
j = collected.j;
|
|
398
407
|
} else if (tableEnd !== null) {
|
|
399
|
-
|
|
408
|
+
descriptionDone = true;
|
|
400
409
|
j = tableEnd;
|
|
401
410
|
} else if (line.trim() === "") {
|
|
402
|
-
if (item.description.length > 0)
|
|
403
|
-
} else if (!
|
|
411
|
+
if (item.description.length > 0) descriptionDone = true;
|
|
412
|
+
} else if (!descriptionDone) {
|
|
404
413
|
item.description.push(line.trim());
|
|
405
414
|
}
|
|
406
415
|
j++;
|
|
@@ -416,12 +425,12 @@ function parseMarkdown(file, text, problems, forwards = []) {
|
|
|
416
425
|
i++;
|
|
417
426
|
continue;
|
|
418
427
|
}
|
|
419
|
-
const
|
|
420
|
-
if (!
|
|
428
|
+
const definition = lines[i].match(DEFINITION_RE);
|
|
429
|
+
if (!definition) {
|
|
421
430
|
i++;
|
|
422
431
|
continue;
|
|
423
432
|
}
|
|
424
|
-
const item = newItem(
|
|
433
|
+
const item = newItem(makeId(definition[1], definition[2], definition[3], definition[4]), "markdown", file, i + 1);
|
|
425
434
|
item.title = titleAbove(lines, i);
|
|
426
435
|
i = parseItemBody(lines, i + 1, item, file, problems, forwards);
|
|
427
436
|
items.push(item);
|
|
@@ -440,39 +449,39 @@ function activeLeaf(grammar, state) {
|
|
|
440
449
|
if (state.region) return state.region.grammar;
|
|
441
450
|
return grammar.regions ? grammar.default : grammar;
|
|
442
451
|
}
|
|
443
|
-
function matchAt(re,
|
|
452
|
+
function matchAt(re, line, pos) {
|
|
444
453
|
re.lastIndex = pos;
|
|
445
|
-
return re.exec(
|
|
454
|
+
return re.exec(line);
|
|
446
455
|
}
|
|
447
|
-
function* regionEvents(
|
|
456
|
+
function* regionEvents(line, pos, grammar, state) {
|
|
448
457
|
if (!grammar.regions) return;
|
|
449
458
|
if (state.region) {
|
|
450
|
-
const m = matchAt(state.region.exit,
|
|
459
|
+
const m = matchAt(state.region.exit, line, pos);
|
|
451
460
|
if (m) yield { idx: m.index, kind: "exit", len: m[0].length };
|
|
452
461
|
return;
|
|
453
462
|
}
|
|
454
|
-
for (const
|
|
455
|
-
const m = matchAt(
|
|
463
|
+
for (const region of grammar.regions) {
|
|
464
|
+
const m = matchAt(region.enter, line, pos);
|
|
456
465
|
if (m) {
|
|
457
|
-
const leaf = typeof
|
|
458
|
-
yield { idx: m.index, kind: "enter", len: m[0].length, region: { exit:
|
|
466
|
+
const leaf = typeof region.grammar === "function" ? region.grammar(m[0]) : region.grammar;
|
|
467
|
+
yield { idx: m.index, kind: "enter", len: m[0].length, region: { exit: region.exit, grammar: leaf } };
|
|
459
468
|
}
|
|
460
469
|
}
|
|
461
470
|
}
|
|
462
|
-
function nextEvent(
|
|
471
|
+
function nextEvent(line, pos, grammar, state) {
|
|
463
472
|
const leaf = activeLeaf(grammar, state);
|
|
464
473
|
let best = null;
|
|
465
|
-
const consider = (idx,
|
|
474
|
+
const consider = (idx, event) => {
|
|
466
475
|
if (idx === -1) return;
|
|
467
|
-
if (best === null || idx < best.idx || idx === best.idx &&
|
|
468
|
-
best = { ...
|
|
476
|
+
if (best === null || idx < best.idx || idx === best.idx && event.len > best.len) {
|
|
477
|
+
best = { ...event, idx };
|
|
469
478
|
}
|
|
470
479
|
};
|
|
471
480
|
for (const marker of leaf.line) {
|
|
472
|
-
consider(
|
|
481
|
+
consider(line.indexOf(marker, pos), { kind: "line", len: marker.length });
|
|
473
482
|
}
|
|
474
483
|
for (const [open, close, nestable] of leaf.block) {
|
|
475
|
-
consider(
|
|
484
|
+
consider(line.indexOf(open, pos), {
|
|
476
485
|
kind: "block",
|
|
477
486
|
len: open.length,
|
|
478
487
|
open,
|
|
@@ -480,16 +489,16 @@ function nextEvent(s, pos, grammar, state) {
|
|
|
480
489
|
nestable: Boolean(nestable)
|
|
481
490
|
});
|
|
482
491
|
}
|
|
483
|
-
for (const
|
|
492
|
+
for (const event of regionEvents(line, pos, grammar, state)) consider(event.idx, event);
|
|
484
493
|
return best;
|
|
485
494
|
}
|
|
486
|
-
function readBlockRest(
|
|
495
|
+
function readBlockRest(line, pos, block, limit = line.length) {
|
|
487
496
|
const { open, close, nestable } = block;
|
|
488
497
|
const within = (idx) => idx !== -1 && idx < limit ? idx : -1;
|
|
489
498
|
let i = pos;
|
|
490
499
|
while (i < limit) {
|
|
491
|
-
const closeIdx = within(
|
|
492
|
-
const openIdx = within(nestable ?
|
|
500
|
+
const closeIdx = within(line.indexOf(close, i));
|
|
501
|
+
const openIdx = within(nestable ? line.indexOf(open, i) : -1);
|
|
493
502
|
if (closeIdx === -1 && openIdx === -1) break;
|
|
494
503
|
if (openIdx !== -1 && (closeIdx === -1 || openIdx < closeIdx)) {
|
|
495
504
|
block.depth++;
|
|
@@ -498,16 +507,16 @@ function readBlockRest(s, pos, block, limit = s.length) {
|
|
|
498
507
|
}
|
|
499
508
|
block.depth--;
|
|
500
509
|
i = closeIdx + close.length;
|
|
501
|
-
if (block.depth === 0) return { text:
|
|
510
|
+
if (block.depth === 0) return { text: line.slice(pos, closeIdx) + " ", pos: i, closed: true };
|
|
502
511
|
}
|
|
503
|
-
return { text:
|
|
512
|
+
return { text: line.slice(pos, limit) + " ", pos: limit, closed: false };
|
|
504
513
|
}
|
|
505
|
-
function regionExitAt(
|
|
506
|
-
const m = matchAt(region.exit,
|
|
514
|
+
function regionExitAt(line, pos, region) {
|
|
515
|
+
const m = matchAt(region.exit, line, pos);
|
|
507
516
|
return m ? { idx: m.index, len: m[0].length } : null;
|
|
508
517
|
}
|
|
509
|
-
function consumeBlock(
|
|
510
|
-
const rest = readBlockRest(
|
|
518
|
+
function consumeBlock(line, pos, state, exit) {
|
|
519
|
+
const rest = readBlockRest(line, pos, state.block, exit ? exit.idx : line.length);
|
|
511
520
|
if (rest.closed) {
|
|
512
521
|
state.block = null;
|
|
513
522
|
return { text: rest.text, pos: rest.pos };
|
|
@@ -519,36 +528,36 @@ function consumeBlock(s, pos, state, exit) {
|
|
|
519
528
|
}
|
|
520
529
|
return { text: rest.text, pos: rest.pos };
|
|
521
530
|
}
|
|
522
|
-
function consumeLine(
|
|
531
|
+
function consumeLine(line, pos, state, exit) {
|
|
523
532
|
if (exit) {
|
|
524
533
|
state.region = null;
|
|
525
|
-
return { text:
|
|
534
|
+
return { text: line.slice(pos, exit.idx) + " ", pos: exit.idx + exit.len, done: false };
|
|
526
535
|
}
|
|
527
|
-
return { text:
|
|
536
|
+
return { text: line.slice(pos) + " ", pos: line.length, done: true };
|
|
528
537
|
}
|
|
529
|
-
function commentText(
|
|
538
|
+
function commentText(line, state, grammar) {
|
|
530
539
|
let comment = "";
|
|
531
540
|
let pos = 0;
|
|
532
|
-
while (pos <
|
|
533
|
-
const exit = state.region ? regionExitAt(
|
|
541
|
+
while (pos < line.length) {
|
|
542
|
+
const exit = state.region ? regionExitAt(line, pos, state.region) : null;
|
|
534
543
|
if (state.block) {
|
|
535
|
-
const
|
|
536
|
-
comment +=
|
|
537
|
-
pos =
|
|
544
|
+
const consumed = consumeBlock(line, pos, state, exit);
|
|
545
|
+
comment += consumed.text;
|
|
546
|
+
pos = consumed.pos;
|
|
538
547
|
continue;
|
|
539
548
|
}
|
|
540
|
-
const
|
|
541
|
-
if (!
|
|
542
|
-
pos =
|
|
543
|
-
if (
|
|
544
|
-
const
|
|
545
|
-
comment +=
|
|
546
|
-
pos =
|
|
547
|
-
if (
|
|
548
|
-
} else if (
|
|
549
|
-
state.block = { open:
|
|
550
|
-
} else if (
|
|
551
|
-
state.region =
|
|
549
|
+
const event = nextEvent(line, pos, grammar, state);
|
|
550
|
+
if (!event) break;
|
|
551
|
+
pos = event.idx + event.len;
|
|
552
|
+
if (event.kind === "line") {
|
|
553
|
+
const consumed = consumeLine(line, pos, state, exit);
|
|
554
|
+
comment += consumed.text;
|
|
555
|
+
pos = consumed.pos;
|
|
556
|
+
if (consumed.done) break;
|
|
557
|
+
} else if (event.kind === "block") {
|
|
558
|
+
state.block = { open: event.open, close: event.close, nestable: event.nestable, depth: 1 };
|
|
559
|
+
} else if (event.kind === "enter") {
|
|
560
|
+
state.region = event.region;
|
|
552
561
|
} else {
|
|
553
562
|
state.region = null;
|
|
554
563
|
}
|
|
@@ -558,29 +567,31 @@ function commentText(s, state, grammar) {
|
|
|
558
567
|
function collectTags(comment, file, line, state, items, problems) {
|
|
559
568
|
for (const m of comment.matchAll(TAG_RE)) {
|
|
560
569
|
if (m[9]) {
|
|
561
|
-
const item = newItem(
|
|
562
|
-
state.
|
|
570
|
+
const item = newItem(makeId(m[9], m[10], m[11], m[12]), "code", file, line);
|
|
571
|
+
state.lastItem = item;
|
|
563
572
|
state.byId.set(item.id, item);
|
|
564
573
|
items.push(item);
|
|
565
574
|
continue;
|
|
566
575
|
}
|
|
567
|
-
const id =
|
|
576
|
+
const id = makeId(m[5], m[6], m[7], m[8]);
|
|
568
577
|
if (m[1]) {
|
|
569
|
-
const source =
|
|
578
|
+
const source = makeId(m[1], m[2], m[3], m[4]);
|
|
570
579
|
const anchor = state.byId.get(source);
|
|
571
580
|
if (anchor) {
|
|
572
581
|
anchor.needs.push(id);
|
|
573
582
|
} else {
|
|
574
583
|
problems.push({
|
|
584
|
+
severity: "error",
|
|
575
585
|
file,
|
|
576
586
|
line,
|
|
577
587
|
message: `need tag [${source} >> ${id}] has no preceding item tag [${source}] in this file`
|
|
578
588
|
});
|
|
579
589
|
}
|
|
580
|
-
} else if (state.
|
|
581
|
-
state.
|
|
590
|
+
} else if (state.lastItem) {
|
|
591
|
+
state.lastItem.needs.push(id);
|
|
582
592
|
} else {
|
|
583
593
|
problems.push({
|
|
594
|
+
severity: "error",
|
|
584
595
|
file,
|
|
585
596
|
line,
|
|
586
597
|
message: `need tag [>>${id}] has no preceding item tag in this file`
|
|
@@ -593,11 +604,11 @@ function parseCode(file, text, problems, forwards = []) {
|
|
|
593
604
|
const grammar = grammarFor(ext) ?? cLike;
|
|
594
605
|
const lines = text.split(/\r?\n/);
|
|
595
606
|
const items = [];
|
|
596
|
-
const state = {
|
|
607
|
+
const state = { lastItem: null, byId: /* @__PURE__ */ new Map(), block: null, region: null };
|
|
597
608
|
for (let i = 0; i < lines.length; i++) {
|
|
598
609
|
const comment = commentText(lines[i], state, grammar);
|
|
599
610
|
for (const m of comment.matchAll(FORWARD_RE)) {
|
|
600
|
-
forwards.push(
|
|
611
|
+
forwards.push(makeForward(m, 1, file, i + 1));
|
|
601
612
|
}
|
|
602
613
|
collectTags(comment, file, i + 1, state, items, problems);
|
|
603
614
|
}
|
|
@@ -605,82 +616,83 @@ function parseCode(file, text, problems, forwards = []) {
|
|
|
605
616
|
}
|
|
606
617
|
|
|
607
618
|
// src/analyze.mjs
|
|
608
|
-
function checkItemReferences(
|
|
609
|
-
const
|
|
610
|
-
if (
|
|
611
|
-
if (!byId.has(
|
|
612
|
-
|
|
619
|
+
function checkItemReferences(item, byId, matchesOf, isNeeded, revHint, forwardTargets) {
|
|
620
|
+
const forwardTarget = forwardTargets.get(item.id);
|
|
621
|
+
if (forwardTarget !== void 0) {
|
|
622
|
+
if (!byId.has(forwardTarget))
|
|
623
|
+
item.defects.push(`uncovered: forwards to ${forwardTarget}, which does not exist${revHint(forwardTarget)}`);
|
|
613
624
|
} else {
|
|
614
|
-
for (const
|
|
615
|
-
if (matchesOf(
|
|
616
|
-
|
|
625
|
+
for (const need of item.needs) {
|
|
626
|
+
if (matchesOf(need).length === 0)
|
|
627
|
+
item.defects.push(`uncovered: needs ${need}, which does not exist${revHint(need)}`);
|
|
617
628
|
}
|
|
618
629
|
}
|
|
619
|
-
for (const
|
|
620
|
-
const targets = byId.get(
|
|
630
|
+
for (const coverId of item.covers) {
|
|
631
|
+
const targets = byId.get(coverId);
|
|
621
632
|
if (!targets) {
|
|
622
|
-
|
|
623
|
-
} else if (!targets.some((
|
|
624
|
-
|
|
633
|
+
item.defects.push(`orphaned: covers ${coverId}, which does not exist${revHint(coverId)}`);
|
|
634
|
+
} else if (!targets.some((target) => target.needs.some((need) => idMatches(need, item.id)))) {
|
|
635
|
+
item.defects.push(`unwanted: covers ${coverId}, but ${coverId} does not need ${item.id}`);
|
|
625
636
|
}
|
|
626
637
|
}
|
|
627
|
-
if (
|
|
628
|
-
|
|
638
|
+
if (item.origin === "code" && !isNeeded(item.id)) {
|
|
639
|
+
item.defects.push(`unwanted: no item needs ${item.id}`);
|
|
629
640
|
}
|
|
630
641
|
}
|
|
631
|
-
function dropCyclicForwards(
|
|
642
|
+
function dropCyclicForwards(forwardTargets, declarationBySource, problems) {
|
|
632
643
|
const done = /* @__PURE__ */ new Set();
|
|
633
|
-
for (const start of
|
|
644
|
+
for (const start of forwardTargets.keys()) {
|
|
634
645
|
if (done.has(start)) continue;
|
|
635
646
|
const seen = /* @__PURE__ */ new Map();
|
|
636
647
|
const path5 = [];
|
|
637
|
-
let
|
|
638
|
-
while (
|
|
639
|
-
seen.set(
|
|
640
|
-
path5.push(
|
|
641
|
-
|
|
648
|
+
let current = start;
|
|
649
|
+
while (forwardTargets.has(current) && !done.has(current) && !seen.has(current)) {
|
|
650
|
+
seen.set(current, path5.length);
|
|
651
|
+
path5.push(current);
|
|
652
|
+
current = forwardTargets.get(current);
|
|
642
653
|
}
|
|
643
|
-
if (seen.has(
|
|
644
|
-
const cycle = path5.slice(seen.get(
|
|
645
|
-
const chain = [...cycle,
|
|
654
|
+
if (seen.has(current)) {
|
|
655
|
+
const cycle = path5.slice(seen.get(current));
|
|
656
|
+
const chain = [...cycle, current].join(" --> ");
|
|
646
657
|
for (const id of cycle) {
|
|
647
|
-
const
|
|
648
|
-
problems.push({ file:
|
|
649
|
-
|
|
658
|
+
const declaration = declarationBySource.get(id);
|
|
659
|
+
problems.push({ severity: "error", file: declaration.file, line: declaration.line, message: `cyclic forwarding: ${chain}` });
|
|
660
|
+
forwardTargets.delete(id);
|
|
650
661
|
}
|
|
651
662
|
}
|
|
652
663
|
for (const id of path5) done.add(id);
|
|
653
664
|
}
|
|
654
665
|
}
|
|
655
666
|
function buildForwardMap(forwards, byId, neededIds, revHint, problems) {
|
|
656
|
-
const
|
|
657
|
-
const
|
|
658
|
-
const
|
|
659
|
-
for (const
|
|
660
|
-
(
|
|
667
|
+
const forwardTargets = /* @__PURE__ */ new Map();
|
|
668
|
+
const declarationBySource = /* @__PURE__ */ new Map();
|
|
669
|
+
const forwardsBySource = /* @__PURE__ */ new Map();
|
|
670
|
+
for (const forward of forwards) {
|
|
671
|
+
(forwardsBySource.get(forward.from) ?? forwardsBySource.set(forward.from, []).get(forward.from)).push(forward);
|
|
661
672
|
}
|
|
662
|
-
for (const [from, group] of
|
|
673
|
+
for (const [from, group] of forwardsBySource) {
|
|
663
674
|
const sources = byId.get(from);
|
|
664
675
|
if (!sources) {
|
|
665
|
-
for (const
|
|
676
|
+
for (const forward of group)
|
|
666
677
|
problems.push({
|
|
667
|
-
|
|
668
|
-
|
|
678
|
+
severity: "error",
|
|
679
|
+
file: forward.file,
|
|
680
|
+
line: forward.line,
|
|
669
681
|
message: `forwarding from ${from}, which does not exist${revHint(from)}`
|
|
670
682
|
});
|
|
671
683
|
continue;
|
|
672
684
|
}
|
|
673
685
|
if (group.length > 1)
|
|
674
|
-
for (const
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
686
|
+
for (const item of sources)
|
|
687
|
+
item.defects.push(`duplicate: forwarding for ${from} is declared ${group.length} times`);
|
|
688
|
+
forwardTargets.set(from, group[0].to);
|
|
689
|
+
declarationBySource.set(from, group[0]);
|
|
678
690
|
}
|
|
679
|
-
dropCyclicForwards(
|
|
680
|
-
for (const to of
|
|
681
|
-
return
|
|
691
|
+
dropCyclicForwards(forwardTargets, declarationBySource, problems);
|
|
692
|
+
for (const to of forwardTargets.values()) neededIds.add(to);
|
|
693
|
+
return forwardTargets;
|
|
682
694
|
}
|
|
683
|
-
function markDeepCoverage(items, byId, matchesOf,
|
|
695
|
+
function markDeepCoverage(items, byId, matchesOf, forwardTargets) {
|
|
684
696
|
const memo = /* @__PURE__ */ new Map();
|
|
685
697
|
const deep = (id) => {
|
|
686
698
|
if (memo.has(id)) return memo.get(id);
|
|
@@ -690,18 +702,18 @@ function markDeepCoverage(items, byId, matchesOf, fwdTarget) {
|
|
|
690
702
|
memo.set(id, false);
|
|
691
703
|
return false;
|
|
692
704
|
}
|
|
693
|
-
const
|
|
705
|
+
const forwardTarget = forwardTargets.get(id);
|
|
694
706
|
let ok = true;
|
|
695
|
-
if (
|
|
696
|
-
ok = deep(
|
|
707
|
+
if (forwardTarget !== void 0) {
|
|
708
|
+
ok = deep(forwardTarget);
|
|
697
709
|
} else {
|
|
698
|
-
for (const
|
|
710
|
+
for (const item of group) for (const need of item.needs) if (!needDeep(need)) ok = false;
|
|
699
711
|
}
|
|
700
712
|
memo.set(id, ok);
|
|
701
713
|
return ok;
|
|
702
714
|
};
|
|
703
|
-
const needDeep = (
|
|
704
|
-
for (const
|
|
715
|
+
const needDeep = (need) => matchesOf(need).some((id) => deep(id));
|
|
716
|
+
for (const item of items) item.deepCovered = deep(item.id);
|
|
705
717
|
}
|
|
706
718
|
function groupIdsByKey(byId) {
|
|
707
719
|
const idsByKey = /* @__PURE__ */ new Map();
|
|
@@ -711,7 +723,7 @@ function groupIdsByKey(byId) {
|
|
|
711
723
|
}
|
|
712
724
|
function buildResolver(items) {
|
|
713
725
|
const byId = /* @__PURE__ */ new Map();
|
|
714
|
-
for (const
|
|
726
|
+
for (const item of items) (byId.get(item.id) ?? byId.set(item.id, []).get(item.id)).push(item);
|
|
715
727
|
const idsByKey = groupIdsByKey(byId);
|
|
716
728
|
const matchesOf = (ref) => (idsByKey.get(keyOf(ref)) ?? []).filter((id) => idMatches(ref, id));
|
|
717
729
|
return { byId, matchesOf };
|
|
@@ -719,34 +731,34 @@ function buildResolver(items) {
|
|
|
719
731
|
function splitNeeds(items) {
|
|
720
732
|
const exact = /* @__PURE__ */ new Set();
|
|
721
733
|
const wildcard = [];
|
|
722
|
-
for (const
|
|
723
|
-
for (const
|
|
724
|
-
if (isWildcardRev(revOf(
|
|
725
|
-
else exact.add(
|
|
734
|
+
for (const item of items)
|
|
735
|
+
for (const need of item.needs) {
|
|
736
|
+
if (isWildcardRev(revOf(need))) wildcard.push(need);
|
|
737
|
+
else exact.add(need);
|
|
726
738
|
}
|
|
727
739
|
return { exact, wildcard };
|
|
728
740
|
}
|
|
729
741
|
function analyze(items, forwards = [], problems = []) {
|
|
730
742
|
const { byId, matchesOf } = buildResolver(items);
|
|
731
743
|
const revsByKey = /* @__PURE__ */ new Map();
|
|
732
|
-
for (const
|
|
733
|
-
(revsByKey.get(
|
|
744
|
+
for (const item of items)
|
|
745
|
+
(revsByKey.get(item.key) ?? revsByKey.set(item.key, /* @__PURE__ */ new Set()).get(item.key)).add(item.revision);
|
|
734
746
|
const { exact: exactNeeds, wildcard: wildcardNeeds } = splitNeeds(items);
|
|
735
|
-
const isNeeded = (id) => exactNeeds.has(id) || wildcardNeeds.some((
|
|
747
|
+
const isNeeded = (id) => exactNeeds.has(id) || wildcardNeeds.some((wildcard) => idMatches(wildcard, id));
|
|
736
748
|
for (const [id, group] of byId) {
|
|
737
749
|
if (group.length > 1)
|
|
738
|
-
for (const
|
|
750
|
+
for (const item of group) item.defects.push(`duplicate: ID ${id} is defined ${group.length} times`);
|
|
739
751
|
}
|
|
740
752
|
const revHint = (id) => {
|
|
741
753
|
const revs = revsByKey.get(keyOf(id));
|
|
742
754
|
return revs ? ` (revision mismatch: existing revision(s) of ${keyOf(id)}: ${[...revs].sort(compareRev).join(", ")})` : "";
|
|
743
755
|
};
|
|
744
|
-
const
|
|
745
|
-
for (const
|
|
746
|
-
|
|
747
|
-
checkItemReferences(
|
|
756
|
+
const forwardTargets = buildForwardMap(forwards, byId, exactNeeds, revHint, problems);
|
|
757
|
+
for (const item of items) {
|
|
758
|
+
item.forwardsTo = forwardTargets.get(item.id) ?? null;
|
|
759
|
+
checkItemReferences(item, byId, matchesOf, isNeeded, revHint, forwardTargets);
|
|
748
760
|
}
|
|
749
|
-
markDeepCoverage(items, byId, matchesOf,
|
|
761
|
+
markDeepCoverage(items, byId, matchesOf, forwardTargets);
|
|
750
762
|
}
|
|
751
763
|
|
|
752
764
|
// src/report.mjs
|
|
@@ -754,9 +766,10 @@ import path3 from "node:path";
|
|
|
754
766
|
import process2 from "node:process";
|
|
755
767
|
function makeStyler() {
|
|
756
768
|
const on = process2.stdout.isTTY && !process2.env.NO_COLOR;
|
|
757
|
-
const wrap = (code) => (
|
|
769
|
+
const wrap = (code) => (text) => on ? `\x1B[${code}m${text}\x1B[0m` : text;
|
|
758
770
|
return {
|
|
759
771
|
red: wrap("31"),
|
|
772
|
+
brightRed: wrap("91"),
|
|
760
773
|
green: wrap("32"),
|
|
761
774
|
yellow: wrap("33"),
|
|
762
775
|
cyan: wrap("36"),
|
|
@@ -764,124 +777,130 @@ function makeStyler() {
|
|
|
764
777
|
bold: wrap("1")
|
|
765
778
|
};
|
|
766
779
|
}
|
|
767
|
-
function statusOf(
|
|
768
|
-
if (
|
|
769
|
-
if (!
|
|
770
|
-
return { mark:
|
|
780
|
+
function statusOf(item, style) {
|
|
781
|
+
if (item.defects.length > 0) return { mark: style.red("\u2718"), tag: style.red("[defective]") };
|
|
782
|
+
if (!item.deepCovered) return { mark: style.yellow("~"), tag: style.yellow("[shallow-covered]") };
|
|
783
|
+
return { mark: style.green("\u2714"), tag: style.green("[deep-covered]") };
|
|
771
784
|
}
|
|
772
785
|
function buildWantedBy(items, byId, matchesOf) {
|
|
773
786
|
const wantedBy = /* @__PURE__ */ new Map();
|
|
774
|
-
for (const
|
|
775
|
-
for (const
|
|
776
|
-
for (const id of matchesOf(
|
|
777
|
-
for (const
|
|
778
|
-
(wantedBy.get(
|
|
787
|
+
for (const item of items)
|
|
788
|
+
for (const need of item.needs)
|
|
789
|
+
for (const id of matchesOf(need))
|
|
790
|
+
for (const provider of byId.get(id))
|
|
791
|
+
(wantedBy.get(provider) ?? wantedBy.set(provider, /* @__PURE__ */ new Set()).get(provider)).add(item);
|
|
779
792
|
return wantedBy;
|
|
780
793
|
}
|
|
781
794
|
function byFileLine(a, b) {
|
|
782
795
|
if (a.file !== b.file) return a.file < b.file ? -1 : 1;
|
|
783
796
|
return a.line - b.line;
|
|
784
797
|
}
|
|
785
|
-
function forwardEdge(
|
|
786
|
-
const target = byId.get(
|
|
787
|
-
if (!target) return ` ${
|
|
788
|
-
return ` ${
|
|
798
|
+
function forwardEdge(item, byId, style, dimLocation) {
|
|
799
|
+
const target = byId.get(item.forwardsTo)?.[0];
|
|
800
|
+
if (!target) return ` ${style.cyan("\u2192")} ${item.forwardsTo} ${style.red("\u2718 missing")}`;
|
|
801
|
+
return ` ${style.cyan("\u2192")} ${item.forwardsTo} ${statusOf(target, style).mark} ${dimLocation(target.file, target.line)}`;
|
|
789
802
|
}
|
|
790
|
-
function needEdges(
|
|
803
|
+
function needEdges(item, byId, matchesOf, style, dimLocation) {
|
|
791
804
|
const lines = [];
|
|
792
|
-
for (const
|
|
793
|
-
const ids = matchesOf(
|
|
805
|
+
for (const need of item.needs) {
|
|
806
|
+
const ids = matchesOf(need);
|
|
794
807
|
if (ids.length === 0) {
|
|
795
|
-
lines.push(` ${
|
|
808
|
+
lines.push(` ${style.dim("needs")} ${need} ${style.red("\u2718 missing")}`);
|
|
796
809
|
continue;
|
|
797
810
|
}
|
|
798
|
-
const
|
|
811
|
+
const wildcard = isWildcardRev(revOf(need));
|
|
799
812
|
for (const id of ids) {
|
|
800
|
-
const
|
|
801
|
-
const arrow =
|
|
802
|
-
const ref =
|
|
803
|
-
lines.push(` ${
|
|
813
|
+
const covering = byId.get(id)[0];
|
|
814
|
+
const arrow = style.dim(`(\u2192 ${id})`);
|
|
815
|
+
const ref = wildcard ? `${need} ${arrow}` : need;
|
|
816
|
+
lines.push(` ${style.dim("needs")} ${ref} ${statusOf(covering, style).mark} ${dimLocation(covering.file, covering.line)}`);
|
|
804
817
|
}
|
|
805
818
|
}
|
|
806
819
|
return lines;
|
|
807
820
|
}
|
|
808
|
-
function coverEdges(
|
|
821
|
+
function coverEdges(item, byId, style, dimLocation) {
|
|
809
822
|
const lines = [];
|
|
810
|
-
for (const
|
|
811
|
-
const target = byId.get(
|
|
812
|
-
if (!target) lines.push(` ${
|
|
813
|
-
else lines.push(` ${
|
|
823
|
+
for (const coverId of item.covers) {
|
|
824
|
+
const target = byId.get(coverId)?.[0];
|
|
825
|
+
if (!target) lines.push(` ${style.dim("covers")} ${coverId} ${style.red("\u2718 missing")}`);
|
|
826
|
+
else lines.push(` ${style.dim("covers")} ${coverId} ${style.green("\u2714")} ${dimLocation(target.file, target.line)}`);
|
|
814
827
|
}
|
|
815
828
|
return lines;
|
|
816
829
|
}
|
|
817
|
-
function edgeLines(
|
|
830
|
+
function edgeLines(item, byId, matchesOf, wantedBy, style, dimLocation) {
|
|
818
831
|
const lines = [];
|
|
819
|
-
if (
|
|
820
|
-
else if (
|
|
821
|
-
lines.push(...coverEdges(
|
|
822
|
-
for (const
|
|
823
|
-
lines.push(` ${
|
|
832
|
+
if (item.forwardsTo !== null) lines.push(forwardEdge(item, byId, style, dimLocation));
|
|
833
|
+
else if (item.origin === "markdown") lines.push(...needEdges(item, byId, matchesOf, style, dimLocation));
|
|
834
|
+
lines.push(...coverEdges(item, byId, style, dimLocation));
|
|
835
|
+
for (const wanting of wantedBy.get(item) ?? [])
|
|
836
|
+
lines.push(` ${style.dim("wanted by")} ${wanting.id} ${dimLocation(wanting.file, wanting.line)}`);
|
|
824
837
|
return lines;
|
|
825
838
|
}
|
|
826
|
-
function renderVerbose(items, out,
|
|
839
|
+
function renderVerbose(items, out, style, dimLocation) {
|
|
827
840
|
const { byId, matchesOf } = buildResolver(items);
|
|
828
841
|
const wantedBy = buildWantedBy(items, byId, matchesOf);
|
|
829
842
|
const sorted = [...items].sort(byFileLine);
|
|
830
843
|
let prevFile = null;
|
|
831
|
-
for (const
|
|
832
|
-
if (prevFile !== null &&
|
|
833
|
-
prevFile =
|
|
834
|
-
const { mark, tag } = statusOf(
|
|
835
|
-
const title =
|
|
844
|
+
for (const item of sorted) {
|
|
845
|
+
if (prevFile !== null && item.file !== prevFile) out.push("");
|
|
846
|
+
prevFile = item.file;
|
|
847
|
+
const { mark, tag } = statusOf(item, style);
|
|
848
|
+
const title = item.title ? " " + style.dim(`"${item.title}"`) : "";
|
|
836
849
|
out.push(
|
|
837
|
-
`${mark} ${
|
|
838
|
-
...edgeLines(
|
|
850
|
+
`${mark} ${style.bold(item.id)}${title} ${dimLocation(item.file, item.line)} ${tag}`,
|
|
851
|
+
...edgeLines(item, byId, matchesOf, wantedBy, style, dimLocation)
|
|
839
852
|
);
|
|
840
|
-
for (const
|
|
853
|
+
for (const defect of item.defects) out.push(` ${style.red("\u2022")} ${defect}`);
|
|
841
854
|
}
|
|
842
855
|
if (sorted.length) out.push("");
|
|
843
856
|
}
|
|
844
|
-
function renderDefective(defective, out,
|
|
845
|
-
for (const
|
|
846
|
-
const title =
|
|
857
|
+
function renderDefective(defective, out, style, dimLocation) {
|
|
858
|
+
for (const item of defective) {
|
|
859
|
+
const title = item.title ? " " + style.dim(`"${item.title}"`) : "";
|
|
847
860
|
out.push(
|
|
848
|
-
`${statusOf(
|
|
861
|
+
`${statusOf(item, style).mark} ${style.bold(item.id)}${title} ${dimLocation(item.file, item.line)}`
|
|
849
862
|
);
|
|
850
|
-
for (const
|
|
863
|
+
for (const defect of item.defects) out.push(` ${style.red("\u2022")} ${defect}`);
|
|
851
864
|
out.push("");
|
|
852
865
|
}
|
|
853
866
|
}
|
|
854
|
-
function renderSummary(items, defective,
|
|
867
|
+
function renderSummary(items, defective, errorCount, warningCount, out, style) {
|
|
855
868
|
const okCount = items.length - defective.length;
|
|
856
|
-
const
|
|
857
|
-
const
|
|
858
|
-
const originBreakdown =
|
|
869
|
+
const shallowCount = items.filter((item) => item.defects.length === 0 && !item.deepCovered).length;
|
|
870
|
+
const markdownCount = items.filter((item) => item.origin === "markdown").length;
|
|
871
|
+
const originBreakdown = style.dim(`(${markdownCount} from markdown, ${items.length - markdownCount} from code)`);
|
|
859
872
|
out.push(
|
|
860
|
-
|
|
873
|
+
style.bold("Summary"),
|
|
861
874
|
` items ${items.length} ${originBreakdown}`,
|
|
862
|
-
` ok ${
|
|
863
|
-
` defective ${defective.length ?
|
|
875
|
+
` ok ${style.green(String(okCount))}`,
|
|
876
|
+
` defective ${defective.length ? style.red(String(defective.length)) : "0"}`
|
|
864
877
|
);
|
|
865
|
-
if (
|
|
866
|
-
if (
|
|
878
|
+
if (shallowCount) out.push(" " + style.dim(`of the ok items, ${shallowCount} are only shallow-covered (an item further down the tracing chain is defective)`));
|
|
879
|
+
if (errorCount) out.push(` errors ${style.brightRed(String(errorCount))}`);
|
|
880
|
+
if (warningCount) out.push(` warnings ${style.yellow(String(warningCount))}`);
|
|
867
881
|
out.push("");
|
|
868
882
|
}
|
|
883
|
+
function problemMark(problem, style) {
|
|
884
|
+
return problem.severity === "warning" ? style.yellow("\u26A0") : style.brightRed("\u25B2");
|
|
885
|
+
}
|
|
869
886
|
function report(items, problems, cwd, opts = {}) {
|
|
870
887
|
const { verbose = false } = opts;
|
|
871
|
-
const
|
|
872
|
-
const
|
|
873
|
-
const
|
|
874
|
-
const defective = items.filter((
|
|
888
|
+
const style = makeStyler();
|
|
889
|
+
const relativePath = (file) => path3.relative(cwd, file) || file;
|
|
890
|
+
const dimLocation = (file, line) => style.dim(`${relativePath(file)}:${line}`);
|
|
891
|
+
const defective = items.filter((item) => item.defects.length > 0);
|
|
892
|
+
const warningCount = problems.filter((problem) => problem.severity === "warning").length;
|
|
893
|
+
const errorCount = problems.length - warningCount;
|
|
875
894
|
const out = [];
|
|
876
|
-
if (verbose) renderVerbose(items, out,
|
|
877
|
-
else renderDefective(defective, out,
|
|
878
|
-
for (const
|
|
879
|
-
out.push(`${
|
|
895
|
+
if (verbose) renderVerbose(items, out, style, dimLocation);
|
|
896
|
+
else renderDefective(defective, out, style, dimLocation);
|
|
897
|
+
for (const problem of problems) {
|
|
898
|
+
out.push(`${problemMark(problem, style)} ${problem.message} ${dimLocation(problem.file, problem.line)}`);
|
|
880
899
|
}
|
|
881
900
|
if (problems.length) out.push("");
|
|
882
|
-
renderSummary(items, defective,
|
|
883
|
-
const clean = defective.length === 0 &&
|
|
884
|
-
out.push(clean ?
|
|
901
|
+
renderSummary(items, defective, errorCount, warningCount, out, style);
|
|
902
|
+
const clean = defective.length === 0 && errorCount === 0;
|
|
903
|
+
out.push(clean ? style.green(style.bold("ok")) : style.red(style.bold("not ok")));
|
|
885
904
|
console.log(out.join("\n"));
|
|
886
905
|
return clean;
|
|
887
906
|
}
|
|
@@ -901,31 +920,44 @@ Options:
|
|
|
901
920
|
-V, --version print the version number
|
|
902
921
|
-h, --help show this help
|
|
903
922
|
|
|
904
|
-
|
|
923
|
+
Long options also accept "="-attached values, e.g. --tags=a,b.
|
|
924
|
+
|
|
925
|
+
Exit codes: 0 clean, 1 defects or errors found, 2 usage error
|
|
926
|
+
(warning problems alone leave the run clean)`;
|
|
905
927
|
function packageVersion() {
|
|
906
928
|
const pkg = new URL("../package.json", import.meta.url);
|
|
907
929
|
return JSON.parse(readFileSync(pkg, "utf8")).version;
|
|
908
930
|
}
|
|
931
|
+
function splitLongOption(token) {
|
|
932
|
+
const eqIndex = token.startsWith("--") ? token.indexOf("=") : -1;
|
|
933
|
+
return eqIndex === -1 ? [token, null] : [token.slice(0, eqIndex), token.slice(eqIndex + 1)];
|
|
934
|
+
}
|
|
935
|
+
function rejectValue(name, inline) {
|
|
936
|
+
if (inline !== null) throw new UsageError(`option ${name} does not take a value`);
|
|
937
|
+
}
|
|
909
938
|
function parseArgs(argv) {
|
|
910
939
|
const opts = { dirs: [], tags: null, verbose: false };
|
|
911
940
|
for (let i = 0; i < argv.length; i++) {
|
|
912
|
-
const
|
|
913
|
-
if (
|
|
941
|
+
const [name, inline] = splitLongOption(argv[i]);
|
|
942
|
+
if (name === "-h" || name === "--help") {
|
|
943
|
+
rejectValue(name, inline);
|
|
914
944
|
console.log(HELP);
|
|
915
945
|
process3.exit(0);
|
|
916
|
-
} else if (
|
|
946
|
+
} else if (name === "-v" || name === "--verbose") {
|
|
947
|
+
rejectValue(name, inline);
|
|
917
948
|
opts.verbose = true;
|
|
918
|
-
} else if (
|
|
949
|
+
} else if (name === "-V" || name === "--version") {
|
|
950
|
+
rejectValue(name, inline);
|
|
919
951
|
console.log(packageVersion());
|
|
920
952
|
process3.exit(0);
|
|
921
|
-
} else if (
|
|
922
|
-
const
|
|
923
|
-
if (!
|
|
924
|
-
opts.tags =
|
|
925
|
-
} else if (
|
|
926
|
-
throw new UsageError(`unknown option: ${
|
|
953
|
+
} else if (name === "-t" || name === "--tags") {
|
|
954
|
+
const value = inline ?? argv[++i];
|
|
955
|
+
if (!value) throw new UsageError(`missing value for ${name}`);
|
|
956
|
+
opts.tags = value.split(",").map((s) => s.trim()).filter(Boolean);
|
|
957
|
+
} else if (name.startsWith("-")) {
|
|
958
|
+
throw new UsageError(`unknown option: ${name}`);
|
|
927
959
|
} else {
|
|
928
|
-
opts.dirs.push(
|
|
960
|
+
opts.dirs.push(name);
|
|
929
961
|
}
|
|
930
962
|
}
|
|
931
963
|
if (opts.dirs.length === 0) opts.dirs.push(".");
|
|
@@ -947,7 +979,7 @@ async function main() {
|
|
|
947
979
|
if (opts.tags) {
|
|
948
980
|
const wantUntagged = opts.tags.includes("_");
|
|
949
981
|
items = items.filter(
|
|
950
|
-
(
|
|
982
|
+
(item) => item.origin === "code" || item.tags.some((tag) => opts.tags.includes(tag)) || wantUntagged && item.tags.length === 0
|
|
951
983
|
);
|
|
952
984
|
}
|
|
953
985
|
analyze(items, forwards, problems);
|