flashtrace 0.2.0 → 0.4.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 +94 -20
  2. package/package.json +1 -1
@@ -84,8 +84,13 @@ async function collectFiles(dirs) {
84
84
 
85
85
  // src/ids.mjs
86
86
  var SEG_SRC = "[A-Za-z][A-Za-z0-9_.-]*";
87
- var ID_SRC = String.raw`([A-Za-z]+):(?:((?:${SEG_SRC}\/)*${SEG_SRC})\/)?(${SEG_SRC})#(\d+)`;
87
+ var REV_SRC = String.raw`\d+(?:\.\d+){0,2}`;
88
+ var WILD_SRC = String.raw`(?:\d+\.\d+\.x|\d+\.x\.y|x\.y\.z|\d+\.x|x\.y|x)`;
89
+ var REVREF_SRC = `(?:${WILD_SRC}|${REV_SRC})`;
90
+ var ID_SRC = String.raw`([A-Za-z]+):(?:((?:${SEG_SRC}\/)*${SEG_SRC})\/)?(${SEG_SRC})#(${REV_SRC})`;
88
91
  var ID_RE = new RegExp(`^${ID_SRC}$`);
92
+ var NEED_ID_SRC = String.raw`([A-Za-z]+):(?:((?:${SEG_SRC}\/)*${SEG_SRC})\/)?(${SEG_SRC})#(${REVREF_SRC})`;
93
+ var NEED_ID_RE = new RegExp(`^${NEED_ID_SRC}$`);
89
94
  var FORWARD_SRC = String.raw`\[\s*${ID_SRC}\s*-->\s*${ID_SRC}\s*\]`;
90
95
  var mkId = (type, group, name, rev) => `${type}:${group ? group + "/" : ""}${name}#${rev}`;
91
96
  var mkForward = (m, base, file, line) => ({
@@ -95,12 +100,40 @@ var mkForward = (m, base, file, line) => ({
95
100
  line
96
101
  });
97
102
  var keyOf = (id) => id.slice(0, id.lastIndexOf("#"));
98
- var revOf = (id) => Number(id.slice(id.lastIndexOf("#") + 1));
103
+ var revOf = (id) => id.slice(id.lastIndexOf("#") + 1);
104
+ function compareRev(a, b) {
105
+ const pa = a.split(".");
106
+ const pb = b.split(".");
107
+ for (let i = 0; i < Math.max(pa.length, pb.length); i++) {
108
+ if (i >= pa.length) return -1;
109
+ if (i >= pb.length) return 1;
110
+ if (pa[i] !== pb[i]) return Number(pa[i]) - Number(pb[i]);
111
+ }
112
+ return 0;
113
+ }
114
+ var WILD_LAYER = /* @__PURE__ */ new Set(["x", "y", "z"]);
115
+ var isWildcardRev = (rev) => /[xyz]/.test(rev);
116
+ function revMatches(pattern, concrete) {
117
+ const pp = pattern.split(".");
118
+ const cp = concrete.split(".");
119
+ if (pp.length !== cp.length) return false;
120
+ for (let i = 0; i < pp.length; i++) {
121
+ if (WILD_LAYER.has(pp[i])) continue;
122
+ if (pp[i] !== cp[i]) return false;
123
+ }
124
+ return true;
125
+ }
126
+ var idMatches = (need, id) => keyOf(need) === keyOf(id) && revMatches(revOf(need), revOf(id));
99
127
  function parseIdEntry(raw) {
100
128
  const cleaned = raw.replaceAll("`", "").trim();
101
129
  const m = cleaned.match(ID_RE);
102
130
  return m ? mkId(m[1], m[2], m[3], m[4]) : null;
103
131
  }
132
+ function parseNeedEntry(raw) {
133
+ const cleaned = raw.replaceAll("`", "").trim();
134
+ const m = cleaned.match(NEED_ID_RE);
135
+ return m ? mkId(m[1], m[2], m[3], m[4]) : null;
136
+ }
104
137
  function newItem(id, origin, file, line) {
105
138
  return {
106
139
  id,
@@ -159,8 +192,9 @@ function applyKeyword(item, keyword, entries, file, kwLine, problems) {
159
192
  return;
160
193
  }
161
194
  const target = keyword === "Needs" ? "needs" : "covers";
195
+ const parse = keyword === "Needs" ? parseNeedEntry : parseIdEntry;
162
196
  for (const e of entries) {
163
- const id = parseIdEntry(e);
197
+ const id = parse(e);
164
198
  if (id) item[target].push(id);
165
199
  else
166
200
  problems.push({
@@ -218,7 +252,10 @@ function parseMarkdown(file, text, problems, forwards = []) {
218
252
 
219
253
  // src/parse-code.mjs
220
254
  import path2 from "node:path";
221
- var TAG_RE = new RegExp(String.raw`\[(>>)?\s*${ID_SRC}\s*\]`, "g");
255
+ var TAG_RE = new RegExp(
256
+ String.raw`\[(?:\s*${ID_SRC}\s*)?>>\s*${NEED_ID_SRC}\s*\]|\[\s*${ID_SRC}\s*\]`,
257
+ "g"
258
+ );
222
259
  var FORWARD_RE = new RegExp(FORWARD_SRC, "g");
223
260
  var BLOCK_CLOSERS = { c: "*/", html: "-->" };
224
261
  function findCommentStart(s, pos, lineMarkers, htmlBlocks) {
@@ -261,10 +298,26 @@ function commentText(s, state, lineMarkers, htmlBlocks) {
261
298
  }
262
299
  function collectTags(comment, file, line, state, items, problems) {
263
300
  for (const m of comment.matchAll(TAG_RE)) {
264
- const id = mkId(m[2], m[3], m[4], m[5]);
265
- if (!m[1]) {
266
- state.last = newItem(id, "code", file, line);
267
- items.push(state.last);
301
+ if (m[9]) {
302
+ const item = newItem(mkId(m[9], m[10], m[11], m[12]), "code", file, line);
303
+ state.last = item;
304
+ state.byId.set(item.id, item);
305
+ items.push(item);
306
+ continue;
307
+ }
308
+ const id = mkId(m[5], m[6], m[7], m[8]);
309
+ if (m[1]) {
310
+ const source = mkId(m[1], m[2], m[3], m[4]);
311
+ const anchor = state.byId.get(source);
312
+ if (anchor) {
313
+ anchor.needs.push(id);
314
+ } else {
315
+ problems.push({
316
+ file,
317
+ line,
318
+ message: `need tag [${source} >> ${id}] has no preceding item tag [${source}] in this file`
319
+ });
320
+ }
268
321
  } else if (state.last) {
269
322
  state.last.needs.push(id);
270
323
  } else {
@@ -282,7 +335,7 @@ function parseCode(file, text, problems, forwards = []) {
282
335
  const items = [];
283
336
  const lineMarkers = ext === ".sql" ? ["--"] : ["//"];
284
337
  const htmlBlocks = ext === ".vue";
285
- const state = { last: null, block: null };
338
+ const state = { last: null, byId: /* @__PURE__ */ new Map(), block: null };
286
339
  for (let i = 0; i < lines.length; i++) {
287
340
  const comment = commentText(lines[i], state, lineMarkers, htmlBlocks);
288
341
  for (const m of comment.matchAll(FORWARD_RE)) {
@@ -294,25 +347,26 @@ function parseCode(file, text, problems, forwards = []) {
294
347
  }
295
348
 
296
349
  // src/analyze.mjs
297
- function checkItemReferences(it, byId, neededIds, revHint, fwdTarget) {
350
+ function checkItemReferences(it, byId, matchesOf, isNeeded, revHint, fwdTarget) {
298
351
  const fwd = fwdTarget.get(it.id);
299
352
  if (fwd !== void 0) {
300
353
  if (!byId.has(fwd))
301
354
  it.defects.push(`uncovered: forwards to ${fwd}, which does not exist${revHint(fwd)}`);
302
355
  } else {
303
356
  for (const n of it.needs) {
304
- if (!byId.has(n)) it.defects.push(`uncovered: needs ${n}, which does not exist${revHint(n)}`);
357
+ if (matchesOf(n).length === 0)
358
+ it.defects.push(`uncovered: needs ${n}, which does not exist${revHint(n)}`);
305
359
  }
306
360
  }
307
361
  for (const c of it.covers) {
308
362
  const targets = byId.get(c);
309
363
  if (!targets) {
310
364
  it.defects.push(`orphaned: covers ${c}, which does not exist${revHint(c)}`);
311
- } else if (!targets.some((t) => t.needs.includes(it.id))) {
365
+ } else if (!targets.some((t) => t.needs.some((n) => idMatches(n, it.id)))) {
312
366
  it.defects.push(`unwanted: covers ${c}, but ${c} does not need ${it.id}`);
313
367
  }
314
368
  }
315
- if (it.origin === "code" && !neededIds.has(it.id)) {
369
+ if (it.origin === "code" && !isNeeded(it.id)) {
316
370
  it.defects.push(`unwanted: no item needs ${it.id}`);
317
371
  }
318
372
  }
@@ -368,7 +422,7 @@ function buildForwardMap(forwards, byId, neededIds, revHint, problems) {
368
422
  for (const to of fwdTarget.values()) neededIds.add(to);
369
423
  return fwdTarget;
370
424
  }
371
- function markDeepCoverage(items, byId, fwdTarget) {
425
+ function markDeepCoverage(items, byId, matchesOf, fwdTarget) {
372
426
  const memo = /* @__PURE__ */ new Map();
373
427
  const deep = (id) => {
374
428
  if (memo.has(id)) return memo.get(id);
@@ -383,13 +437,30 @@ function markDeepCoverage(items, byId, fwdTarget) {
383
437
  if (fwd !== void 0) {
384
438
  ok = deep(fwd);
385
439
  } else {
386
- for (const it of group) for (const n of it.needs) if (!deep(n)) ok = false;
440
+ for (const it of group) for (const n of it.needs) if (!needDeep(n)) ok = false;
387
441
  }
388
442
  memo.set(id, ok);
389
443
  return ok;
390
444
  };
445
+ const needDeep = (n) => matchesOf(n).some((id) => deep(id));
391
446
  for (const it of items) it.deepCovered = deep(it.id);
392
447
  }
448
+ function groupIdsByKey(byId) {
449
+ const idsByKey = /* @__PURE__ */ new Map();
450
+ for (const id of byId.keys())
451
+ (idsByKey.get(keyOf(id)) ?? idsByKey.set(keyOf(id), []).get(keyOf(id))).push(id);
452
+ return idsByKey;
453
+ }
454
+ function splitNeeds(items) {
455
+ const exact = /* @__PURE__ */ new Set();
456
+ const wildcard = [];
457
+ for (const it of items)
458
+ for (const n of it.needs) {
459
+ if (isWildcardRev(revOf(n))) wildcard.push(n);
460
+ else exact.add(n);
461
+ }
462
+ return { exact, wildcard };
463
+ }
393
464
  function analyze(items, forwards = [], problems = []) {
394
465
  const byId = /* @__PURE__ */ new Map();
395
466
  const revsByKey = /* @__PURE__ */ new Map();
@@ -397,18 +468,21 @@ function analyze(items, forwards = [], problems = []) {
397
468
  (byId.get(it.id) ?? byId.set(it.id, []).get(it.id)).push(it);
398
469
  (revsByKey.get(it.key) ?? revsByKey.set(it.key, /* @__PURE__ */ new Set()).get(it.key)).add(it.revision);
399
470
  }
400
- const neededIds = new Set(items.flatMap((it) => it.needs));
471
+ const idsByKey = groupIdsByKey(byId);
472
+ const matchesOf = (ref) => (idsByKey.get(keyOf(ref)) ?? []).filter((id) => idMatches(ref, id));
473
+ const { exact: exactNeeds, wildcard: wildcardNeeds } = splitNeeds(items);
474
+ const isNeeded = (id) => exactNeeds.has(id) || wildcardNeeds.some((w) => idMatches(w, id));
401
475
  for (const [id, group] of byId) {
402
476
  if (group.length > 1)
403
477
  for (const it of group) it.defects.push(`duplicate: ID ${id} is defined ${group.length} times`);
404
478
  }
405
479
  const revHint = (id) => {
406
480
  const revs = revsByKey.get(keyOf(id));
407
- return revs ? ` (revision mismatch: existing revision(s) of ${keyOf(id)}: ${[...revs].sort((a, b) => a - b).join(", ")})` : "";
481
+ return revs ? ` (revision mismatch: existing revision(s) of ${keyOf(id)}: ${[...revs].sort(compareRev).join(", ")})` : "";
408
482
  };
409
- const fwdTarget = buildForwardMap(forwards, byId, neededIds, revHint, problems);
410
- for (const it of items) checkItemReferences(it, byId, neededIds, revHint, fwdTarget);
411
- markDeepCoverage(items, byId, fwdTarget);
483
+ const fwdTarget = buildForwardMap(forwards, byId, exactNeeds, revHint, problems);
484
+ for (const it of items) checkItemReferences(it, byId, matchesOf, isNeeded, revHint, fwdTarget);
485
+ markDeepCoverage(items, byId, matchesOf, fwdTarget);
412
486
  }
413
487
 
414
488
  // src/report.mjs
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flashtrace",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "description": "Lightning-fast, reference-based requirement tracing that works anywhere.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {