flashtrace 0.1.1 → 0.1.2

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 +169 -145
  2. package/package.json +1 -1
@@ -1,8 +1,9 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  // src/main.mjs
4
+ import { realpathSync } from "node:fs";
4
5
  import process4 from "node:process";
5
- import { pathToFileURL } from "node:url";
6
+ import { fileURLToPath, pathToFileURL } from "node:url";
6
7
 
7
8
  // src/cli.mjs
8
9
  import { promises as fs2 } from "node:fs";
@@ -30,6 +31,11 @@ function findGit() {
30
31
  }
31
32
  return gitBin;
32
33
  }
34
+ function compareStrings(a, b) {
35
+ if (a < b) return -1;
36
+ if (a > b) return 1;
37
+ return 0;
38
+ }
33
39
  async function walk(dir, out) {
34
40
  let entries;
35
41
  try {
@@ -73,18 +79,18 @@ async function collectFiles(dirs) {
73
79
  return [...files].filter((f) => {
74
80
  const ext = path.extname(f).toLowerCase();
75
81
  return MD_EXT.has(ext) || CODE_EXT.has(ext);
76
- }).sort();
82
+ }).sort(compareStrings);
77
83
  }
78
84
 
79
85
  // src/ids.mjs
80
- var SEG_SRC = String.raw`[A-Za-z][A-Za-z0-9_.-]*`;
86
+ var SEG_SRC = "[A-Za-z][A-Za-z0-9_.-]*";
81
87
  var ID_SRC = String.raw`([A-Za-z]+):(?:((?:${SEG_SRC}\/)*${SEG_SRC})\/)?(${SEG_SRC})#(\d+)`;
82
88
  var ID_RE = new RegExp(`^${ID_SRC}$`);
83
89
  var mkId = (type, group, name, rev) => `${type}:${group ? group + "/" : ""}${name}#${rev}`;
84
90
  var keyOf = (id) => id.slice(0, id.lastIndexOf("#"));
85
91
  var revOf = (id) => Number(id.slice(id.lastIndexOf("#") + 1));
86
92
  function parseIdEntry(raw) {
87
- const cleaned = raw.replace(/`/g, "").trim();
93
+ const cleaned = raw.replaceAll("`", "").trim();
88
94
  const m = cleaned.match(ID_RE);
89
95
  return m ? mkId(m[1], m[2], m[3], m[4]) : null;
90
96
  }
@@ -108,9 +114,10 @@ function newItem(id, origin, file, line) {
108
114
 
109
115
  // src/parse-markdown.mjs
110
116
  var DEF_RE = new RegExp(String.raw`^\s*\`${ID_SRC}\`\s*$`);
111
- var HEADING_RE = /^(#{1,6})\s+(.*\S)\s*$/;
112
- var KEYWORD_RE = /^(Needs|Covers|Tags):\s*(.*)$/;
113
- var BULLET_RE = /^\s*[-*+]\s+(.*\S)\s*$/;
117
+ var HEADING_RE = /^(#{1,6})\s+(\S(?:.*\S)?)\s*$/;
118
+ var KEYWORD_RE = /^(Needs|Covers|Tags):\s*((?:\S.*)?)$/;
119
+ var BULLET_RE = /^\s*[-*+]\s+(\S(?:.*\S)?)\s*$/;
120
+ var isBoundary = (l) => DEF_RE.test(l) || HEADING_RE.test(l);
114
121
  function titleAbove(lines, defIndex) {
115
122
  for (let k = defIndex - 1; k >= 0; k--) {
116
123
  const l = lines[k];
@@ -120,57 +127,70 @@ function titleAbove(lines, defIndex) {
120
127
  }
121
128
  return null;
122
129
  }
130
+ function keywordEntries(lines, j, inline) {
131
+ if (inline.trim() !== "") {
132
+ return { entries: inline.split(",").map((s) => s.trim()).filter(Boolean), j };
133
+ }
134
+ const entries = [];
135
+ while (j + 1 < lines.length) {
136
+ const b = lines[j + 1].match(BULLET_RE);
137
+ if (!b) break;
138
+ entries.push(b[1].trim());
139
+ j++;
140
+ }
141
+ return { entries, j };
142
+ }
143
+ function applyKeyword(item, keyword, entries, file, kwLine, problems) {
144
+ if (keyword === "Tags") {
145
+ item.tags.push(...entries);
146
+ return;
147
+ }
148
+ const target = keyword === "Needs" ? "needs" : "covers";
149
+ for (const e of entries) {
150
+ const id = parseIdEntry(e);
151
+ if (id) item[target].push(id);
152
+ else
153
+ problems.push({
154
+ file,
155
+ line: kwLine,
156
+ message: `invalid ID "${e}" in ${keyword}: list of ${item.id}`
157
+ });
158
+ }
159
+ }
160
+ function parseItemBody(lines, start, item, file, problems) {
161
+ let j = start;
162
+ let descDone = false;
163
+ while (j < lines.length && !isBoundary(lines[j])) {
164
+ const line = lines[j];
165
+ const kw = line.match(KEYWORD_RE);
166
+ if (kw) {
167
+ descDone = true;
168
+ const collected = keywordEntries(lines, j, kw[2]);
169
+ applyKeyword(item, kw[1], collected.entries, file, j + 1, problems);
170
+ j = collected.j;
171
+ } else if (line.trim() === "") {
172
+ if (item.description.length > 0) descDone = true;
173
+ } else if (!descDone) {
174
+ item.description.push(line.trim());
175
+ }
176
+ j++;
177
+ }
178
+ return j;
179
+ }
123
180
  function parseMarkdown(file, text, problems) {
124
181
  const lines = text.split(/\r?\n/);
125
182
  const items = [];
126
- const isBoundary = (l) => DEF_RE.test(l) || HEADING_RE.test(l);
127
- for (let i = 0; i < lines.length; i++) {
183
+ let i = 0;
184
+ while (i < lines.length) {
128
185
  const def = lines[i].match(DEF_RE);
129
- if (!def) continue;
186
+ if (!def) {
187
+ i++;
188
+ continue;
189
+ }
130
190
  const item = newItem(mkId(def[1], def[2], def[3], def[4]), "markdown", file, i + 1);
131
191
  item.title = titleAbove(lines, i);
132
- let j = i + 1;
133
- let descDone = false;
134
- while (j < lines.length && !isBoundary(lines[j])) {
135
- const line = lines[j];
136
- const kw = line.match(KEYWORD_RE);
137
- if (kw) {
138
- descDone = true;
139
- const kwLine = j + 1;
140
- const entries = [];
141
- if (kw[2].trim() !== "") {
142
- entries.push(...kw[2].split(",").map((s) => s.trim()).filter(Boolean));
143
- } else {
144
- while (j + 1 < lines.length) {
145
- const b = lines[j + 1].match(BULLET_RE);
146
- if (!b) break;
147
- entries.push(b[1].trim());
148
- j++;
149
- }
150
- }
151
- if (kw[1] === "Tags") {
152
- item.tags.push(...entries);
153
- } else {
154
- for (const e of entries) {
155
- const id = parseIdEntry(e);
156
- if (id) item[kw[1] === "Needs" ? "needs" : "covers"].push(id);
157
- else
158
- problems.push({
159
- file,
160
- line: kwLine,
161
- message: `invalid ID "${e}" in ${kw[1]}: list of ${item.id}`
162
- });
163
- }
164
- }
165
- } else if (line.trim() === "") {
166
- if (item.description.length > 0) descDone = true;
167
- } else if (!descDone) {
168
- item.description.push(line.trim());
169
- }
170
- j++;
171
- }
192
+ i = parseItemBody(lines, i + 1, item, file, problems);
172
193
  items.push(item);
173
- i = j - 1;
174
194
  }
175
195
  return items;
176
196
  }
@@ -178,86 +198,93 @@ function parseMarkdown(file, text, problems) {
178
198
  // src/parse-code.mjs
179
199
  import path2 from "node:path";
180
200
  var TAG_RE = new RegExp(String.raw`\[(>>)?\s*${ID_SRC}\s*\]`, "g");
201
+ var BLOCK_CLOSERS = { c: "*/", html: "-->" };
202
+ function findCommentStart(s, pos, lineMarkers, htmlBlocks) {
203
+ const candidates = lineMarkers.map((m) => ({ idx: s.indexOf(m, pos), kind: "line", len: m.length }));
204
+ candidates.push({ idx: s.indexOf("/*", pos), kind: "c", len: 2 });
205
+ if (htmlBlocks) candidates.push({ idx: s.indexOf("<!--", pos), kind: "html", len: 4 });
206
+ let best = null;
207
+ for (const cand of candidates) {
208
+ if (cand.idx !== -1 && (best === null || cand.idx < best.idx)) best = cand;
209
+ }
210
+ return best;
211
+ }
212
+ function readBlockRest(s, pos, closer) {
213
+ const end = s.indexOf(closer, pos);
214
+ if (end === -1) return { text: s.slice(pos) + " ", pos: s.length, closed: false };
215
+ return { text: s.slice(pos, end) + " ", pos: end + closer.length, closed: true };
216
+ }
217
+ function commentText(s, state, lineMarkers, htmlBlocks) {
218
+ let comment = "";
219
+ let pos = 0;
220
+ while (pos < s.length) {
221
+ if (state.block) {
222
+ const rest = readBlockRest(s, pos, BLOCK_CLOSERS[state.block]);
223
+ comment += rest.text;
224
+ pos = rest.pos;
225
+ if (rest.closed) state.block = null;
226
+ } else {
227
+ const start = findCommentStart(s, pos, lineMarkers, htmlBlocks);
228
+ if (!start) break;
229
+ pos = start.idx + start.len;
230
+ if (start.kind === "line") {
231
+ comment += s.slice(pos) + " ";
232
+ pos = s.length;
233
+ } else {
234
+ state.block = start.kind;
235
+ }
236
+ }
237
+ }
238
+ return comment;
239
+ }
240
+ function collectTags(comment, file, line, state, items, problems) {
241
+ 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);
246
+ } else if (state.last) {
247
+ state.last.needs.push(id);
248
+ } else {
249
+ problems.push({
250
+ file,
251
+ line,
252
+ message: `need tag [>>${id}] has no preceding item tag in this file`
253
+ });
254
+ }
255
+ }
256
+ }
181
257
  function parseCode(file, text, problems) {
182
258
  const ext = path2.extname(file).toLowerCase();
183
259
  const lines = text.split(/\r?\n/);
184
260
  const items = [];
185
- let last = null;
186
- let block = null;
187
261
  const lineMarkers = ext === ".sql" ? ["--"] : ["//"];
188
262
  const htmlBlocks = ext === ".vue";
263
+ const state = { last: null, block: null };
189
264
  for (let i = 0; i < lines.length; i++) {
190
- const s = lines[i];
191
- let comment = "";
192
- let pos = 0;
193
- while (pos < s.length) {
194
- if (block === "c" || block === "html") {
195
- const closer = block === "c" ? "*/" : "-->";
196
- const end = s.indexOf(closer, pos);
197
- if (end === -1) {
198
- comment += s.slice(pos) + " ";
199
- pos = s.length;
200
- } else {
201
- comment += s.slice(pos, end) + " ";
202
- pos = end + closer.length;
203
- block = null;
204
- }
205
- } else {
206
- let best = -1;
207
- let kind = null;
208
- let len = 0;
209
- for (const m of lineMarkers) {
210
- const idx = s.indexOf(m, pos);
211
- if (idx !== -1 && (best === -1 || idx < best)) {
212
- best = idx;
213
- kind = "line";
214
- len = m.length;
215
- }
216
- }
217
- const cb = s.indexOf("/*", pos);
218
- if (cb !== -1 && (best === -1 || cb < best)) {
219
- best = cb;
220
- kind = "c";
221
- len = 2;
222
- }
223
- if (htmlBlocks) {
224
- const hb = s.indexOf("<!--", pos);
225
- if (hb !== -1 && (best === -1 || hb < best)) {
226
- best = hb;
227
- kind = "html";
228
- len = 4;
229
- }
230
- }
231
- if (best === -1) break;
232
- pos = best + len;
233
- if (kind === "line") {
234
- comment += s.slice(pos) + " ";
235
- pos = s.length;
236
- } else {
237
- block = kind;
238
- }
239
- }
240
- }
241
- 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
- if (!last)
245
- problems.push({
246
- file,
247
- line: i + 1,
248
- message: `need tag [>>${id}] has no preceding item tag in this file`
249
- });
250
- else last.needs.push(id);
251
- } else {
252
- last = newItem(id, "code", file, i + 1);
253
- items.push(last);
254
- }
255
- }
265
+ const comment = commentText(lines[i], state, lineMarkers, htmlBlocks);
266
+ collectTags(comment, file, i + 1, state, items, problems);
256
267
  }
257
268
  return items;
258
269
  }
259
270
 
260
271
  // 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)}`);
275
+ }
276
+ for (const c of it.covers) {
277
+ const targets = byId.get(c);
278
+ if (!targets) {
279
+ it.defects.push(`orphaned: covers ${c}, which does not exist${revHint(c)}`);
280
+ } else if (!targets.some((t) => t.needs.includes(it.id))) {
281
+ it.defects.push(`unwanted: covers ${c}, but ${c} does not need ${it.id}`);
282
+ }
283
+ }
284
+ if (it.origin === "code" && !neededIds.has(it.id)) {
285
+ it.defects.push(`unwanted: no item needs ${it.id}`);
286
+ }
287
+ }
261
288
  function analyze(items) {
262
289
  const byId = /* @__PURE__ */ new Map();
263
290
  const revsByKey = /* @__PURE__ */ new Map();
@@ -274,22 +301,7 @@ function analyze(items) {
274
301
  const revs = revsByKey.get(keyOf(id));
275
302
  return revs ? ` (revision mismatch: existing revision(s) of ${keyOf(id)}: ${[...revs].sort((a, b) => a - b).join(", ")})` : "";
276
303
  };
277
- for (const it of items) {
278
- for (const n of it.needs) {
279
- if (!byId.has(n)) it.defects.push(`uncovered: needs ${n}, which does not exist${revHint(n)}`);
280
- }
281
- for (const c of it.covers) {
282
- const targets = byId.get(c);
283
- if (!targets) {
284
- it.defects.push(`orphaned: covers ${c}, which does not exist${revHint(c)}`);
285
- } else if (!targets.some((t) => t.needs.includes(it.id))) {
286
- it.defects.push(`unwanted: covers ${c}, but ${c} does not need ${it.id}`);
287
- }
288
- }
289
- if (it.origin === "code" && !neededIds.has(it.id)) {
290
- it.defects.push(`unwanted: no item needs ${it.id}`);
291
- }
292
- }
304
+ for (const it of items) checkItemReferences(it, byId, neededIds, revHint);
293
305
  const memo = /* @__PURE__ */ new Map();
294
306
  const deep = (id) => {
295
307
  if (memo.has(id)) return memo.get(id);
@@ -325,28 +337,32 @@ function makeStyler() {
325
337
  function report(items, problems, cwd) {
326
338
  const c = makeStyler();
327
339
  const rel = (f) => path3.relative(cwd, f) || f;
340
+ const dimLoc = (file, line) => c.dim(`${rel(file)}:${line}`);
328
341
  const defective = items.filter((it) => it.defects.length > 0);
329
342
  const out = [];
330
343
  for (const it of defective) {
331
- const title = it.title ? ` ${c.dim(`"${it.title}"`)}` : "";
344
+ const title = it.title ? " " + c.dim(`"${it.title}"`) : "";
332
345
  out.push(
333
- `${c.red("\u2718")} ${c.bold(it.id)}${title} ${c.dim(`${rel(it.file)}:${it.line}`)}`
346
+ `${c.red("\u2718")} ${c.bold(it.id)}${title} ${dimLoc(it.file, it.line)}`
334
347
  );
335
348
  for (const d of it.defects) out.push(` ${c.red("\u2022")} ${d}`);
336
349
  out.push("");
337
350
  }
338
351
  for (const p of problems) {
339
- out.push(`${c.yellow("\u26A0")} ${p.message} ${c.dim(`${rel(p.file)}:${p.line}`)}`);
352
+ out.push(`${c.yellow("\u26A0")} ${p.message} ${dimLoc(p.file, p.line)}`);
340
353
  }
341
354
  if (problems.length) out.push("");
342
355
  const okCount = items.length - defective.length;
343
356
  const notDeep = items.filter((it) => it.defects.length === 0 && !it.deepCovered).length;
344
357
  const md = items.filter((i) => i.origin === "markdown").length;
345
- out.push(c.bold("Summary"));
346
- out.push(` items ${items.length} ${c.dim(`(${md} from markdown, ${items.length - md} from code)`)}`);
347
- out.push(` ok ${c.green(String(okCount))}`);
348
- out.push(` defective ${defective.length ? c.red(String(defective.length)) : "0"}`);
349
- if (notDeep) out.push(` ${c.dim(`of the ok items, ${notDeep} are only shallow-covered (a needed item is itself defective)`)}`);
358
+ const originBreakdown = c.dim(`(${md} from markdown, ${items.length - md} from code)`);
359
+ out.push(
360
+ c.bold("Summary"),
361
+ ` items ${items.length} ${originBreakdown}`,
362
+ ` ok ${c.green(String(okCount))}`,
363
+ ` defective ${defective.length ? c.red(String(defective.length)) : "0"}`
364
+ );
365
+ if (notDeep) out.push(" " + c.dim(`of the ok items, ${notDeep} are only shallow-covered (a needed item is itself defective)`));
350
366
  if (problems.length) out.push(` problems ${c.yellow(String(problems.length))}`);
351
367
  out.push("");
352
368
  const clean = defective.length === 0 && problems.length === 0;
@@ -424,8 +440,16 @@ ${HELP}`);
424
440
  }
425
441
 
426
442
  // src/main.mjs
427
- var runAsCli = process4.argv[1] && import.meta.url === pathToFileURL(process4.argv[1]).href;
428
- if (runAsCli) runCli();
443
+ function runAsCli() {
444
+ const argvPath = process4.argv[1];
445
+ if (!argvPath) return false;
446
+ try {
447
+ return realpathSync(argvPath) === realpathSync(fileURLToPath(import.meta.url));
448
+ } catch {
449
+ return import.meta.url === pathToFileURL(argvPath).href;
450
+ }
451
+ }
452
+ if (runAsCli()) runCli();
429
453
  export {
430
454
  UsageError,
431
455
  analyze,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flashtrace",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
4
4
  "description": "Lightning-fast, reference-based requirement tracing that works anywhere.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {