flashtrace 0.1.2 → 0.3.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 +143 -31
  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;
@@ -197,7 +218,11 @@ function parseMarkdown(file, text, problems) {
197
218
 
198
219
  // src/parse-code.mjs
199
220
  import path2 from "node:path";
200
- var TAG_RE = new RegExp(String.raw`\[(>>)?\s*${ID_SRC}\s*\]`, "g");
221
+ var TAG_RE = new RegExp(
222
+ String.raw`\[(?:\s*${ID_SRC}\s*)?>>\s*${ID_SRC}\s*\]|\[\s*${ID_SRC}\s*\]`,
223
+ "g"
224
+ );
225
+ var FORWARD_RE = new RegExp(FORWARD_SRC, "g");
201
226
  var BLOCK_CLOSERS = { c: "*/", html: "-->" };
202
227
  function findCommentStart(s, pos, lineMarkers, htmlBlocks) {
203
228
  const candidates = lineMarkers.map((m) => ({ idx: s.indexOf(m, pos), kind: "line", len: m.length }));
@@ -239,10 +264,26 @@ function commentText(s, state, lineMarkers, htmlBlocks) {
239
264
  }
240
265
  function collectTags(comment, file, line, state, items, problems) {
241
266
  for (const m of comment.matchAll(TAG_RE)) {
242
- const id = mkId(m[2], m[3], m[4], m[5]);
243
- if (!m[1]) {
244
- state.last = newItem(id, "code", file, line);
245
- items.push(state.last);
267
+ if (m[9]) {
268
+ const item = newItem(mkId(m[9], m[10], m[11], m[12]), "code", file, line);
269
+ state.last = item;
270
+ state.byId.set(item.id, item);
271
+ items.push(item);
272
+ continue;
273
+ }
274
+ const id = mkId(m[5], m[6], m[7], m[8]);
275
+ if (m[1]) {
276
+ const source = mkId(m[1], m[2], m[3], m[4]);
277
+ const anchor = state.byId.get(source);
278
+ if (anchor) {
279
+ anchor.needs.push(id);
280
+ } else {
281
+ problems.push({
282
+ file,
283
+ line,
284
+ message: `need tag [${source} >> ${id}] has no preceding item tag [${source}] in this file`
285
+ });
286
+ }
246
287
  } else if (state.last) {
247
288
  state.last.needs.push(id);
248
289
  } else {
@@ -254,24 +295,33 @@ function collectTags(comment, file, line, state, items, problems) {
254
295
  }
255
296
  }
256
297
  }
257
- function parseCode(file, text, problems) {
298
+ function parseCode(file, text, problems, forwards = []) {
258
299
  const ext = path2.extname(file).toLowerCase();
259
300
  const lines = text.split(/\r?\n/);
260
301
  const items = [];
261
302
  const lineMarkers = ext === ".sql" ? ["--"] : ["//"];
262
303
  const htmlBlocks = ext === ".vue";
263
- const state = { last: null, block: null };
304
+ const state = { last: null, byId: /* @__PURE__ */ new Map(), block: null };
264
305
  for (let i = 0; i < lines.length; i++) {
265
306
  const comment = commentText(lines[i], state, lineMarkers, htmlBlocks);
307
+ for (const m of comment.matchAll(FORWARD_RE)) {
308
+ forwards.push(mkForward(m, 1, file, i + 1));
309
+ }
266
310
  collectTags(comment, file, i + 1, state, items, problems);
267
311
  }
268
312
  return items;
269
313
  }
270
314
 
271
315
  // 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)}`);
316
+ function checkItemReferences(it, byId, neededIds, revHint, fwdTarget) {
317
+ const fwd = fwdTarget.get(it.id);
318
+ if (fwd !== void 0) {
319
+ if (!byId.has(fwd))
320
+ it.defects.push(`uncovered: forwards to ${fwd}, which does not exist${revHint(fwd)}`);
321
+ } else {
322
+ for (const n of it.needs) {
323
+ if (!byId.has(n)) it.defects.push(`uncovered: needs ${n}, which does not exist${revHint(n)}`);
324
+ }
275
325
  }
276
326
  for (const c of it.covers) {
277
327
  const targets = byId.get(c);
@@ -285,23 +335,59 @@ function checkItemReferences(it, byId, neededIds, revHint) {
285
335
  it.defects.push(`unwanted: no item needs ${it.id}`);
286
336
  }
287
337
  }
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);
338
+ function dropCyclicForwards(fwdTarget, declBySource, problems) {
339
+ const done = /* @__PURE__ */ new Set();
340
+ for (const start of fwdTarget.keys()) {
341
+ if (done.has(start)) continue;
342
+ const seen = /* @__PURE__ */ new Map();
343
+ const path5 = [];
344
+ let cur = start;
345
+ while (fwdTarget.has(cur) && !done.has(cur) && !seen.has(cur)) {
346
+ seen.set(cur, path5.length);
347
+ path5.push(cur);
348
+ cur = fwdTarget.get(cur);
349
+ }
350
+ if (seen.has(cur)) {
351
+ const cycle = path5.slice(seen.get(cur));
352
+ const chain = [...cycle, cur].join(" --> ");
353
+ for (const id of cycle) {
354
+ const f = declBySource.get(id);
355
+ problems.push({ file: f.file, line: f.line, message: `cyclic forwarding: ${chain}` });
356
+ fwdTarget.delete(id);
357
+ }
358
+ }
359
+ for (const id of path5) done.add(id);
294
360
  }
295
- const neededIds = new Set(items.flatMap((it) => it.needs));
296
- for (const [id, group] of byId) {
361
+ }
362
+ function buildForwardMap(forwards, byId, neededIds, revHint, problems) {
363
+ const fwdTarget = /* @__PURE__ */ new Map();
364
+ const declBySource = /* @__PURE__ */ new Map();
365
+ const fwdBySource = /* @__PURE__ */ new Map();
366
+ for (const f of forwards) {
367
+ (fwdBySource.get(f.from) ?? fwdBySource.set(f.from, []).get(f.from)).push(f);
368
+ }
369
+ for (const [from, group] of fwdBySource) {
370
+ const sources = byId.get(from);
371
+ if (!sources) {
372
+ for (const f of group)
373
+ problems.push({
374
+ file: f.file,
375
+ line: f.line,
376
+ message: `forwarding from ${from}, which does not exist${revHint(from)}`
377
+ });
378
+ continue;
379
+ }
297
380
  if (group.length > 1)
298
- for (const it of group) it.defects.push(`duplicate: ID ${id} is defined ${group.length} times`);
381
+ for (const it of sources)
382
+ it.defects.push(`duplicate: forwarding for ${from} is declared ${group.length} times`);
383
+ fwdTarget.set(from, group[0].to);
384
+ declBySource.set(from, group[0]);
299
385
  }
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);
386
+ dropCyclicForwards(fwdTarget, declBySource, problems);
387
+ for (const to of fwdTarget.values()) neededIds.add(to);
388
+ return fwdTarget;
389
+ }
390
+ function markDeepCoverage(items, byId, fwdTarget) {
305
391
  const memo = /* @__PURE__ */ new Map();
306
392
  const deep = (id) => {
307
393
  if (memo.has(id)) return memo.get(id);
@@ -311,13 +397,38 @@ function analyze(items) {
311
397
  memo.set(id, false);
312
398
  return false;
313
399
  }
400
+ const fwd = fwdTarget.get(id);
314
401
  let ok = true;
315
- for (const it of group) for (const n of it.needs) if (!deep(n)) ok = false;
402
+ if (fwd !== void 0) {
403
+ ok = deep(fwd);
404
+ } else {
405
+ for (const it of group) for (const n of it.needs) if (!deep(n)) ok = false;
406
+ }
316
407
  memo.set(id, ok);
317
408
  return ok;
318
409
  };
319
410
  for (const it of items) it.deepCovered = deep(it.id);
320
411
  }
412
+ function analyze(items, forwards = [], problems = []) {
413
+ const byId = /* @__PURE__ */ new Map();
414
+ const revsByKey = /* @__PURE__ */ new Map();
415
+ for (const it of items) {
416
+ (byId.get(it.id) ?? byId.set(it.id, []).get(it.id)).push(it);
417
+ (revsByKey.get(it.key) ?? revsByKey.set(it.key, /* @__PURE__ */ new Set()).get(it.key)).add(it.revision);
418
+ }
419
+ const neededIds = new Set(items.flatMap((it) => it.needs));
420
+ for (const [id, group] of byId) {
421
+ if (group.length > 1)
422
+ for (const it of group) it.defects.push(`duplicate: ID ${id} is defined ${group.length} times`);
423
+ }
424
+ const revHint = (id) => {
425
+ const revs = revsByKey.get(keyOf(id));
426
+ return revs ? ` (revision mismatch: existing revision(s) of ${keyOf(id)}: ${[...revs].sort((a, b) => a - b).join(", ")})` : "";
427
+ };
428
+ const fwdTarget = buildForwardMap(forwards, byId, neededIds, revHint, problems);
429
+ for (const it of items) checkItemReferences(it, byId, neededIds, revHint, fwdTarget);
430
+ markDeepCoverage(items, byId, fwdTarget);
431
+ }
321
432
 
322
433
  // src/report.mjs
323
434
  import path3 from "node:path";
@@ -362,7 +473,7 @@ function report(items, problems, cwd) {
362
473
  ` ok ${c.green(String(okCount))}`,
363
474
  ` defective ${defective.length ? c.red(String(defective.length)) : "0"}`
364
475
  );
365
- if (notDeep) out.push(" " + c.dim(`of the ok items, ${notDeep} are only shallow-covered (a needed item is itself defective)`));
476
+ 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
477
  if (problems.length) out.push(` problems ${c.yellow(String(problems.length))}`);
367
478
  out.push("");
368
479
  const clean = defective.length === 0 && problems.length === 0;
@@ -408,12 +519,13 @@ async function main() {
408
519
  const opts = parseArgs(process3.argv.slice(2));
409
520
  const files = await collectFiles(opts.dirs);
410
521
  const problems = [];
522
+ const forwards = [];
411
523
  let items = [];
412
524
  for (const file of files) {
413
525
  const text = await fs2.readFile(file, "utf8");
414
526
  const ext = path4.extname(file).toLowerCase();
415
527
  items.push(
416
- ...MD_EXT.has(ext) ? parseMarkdown(file, text, problems) : parseCode(file, text, problems)
528
+ ...MD_EXT.has(ext) ? parseMarkdown(file, text, problems, forwards) : parseCode(file, text, problems, forwards)
417
529
  );
418
530
  }
419
531
  if (opts.tags) {
@@ -422,7 +534,7 @@ async function main() {
422
534
  (it) => it.origin === "code" || it.tags.some((t) => opts.tags.includes(t)) || wantUntagged && it.tags.length === 0
423
535
  );
424
536
  }
425
- analyze(items);
537
+ analyze(items, forwards, problems);
426
538
  const clean = report(items, problems, process3.cwd());
427
539
  process3.exit(clean ? 0 : 1);
428
540
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flashtrace",
3
- "version": "0.1.2",
3
+ "version": "0.3.0",
4
4
  "description": "Lightning-fast, reference-based requirement tracing that works anywhere.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {