flashtrace 0.1.2 → 0.2.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 +118 -25
  2. package/package.json +1 -1
@@ -86,7 +86,14 @@ async function collectFiles(dirs) {
86
86
  var SEG_SRC = "[A-Za-z][A-Za-z0-9_.-]*";
87
87
  var ID_SRC = String.raw`([A-Za-z]+):(?:((?:${SEG_SRC}\/)*${SEG_SRC})\/)?(${SEG_SRC})#(\d+)`;
88
88
  var ID_RE = new RegExp(`^${ID_SRC}$`);
89
+ var FORWARD_SRC = String.raw`\[\s*${ID_SRC}\s*-->\s*${ID_SRC}\s*\]`;
89
90
  var mkId = (type, group, name, rev) => `${type}:${group ? group + "/" : ""}${name}#${rev}`;
91
+ var mkForward = (m, base, file, line) => ({
92
+ from: mkId(m[base], m[base + 1], m[base + 2], m[base + 3]),
93
+ to: mkId(m[base + 4], m[base + 5], m[base + 6], m[base + 7]),
94
+ file,
95
+ line
96
+ });
90
97
  var keyOf = (id) => id.slice(0, id.lastIndexOf("#"));
91
98
  var revOf = (id) => Number(id.slice(id.lastIndexOf("#") + 1));
92
99
  function parseIdEntry(raw) {
@@ -117,7 +124,13 @@ var DEF_RE = new RegExp(String.raw`^\s*\`${ID_SRC}\`\s*$`);
117
124
  var HEADING_RE = /^(#{1,6})\s+(\S(?:.*\S)?)\s*$/;
118
125
  var KEYWORD_RE = /^(Needs|Covers|Tags):\s*((?:\S.*)?)$/;
119
126
  var BULLET_RE = /^\s*[-*+]\s+(\S(?:.*\S)?)\s*$/;
127
+ var FORWARD_LINE_RE = new RegExp(String.raw`^\s*(\`?)${FORWARD_SRC}\1\s*$`);
120
128
  var isBoundary = (l) => DEF_RE.test(l) || HEADING_RE.test(l);
129
+ function takeForward(line, file, n, forwards) {
130
+ const f = line.match(FORWARD_LINE_RE);
131
+ if (f) forwards.push(mkForward(f, 2, file, n + 1));
132
+ return !!f;
133
+ }
121
134
  function titleAbove(lines, defIndex) {
122
135
  for (let k = defIndex - 1; k >= 0; k--) {
123
136
  const l = lines[k];
@@ -157,11 +170,15 @@ function applyKeyword(item, keyword, entries, file, kwLine, problems) {
157
170
  });
158
171
  }
159
172
  }
160
- function parseItemBody(lines, start, item, file, problems) {
173
+ function parseItemBody(lines, start, item, file, problems, forwards) {
161
174
  let j = start;
162
175
  let descDone = false;
163
176
  while (j < lines.length && !isBoundary(lines[j])) {
164
177
  const line = lines[j];
178
+ if (takeForward(line, file, j, forwards)) {
179
+ j++;
180
+ continue;
181
+ }
165
182
  const kw = line.match(KEYWORD_RE);
166
183
  if (kw) {
167
184
  descDone = true;
@@ -177,11 +194,15 @@ function parseItemBody(lines, start, item, file, problems) {
177
194
  }
178
195
  return j;
179
196
  }
180
- function parseMarkdown(file, text, problems) {
197
+ function parseMarkdown(file, text, problems, forwards = []) {
181
198
  const lines = text.split(/\r?\n/);
182
199
  const items = [];
183
200
  let i = 0;
184
201
  while (i < lines.length) {
202
+ if (takeForward(lines[i], file, i, forwards)) {
203
+ i++;
204
+ continue;
205
+ }
185
206
  const def = lines[i].match(DEF_RE);
186
207
  if (!def) {
187
208
  i++;
@@ -189,7 +210,7 @@ function parseMarkdown(file, text, problems) {
189
210
  }
190
211
  const item = newItem(mkId(def[1], def[2], def[3], def[4]), "markdown", file, i + 1);
191
212
  item.title = titleAbove(lines, i);
192
- i = parseItemBody(lines, i + 1, item, file, problems);
213
+ i = parseItemBody(lines, i + 1, item, file, problems, forwards);
193
214
  items.push(item);
194
215
  }
195
216
  return items;
@@ -198,6 +219,7 @@ function parseMarkdown(file, text, problems) {
198
219
  // src/parse-code.mjs
199
220
  import path2 from "node:path";
200
221
  var TAG_RE = new RegExp(String.raw`\[(>>)?\s*${ID_SRC}\s*\]`, "g");
222
+ var FORWARD_RE = new RegExp(FORWARD_SRC, "g");
201
223
  var BLOCK_CLOSERS = { c: "*/", html: "-->" };
202
224
  function findCommentStart(s, pos, lineMarkers, htmlBlocks) {
203
225
  const candidates = lineMarkers.map((m) => ({ idx: s.indexOf(m, pos), kind: "line", len: m.length }));
@@ -254,7 +276,7 @@ function collectTags(comment, file, line, state, items, problems) {
254
276
  }
255
277
  }
256
278
  }
257
- function parseCode(file, text, problems) {
279
+ function parseCode(file, text, problems, forwards = []) {
258
280
  const ext = path2.extname(file).toLowerCase();
259
281
  const lines = text.split(/\r?\n/);
260
282
  const items = [];
@@ -263,15 +285,24 @@ function parseCode(file, text, problems) {
263
285
  const state = { last: null, block: null };
264
286
  for (let i = 0; i < lines.length; i++) {
265
287
  const comment = commentText(lines[i], state, lineMarkers, htmlBlocks);
288
+ for (const m of comment.matchAll(FORWARD_RE)) {
289
+ forwards.push(mkForward(m, 1, file, i + 1));
290
+ }
266
291
  collectTags(comment, file, i + 1, state, items, problems);
267
292
  }
268
293
  return items;
269
294
  }
270
295
 
271
296
  // src/analyze.mjs
272
- function checkItemReferences(it, byId, neededIds, revHint) {
273
- for (const n of it.needs) {
274
- if (!byId.has(n)) it.defects.push(`uncovered: needs ${n}, which does not exist${revHint(n)}`);
297
+ function checkItemReferences(it, byId, neededIds, revHint, fwdTarget) {
298
+ const fwd = fwdTarget.get(it.id);
299
+ if (fwd !== void 0) {
300
+ if (!byId.has(fwd))
301
+ it.defects.push(`uncovered: forwards to ${fwd}, which does not exist${revHint(fwd)}`);
302
+ } else {
303
+ for (const n of it.needs) {
304
+ if (!byId.has(n)) it.defects.push(`uncovered: needs ${n}, which does not exist${revHint(n)}`);
305
+ }
275
306
  }
276
307
  for (const c of it.covers) {
277
308
  const targets = byId.get(c);
@@ -285,23 +316,59 @@ function checkItemReferences(it, byId, neededIds, revHint) {
285
316
  it.defects.push(`unwanted: no item needs ${it.id}`);
286
317
  }
287
318
  }
288
- function analyze(items) {
289
- const byId = /* @__PURE__ */ new Map();
290
- const revsByKey = /* @__PURE__ */ new Map();
291
- for (const it of items) {
292
- (byId.get(it.id) ?? byId.set(it.id, []).get(it.id)).push(it);
293
- (revsByKey.get(it.key) ?? revsByKey.set(it.key, /* @__PURE__ */ new Set()).get(it.key)).add(it.revision);
319
+ function dropCyclicForwards(fwdTarget, declBySource, problems) {
320
+ const done = /* @__PURE__ */ new Set();
321
+ for (const start of fwdTarget.keys()) {
322
+ if (done.has(start)) continue;
323
+ const seen = /* @__PURE__ */ new Map();
324
+ const path5 = [];
325
+ let cur = start;
326
+ while (fwdTarget.has(cur) && !done.has(cur) && !seen.has(cur)) {
327
+ seen.set(cur, path5.length);
328
+ path5.push(cur);
329
+ cur = fwdTarget.get(cur);
330
+ }
331
+ if (seen.has(cur)) {
332
+ const cycle = path5.slice(seen.get(cur));
333
+ const chain = [...cycle, cur].join(" --> ");
334
+ for (const id of cycle) {
335
+ const f = declBySource.get(id);
336
+ problems.push({ file: f.file, line: f.line, message: `cyclic forwarding: ${chain}` });
337
+ fwdTarget.delete(id);
338
+ }
339
+ }
340
+ for (const id of path5) done.add(id);
294
341
  }
295
- const neededIds = new Set(items.flatMap((it) => it.needs));
296
- for (const [id, group] of byId) {
342
+ }
343
+ function buildForwardMap(forwards, byId, neededIds, revHint, problems) {
344
+ const fwdTarget = /* @__PURE__ */ new Map();
345
+ const declBySource = /* @__PURE__ */ new Map();
346
+ const fwdBySource = /* @__PURE__ */ new Map();
347
+ for (const f of forwards) {
348
+ (fwdBySource.get(f.from) ?? fwdBySource.set(f.from, []).get(f.from)).push(f);
349
+ }
350
+ for (const [from, group] of fwdBySource) {
351
+ const sources = byId.get(from);
352
+ if (!sources) {
353
+ for (const f of group)
354
+ problems.push({
355
+ file: f.file,
356
+ line: f.line,
357
+ message: `forwarding from ${from}, which does not exist${revHint(from)}`
358
+ });
359
+ continue;
360
+ }
297
361
  if (group.length > 1)
298
- for (const it of group) it.defects.push(`duplicate: ID ${id} is defined ${group.length} times`);
362
+ for (const it of sources)
363
+ it.defects.push(`duplicate: forwarding for ${from} is declared ${group.length} times`);
364
+ fwdTarget.set(from, group[0].to);
365
+ declBySource.set(from, group[0]);
299
366
  }
300
- const revHint = (id) => {
301
- const revs = revsByKey.get(keyOf(id));
302
- return revs ? ` (revision mismatch: existing revision(s) of ${keyOf(id)}: ${[...revs].sort((a, b) => a - b).join(", ")})` : "";
303
- };
304
- for (const it of items) checkItemReferences(it, byId, neededIds, revHint);
367
+ dropCyclicForwards(fwdTarget, declBySource, problems);
368
+ for (const to of fwdTarget.values()) neededIds.add(to);
369
+ return fwdTarget;
370
+ }
371
+ function markDeepCoverage(items, byId, fwdTarget) {
305
372
  const memo = /* @__PURE__ */ new Map();
306
373
  const deep = (id) => {
307
374
  if (memo.has(id)) return memo.get(id);
@@ -311,13 +378,38 @@ function analyze(items) {
311
378
  memo.set(id, false);
312
379
  return false;
313
380
  }
381
+ const fwd = fwdTarget.get(id);
314
382
  let ok = true;
315
- for (const it of group) for (const n of it.needs) if (!deep(n)) ok = false;
383
+ if (fwd !== void 0) {
384
+ ok = deep(fwd);
385
+ } else {
386
+ for (const it of group) for (const n of it.needs) if (!deep(n)) ok = false;
387
+ }
316
388
  memo.set(id, ok);
317
389
  return ok;
318
390
  };
319
391
  for (const it of items) it.deepCovered = deep(it.id);
320
392
  }
393
+ function analyze(items, forwards = [], problems = []) {
394
+ const byId = /* @__PURE__ */ new Map();
395
+ const revsByKey = /* @__PURE__ */ new Map();
396
+ for (const it of items) {
397
+ (byId.get(it.id) ?? byId.set(it.id, []).get(it.id)).push(it);
398
+ (revsByKey.get(it.key) ?? revsByKey.set(it.key, /* @__PURE__ */ new Set()).get(it.key)).add(it.revision);
399
+ }
400
+ const neededIds = new Set(items.flatMap((it) => it.needs));
401
+ for (const [id, group] of byId) {
402
+ if (group.length > 1)
403
+ for (const it of group) it.defects.push(`duplicate: ID ${id} is defined ${group.length} times`);
404
+ }
405
+ const revHint = (id) => {
406
+ const revs = revsByKey.get(keyOf(id));
407
+ return revs ? ` (revision mismatch: existing revision(s) of ${keyOf(id)}: ${[...revs].sort((a, b) => a - b).join(", ")})` : "";
408
+ };
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);
412
+ }
321
413
 
322
414
  // src/report.mjs
323
415
  import path3 from "node:path";
@@ -362,7 +454,7 @@ function report(items, problems, cwd) {
362
454
  ` ok ${c.green(String(okCount))}`,
363
455
  ` defective ${defective.length ? c.red(String(defective.length)) : "0"}`
364
456
  );
365
- if (notDeep) out.push(" " + c.dim(`of the ok items, ${notDeep} are only shallow-covered (a needed item is itself defective)`));
457
+ if (notDeep) out.push(" " + c.dim(`of the ok items, ${notDeep} are only shallow-covered (an item further down the tracing chain is defective)`));
366
458
  if (problems.length) out.push(` problems ${c.yellow(String(problems.length))}`);
367
459
  out.push("");
368
460
  const clean = defective.length === 0 && problems.length === 0;
@@ -408,12 +500,13 @@ async function main() {
408
500
  const opts = parseArgs(process3.argv.slice(2));
409
501
  const files = await collectFiles(opts.dirs);
410
502
  const problems = [];
503
+ const forwards = [];
411
504
  let items = [];
412
505
  for (const file of files) {
413
506
  const text = await fs2.readFile(file, "utf8");
414
507
  const ext = path4.extname(file).toLowerCase();
415
508
  items.push(
416
- ...MD_EXT.has(ext) ? parseMarkdown(file, text, problems) : parseCode(file, text, problems)
509
+ ...MD_EXT.has(ext) ? parseMarkdown(file, text, problems, forwards) : parseCode(file, text, problems, forwards)
417
510
  );
418
511
  }
419
512
  if (opts.tags) {
@@ -422,7 +515,7 @@ async function main() {
422
515
  (it) => it.origin === "code" || it.tags.some((t) => opts.tags.includes(t)) || wantUntagged && it.tags.length === 0
423
516
  );
424
517
  }
425
- analyze(items);
518
+ analyze(items, forwards, problems);
426
519
  const clean = report(items, problems, process3.cwd());
427
520
  process3.exit(clean ? 0 : 1);
428
521
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flashtrace",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "description": "Lightning-fast, reference-based requirement tracing that works anywhere.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {