claude-code-token-optimization-by-tool-call 0.0.0-stage → 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.
@@ -0,0 +1,95 @@
1
+ // Jest and Vitest (default reporters).
2
+ import { isBlank } from './util.mjs';
3
+
4
+ // Failing test headers
5
+ const JEST_BULLET = /^\s*● .*›|^\s*● Test suite failed to run|^\s*● [^\n]*\S\s*$/;
6
+ const BULLET_NOT_TEST = /^\s*● (Validation (Warning|Error)|Deprecation Warning|Console)/i;
7
+ const FAIL_FILE = /^\s*FAIL\s+\S/;
8
+ const CROSS = /^\s*[×✗✕]\s+\S/;
9
+ const VITEST_FAILED_TESTS = /^\s*⎯+\s*Failed (Tests?|Suites?) \d+\s*⎯+/;
10
+ const VITEST_ERRORS = /^\s*⎯+\s*(Unhandled Errors?|Unhandled Rejection)/;
11
+ const VITEST_SEP = /^\s*⎯{3,}.*$/;
12
+ // vitest file line with failures: ` ❯ src/a.test.ts (28 tests | 1 failed) 62ms`
13
+ const VITEST_FILE_FAIL = /^\s*❯ .*\(\d+ tests?\b.*\d+ failed\)/;
14
+ const VITEST_FILE = /^\s*[❯✓] .*\(\d+ tests?\b/;
15
+
16
+ // Other test headers that end a failing block
17
+ const PASS_LINE = /^\s*(✓|√|✔|○|↓)\s+\S|^\s*PASS\s+\S/;
18
+
19
+ const SUMMARY = /^\s*(Tests?:|Test Suites:|Test Files\s|Tests\s+\d|Snapshots:|Suites:|Errors\s+\d)/;
20
+ const SUMMARY_FAIL = /failed|\bfail\b|error/i;
21
+
22
+ function isFailHeader(l) {
23
+ if (BULLET_NOT_TEST.test(l)) return false;
24
+ return JEST_BULLET.test(l) || FAIL_FILE.test(l) || CROSS.test(l) || VITEST_FILE_FAIL.test(l) || VITEST_FAILED_TESTS.test(l) || VITEST_ERRORS.test(l);
25
+ }
26
+
27
+ function isHeader(l) {
28
+ return isFailHeader(l) || PASS_LINE.test(l) || VITEST_FILE.test(l) || /^\s*● /.test(l) || SUMMARY.test(l);
29
+ }
30
+
31
+ export default {
32
+ name: 'jest-vitest',
33
+ hint: /\b(jest|vitest)\b/,
34
+ detect(lines) {
35
+ let bullet = 0;
36
+ let failFile = 0;
37
+ let cross = 0;
38
+ let summary = 0;
39
+ let summaryFail = 0;
40
+ let pass = 0;
41
+ let fileFail = 0;
42
+ for (const l of lines) {
43
+ if (PASS_LINE.test(l)) pass++;
44
+ if (VITEST_FILE_FAIL.test(l)) fileFail++;
45
+ if (/^\s*● .*›|^\s*● Test suite failed to run/.test(l)) bullet++;
46
+ else if (FAIL_FILE.test(l)) failFile++;
47
+ else if (CROSS.test(l)) cross++;
48
+ if (SUMMARY.test(l)) {
49
+ summary++;
50
+ if (SUMMARY_FAIL.test(l)) summaryFail++;
51
+ } else if (VITEST_FAILED_TESTS.test(l)) summaryFail++;
52
+ }
53
+ let s = 0;
54
+ if (summaryFail) s = 0.85;
55
+ else if (summary >= 2) s = 0.6;
56
+ if (bullet && (failFile || summary)) s = Math.max(s, 0.9);
57
+ else if (bullet) s = Math.max(s, 0.7);
58
+ if (failFile && (cross || summary)) s = Math.max(s, 0.85);
59
+ else if (failFile) s = Math.max(s, 0.55);
60
+ if (cross >= 1 && summary) s = Math.max(s, 0.8);
61
+ if (fileFail) s = Math.max(s, 0.8);
62
+ if (cross >= 1 && pass >= 1) s = Math.max(s, 0.7);
63
+ return s;
64
+ },
65
+ blocks(lines) {
66
+ const out = [];
67
+ for (let i = 0; i < lines.length; i++) {
68
+ const l = lines[i];
69
+ if (SUMMARY.test(l)) {
70
+ out.push({ start: i, end: i, priority: 2 });
71
+ continue;
72
+ }
73
+ if (!isFailHeader(l)) continue;
74
+ if (VITEST_FAILED_TESTS.test(l) || VITEST_ERRORS.test(l)) {
75
+ out.push({ start: i, end: i, priority: 1 });
76
+ continue;
77
+ }
78
+ // From the header to the next test header, vitest separator, or blank-blank.
79
+ // (A `FAIL file` line directly followed by a test header is a one-liner.)
80
+ // A failure ends at the first blank line after its stack frames (what
81
+ // follows is console output of other tests).
82
+ let j = i + 1;
83
+ let sawFrame = false;
84
+ for (; j < lines.length; j++) {
85
+ if (isHeader(lines[j]) || VITEST_SEP.test(lines[j])) break;
86
+ if (isBlank(lines[j]) && (isBlank(lines[j + 1]) || sawFrame)) break;
87
+ if (/^\s+at\s|^\s*❯ \S+:\d+:\d+/.test(lines[j])) sawFrame = true;
88
+ }
89
+ let end = j - 1;
90
+ while (end > i && isBlank(lines[end])) end--;
91
+ out.push({ start: i, end, priority: 1 });
92
+ }
93
+ return out;
94
+ },
95
+ };
@@ -0,0 +1,32 @@
1
+ // next build failures.
2
+ import { untilBlankBlank, trimBlankEnd } from './util.mjs';
3
+
4
+ const MARKER = /Failed to compile|Type error:|Build error occurred|Module not found:|Error occurred prerendering page/;
5
+ const LOCATION = /^\.?\.?\/?[\w@[\]().\/-]+\.\w+:\d+(:\d+)?\s*$/;
6
+
7
+ export default {
8
+ name: 'nextjs-build',
9
+ hint: /\bnext\s+build\b|\bnext\b/,
10
+ detect(lines) {
11
+ let s = 0;
12
+ for (const l of lines) {
13
+ if (/Failed to compile|Build error occurred/.test(l)) return 0.9;
14
+ if (/^Type error:|Error occurred prerendering page/.test(l)) s = 0.8;
15
+ else if (/Type error:/.test(l)) s = Math.max(s, 0.6);
16
+ }
17
+ return s;
18
+ },
19
+ blocks(lines) {
20
+ const out = [];
21
+ for (let i = 0; i < lines.length; i++) {
22
+ if (!MARKER.test(lines[i])) continue;
23
+ let start = i;
24
+ if (i > 0 && LOCATION.test(lines[i - 1])) start = i - 1;
25
+ let end = untilBlankBlank(lines, i, 25);
26
+ end = trimBlankEnd(lines, i, end);
27
+ out.push({ start, end, priority: 1 });
28
+ i = end;
29
+ }
30
+ return out;
31
+ },
32
+ };
@@ -0,0 +1,25 @@
1
+ // npm / pnpm / yarn error lines (often wrapping another tool's output).
2
+
3
+ const ERR = /npm ERR!|^npm error\b|ERR_PNPM_|\bELIFECYCLE\b|ERR! code|^error Command failed with exit code|^\s*YN\d{4}: .*(error|failed)/i;
4
+
5
+ export default {
6
+ name: 'npm-pnpm',
7
+ hint: /\b(npm|pnpm|yarn)\b/,
8
+ detect(lines) {
9
+ let n = 0;
10
+ for (const l of lines) if (ERR.test(l)) n++;
11
+ if (!n) return 0;
12
+ return n >= 2 ? 0.65 : 0.55;
13
+ },
14
+ blocks(lines) {
15
+ const out = [];
16
+ for (let i = 0; i < lines.length; i++) {
17
+ if (!ERR.test(lines[i])) continue;
18
+ const start = Math.max(0, i - 3);
19
+ const prev = out[out.length - 1];
20
+ if (prev && start <= prev.end + 1) prev.end = i;
21
+ else out.push({ start, end: i, priority: 0.5 });
22
+ }
23
+ return out;
24
+ },
25
+ };
@@ -0,0 +1,56 @@
1
+ // Playwright test runner (list/line reporters and the failure details).
2
+ import { isBlank } from './util.mjs';
3
+
4
+ const CROSS = /^\s*✘\s/;
5
+ const DETAIL = /^\s*\d+\) \[[\w-]+\] › /;
6
+ const SUMMARY = /^\s*\d+ (failed|passed|flaky|skipped|did not run|interrupted)\b/;
7
+ const PROJECT = /\[(chromium|firefox|webkit|Mobile [\w ]+|[\w-]+)\] ›/;
8
+
9
+ export default {
10
+ name: 'playwright',
11
+ hint: /\bplaywright\b/,
12
+ detect(lines) {
13
+ let cross = 0;
14
+ let detail = 0;
15
+ let failedSummary = false;
16
+ let project = false;
17
+ let expectErr = false;
18
+ for (const l of lines) {
19
+ if (CROSS.test(l)) cross++;
20
+ if (DETAIL.test(l)) detail++;
21
+ if (/^\s*\d+ failed\b/.test(l)) failedSummary = true;
22
+ if (/\[(chromium|firefox|webkit)\]/.test(l)) project = true;
23
+ if (/Error: expect\(/.test(l)) expectErr = true;
24
+ }
25
+ let s = 0;
26
+ if (failedSummary && project) s = 0.9;
27
+ if (detail && project) s = Math.max(s, 0.9);
28
+ if (cross && project) s = Math.max(s, 0.8);
29
+ else if (cross) s = Math.max(s, 0.5);
30
+ if (expectErr && (project || cross)) s = Math.max(s, 0.85);
31
+ return s;
32
+ },
33
+ blocks(lines) {
34
+ const out = [];
35
+ for (let i = 0; i < lines.length; i++) {
36
+ const l = lines[i];
37
+ if (CROSS.test(l)) {
38
+ out.push({ start: i, end: i, priority: 1 });
39
+ } else if (DETAIL.test(l)) {
40
+ let j = i + 1;
41
+ while (j < lines.length && !DETAIL.test(lines[j]) && !SUMMARY.test(lines[j]) && !(isBlank(lines[j]) && isBlank(lines[j + 1]) && isBlank(lines[j + 2]))) j++;
42
+ let end = j - 1;
43
+ while (end > i && isBlank(lines[end])) end--;
44
+ out.push({ start: i, end, priority: 1 });
45
+ i = end;
46
+ } else if (SUMMARY.test(l)) {
47
+ // the summary line plus the indented list of failing tests under it
48
+ let j = i + 1;
49
+ while (j < lines.length && PROJECT.test(lines[j]) && /^\s+/.test(lines[j])) j++;
50
+ out.push({ start: i, end: j - 1, priority: 2 });
51
+ i = j - 1;
52
+ }
53
+ }
54
+ return out;
55
+ },
56
+ };
@@ -0,0 +1,36 @@
1
+ // prettier --check: the `[warn] <file>` list and `[error]` syntax errors.
2
+
3
+ const WARN_FILE = /^\[warn\] (?!Code style issues)\S/;
4
+ const ERROR = /^\[error\] /;
5
+ const SUMMARY = /^\[warn\] Code style issues|^All matched files use Prettier/;
6
+
7
+ export default {
8
+ name: 'prettier',
9
+ hint: /\bprettier\b/,
10
+ detect(lines) {
11
+ let s = 0;
12
+ for (const l of lines) {
13
+ if (SUMMARY.test(l)) return 0.85;
14
+ if (/^Checking formatting\.\.\./.test(l)) s = Math.max(s, 0.6);
15
+ if (ERROR.test(l) && s) s = 0.8;
16
+ }
17
+ return s;
18
+ },
19
+ blocks(lines) {
20
+ const out = [];
21
+ for (let i = 0; i < lines.length; i++) {
22
+ const l = lines[i];
23
+ if (SUMMARY.test(l)) out.push({ start: i, end: i, priority: 2 });
24
+ else if (WARN_FILE.test(l) || ERROR.test(l)) {
25
+ let j = i;
26
+ const re = ERROR.test(l) ? ERROR : WARN_FILE;
27
+ while (j + 1 < lines.length && re.test(lines[j + 1])) j++;
28
+ // keep the `Checking formatting...` banner that introduces the list
29
+ const start = i > 0 && /^Checking formatting/.test(lines[i - 1]) ? i - 1 : i;
30
+ out.push({ start, end: j, priority: 1 });
31
+ i = j;
32
+ }
33
+ }
34
+ return out;
35
+ },
36
+ };
@@ -0,0 +1,69 @@
1
+ // Python tracebacks and pytest.
2
+ import { isBlank } from './util.mjs';
3
+
4
+ const TRACEBACK = /^\s*Traceback \(most recent call last\):/;
5
+ const EXC_LINE = /^(\w+\.)*\w*(Error|Exception|Exit|Interrupt|Warning|Failure)\b.*|^\w+: .+/;
6
+ const FAILURES = /^=+ (FAILURES|ERRORS) =+$/;
7
+ const SECTION = /^_{3,} .+ _{3,}$/;
8
+ const RULE = /^=+ .* =+$/;
9
+ const SUMMARY = /^=+ .*\b(\d+ (failed|passed|errors?|error))\b.* =+$/;
10
+ const SHORT_SUMMARY = /^=+ short test summary info =+$/;
11
+ const FAILED_LINE = /^(FAILED|ERROR) \S+/;
12
+
13
+ export default {
14
+ name: 'python',
15
+ hint: /\bpython3?\b|\bpytest\b|\.py\b/,
16
+ detect(lines) {
17
+ let s = 0;
18
+ for (const l of lines) {
19
+ if (TRACEBACK.test(l) || FAILURES.test(l) || SHORT_SUMMARY.test(l)) return 0.9;
20
+ if (/^FAILED \S+::/.test(l)) s = Math.max(s, 0.85);
21
+ else if (/^E\s{3}\S/.test(l)) s = Math.max(s, 0.5);
22
+ else if (/^\w+Error: /.test(l)) s = Math.max(s, 0.3);
23
+ }
24
+ return s;
25
+ },
26
+ blocks(lines) {
27
+ const out = [];
28
+ // the last traceback block
29
+ let lastTb = -1;
30
+ for (let i = 0; i < lines.length; i++) if (TRACEBACK.test(lines[i])) lastTb = i;
31
+ if (lastTb >= 0) {
32
+ let j = lastTb + 1;
33
+ while (j < lines.length && (/^\s/.test(lines[j]) || isBlank(lines[j]))) j++;
34
+ // j is the exception line; include indented continuation after it
35
+ let end = Math.min(j, lines.length - 1);
36
+ while (end + 1 < lines.length && /^\s+\S/.test(lines[end + 1]) && end - j < 10) end++;
37
+ out.push({ start: lastTb, end, priority: 1.5 });
38
+ }
39
+ // pytest failure sections
40
+ let inFailures = false;
41
+ for (let i = 0; i < lines.length; i++) {
42
+ const l = lines[i];
43
+ if (FAILURES.test(l)) {
44
+ inFailures = true;
45
+ continue;
46
+ }
47
+ if (inFailures && SECTION.test(l)) {
48
+ let j = i + 1;
49
+ while (j < lines.length && !SECTION.test(lines[j]) && !RULE.test(lines[j])) j++;
50
+ let end = j - 1;
51
+ while (end > i && isBlank(lines[end])) end--;
52
+ out.push({ start: i, end, priority: 1 });
53
+ i = end;
54
+ continue;
55
+ }
56
+ if (RULE.test(l)) inFailures = false;
57
+ if (SHORT_SUMMARY.test(l)) {
58
+ let j = i + 1;
59
+ while (j < lines.length && FAILED_LINE.test(lines[j])) j++;
60
+ out.push({ start: i, end: j - 1, priority: 2 });
61
+ i = j - 1;
62
+ continue;
63
+ }
64
+ if (SUMMARY.test(l)) out.push({ start: i, end: i, priority: 2 });
65
+ else if (lastTb < 0 && EXC_LINE.test(l) && /^\w+(Error|Exception)\b/.test(l)) out.push({ start: i, end: i, priority: 1 });
66
+ }
67
+ return out;
68
+ },
69
+ };
@@ -0,0 +1,53 @@
1
+ // cargo build / cargo test.
2
+ import { isBlank } from './util.mjs';
3
+
4
+ const ERROR_CODE = /^error\[E\d+\]/;
5
+ const ERROR = /^error(\[E\d+\])?: /;
6
+ const ARROW = /^\s+--> \S+:\d+:\d+/;
7
+ const RESULT_FAILED = /^test result: FAILED/;
8
+ const STDOUT_SECTION = /^---- .+ stdout ----$/;
9
+
10
+ export default {
11
+ name: 'rust',
12
+ hint: /\bcargo\b|\brustc\b/,
13
+ detect(lines) {
14
+ let s = 0;
15
+ let err = false;
16
+ for (let i = 0; i < lines.length; i++) {
17
+ const l = lines[i];
18
+ if (ERROR_CODE.test(l) || RESULT_FAILED.test(l)) return 0.9;
19
+ if (ERROR.test(l)) {
20
+ err = true;
21
+ if (lines[i + 1] && ARROW.test(lines[i + 1])) return 0.9;
22
+ }
23
+ if (/^\s+Compiling \S+ v\d/.test(l) || /^error: could not compile/.test(l)) s = Math.max(s, 0.8);
24
+ }
25
+ if (err) s = Math.max(s, 0.5);
26
+ return s;
27
+ },
28
+ blocks(lines) {
29
+ const out = [];
30
+ for (let i = 0; i < lines.length; i++) {
31
+ const l = lines[i];
32
+ if (ERROR.test(l)) {
33
+ let j = i + 1;
34
+ while (j < lines.length && !isBlank(lines[j]) && !ERROR.test(lines[j])) j++;
35
+ out.push({ start: i, end: j - 1, priority: 1 });
36
+ i = j - 1;
37
+ } else if (STDOUT_SECTION.test(l)) {
38
+ let j = i + 1;
39
+ while (j < lines.length && !isBlank(lines[j]) && !STDOUT_SECTION.test(lines[j])) j++;
40
+ out.push({ start: i, end: j - 1, priority: 1 });
41
+ i = j - 1;
42
+ } else if (/^failures:\s*$/.test(l) && lines[i + 1] && /^\s{4}\S/.test(lines[i + 1])) {
43
+ let j = i + 1;
44
+ while (j < lines.length && /^\s{4}\S/.test(lines[j])) j++;
45
+ out.push({ start: i, end: j - 1, priority: 1 });
46
+ i = j - 1;
47
+ } else if (RESULT_FAILED.test(l)) {
48
+ out.push({ start: i, end: i, priority: 2 });
49
+ }
50
+ }
51
+ return out;
52
+ },
53
+ };
@@ -0,0 +1,83 @@
1
+ // TAP (node --test, tap, prove...) and the node:test spec reporter.
2
+ import { indentOf, isBlank } from './util.mjs';
3
+
4
+ const NOT_OK = /^(\s*)not ok \d+/;
5
+ const SUMMARY = /^#\s*(fail|pass|tests|cancelled|todo|skipped)\s+\d+\s*$/;
6
+ // node:test spec reporter
7
+ const SPEC_FAIL = /^\s*✖ /;
8
+ const SPEC_SUMMARY = /^ℹ (tests|pass|fail|cancelled|skipped|todo|suites) \d+/;
9
+ const SPEC_FAILING_HEADER = /^✖ failing tests:/;
10
+
11
+ export default {
12
+ name: 'tap',
13
+ hint: /\bnode\b.*--test|\btap\b|\bprove\b/,
14
+ detect(lines) {
15
+ let score = 0;
16
+ let notOk = 0;
17
+ let tapv = false;
18
+ let spec = 0;
19
+ let specFail = 0;
20
+ for (const l of lines) {
21
+ if (/^TAP version \d+/.test(l)) tapv = true;
22
+ else if (NOT_OK.test(l)) notOk++;
23
+ else if (SPEC_SUMMARY.test(l)) spec++;
24
+ else if (SPEC_FAIL.test(l)) specFail++;
25
+ }
26
+ if (tapv) score = 0.7;
27
+ if (notOk) score = Math.max(score, tapv ? 0.95 : 0.75);
28
+ if (spec >= 2) score = Math.max(score, specFail ? 0.85 : 0.6);
29
+ return score;
30
+ },
31
+ blocks(lines) {
32
+ const out = [];
33
+ for (let i = 0; i < lines.length; i++) {
34
+ const l = lines[i];
35
+ const m = NOT_OK.exec(l);
36
+ if (m) {
37
+ const ind = m[1].length;
38
+ let end = i;
39
+ // YAML diagnostics: ` ---` ... ` ...` indented under the test line.
40
+ if (lines[i + 1] !== undefined && /^\s*---\s*$/.test(lines[i + 1]) && indentOf(lines[i + 1]) > ind) {
41
+ const yInd = indentOf(lines[i + 1]);
42
+ let j = i + 2;
43
+ for (; j < lines.length; j++) {
44
+ if (indentOf(lines[j]) === yInd && /^\s*\.\.\.\s*$/.test(lines[j])) break;
45
+ if (!isBlank(lines[j]) && indentOf(lines[j]) < yInd) {
46
+ j--;
47
+ break;
48
+ }
49
+ }
50
+ end = Math.min(j, lines.length - 1);
51
+ }
52
+ out.push({ start: i, end, priority: 1 });
53
+ i = end;
54
+ continue;
55
+ }
56
+ if (SPEC_FAILING_HEADER.test(l)) {
57
+ // spec reporter's failure recap: until a blank-blank or the summary
58
+ let j = i + 1;
59
+ while (j < lines.length && !SPEC_SUMMARY.test(lines[j]) && !(isBlank(lines[j]) && isBlank(lines[j + 1]))) j++;
60
+ out.push({ start: i, end: j - 1, priority: 1 });
61
+ i = j - 1;
62
+ continue;
63
+ }
64
+ if (SPEC_FAIL.test(l)) {
65
+ // a failing test in spec output: its indented error lines follow
66
+ const ind = indentOf(l);
67
+ let j = i + 1;
68
+ while (j < lines.length && (isBlank(lines[j]) || indentOf(lines[j]) > ind) && !SPEC_FAIL.test(lines[j]) && j - i < 40) j++;
69
+ while (j - 1 > i && isBlank(lines[j - 1])) j--;
70
+ out.push({ start: i, end: j - 1, priority: 1 });
71
+ continue;
72
+ }
73
+ if (SUMMARY.test(l) && /fail/.test(l)) {
74
+ out.push({ start: i, end: i, priority: 2 });
75
+ } else if (SPEC_SUMMARY.test(l) && /fail/.test(l)) {
76
+ out.push({ start: i, end: i, priority: 2 });
77
+ } else if (/^#\s*pass\s+\d+/.test(l)) {
78
+ out.push({ start: i, end: i, priority: 2 });
79
+ }
80
+ }
81
+ return out;
82
+ },
83
+ };
@@ -0,0 +1,55 @@
1
+ // TypeScript compiler (plain and --pretty output).
2
+ import { isBlank } from './util.mjs';
3
+
4
+ const PLAIN = /\(\d+,\d+\): error TS\d+/;
5
+ const PRETTY = /:\d+:\d+ - error TS\d+/;
6
+ const BARE = /^\s*error TS\d+:/;
7
+ const FOUND = /^Found \d+ errors?\b/;
8
+
9
+ export default {
10
+ name: 'tsc',
11
+ hint: /\btsc\b|\btypecheck\b|\bvue-tsc\b/,
12
+ detect(lines) {
13
+ for (const l of lines) {
14
+ if (PLAIN.test(l) || PRETTY.test(l)) return 0.95;
15
+ }
16
+ for (const l of lines) if (BARE.test(l)) return 0.7;
17
+ return 0;
18
+ },
19
+ blocks(lines) {
20
+ const out = [];
21
+ for (let i = 0; i < lines.length; i++) {
22
+ const l = lines[i];
23
+ if (FOUND.test(l)) {
24
+ out.push({ start: i, end: i, priority: 2 });
25
+ continue;
26
+ }
27
+ if (!(PLAIN.test(l) || PRETTY.test(l) || BARE.test(l))) continue;
28
+ let end = i;
29
+ if (PRETTY.test(l)) {
30
+ // code frame: blank, `12 code`, ` ~~~`, blank, maybe related info
31
+ let j = i + 1;
32
+ for (; j < lines.length; j++) {
33
+ const n = lines[j];
34
+ if (PRETTY.test(n) || PLAIN.test(n) || FOUND.test(n)) break;
35
+ if (isBlank(n)) {
36
+ if (isBlank(lines[j + 1])) break;
37
+ continue;
38
+ }
39
+ if (/^\S/.test(n) && !/^\d+\s/.test(n) && !/^\s*~/.test(n)) break;
40
+ end = j;
41
+ }
42
+ } else {
43
+ // continuation: indented lines (elaborations of the message)
44
+ let j = i + 1;
45
+ while (j < lines.length && /^\s+\S/.test(lines[j]) && !PLAIN.test(lines[j]) && !BARE.test(lines[j])) {
46
+ end = j;
47
+ j++;
48
+ }
49
+ }
50
+ out.push({ start: i, end, priority: 1 });
51
+ i = end;
52
+ }
53
+ return out;
54
+ },
55
+ };
@@ -0,0 +1,52 @@
1
+ // Shared helpers for parsers. Blocks are {start, end, priority} with 0-based
2
+ // inclusive line indices.
3
+
4
+ export const isBlank = (l) => l === undefined || l.trim() === '';
5
+
6
+ export function countMatches(lines, re, limit = Infinity) {
7
+ let n = 0;
8
+ for (const l of lines) {
9
+ if (re.test(l)) {
10
+ n++;
11
+ if (n >= limit) break;
12
+ }
13
+ }
14
+ return n;
15
+ }
16
+
17
+ export function anyMatch(lines, re) {
18
+ for (const l of lines) if (re.test(l)) return true;
19
+ return false;
20
+ }
21
+
22
+ export function indexOfMatch(lines, re, from = 0) {
23
+ for (let i = from; i < lines.length; i++) if (re.test(lines[i])) return i;
24
+ return -1;
25
+ }
26
+
27
+ // Index of the line before the first blank-blank (two consecutive blank
28
+ // lines) at or after `from`, or of the last line.
29
+ export function untilBlankBlank(lines, from, max = Infinity) {
30
+ let i = from;
31
+ const stop = Math.min(lines.length - 1, from + max - 1);
32
+ for (; i < stop; i++) {
33
+ if (isBlank(lines[i + 1]) && isBlank(lines[i + 2])) return i;
34
+ }
35
+ return stop;
36
+ }
37
+
38
+ export function trimBlankEnd(lines, start, end) {
39
+ while (end > start && isBlank(lines[end])) end--;
40
+ return end;
41
+ }
42
+
43
+ export const indentOf = (l) => /^\s*/.exec(l)[0].length;
44
+
45
+ // Line-wise blocks: each matching line (plus `before`/`after` context lines).
46
+ export function lineBlocks(lines, re, { before = 0, after = 0, priority = 1 } = {}) {
47
+ const out = [];
48
+ for (let i = 0; i < lines.length; i++) {
49
+ if (re.test(lines[i])) out.push({ start: Math.max(0, i - before), end: Math.min(lines.length - 1, i + after), priority });
50
+ }
51
+ return out;
52
+ }
@@ -0,0 +1,64 @@
1
+ // extractWarnings(text, opts) -> string. Pure: no I/O.
2
+ // Returns '' when there are no warnings, else the section printed under `0`.
3
+ import { toLines, cutLine } from './clean.mjs';
4
+
5
+ const WARN = /\bwarn(ing)?\b|WARN|deprecat|⚠/i;
6
+ const ZERO = /\b(0|no) warnings?\b/i;
7
+ const FLAG = /(^|\s)--?[\w-]*(warn|deprecat)[\w=,-]*/gi;
8
+
9
+ export function isWarning(line) {
10
+ if (!WARN.test(line)) return false;
11
+ if (ZERO.test(line)) return false;
12
+ // `--warning`-style flags echoed in commands: ignore the flag text itself
13
+ const stripped = line.replace(FLAG, ' ');
14
+ return WARN.test(stripped);
15
+ }
16
+
17
+ export function normalizeWarning(line) {
18
+ return line
19
+ .trim()
20
+ .replace(/\b(?=[0-9a-f]*\d)[0-9a-f]{6,}\b/gi, 'H')
21
+ .replace(/\d+:\d+(?::\d+)?/g, 'N:N')
22
+ .replace(/\d+/g, 'N');
23
+ }
24
+
25
+ export function groupWarnings(lines) {
26
+ const groups = new Map();
27
+ let total = 0;
28
+ lines.forEach((l, i) => {
29
+ if (!isWarning(l)) return;
30
+ total++;
31
+ const key = normalizeWarning(l);
32
+ let g = groups.get(key);
33
+ if (!g) groups.set(key, (g = { key, first: l, lines: [] }));
34
+ g.lines.push(i + 1);
35
+ });
36
+ return { total, groups: [...groups.values()] };
37
+ }
38
+
39
+ const MAX_REFS = 3;
40
+
41
+ export function extractWarnings(text, { budgetChars = 6000, logPath = '' } = {}) {
42
+ const { total, groups } = groupWarnings(toLines(text));
43
+ if (!total) return '';
44
+ const out = [`warnings: ${total} (${groups.length} unique) log=${logPath}`];
45
+ let used = out[0].length;
46
+ const reserve = 60;
47
+ let shown = 0;
48
+ for (const g of groups) {
49
+ let line;
50
+ if (g.lines.length === 1) line = `L${g.lines[0]}: ${cutLine(g.first.trim())}`;
51
+ else {
52
+ const refs = g.lines.slice(0, MAX_REFS).map((n) => `L${n}`).join(',') + (g.lines.length > MAX_REFS ? ',…' : '');
53
+ line = `${g.lines.length}× ${refs}: ${cutLine(g.key)}`;
54
+ }
55
+ if (used + 1 + line.length > budgetChars - reserve) break;
56
+ out.push(line);
57
+ used += 1 + line.length;
58
+ shown++;
59
+ }
60
+ if (shown < groups.length) out.push(`… +${groups.length - shown} more unique warnings`);
61
+ let s = out.join('\n');
62
+ if (s.length > budgetChars) s = s.slice(0, budgetChars - 1) + '…';
63
+ return s;
64
+ }