flashtrace 0.4.1 → 0.5.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 +246 -30
  2. package/package.json +1 -1
@@ -18,8 +18,145 @@ var UsageError = class extends Error {
18
18
  import { existsSync, promises as fs } from "node:fs";
19
19
  import path from "node:path";
20
20
  import { execFileSync } from "node:child_process";
21
+
22
+ // src/languages.mjs
23
+ var cLike = { line: ["//"], block: [["/*", "*/"]] };
24
+ var cLikeNested = { line: ["//"], block: [["/*", "*/", true]] };
25
+ var php = { line: ["//", "#"], block: [["/*", "*/"]] };
26
+ var hash = { line: ["#"], block: [] };
27
+ var powershell = { line: ["#"], block: [["<#", "#>"]] };
28
+ var sql = { line: ["--"], block: [["/*", "*/"]] };
29
+ var lua = { line: ["--"], block: [["--[[", "]]"]] };
30
+ var haskell = { line: ["--"], block: [["{-", "-}", true]] };
31
+ var css = { line: [], block: [["/*", "*/"]] };
32
+ var xml = { line: [], block: [["<!--", "-->"]] };
33
+ var none = { line: [], block: [] };
34
+ var semicolon = { line: [";"], block: [] };
35
+ var percent = { line: ["%"], block: [] };
36
+ var dashLine = { line: ["--"], block: [] };
37
+ var ml = { line: [], block: [["(*", "*)", true]] };
38
+ var fsharp = { line: ["//"], block: [["(*", "*)", true]] };
39
+ var pascal = { line: ["//"], block: [["{", "}"], ["(*", "*)"]] };
40
+ var hcl = { line: ["#", "//"], block: [["/*", "*/"]] };
41
+ function attrOf(tag, name) {
42
+ const m = new RegExp(String.raw`\s${name}\s*=\s*["']?([^"'\s>]+)`, "i").exec(tag);
43
+ return m ? m[1].toLowerCase() : "";
44
+ }
45
+ function scriptGrammar(tag) {
46
+ const type = attrOf(tag, "type");
47
+ if (/json|importmap/.test(type)) return none;
48
+ if (/template|html/.test(type)) return xml;
49
+ if (/coffee/.test(attrOf(tag, "lang"))) return hash;
50
+ return cLike;
51
+ }
52
+ function styleGrammar(tag) {
53
+ return /s[ac]ss|less|stylus|styl/.test(attrOf(tag, "lang")) ? cLike : css;
54
+ }
55
+ var html = {
56
+ default: xml,
57
+ regions: [
58
+ { enter: /<script\b[^>]*>/gi, exit: /<\/script\s*>/gi, grammar: scriptGrammar },
59
+ { enter: /<style\b[^>]*>/gi, exit: /<\/style\s*>/gi, grammar: styleGrammar }
60
+ ]
61
+ };
62
+ var BY_EXT = {
63
+ // C-family: // line, /* */ block
64
+ ".ts": cLike,
65
+ ".js": cLike,
66
+ ".mjs": cLike,
67
+ ".cjs": cLike,
68
+ ".jsx": cLike,
69
+ ".tsx": cLike,
70
+ ".cts": cLike,
71
+ ".mts": cLike,
72
+ ".c": cLike,
73
+ ".h": cLike,
74
+ ".cpp": cLike,
75
+ ".cc": cLike,
76
+ ".hpp": cLike,
77
+ ".cs": cLike,
78
+ ".java": cLike,
79
+ ".go": cLike,
80
+ ".dart": cLike,
81
+ ".php": php,
82
+ ".proto": cLike,
83
+ ".scss": cLike,
84
+ ".less": cLike,
85
+ // C-family with nested block comments
86
+ ".rs": cLikeNested,
87
+ ".swift": cLikeNested,
88
+ ".kt": cLikeNested,
89
+ ".kts": cLikeNested,
90
+ ".scala": cLikeNested,
91
+ // hash line comments
92
+ ".py": hash,
93
+ ".rb": hash,
94
+ ".sh": hash,
95
+ ".bash": hash,
96
+ ".zsh": hash,
97
+ ".yaml": hash,
98
+ ".yml": hash,
99
+ ".toml": hash,
100
+ ".r": hash,
101
+ ".pm": hash,
102
+ ".ex": hash,
103
+ ".exs": hash,
104
+ ".tcl": hash,
105
+ ".jl": hash,
106
+ ".nim": hash,
107
+ ".graphql": hash,
108
+ ".gql": hash,
109
+ ".coffee": hash,
110
+ ".ps1": powershell,
111
+ ".psm1": powershell,
112
+ ".tf": hcl,
113
+ ".tfvars": hcl,
114
+ ".hcl": hcl,
115
+ // semicolon (Lisp family)
116
+ ".clj": semicolon,
117
+ ".cljs": semicolon,
118
+ ".cljc": semicolon,
119
+ ".edn": semicolon,
120
+ ".el": semicolon,
121
+ ".lisp": semicolon,
122
+ ".scm": semicolon,
123
+ ".ss": semicolon,
124
+ // percent (Erlang, LaTeX)
125
+ ".erl": percent,
126
+ ".hrl": percent,
127
+ ".tex": percent,
128
+ ".sty": percent,
129
+ // dash line-only (Ada, VHDL)
130
+ ".adb": dashLine,
131
+ ".ads": dashLine,
132
+ ".vhd": dashLine,
133
+ ".vhdl": dashLine,
134
+ // ML-family
135
+ ".ml": ml,
136
+ ".mli": ml,
137
+ ".fs": fsharp,
138
+ ".fsi": fsharp,
139
+ ".fsx": fsharp,
140
+ ".pas": pascal,
141
+ ".dpr": pascal,
142
+ // dashes and others
143
+ ".sql": sql,
144
+ ".lua": lua,
145
+ ".hs": haskell,
146
+ ".css": css,
147
+ ".xml": xml,
148
+ ".svg": xml,
149
+ // composite: HTML markup with embedded <script>/<style> regions
150
+ ".vue": html,
151
+ ".html": html,
152
+ ".htm": html,
153
+ ".svelte": html
154
+ };
155
+ var CODE_EXT = new Set(Object.keys(BY_EXT));
156
+ var grammarFor = (ext) => BY_EXT[ext] ?? null;
157
+
158
+ // src/files.mjs
21
159
  var MD_EXT = /* @__PURE__ */ new Set([".md", ".markdown"]);
22
- var CODE_EXT = /* @__PURE__ */ new Set([".ts", ".js", ".mjs", ".sql", ".vue"]);
23
160
  var GIT_LOCATIONS = process.platform === "win32" ? [
24
161
  String.raw`C:\Program Files\Git\cmd\git.exe`,
25
162
  String.raw`C:\Program Files (x86)\Git\cmd\git.exe`
@@ -257,41 +394,121 @@ var TAG_RE = new RegExp(
257
394
  "g"
258
395
  );
259
396
  var FORWARD_RE = new RegExp(FORWARD_SRC, "g");
260
- var BLOCK_CLOSERS = { c: "*/", html: "-->" };
261
- function findCommentStart(s, pos, lineMarkers, htmlBlocks) {
262
- const candidates = lineMarkers.map((m) => ({ idx: s.indexOf(m, pos), kind: "line", len: m.length }));
263
- candidates.push({ idx: s.indexOf("/*", pos), kind: "c", len: 2 });
264
- if (htmlBlocks) candidates.push({ idx: s.indexOf("<!--", pos), kind: "html", len: 4 });
397
+ function activeLeaf(grammar, state) {
398
+ if (state.region) return state.region.grammar;
399
+ return grammar.regions ? grammar.default : grammar;
400
+ }
401
+ function matchAt(re, s, pos) {
402
+ re.lastIndex = pos;
403
+ return re.exec(s);
404
+ }
405
+ function* regionEvents(s, pos, grammar, state) {
406
+ if (!grammar.regions) return;
407
+ if (state.region) {
408
+ const m = matchAt(state.region.exit, s, pos);
409
+ if (m) yield { idx: m.index, kind: "exit", len: m[0].length };
410
+ return;
411
+ }
412
+ for (const r of grammar.regions) {
413
+ const m = matchAt(r.enter, s, pos);
414
+ if (m) {
415
+ const leaf = typeof r.grammar === "function" ? r.grammar(m[0]) : r.grammar;
416
+ yield { idx: m.index, kind: "enter", len: m[0].length, region: { exit: r.exit, grammar: leaf } };
417
+ }
418
+ }
419
+ }
420
+ function nextEvent(s, pos, grammar, state) {
421
+ const leaf = activeLeaf(grammar, state);
265
422
  let best = null;
266
- for (const cand of candidates) {
267
- if (cand.idx !== -1 && (best === null || cand.idx < best.idx)) best = cand;
423
+ const consider = (idx, ev) => {
424
+ if (idx === -1) return;
425
+ if (best === null || idx < best.idx || idx === best.idx && ev.len > best.len) {
426
+ best = { ...ev, idx };
427
+ }
428
+ };
429
+ for (const marker of leaf.line) {
430
+ consider(s.indexOf(marker, pos), { kind: "line", len: marker.length });
431
+ }
432
+ for (const [open, close, nestable] of leaf.block) {
433
+ consider(s.indexOf(open, pos), {
434
+ kind: "block",
435
+ len: open.length,
436
+ open,
437
+ close,
438
+ nestable: Boolean(nestable)
439
+ });
268
440
  }
441
+ for (const ev of regionEvents(s, pos, grammar, state)) consider(ev.idx, ev);
269
442
  return best;
270
443
  }
271
- function readBlockRest(s, pos, closer) {
272
- const end = s.indexOf(closer, pos);
273
- if (end === -1) return { text: s.slice(pos) + " ", pos: s.length, closed: false };
274
- return { text: s.slice(pos, end) + " ", pos: end + closer.length, closed: true };
444
+ function readBlockRest(s, pos, block, limit = s.length) {
445
+ const { open, close, nestable } = block;
446
+ const within = (idx) => idx !== -1 && idx < limit ? idx : -1;
447
+ let i = pos;
448
+ while (i < limit) {
449
+ const closeIdx = within(s.indexOf(close, i));
450
+ const openIdx = within(nestable ? s.indexOf(open, i) : -1);
451
+ if (closeIdx === -1 && openIdx === -1) break;
452
+ if (openIdx !== -1 && (closeIdx === -1 || openIdx < closeIdx)) {
453
+ block.depth++;
454
+ i = openIdx + open.length;
455
+ continue;
456
+ }
457
+ block.depth--;
458
+ i = closeIdx + close.length;
459
+ if (block.depth === 0) return { text: s.slice(pos, closeIdx) + " ", pos: i, closed: true };
460
+ }
461
+ return { text: s.slice(pos, limit) + " ", pos: limit, closed: false };
462
+ }
463
+ function regionExitAt(s, pos, region) {
464
+ const m = matchAt(region.exit, s, pos);
465
+ return m ? { idx: m.index, len: m[0].length } : null;
466
+ }
467
+ function consumeBlock(s, pos, state, exit) {
468
+ const rest = readBlockRest(s, pos, state.block, exit ? exit.idx : s.length);
469
+ if (rest.closed) {
470
+ state.block = null;
471
+ return { text: rest.text, pos: rest.pos };
472
+ }
473
+ if (exit) {
474
+ state.block = null;
475
+ state.region = null;
476
+ return { text: rest.text, pos: exit.idx + exit.len };
477
+ }
478
+ return { text: rest.text, pos: rest.pos };
275
479
  }
276
- function commentText(s, state, lineMarkers, htmlBlocks) {
480
+ function consumeLine(s, pos, state, exit) {
481
+ if (exit) {
482
+ state.region = null;
483
+ return { text: s.slice(pos, exit.idx) + " ", pos: exit.idx + exit.len, done: false };
484
+ }
485
+ return { text: s.slice(pos) + " ", pos: s.length, done: true };
486
+ }
487
+ function commentText(s, state, grammar) {
277
488
  let comment = "";
278
489
  let pos = 0;
279
490
  while (pos < s.length) {
491
+ const exit = state.region ? regionExitAt(s, pos, state.region) : null;
280
492
  if (state.block) {
281
- const rest = readBlockRest(s, pos, BLOCK_CLOSERS[state.block]);
282
- comment += rest.text;
283
- pos = rest.pos;
284
- if (rest.closed) state.block = null;
493
+ const r = consumeBlock(s, pos, state, exit);
494
+ comment += r.text;
495
+ pos = r.pos;
496
+ continue;
497
+ }
498
+ const ev = nextEvent(s, pos, grammar, state);
499
+ if (!ev) break;
500
+ pos = ev.idx + ev.len;
501
+ if (ev.kind === "line") {
502
+ const r = consumeLine(s, pos, state, exit);
503
+ comment += r.text;
504
+ pos = r.pos;
505
+ if (r.done) break;
506
+ } else if (ev.kind === "block") {
507
+ state.block = { open: ev.open, close: ev.close, nestable: ev.nestable, depth: 1 };
508
+ } else if (ev.kind === "enter") {
509
+ state.region = ev.region;
285
510
  } else {
286
- const start = findCommentStart(s, pos, lineMarkers, htmlBlocks);
287
- if (!start) break;
288
- pos = start.idx + start.len;
289
- if (start.kind === "line") {
290
- comment += s.slice(pos) + " ";
291
- pos = s.length;
292
- } else {
293
- state.block = start.kind;
294
- }
511
+ state.region = null;
295
512
  }
296
513
  }
297
514
  return comment;
@@ -331,13 +548,12 @@ function collectTags(comment, file, line, state, items, problems) {
331
548
  }
332
549
  function parseCode(file, text, problems, forwards = []) {
333
550
  const ext = path2.extname(file).toLowerCase();
551
+ const grammar = grammarFor(ext) ?? cLike;
334
552
  const lines = text.split(/\r?\n/);
335
553
  const items = [];
336
- const lineMarkers = ext === ".sql" ? ["--"] : ["//"];
337
- const htmlBlocks = ext === ".vue";
338
- const state = { last: null, byId: /* @__PURE__ */ new Map(), block: null };
554
+ const state = { last: null, byId: /* @__PURE__ */ new Map(), block: null, region: null };
339
555
  for (let i = 0; i < lines.length; i++) {
340
- const comment = commentText(lines[i], state, lineMarkers, htmlBlocks);
556
+ const comment = commentText(lines[i], state, grammar);
341
557
  for (const m of comment.matchAll(FORWARD_RE)) {
342
558
  forwards.push(mkForward(m, 1, file, i + 1));
343
559
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flashtrace",
3
- "version": "0.4.1",
3
+ "version": "0.5.0",
4
4
  "description": "Lightning-fast, reference-based requirement tracing that works anywhere.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {