checkchange 0.4.2 → 0.4.4
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.
- package/README.md +5 -4
- package/dist/attribution.d.ts +1 -8
- package/dist/attribution.js +15 -1
- package/dist/auto-coverage.js +7 -1
- package/dist/cache.js +62 -6
- package/dist/cli.d.ts +4 -0
- package/dist/cli.js +40 -2
- package/dist/complexity-providers/csharpComplexityProvider.d.ts +9 -0
- package/dist/complexity-providers/csharpComplexityProvider.js +142 -0
- package/dist/complexity-providers/csharpDescriptorProvider.d.ts +84 -0
- package/dist/complexity-providers/csharpDescriptorProvider.js +397 -0
- package/dist/complexity-providers/csharpFallbackParser.d.ts +17 -0
- package/dist/complexity-providers/csharpFallbackParser.js +449 -0
- package/dist/complexity-providers/pythonASTComplexityProvider.js +18 -1
- package/dist/complexity.d.ts +51 -0
- package/dist/complexity.js +38 -0
- package/dist/coverage-providers/coberturaProvider.d.ts +28 -0
- package/dist/coverage-providers/coberturaProvider.js +188 -0
- package/dist/coverage.d.ts +87 -0
- package/dist/coverage.js +326 -10
- package/dist/crapCalc.d.ts +1 -1
- package/dist/crapCalc.js +1 -1
- package/dist/evidence.d.ts +2 -2
- package/dist/evidence.js +46 -15
- package/dist/help.js +4 -3
- package/dist/providers/config.js +44 -0
- package/dist/providers/prepare.d.ts +1 -0
- package/dist/providers/prepare.js +59 -10
- package/dist/rules.d.ts +1 -1
- package/dist/rules.js +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure-TypeScript `.cs` method + approximate cyclomatic-complexity extractor.
|
|
3
|
+
*
|
|
4
|
+
* This is the FALLBACK path for C# analysis: no SDK, no `dotnet`, no Roslyn, no
|
|
5
|
+
* subprocess of any kind. The only host dependency is `node:fs/promises`
|
|
6
|
+
* `readFile`. It exists so that cycle 1 still produces COMPLETE coverage
|
|
7
|
+
* evaluation on a machine without the .NET SDK (the rich path is Task 3).
|
|
8
|
+
*
|
|
9
|
+
* Shape parity: `MethodDescriptor` and `isValidFileName` are copied
|
|
10
|
+
* field-for-field / byte-for-byte from pythonDescriptorProvider.ts:4-24 so that
|
|
11
|
+
* attribution (src/attribution.ts:143 keys descriptors by
|
|
12
|
+
* `${containerName ? containerName + '.' : ''}${functionName}:${startLine}`)
|
|
13
|
+
* and `coverageForMethods` cannot tell the two providers apart. Do NOT
|
|
14
|
+
* "improve" the guard here without changing the python one in the same commit.
|
|
15
|
+
*
|
|
16
|
+
* CC is an APPROXIMATION, by keyword counting over comment/string-stripped
|
|
17
|
+
* source: base 1, plus one per `if` / `else if` / `foreach` / `for` / `while` /
|
|
18
|
+
* `case` / `catch` / `&&` / `||` / `??` / `?:`. The per-token table and its
|
|
19
|
+
* known blind spots are pinned in test/csharp-fallback-parity.test.ts. A real
|
|
20
|
+
* AST would count `else if` once (this does too, via the `if` token) and would
|
|
21
|
+
* additionally count `when` filters, `switch` expressions and `yield`, which
|
|
22
|
+
* this does not. Divergence against the rich path is DELIBERATE and documented,
|
|
23
|
+
* not accidental.
|
|
24
|
+
*
|
|
25
|
+
* Documented limitations (all pinned or waived in the test file):
|
|
26
|
+
* - Block-bodied members AND expression-bodied METHODS (`int F() => x;`) are
|
|
27
|
+
* reported. Expression-bodied ones carry complexity 1 (the `=>` expression is
|
|
28
|
+
* never branch-parsed) and a span ending at the statement `;`, not at `{`.
|
|
29
|
+
* Expression-bodied PROPERTIES (`int P => 1;`), lambda field initialisers,
|
|
30
|
+
* abstract/interface declarations and fields are NOT methods here. Accepted
|
|
31
|
+
* blind spot: an expression-bodied member whose expression reaches a `{`
|
|
32
|
+
* before its `;` (collection initialiser, switch expression, block lambda) is
|
|
33
|
+
* not reported — the `{` severs the head before the terminator (pinned by the
|
|
34
|
+
* only-props test in test/csharp-fallback-parity.test.ts).
|
|
35
|
+
* - Generic methods (`M<T>(...)`) ARE recognised: `stripTypeParameters` (:281)
|
|
36
|
+
* removes `<...>` groups from the head prefix before the name regex (:313),
|
|
37
|
+
* so `public T Accumulate<T>(` is named `Accumulate` and gets a real span.
|
|
38
|
+
* Before this, EVERY generic head ended in `>`, the name regex rejected it,
|
|
39
|
+
* and the method was silently dropped — losing coverage attribution entirely.
|
|
40
|
+
* REMAINING limitation, by design: an UNBALANCED `<`/`>` in the head
|
|
41
|
+
* (malformed source) hands the head back unchanged, so such a head is decided
|
|
42
|
+
* exactly as it was before — including the pre-existing misname of a head like
|
|
43
|
+
* `public T Broken<T(`, which ends in the identifier `T` and has always been
|
|
44
|
+
* reported as `T`. Both are pinned as parity locks, not endorsed. A nested
|
|
45
|
+
* argument list (`Func<Func<int>>`) is NOT a blind spot: the strip counts
|
|
46
|
+
* depth, so nested groups go whole.
|
|
47
|
+
* - A recognised method body is skipped wholesale, so local functions inside it
|
|
48
|
+
* are never reported.
|
|
49
|
+
* - Code inside interpolated-string holes (`$"{x if}"`) is blanked with the
|
|
50
|
+
* rest of the string.
|
|
51
|
+
* - Constructors ARE reported, as functionName === the type name (so
|
|
52
|
+
* displayName is `Foo.Foo`). Rich-path naming may differ; that difference is
|
|
53
|
+
* in the pinned delta table.
|
|
54
|
+
* - Primary-constructor TYPE DECLARATIONS (`public record R(int X)`,
|
|
55
|
+
* `public record struct S(int Y)`, `public class C(int Z)`) are containers,
|
|
56
|
+
* not methods: their members are reported normally and no phantom descriptor
|
|
57
|
+
* named after the type is emitted. `isMethodHead` returns null for them, so
|
|
58
|
+
* the head reaches the CONTAINER_RE branch instead of the pushMember branch.
|
|
59
|
+
* `record struct X` captures `X`, not `struct`.
|
|
60
|
+
* - Malformed source degrades to whatever was recognised before the imbalance; a
|
|
61
|
+
* file with no recognisable method yields `[]`. Degradation is NON-THROWING
|
|
62
|
+
* but not faithful, and the deviation is worse than "stopped early": an
|
|
63
|
+
* unterminated block-comment opener pairs with the next block-comment closer
|
|
64
|
+
* ANYWHERE later in the file — even one sitting inside a string literal on a
|
|
65
|
+
* later line — so real code after it is retroactively blanked and reported
|
|
66
|
+
* spans shift. An unbalanced `[` in an attribute list is dropped whole (never
|
|
67
|
+
* a method). Only an unreadable path (missing file, bad encoding) THROWS — that
|
|
68
|
+
* is a caller bug, not unparseable content.
|
|
69
|
+
*/
|
|
70
|
+
import { readFile } from 'node:fs/promises';
|
|
71
|
+
import { basename } from 'node:path';
|
|
72
|
+
function isValidFileName(file) {
|
|
73
|
+
const baseName = basename(file);
|
|
74
|
+
const regex = /^[a-zA-Z0-9._/-]+$/;
|
|
75
|
+
return regex.test(baseName) && !baseName.includes('..') && !baseName.includes('\n') && !baseName.includes(';') && !baseName.includes('|');
|
|
76
|
+
}
|
|
77
|
+
// ponytail: keyword denylist, not a C# grammar. A head starting with one of
|
|
78
|
+
// these is a statement/expression, never a member declaration.
|
|
79
|
+
const STATEMENT_KEYWORDS = new Set([
|
|
80
|
+
'add', 'and', 'as', 'async', 'await', 'base', 'break', 'case', 'catch', 'checked', 'class', 'const', 'continue',
|
|
81
|
+
'default', 'delegate', 'do', 'else', 'enum', 'event', 'explicit', 'fixed', 'finally', 'for', 'foreach', 'from',
|
|
82
|
+
'get', 'goto', 'group', 'if', 'implicit', 'in', 'init', 'interface', 'into', 'is', 'join', 'let', 'lock', 'namespace',
|
|
83
|
+
'new', 'not', 'on', 'operator', 'or', 'orderby', 'out', 'params', 'record', 'ref', 'remove', 'return', 'select', 'set',
|
|
84
|
+
'sizeof', 'stackalloc', 'struct', 'switch', 'this', 'throw', 'try', 'typeof', 'unchecked', 'unsafe', 'using',
|
|
85
|
+
'value', 'var', 'when', 'where', 'while', 'with', 'yield',
|
|
86
|
+
]);
|
|
87
|
+
// The `record (struct|class)` alternative MUST come first: C# `record struct X`
|
|
88
|
+
// is a type declaration named `X`, and the plain `record` alternative would
|
|
89
|
+
// otherwise match first and capture `struct` as the container name.
|
|
90
|
+
const CONTAINER_RE = /\b(?:record\s+(?:struct|class)|class|struct|interface|record|enum)\s+([A-Za-z_]\w*)/;
|
|
91
|
+
// One pass, ordered alternation. Word-boundary keywords first (so `foreach` is
|
|
92
|
+
// not also counted as `for`), then the boolean operators, then `??`, then the
|
|
93
|
+
// ternary.
|
|
94
|
+
//
|
|
95
|
+
// The ternary alternative requires a `:` before the next `;`/`{`/`}` so a
|
|
96
|
+
// nullable-type annotation (`int? x`) with no colon in the statement is not
|
|
97
|
+
// counted, and `(?![?.])` excludes null-conditional access (`a?.b`). The
|
|
98
|
+
// lookbehind `(?<![)\]\w])` additionally excludes annotations directly after an
|
|
99
|
+
// identifier, `)`, `]` or `>` — e.g. `List<int?> x` — where the colon of a LATER
|
|
100
|
+
// ternary in the same statement would otherwise be reached. The cost of that
|
|
101
|
+
// guard is a documented false negative for a space-less ternary whose condition
|
|
102
|
+
// ends in an identifier, `)` or `]`: `x==1?a:b`. False negatives are preferred
|
|
103
|
+
// here because a false positive inflates CC against the 30/15 thresholds.
|
|
104
|
+
//
|
|
105
|
+
// `#if` preprocessor lines ARE counted as `if`: they are compile-time
|
|
106
|
+
// decisions. This is a deliberate divergence from a Roslyn AST walk, which
|
|
107
|
+
// ignores preprocessor conditionals.
|
|
108
|
+
const DECISION_RE = /\b(?:if|foreach|for|while|case|catch)\b|&&|\|\||\?\?|(?<![)\]\w])\?(?![?.])[^;{}]*?:/g;
|
|
109
|
+
/** Replace comment and string/char-literal content with spaces, preserving every offset. */
|
|
110
|
+
function blankNonCode(src) {
|
|
111
|
+
const out = [];
|
|
112
|
+
let i = 0;
|
|
113
|
+
while (i < src.length) {
|
|
114
|
+
if (src.startsWith('//', i)) {
|
|
115
|
+
const nl = src.indexOf('\n', i);
|
|
116
|
+
const end = nl === -1 ? src.length : nl;
|
|
117
|
+
out.push(blanks(src.slice(i, end)));
|
|
118
|
+
i = end;
|
|
119
|
+
}
|
|
120
|
+
else if (src.startsWith('/*', i)) {
|
|
121
|
+
const close = src.indexOf('*/', i + 2);
|
|
122
|
+
const end = close === -1 ? src.length : close + 2;
|
|
123
|
+
out.push(blanks(src.slice(i, end)));
|
|
124
|
+
i = end;
|
|
125
|
+
}
|
|
126
|
+
else {
|
|
127
|
+
const ch = src[i] ?? '';
|
|
128
|
+
if (ch === '"' || ch === "'") {
|
|
129
|
+
// Verbatim prefix is `@`, `$@` or `@$` — all three are verbatim (raw
|
|
130
|
+
// `@` is an identifier sigil, `$` interpolation is compatible with
|
|
131
|
+
// verbatim, so `"` after `$` is NOT verbatim).
|
|
132
|
+
const verbatim = ch === '"' && /@\$?|\$@$/.test(src.slice(Math.max(0, i - 2), i));
|
|
133
|
+
let j = i + 1;
|
|
134
|
+
while (j < src.length) {
|
|
135
|
+
const cur = src[j];
|
|
136
|
+
if (!verbatim && cur === '\\') {
|
|
137
|
+
j += 2;
|
|
138
|
+
continue;
|
|
139
|
+
}
|
|
140
|
+
if (cur === ch) {
|
|
141
|
+
if (verbatim && src[j + 1] === '"') {
|
|
142
|
+
j += 2;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
j++;
|
|
146
|
+
break;
|
|
147
|
+
}
|
|
148
|
+
if (ch === "'" && cur === '\n')
|
|
149
|
+
break; // unterminated char literal
|
|
150
|
+
j++;
|
|
151
|
+
}
|
|
152
|
+
out.push(blanks(src.slice(i, j)));
|
|
153
|
+
i = j;
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
out.push(ch);
|
|
157
|
+
i++;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return out.join('');
|
|
162
|
+
}
|
|
163
|
+
function blanks(text) {
|
|
164
|
+
return text.replace(/[^\n]/g, ' ');
|
|
165
|
+
}
|
|
166
|
+
function lineStartsOf(src) {
|
|
167
|
+
const starts = [0];
|
|
168
|
+
for (let i = 0; i < src.length; i++) {
|
|
169
|
+
if (src[i] === '\n')
|
|
170
|
+
starts.push(i + 1);
|
|
171
|
+
}
|
|
172
|
+
return starts;
|
|
173
|
+
}
|
|
174
|
+
function lineIndexAt(starts, offset) {
|
|
175
|
+
let lo = 0;
|
|
176
|
+
let hi = starts.length - 1;
|
|
177
|
+
while (lo < hi) {
|
|
178
|
+
const mid = (lo + hi + 1) >> 1;
|
|
179
|
+
if ((starts[mid] ?? 0) <= offset)
|
|
180
|
+
lo = mid;
|
|
181
|
+
else
|
|
182
|
+
hi = mid - 1;
|
|
183
|
+
}
|
|
184
|
+
return lo;
|
|
185
|
+
}
|
|
186
|
+
function matchBrace(src, open) {
|
|
187
|
+
let depth = 0;
|
|
188
|
+
for (let i = open; i < src.length; i++) {
|
|
189
|
+
const ch = src[i];
|
|
190
|
+
if (ch === '{')
|
|
191
|
+
depth++;
|
|
192
|
+
else if (ch === '}') {
|
|
193
|
+
depth--;
|
|
194
|
+
if (depth === 0)
|
|
195
|
+
return i;
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return src.length - 1;
|
|
199
|
+
}
|
|
200
|
+
/** Index of the `)` closing the `(` at `open`, or -1 when unbalanced. */
|
|
201
|
+
function matchParen(src, open) {
|
|
202
|
+
let depth = 0;
|
|
203
|
+
for (let i = open; i < src.length; i++) {
|
|
204
|
+
const ch = src[i];
|
|
205
|
+
if (ch === '(')
|
|
206
|
+
depth++;
|
|
207
|
+
else if (ch === ')') {
|
|
208
|
+
depth--;
|
|
209
|
+
if (depth === 0)
|
|
210
|
+
return i;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
return -1;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Remove leading attribute lists (`[Obsolete]`, `[Authorize(Roles = "x")]`) from
|
|
217
|
+
* a member head. Without this, a parameterised attribute's own `(` is the FIRST
|
|
218
|
+
* `(` in the head, so the member is reported under the attribute's name and the
|
|
219
|
+
* real method vanishes — which silently drops coverage attribution.
|
|
220
|
+
*
|
|
221
|
+
* Safe because `blankNonCode` runs first: every `[`/`]`/`(` inside a string or
|
|
222
|
+
* comment literal is already a space, so bracket counting cannot be fooled.
|
|
223
|
+
* An unbalanced `[` (malformed source) drops the rest of the head.
|
|
224
|
+
*/
|
|
225
|
+
function stripAttributes(head) {
|
|
226
|
+
let rest = head;
|
|
227
|
+
for (;;) {
|
|
228
|
+
const start = rest.search(/\S/);
|
|
229
|
+
if (start === -1 || rest[start] !== '[')
|
|
230
|
+
return rest;
|
|
231
|
+
let depth = 0;
|
|
232
|
+
let i = start;
|
|
233
|
+
for (; i < rest.length; i++) {
|
|
234
|
+
if (rest[i] === '[')
|
|
235
|
+
depth++;
|
|
236
|
+
else if (rest[i] === ']') {
|
|
237
|
+
depth--;
|
|
238
|
+
if (depth === 0)
|
|
239
|
+
break;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
if (i >= rest.length)
|
|
243
|
+
return '';
|
|
244
|
+
rest = rest.slice(0, start) + ' ' + rest.slice(i + 1);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Remove `<...>` groups from the part of a member head that precedes the `(`,
|
|
249
|
+
* so a generic method's name is an identifier again: `public T Accumulate<T>`
|
|
250
|
+
* becomes `public T Accumulate` and the name regex downstream matches it.
|
|
251
|
+
*
|
|
252
|
+
* Without this, EVERY generic method head ends in `>`, the name regex rejects
|
|
253
|
+
* it, and the whole method is SILENTLY DROPPED — no descriptor, no span, no
|
|
254
|
+
* coverage attribution, so a CRAPPY generic method cannot be reported at all.
|
|
255
|
+
* That is the gap this closes.
|
|
256
|
+
*
|
|
257
|
+
* Depth-counted, deliberately NOT `<.*?>`: a nested argument list
|
|
258
|
+
* (`Func<Func<int>>`) closes at the second `>`, and a non-greedy match would
|
|
259
|
+
* stop at the first and leave the method head half-stripped. Not a C# grammar —
|
|
260
|
+
* generic TYPE declarations and constraints never reach here (a constraint
|
|
261
|
+
* follows the parameter list, i.e. after the `(` this slice stops at).
|
|
262
|
+
*
|
|
263
|
+
* Safe because `blankNonCode` runs first: every `<`/`>` inside a string or
|
|
264
|
+
* comment is already a space, so bracket counting cannot be fooled.
|
|
265
|
+
* An unbalanced bracket (malformed source) hands the head back UNCHANGED, so the
|
|
266
|
+
* name regex decides as it did before this function existed — degradation, never
|
|
267
|
+
* a new phantom member.
|
|
268
|
+
*/
|
|
269
|
+
function stripTypeParameters(before) {
|
|
270
|
+
if (!before.includes('<'))
|
|
271
|
+
return before;
|
|
272
|
+
let out = '';
|
|
273
|
+
let depth = 0;
|
|
274
|
+
for (const ch of before) {
|
|
275
|
+
if (ch === '<')
|
|
276
|
+
depth++;
|
|
277
|
+
else if (ch === '>') {
|
|
278
|
+
depth--;
|
|
279
|
+
if (depth < 0)
|
|
280
|
+
return before; // stray closer: strip nothing
|
|
281
|
+
}
|
|
282
|
+
else if (depth === 0)
|
|
283
|
+
out += ch;
|
|
284
|
+
}
|
|
285
|
+
return depth === 0 ? out : before; // unclosed `<`: strip nothing
|
|
286
|
+
}
|
|
287
|
+
function signatureOf(head) {
|
|
288
|
+
const stripped = stripAttributes(head);
|
|
289
|
+
const open = stripped.indexOf('(');
|
|
290
|
+
if (open < 0)
|
|
291
|
+
return null;
|
|
292
|
+
// `=>` in the head is a method ONLY at body position: the first `(`'s
|
|
293
|
+
// balanced `)` must be immediately followed by `=>` (expression-bodied
|
|
294
|
+
// method — the member loop feeds such heads here at the statement `;`).
|
|
295
|
+
// Any other `=>` is a lambda (`Select(i => i)`, `Wrap((x) => x)`) and stays
|
|
296
|
+
// disqualified, exactly as the old blanket `stripped.includes('=>')` gate did.
|
|
297
|
+
if (stripped.includes('=>')) {
|
|
298
|
+
const close = matchParen(stripped, open);
|
|
299
|
+
if (close < 0 || !/^\s*=>/.test(stripped.slice(close + 1)))
|
|
300
|
+
return null;
|
|
301
|
+
}
|
|
302
|
+
// Strip type-parameter lists BEFORE the name regex, which needs an identifier
|
|
303
|
+
// at the end of the head. `head` itself is left untouched, so `isMethodHead`'s
|
|
304
|
+
// first-token STATEMENT_KEYWORDS read is unaffected, and the `=` / constructor
|
|
305
|
+
// gates below keep seeing the same `returnPart` decision.
|
|
306
|
+
const before = stripTypeParameters(stripped.slice(0, open).trimEnd());
|
|
307
|
+
const nameMatch = /([A-Za-z_]\w*)\s*$/.exec(before);
|
|
308
|
+
if (!nameMatch?.[1])
|
|
309
|
+
return null;
|
|
310
|
+
const name = nameMatch[1];
|
|
311
|
+
return { name, returnPart: before.slice(0, before.length - name.length).trim(), head: stripped };
|
|
312
|
+
}
|
|
313
|
+
function isMethodHead(head, containerName) {
|
|
314
|
+
// A type DECLARATION head is never a method. Checked FIRST, before any `(` /
|
|
315
|
+
// `returnPart` gate, because the gate that used to catch it read the FIRST
|
|
316
|
+
// token of the head — and on `public record CtorRecord(int X)` that is
|
|
317
|
+
// `public`, which is NOT in STATEMENT_KEYWORDS (record/class/struct ARE, but
|
|
318
|
+
// they are not first). So a primary-constructor type produced a phantom
|
|
319
|
+
// method NAMED AFTER THE TYPE and, worse, the `{` caller then did
|
|
320
|
+
// `i = close + 1` past its whole body, silently dropping every real member
|
|
321
|
+
// inside. Returning null routes the head to the CONTAINER_RE branch instead,
|
|
322
|
+
// where its members are scanned normally.
|
|
323
|
+
//
|
|
324
|
+
// ponytail: reuses CONTAINER_RE rather than a second keyword regex — the
|
|
325
|
+
// guard is exactly "this head is a type declaration AND carries a parameter
|
|
326
|
+
// list", which is the primary-constructor shape. `where T : class` and
|
|
327
|
+
// `where T : class, new()` are constrained METHOD heads and do NOT match
|
|
328
|
+
// (their `class` is followed by `,`/nothing, never by an identifier).
|
|
329
|
+
const strippedHead = stripAttributes(head);
|
|
330
|
+
if (CONTAINER_RE.test(strippedHead) && strippedHead.includes('('))
|
|
331
|
+
return null;
|
|
332
|
+
const sig = signatureOf(strippedHead);
|
|
333
|
+
if (!sig)
|
|
334
|
+
return null;
|
|
335
|
+
// Read the first token from the attribute-stripped head, not the raw one.
|
|
336
|
+
const first = /[A-Za-z_]\w*/.exec(sig.head)?.[0];
|
|
337
|
+
if (first === undefined || STATEMENT_KEYWORDS.has(first))
|
|
338
|
+
return null;
|
|
339
|
+
// ponytail: a target-typed `new()` / collection-initializer field
|
|
340
|
+
// (`static readonly Dictionary<string,int> Map = new() { ... }`) has a `(` in
|
|
341
|
+
// its head but is a field, not a method — an `=` before the name is the tell.
|
|
342
|
+
if (sig.returnPart.includes('='))
|
|
343
|
+
return null;
|
|
344
|
+
// No return type means a constructor (or a lambda — already excluded above).
|
|
345
|
+
if (!sig.returnPart)
|
|
346
|
+
return sig.name === containerName ? sig : null;
|
|
347
|
+
return sig;
|
|
348
|
+
}
|
|
349
|
+
export async function parseCsharpFileMethods(filePath) {
|
|
350
|
+
if (!isValidFileName(filePath)) {
|
|
351
|
+
throw new Error(`Invalid file name: ${filePath}`);
|
|
352
|
+
}
|
|
353
|
+
const source = blankNonCode(await readFile(filePath, 'utf8'));
|
|
354
|
+
const lineStarts = lineStartsOf(source);
|
|
355
|
+
const descriptors = [];
|
|
356
|
+
const stack = [];
|
|
357
|
+
let headStart = 0;
|
|
358
|
+
let i = 0;
|
|
359
|
+
/** Push a descriptor for the member spanning source[headStart..end] (end = `}` or `;`, inclusive). */
|
|
360
|
+
const pushMember = (sig, container, start0, end, complexity) => {
|
|
361
|
+
let start = start0;
|
|
362
|
+
while (start < end && ' \t\r\n'.includes(source[start] ?? 'x'))
|
|
363
|
+
start++;
|
|
364
|
+
const startLineIdx = lineIndexAt(lineStarts, start);
|
|
365
|
+
const endLineIdx = lineIndexAt(lineStarts, end);
|
|
366
|
+
descriptors.push({
|
|
367
|
+
functionName: sig.name,
|
|
368
|
+
containerName: container,
|
|
369
|
+
displayName: container ? `${container}.${sig.name}` : sig.name,
|
|
370
|
+
startLine: startLineIdx + 1,
|
|
371
|
+
endLine: endLineIdx + 1,
|
|
372
|
+
complexity,
|
|
373
|
+
bodySpan: {
|
|
374
|
+
startLine: startLineIdx + 1,
|
|
375
|
+
// A body that fits on ONE line anchors at column 0, not at the
|
|
376
|
+
// declaration's column. Cobertura/LCOV emit statements as whole LINES
|
|
377
|
+
// with startColumn 0 (coberturaProvider.ts:122, lcovProvider.ts:64), and
|
|
378
|
+
// the attribution join compares columns whenever the statement and the
|
|
379
|
+
// span share a start line (coverageAttribution.js:121,124 ->
|
|
380
|
+
// comparePosition returns leftColumn - rightColumn for equal lines). At
|
|
381
|
+
// the declaration column, `comparePosition(L, 12, L, 0) <= 0` was false
|
|
382
|
+
// on both containment paths, so the statement was owned by nobody and
|
|
383
|
+
// coverage came back null -> changed-function-high-crap NOT_EVALUATED ->
|
|
384
|
+
// completeness INCOMPLETE. Multi-line bodies start on a DIFFERENT line
|
|
385
|
+
// from any statement inside them, so their startColumn is never read for
|
|
386
|
+
// containment and stays exactly as measured.
|
|
387
|
+
startColumn: startLineIdx === endLineIdx ? 0 : start - (lineStarts[startLineIdx] ?? 0),
|
|
388
|
+
endLine: endLineIdx + 1,
|
|
389
|
+
endColumn: end - (lineStarts[endLineIdx] ?? 0) + 1,
|
|
390
|
+
},
|
|
391
|
+
expectsStatementCoverage: true,
|
|
392
|
+
expectsBranchCoverage: true,
|
|
393
|
+
});
|
|
394
|
+
};
|
|
395
|
+
while (i < source.length) {
|
|
396
|
+
const ch = source[i];
|
|
397
|
+
if (ch === ';') {
|
|
398
|
+
// Expression-bodied members terminate at `;` and never reach the `{`
|
|
399
|
+
// trigger below — evaluate the accumulated head HERE (both gates: this
|
|
400
|
+
// `;` trigger + signatureOf's structural `=>` check). Attribute lists are
|
|
401
|
+
// stripped first so a lambda inside an attribute (`[My(() => 1)]`) does
|
|
402
|
+
// not look like an expression body. complexity is 1: the `=>` expression
|
|
403
|
+
// is never branch-parsed.
|
|
404
|
+
const head = source.slice(headStart, i);
|
|
405
|
+
if (stripAttributes(head).includes('=>')) {
|
|
406
|
+
const container = stack.length ? (stack[stack.length - 1]?.container ?? null) : null;
|
|
407
|
+
const sig = isMethodHead(head, container);
|
|
408
|
+
if (sig)
|
|
409
|
+
pushMember(sig, container, headStart, i, 1);
|
|
410
|
+
}
|
|
411
|
+
headStart = i + 1;
|
|
412
|
+
i++;
|
|
413
|
+
continue;
|
|
414
|
+
}
|
|
415
|
+
if (ch === '}') {
|
|
416
|
+
stack.pop();
|
|
417
|
+
headStart = i + 1;
|
|
418
|
+
i++;
|
|
419
|
+
continue;
|
|
420
|
+
}
|
|
421
|
+
if (ch !== '{') {
|
|
422
|
+
i++;
|
|
423
|
+
continue;
|
|
424
|
+
}
|
|
425
|
+
const head = source.slice(headStart, i);
|
|
426
|
+
const container = stack.length ? (stack[stack.length - 1]?.container ?? null) : null;
|
|
427
|
+
// An attr-stripped `=>` in the head means an expression-bodied member whose
|
|
428
|
+
// EXPRESSION reaches this `{` (initialiser, switch arm, block lambda) —
|
|
429
|
+
// never a block-bodied method. Evaluating it here would anchor the span
|
|
430
|
+
// mid-expression and branch-count the body; the statement `;` path above
|
|
431
|
+
// owns `=>` heads. Matches the old signatureOf `=>` gate byte-for-byte.
|
|
432
|
+
const sig = stripAttributes(head).includes('=>') ? null : isMethodHead(head, container);
|
|
433
|
+
if (sig) {
|
|
434
|
+
const close = matchBrace(source, i);
|
|
435
|
+
const body = source.slice(i + 1, close);
|
|
436
|
+
pushMember(sig, container, headStart, close, 1 + (body.match(DECISION_RE)?.length ?? 0));
|
|
437
|
+
// Jump past the whole body: nested blocks and local functions are not
|
|
438
|
+
// scanned, and `endLine` is the method's own closing brace.
|
|
439
|
+
i = close + 1;
|
|
440
|
+
headStart = i;
|
|
441
|
+
continue;
|
|
442
|
+
}
|
|
443
|
+
const containerMatch = CONTAINER_RE.exec(head);
|
|
444
|
+
stack.push({ container: containerMatch?.[1] ?? container });
|
|
445
|
+
headStart = i + 1;
|
|
446
|
+
i++;
|
|
447
|
+
}
|
|
448
|
+
return descriptors;
|
|
449
|
+
}
|
|
@@ -1,6 +1,13 @@
|
|
|
1
1
|
import { spawnSync } from 'node:child_process';
|
|
2
2
|
import { basename, relative } from 'node:path';
|
|
3
3
|
import { readCoverage } from '../coverage.js';
|
|
4
|
+
// ponytail: `as const` is deliberately NOT used — ComplexityInfo.provenance is a
|
|
5
|
+
// mutable optional field, and a readonly const would not assign cleanly.
|
|
6
|
+
const PYTHON_AST_PROVENANCE = {
|
|
7
|
+
tool: 'python stdlib ast',
|
|
8
|
+
version: 'unknown',
|
|
9
|
+
mode: 'NATIVE',
|
|
10
|
+
};
|
|
4
11
|
export const pythonASTComplexityProvider = {
|
|
5
12
|
extensions: ['.py'],
|
|
6
13
|
collectComplexity: (cwd) => {
|
|
@@ -130,7 +137,17 @@ if __name__ == '__main__':
|
|
|
130
137
|
method: info.method,
|
|
131
138
|
lineStart: info.lineStart,
|
|
132
139
|
lineEnd: info.lineEnd,
|
|
133
|
-
cc: info.cc
|
|
140
|
+
cc: info.cc,
|
|
141
|
+
// GAP 8: these CC values come from the CPython `ast` module run in a
|
|
142
|
+
// `python3 -c` subprocess (pythonDescriptorProvider.ts:118), NOT from
|
|
143
|
+
// @barney-media/crap-typescript-core — which is what evidence reported
|
|
144
|
+
// before, a false provenance claim on every .py function. `version` is
|
|
145
|
+
// 'unknown' because the interpreter version is not probed: the script's
|
|
146
|
+
// stdout contract carries descriptors only, and widening it to carry a
|
|
147
|
+
// version is a bigger change than this. NATIVE, not FALLBACK — `ast` is
|
|
148
|
+
// a real AST parser for Python, so diagnostics.quality stays NATIVE for
|
|
149
|
+
// a .py run and the only delta is the honest tool name.
|
|
150
|
+
provenance: PYTHON_AST_PROVENANCE
|
|
134
151
|
});
|
|
135
152
|
}
|
|
136
153
|
}
|
package/dist/complexity.d.ts
CHANGED
|
@@ -3,12 +3,63 @@ export declare const complexityProvenance: {
|
|
|
3
3
|
readonly tool: "@barney-media/crap-typescript-core";
|
|
4
4
|
readonly version: "0.5.0";
|
|
5
5
|
};
|
|
6
|
+
/**
|
|
7
|
+
* Which analyzer measured one function, and whether it was a real analyzer for
|
|
8
|
+
* that language (`NATIVE`) or an approximation of one (`FALLBACK` — the ADR-0001
|
|
9
|
+
* term reserved at docs/decisions/terminology-reconciliation.md:68,:112).
|
|
10
|
+
*
|
|
11
|
+
* It rides ON the measurement instead of being re-derived at the consumer, so
|
|
12
|
+
* evidence.ts never learns a provider's tool name or its health from a literal
|
|
13
|
+
* (D5). A measurement that omits `provenance` entirely was produced by the
|
|
14
|
+
* default native analyzer above — that absence IS the TypeScript case.
|
|
15
|
+
*/
|
|
16
|
+
export interface MeasurementProvenance {
|
|
17
|
+
tool: string;
|
|
18
|
+
version: string;
|
|
19
|
+
mode: 'NATIVE' | 'FALLBACK';
|
|
20
|
+
/**
|
|
21
|
+
* Provider-owned machine reason code for a FALLBACK measurement, e.g.
|
|
22
|
+
* `csharp-analysis-failed`. Optional and NOT part of `source`: evidence reports
|
|
23
|
+
* only `{ tool, version }` per function (the published Source shape), while this
|
|
24
|
+
* rides the complexity LINEAGE entry — one key per run, so a consumer can tell
|
|
25
|
+
* "approximated" from "approximated because the analyzer was missing".
|
|
26
|
+
*/
|
|
27
|
+
degradation?: string;
|
|
28
|
+
}
|
|
6
29
|
export interface ComplexityInfo {
|
|
7
30
|
file: string;
|
|
8
31
|
method: string;
|
|
9
32
|
lineStart: number;
|
|
10
33
|
lineEnd: number;
|
|
11
34
|
cc: number;
|
|
35
|
+
/** Absent => measured by the default native analyzer (`complexityProvenance`). */
|
|
36
|
+
provenance?: MeasurementProvenance;
|
|
12
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* The `{ tool, version }` evidence reports for one measurement. A FACTORY, not a
|
|
40
|
+
* second const, because the answer depends on the measurement; the default it
|
|
41
|
+
* returns is `complexityProvenance` itself, so the TypeScript path is unchanged.
|
|
42
|
+
*/
|
|
43
|
+
export declare function measurementProvenance(info: ComplexityInfo | undefined): {
|
|
44
|
+
tool: string;
|
|
45
|
+
version: string;
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* ADR-0001 quality for a set of measurements: `FALLBACK` when ANY of them came
|
|
49
|
+
* from an approximation. One approximate file makes the whole stage non-native —
|
|
50
|
+
* reporting `NATIVE` over a mixed set is the false-provenance claim GAP 8 exists
|
|
51
|
+
* to remove. Absent provenance => `NATIVE`.
|
|
52
|
+
*/
|
|
53
|
+
export declare function measurementQuality(infos: ReadonlyArray<{
|
|
54
|
+
provenance?: MeasurementProvenance;
|
|
55
|
+
}>): 'NATIVE' | 'FALLBACK';
|
|
56
|
+
/**
|
|
57
|
+
* The distinct provider reason codes behind a FALLBACK stage, in first-seen order.
|
|
58
|
+
* Empty for a native or failed stage, so callers can spread the key in only when
|
|
59
|
+
* it carries something — a `.ts` run's lineage entry stays byte-identical.
|
|
60
|
+
*/
|
|
61
|
+
export declare function measurementDegradations(infos: ReadonlyArray<{
|
|
62
|
+
provenance?: MeasurementProvenance;
|
|
63
|
+
}>): string[];
|
|
13
64
|
export declare function getGitTrackedCodeFiles(cwd: string): string[];
|
|
14
65
|
export declare function collectComplexity(cwd: string, trace?: TraceRun, changedFiles?: ReadonlySet<string>): Promise<ComplexityInfo[]>;
|
package/dist/complexity.js
CHANGED
|
@@ -2,7 +2,45 @@ import { findAllTypeScriptFilesUnderSourceRoots, parseFileMethods } from '@barne
|
|
|
2
2
|
import { relative, resolve } from 'node:path';
|
|
3
3
|
import { execSync } from 'node:child_process';
|
|
4
4
|
// Provenance for the complexity lineage stage: native analyzer from the core package.
|
|
5
|
+
// RETAINED as a plain const, not folded into the factory below: cache.ts:366 reads
|
|
6
|
+
// `.version` off it and test/lineage.test.ts:43 asserts it in a wholesale list.
|
|
5
7
|
export const complexityProvenance = { tool: '@barney-media/crap-typescript-core', version: '0.5.0' };
|
|
8
|
+
/**
|
|
9
|
+
* The `{ tool, version }` evidence reports for one measurement. A FACTORY, not a
|
|
10
|
+
* second const, because the answer depends on the measurement; the default it
|
|
11
|
+
* returns is `complexityProvenance` itself, so the TypeScript path is unchanged.
|
|
12
|
+
*/
|
|
13
|
+
export function measurementProvenance(info) {
|
|
14
|
+
const p = info?.provenance ?? complexityProvenance;
|
|
15
|
+
// PROJECT, never pass the reference through. A full MeasurementProvenance also
|
|
16
|
+
// carries `mode` and `degradation`, which are STAGE-level facts; returning it as-is
|
|
17
|
+
// would widen the published per-function `source` shape from { tool, version } to
|
|
18
|
+
// five keys and stamp a provider reason code onto every single function.
|
|
19
|
+
return { tool: p.tool, version: p.version };
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* ADR-0001 quality for a set of measurements: `FALLBACK` when ANY of them came
|
|
23
|
+
* from an approximation. One approximate file makes the whole stage non-native —
|
|
24
|
+
* reporting `NATIVE` over a mixed set is the false-provenance claim GAP 8 exists
|
|
25
|
+
* to remove. Absent provenance => `NATIVE`.
|
|
26
|
+
*/
|
|
27
|
+
export function measurementQuality(infos) {
|
|
28
|
+
return infos.some((info) => info.provenance?.mode === 'FALLBACK') ? 'FALLBACK' : 'NATIVE';
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* The distinct provider reason codes behind a FALLBACK stage, in first-seen order.
|
|
32
|
+
* Empty for a native or failed stage, so callers can spread the key in only when
|
|
33
|
+
* it carries something — a `.ts` run's lineage entry stays byte-identical.
|
|
34
|
+
*/
|
|
35
|
+
export function measurementDegradations(infos) {
|
|
36
|
+
const codes = [];
|
|
37
|
+
for (const info of infos) {
|
|
38
|
+
const code = info.provenance?.degradation;
|
|
39
|
+
if (code !== undefined && !codes.includes(code))
|
|
40
|
+
codes.push(code);
|
|
41
|
+
}
|
|
42
|
+
return codes;
|
|
43
|
+
}
|
|
6
44
|
export function getGitTrackedCodeFiles(cwd) {
|
|
7
45
|
try {
|
|
8
46
|
// Get list of tracked files, one per line
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { coverageForMethods } from '@barney-media/crap-typescript-core';
|
|
2
|
+
type FileCoverage = NonNullable<Parameters<typeof coverageForMethods>[1]>;
|
|
3
|
+
/**
|
|
4
|
+
* Typed parse failure. `reason` carries the EXISTING CoverageResult.reason
|
|
5
|
+
* value ('malformed', src/coverage.ts:487) so a caller maps a Cobertura refusal
|
|
6
|
+
* onto the current taxonomy without inventing a reason string.
|
|
7
|
+
* The size/line guards throw this too, so a single `instanceof` catch covers
|
|
8
|
+
* every provider-side refusal.
|
|
9
|
+
*/
|
|
10
|
+
export declare class CoberturaParseError extends Error {
|
|
11
|
+
readonly reason: "malformed";
|
|
12
|
+
constructor(message: string);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Parse Cobertura XML content and convert it to the same Map shape the core
|
|
16
|
+
* package's parseCoverageReport produces, so coverageForMethods can attribute
|
|
17
|
+
* methods.
|
|
18
|
+
* @param coberturaContent The Cobertura XML file content as string
|
|
19
|
+
* @param cwd Current working directory for path normalization
|
|
20
|
+
* @returns Map<filePath, { statements: Array<{ span: { startLine: number, startColumn: number, endLine: number, endColumn: number }, hits: number }>, branches: [], functions: [] }>
|
|
21
|
+
* Branches and functions are always empty: a `<line branch="true">` element
|
|
22
|
+
* does NOT fabricate a branches array (same precedent as LCOV BRDA being
|
|
23
|
+
* ignored at lcovProvider.ts:70-71). Merge Cobertura with LCOV via
|
|
24
|
+
* mergeCoverageMaps rather than by concatenating raw maps.
|
|
25
|
+
* @throws {CoberturaParseError} on an oversized / over-long / malformed document
|
|
26
|
+
*/
|
|
27
|
+
export declare function parseCoberturaContent(coberturaContent: string, cwd: string): Map<string, FileCoverage>;
|
|
28
|
+
export {};
|