flashtrace 0.5.0 → 0.7.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.
Files changed (2) hide show
  1. package/dist/flashtrace.mjs +153 -23
  2. package/package.json +1 -1
@@ -285,7 +285,9 @@ function newItem(id, origin, file, line) {
285
285
  needs: [],
286
286
  covers: [],
287
287
  tags: [],
288
- defects: []
288
+ defects: [],
289
+ forwardsTo: null
290
+ // effective forwarding target, set by analyze
289
291
  };
290
292
  }
291
293
 
@@ -294,6 +296,7 @@ var DEF_RE = new RegExp(String.raw`^\s*\`${ID_SRC}\`\s*$`);
294
296
  var HEADING_RE = /^(#{1,6})\s+(\S(?:.*\S)?)\s*$/;
295
297
  var KEYWORD_RE = /^(Needs|Covers|Tags):\s*((?:\S.*)?)$/;
296
298
  var BULLET_RE = /^\s*[-*+]\s+(\S(?:.*\S)?)\s*$/;
299
+ var DELIM_CELL_RE = /^:?-+:?$/;
297
300
  var FORWARD_LINE_RE = new RegExp(String.raw`^\s*(\`?)${FORWARD_SRC}\1\s*$`);
298
301
  var isBoundary = (l) => DEF_RE.test(l) || HEADING_RE.test(l);
299
302
  function takeForward(line, file, n, forwards) {
@@ -323,6 +326,34 @@ function keywordEntries(lines, j, inline) {
323
326
  }
324
327
  return { entries, j };
325
328
  }
329
+ function rowCells(line) {
330
+ let s = line.trim();
331
+ if (!s.includes("|")) return null;
332
+ if (s.startsWith("|")) s = s.slice(1);
333
+ if (s.endsWith("|")) s = s.slice(0, -1);
334
+ return s.split("|").map((c) => c.trim());
335
+ }
336
+ function takeKeywordTable(lines, j, item, file, problems) {
337
+ const header = rowCells(lines[j]);
338
+ if (!header) return null;
339
+ const columns = [];
340
+ header.forEach((cell, col) => {
341
+ if (cell === "Needs" || cell === "Covers" || cell === "Tags") columns.push([col, cell]);
342
+ });
343
+ if (columns.length === 0) return null;
344
+ const delim = j + 1 < lines.length ? rowCells(lines[j + 1]) : null;
345
+ if (delim?.length !== header.length || !delim.every((c) => DELIM_CELL_RE.test(c))) return null;
346
+ j++;
347
+ while (j + 1 < lines.length && !isBoundary(lines[j + 1])) {
348
+ const cells = rowCells(lines[j + 1]);
349
+ if (!cells) break;
350
+ j++;
351
+ for (const [col, keyword] of columns) {
352
+ if (cells[col]) applyKeyword(item, keyword, [cells[col]], file, j + 1, problems);
353
+ }
354
+ }
355
+ return j;
356
+ }
326
357
  function applyKeyword(item, keyword, entries, file, kwLine, problems) {
327
358
  if (keyword === "Tags") {
328
359
  item.tags.push(...entries);
@@ -351,11 +382,15 @@ function parseItemBody(lines, start, item, file, problems, forwards) {
351
382
  continue;
352
383
  }
353
384
  const kw = line.match(KEYWORD_RE);
385
+ const tableEnd = kw ? null : takeKeywordTable(lines, j, item, file, problems);
354
386
  if (kw) {
355
387
  descDone = true;
356
388
  const collected = keywordEntries(lines, j, kw[2]);
357
389
  applyKeyword(item, kw[1], collected.entries, file, j + 1, problems);
358
390
  j = collected.j;
391
+ } else if (tableEnd !== null) {
392
+ descDone = true;
393
+ j = tableEnd;
359
394
  } else if (line.trim() === "") {
360
395
  if (item.description.length > 0) descDone = true;
361
396
  } else if (!descDone) {
@@ -667,6 +702,13 @@ function groupIdsByKey(byId) {
667
702
  (idsByKey.get(keyOf(id)) ?? idsByKey.set(keyOf(id), []).get(keyOf(id))).push(id);
668
703
  return idsByKey;
669
704
  }
705
+ function buildResolver(items) {
706
+ const byId = /* @__PURE__ */ new Map();
707
+ for (const it of items) (byId.get(it.id) ?? byId.set(it.id, []).get(it.id)).push(it);
708
+ const idsByKey = groupIdsByKey(byId);
709
+ const matchesOf = (ref) => (idsByKey.get(keyOf(ref)) ?? []).filter((id) => idMatches(ref, id));
710
+ return { byId, matchesOf };
711
+ }
670
712
  function splitNeeds(items) {
671
713
  const exact = /* @__PURE__ */ new Set();
672
714
  const wildcard = [];
@@ -678,14 +720,10 @@ function splitNeeds(items) {
678
720
  return { exact, wildcard };
679
721
  }
680
722
  function analyze(items, forwards = [], problems = []) {
681
- const byId = /* @__PURE__ */ new Map();
723
+ const { byId, matchesOf } = buildResolver(items);
682
724
  const revsByKey = /* @__PURE__ */ new Map();
683
- for (const it of items) {
684
- (byId.get(it.id) ?? byId.set(it.id, []).get(it.id)).push(it);
725
+ for (const it of items)
685
726
  (revsByKey.get(it.key) ?? revsByKey.set(it.key, /* @__PURE__ */ new Set()).get(it.key)).add(it.revision);
686
- }
687
- const idsByKey = groupIdsByKey(byId);
688
- const matchesOf = (ref) => (idsByKey.get(keyOf(ref)) ?? []).filter((id) => idMatches(ref, id));
689
727
  const { exact: exactNeeds, wildcard: wildcardNeeds } = splitNeeds(items);
690
728
  const isNeeded = (id) => exactNeeds.has(id) || wildcardNeeds.some((w) => idMatches(w, id));
691
729
  for (const [id, group] of byId) {
@@ -697,7 +735,10 @@ function analyze(items, forwards = [], problems = []) {
697
735
  return revs ? ` (revision mismatch: existing revision(s) of ${keyOf(id)}: ${[...revs].sort(compareRev).join(", ")})` : "";
698
736
  };
699
737
  const fwdTarget = buildForwardMap(forwards, byId, exactNeeds, revHint, problems);
700
- for (const it of items) checkItemReferences(it, byId, matchesOf, isNeeded, revHint, fwdTarget);
738
+ for (const it of items) {
739
+ it.forwardsTo = fwdTarget.get(it.id) ?? null;
740
+ checkItemReferences(it, byId, matchesOf, isNeeded, revHint, fwdTarget);
741
+ }
701
742
  markDeepCoverage(items, byId, matchesOf, fwdTarget);
702
743
  }
703
744
 
@@ -716,24 +757,94 @@ function makeStyler() {
716
757
  bold: wrap("1")
717
758
  };
718
759
  }
719
- function report(items, problems, cwd) {
720
- const c = makeStyler();
721
- const rel = (f) => path3.relative(cwd, f) || f;
722
- const dimLoc = (file, line) => c.dim(`${rel(file)}:${line}`);
723
- const defective = items.filter((it) => it.defects.length > 0);
724
- const out = [];
760
+ function statusOf(it, c) {
761
+ if (it.defects.length > 0) return { mark: c.red("\u2718"), tag: c.red("[defective]") };
762
+ if (!it.deepCovered) return { mark: c.yellow("~"), tag: c.yellow("[shallow-covered]") };
763
+ return { mark: c.green("\u2714"), tag: c.green("[deep-covered]") };
764
+ }
765
+ function buildWantedBy(items, byId, matchesOf) {
766
+ const wantedBy = /* @__PURE__ */ new Map();
767
+ for (const it of items)
768
+ for (const n of it.needs)
769
+ for (const id of matchesOf(n))
770
+ for (const m of byId.get(id))
771
+ (wantedBy.get(m) ?? wantedBy.set(m, /* @__PURE__ */ new Set()).get(m)).add(it);
772
+ return wantedBy;
773
+ }
774
+ function byFileLine(a, b) {
775
+ if (a.file !== b.file) return a.file < b.file ? -1 : 1;
776
+ return a.line - b.line;
777
+ }
778
+ function forwardEdge(it, byId, c, dimLoc) {
779
+ const target = byId.get(it.forwardsTo)?.[0];
780
+ if (!target) return ` ${c.cyan("\u2192")} ${it.forwardsTo} ${c.red("\u2718 missing")}`;
781
+ return ` ${c.cyan("\u2192")} ${it.forwardsTo} ${statusOf(target, c).mark} ${dimLoc(target.file, target.line)}`;
782
+ }
783
+ function needEdges(it, byId, matchesOf, c, dimLoc) {
784
+ const lines = [];
785
+ for (const n of it.needs) {
786
+ const ids = matchesOf(n);
787
+ if (ids.length === 0) {
788
+ lines.push(` ${c.dim("needs")} ${n} ${c.red("\u2718 missing")}`);
789
+ continue;
790
+ }
791
+ const wild = isWildcardRev(revOf(n));
792
+ for (const id of ids) {
793
+ const m = byId.get(id)[0];
794
+ const arrow = c.dim(`(\u2192 ${id})`);
795
+ const ref = wild ? `${n} ${arrow}` : n;
796
+ lines.push(` ${c.dim("needs")} ${ref} ${statusOf(m, c).mark} ${dimLoc(m.file, m.line)}`);
797
+ }
798
+ }
799
+ return lines;
800
+ }
801
+ function coverEdges(it, byId, c, dimLoc) {
802
+ const lines = [];
803
+ for (const cv of it.covers) {
804
+ const target = byId.get(cv)?.[0];
805
+ if (!target) lines.push(` ${c.dim("covers")} ${cv} ${c.red("\u2718 missing")}`);
806
+ else lines.push(` ${c.dim("covers")} ${cv} ${c.green("\u2714")} ${dimLoc(target.file, target.line)}`);
807
+ }
808
+ return lines;
809
+ }
810
+ function edgeLines(it, byId, matchesOf, wantedBy, c, dimLoc) {
811
+ const lines = [];
812
+ if (it.forwardsTo !== null) lines.push(forwardEdge(it, byId, c, dimLoc));
813
+ else if (it.origin === "markdown") lines.push(...needEdges(it, byId, matchesOf, c, dimLoc));
814
+ lines.push(...coverEdges(it, byId, c, dimLoc));
815
+ for (const w of wantedBy.get(it) ?? [])
816
+ lines.push(` ${c.dim("wanted by")} ${w.id} ${dimLoc(w.file, w.line)}`);
817
+ return lines;
818
+ }
819
+ function renderVerbose(items, out, c, dimLoc) {
820
+ const { byId, matchesOf } = buildResolver(items);
821
+ const wantedBy = buildWantedBy(items, byId, matchesOf);
822
+ const sorted = [...items].sort(byFileLine);
823
+ let prevFile = null;
824
+ for (const it of sorted) {
825
+ if (prevFile !== null && it.file !== prevFile) out.push("");
826
+ prevFile = it.file;
827
+ const { mark, tag } = statusOf(it, c);
828
+ const title = it.title ? " " + c.dim(`"${it.title}"`) : "";
829
+ out.push(
830
+ `${mark} ${c.bold(it.id)}${title} ${dimLoc(it.file, it.line)} ${tag}`,
831
+ ...edgeLines(it, byId, matchesOf, wantedBy, c, dimLoc)
832
+ );
833
+ for (const d of it.defects) out.push(` ${c.red("\u2022")} ${d}`);
834
+ }
835
+ if (sorted.length) out.push("");
836
+ }
837
+ function renderDefective(defective, out, c, dimLoc) {
725
838
  for (const it of defective) {
726
839
  const title = it.title ? " " + c.dim(`"${it.title}"`) : "";
727
840
  out.push(
728
- `${c.red("\u2718")} ${c.bold(it.id)}${title} ${dimLoc(it.file, it.line)}`
841
+ `${statusOf(it, c).mark} ${c.bold(it.id)}${title} ${dimLoc(it.file, it.line)}`
729
842
  );
730
843
  for (const d of it.defects) out.push(` ${c.red("\u2022")} ${d}`);
731
844
  out.push("");
732
845
  }
733
- for (const p of problems) {
734
- out.push(`${c.yellow("\u26A0")} ${p.message} ${dimLoc(p.file, p.line)}`);
735
- }
736
- if (problems.length) out.push("");
846
+ }
847
+ function renderSummary(items, defective, problems, out, c) {
737
848
  const okCount = items.length - defective.length;
738
849
  const notDeep = items.filter((it) => it.defects.length === 0 && !it.deepCovered).length;
739
850
  const md = items.filter((i) => i.origin === "markdown").length;
@@ -747,6 +858,21 @@ function report(items, problems, cwd) {
747
858
  if (notDeep) out.push(" " + c.dim(`of the ok items, ${notDeep} are only shallow-covered (an item further down the tracing chain is defective)`));
748
859
  if (problems.length) out.push(` problems ${c.yellow(String(problems.length))}`);
749
860
  out.push("");
861
+ }
862
+ function report(items, problems, cwd, opts = {}) {
863
+ const { verbose = false } = opts;
864
+ const c = makeStyler();
865
+ const rel = (f) => path3.relative(cwd, f) || f;
866
+ const dimLoc = (file, line) => c.dim(`${rel(file)}:${line}`);
867
+ const defective = items.filter((it) => it.defects.length > 0);
868
+ const out = [];
869
+ if (verbose) renderVerbose(items, out, c, dimLoc);
870
+ else renderDefective(defective, out, c, dimLoc);
871
+ for (const p of problems) {
872
+ out.push(`${c.yellow("\u26A0")} ${p.message} ${dimLoc(p.file, p.line)}`);
873
+ }
874
+ if (problems.length) out.push("");
875
+ renderSummary(items, defective, problems, out, c);
750
876
  const clean = defective.length === 0 && problems.length === 0;
751
877
  out.push(clean ? c.green(c.bold("ok")) : c.red(c.bold("not ok")));
752
878
  console.log(out.join("\n"));
@@ -763,8 +889,10 @@ by git are excluded.
763
889
  Options:
764
890
  -t, --tags <t1,t2,...> only import markdown items carrying one of these
765
891
  tags; add "_" to also include untagged items
892
+ -v, --verbose list every item with its coverage status and trace
893
+ edges, not only the defective ones
894
+ -V, --version print the version number
766
895
  -h, --help show this help
767
- -v, --version print the version number
768
896
 
769
897
  Exit codes: 0 clean, 1 defects or problems found, 2 usage error`;
770
898
  function packageVersion() {
@@ -772,13 +900,15 @@ function packageVersion() {
772
900
  return JSON.parse(readFileSync(pkg, "utf8")).version;
773
901
  }
774
902
  function parseArgs(argv) {
775
- const opts = { dirs: [], tags: null };
903
+ const opts = { dirs: [], tags: null, verbose: false };
776
904
  for (let i = 0; i < argv.length; i++) {
777
905
  const a = argv[i];
778
906
  if (a === "-h" || a === "--help") {
779
907
  console.log(HELP);
780
908
  process3.exit(0);
781
- } else if (a === "-v" || a === "--version") {
909
+ } else if (a === "-v" || a === "--verbose") {
910
+ opts.verbose = true;
911
+ } else if (a === "-V" || a === "--version") {
782
912
  console.log(packageVersion());
783
913
  process3.exit(0);
784
914
  } else if (a === "-t" || a === "--tags") {
@@ -814,7 +944,7 @@ async function main() {
814
944
  );
815
945
  }
816
946
  analyze(items, forwards, problems);
817
- const clean = report(items, problems, process3.cwd());
947
+ const clean = report(items, problems, process3.cwd(), { verbose: opts.verbose });
818
948
  process3.exit(clean ? 0 : 1);
819
949
  }
820
950
  function runCli() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flashtrace",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "Lightning-fast, reference-based requirement tracing that works anywhere.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {