pi-lens 3.8.65 → 3.8.66
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/CHANGELOG.md +34 -0
- package/dist/clients/biome-client.js +10 -2
- package/dist/clients/bootstrap.js +1 -3
- package/dist/clients/complexity-client.js +397 -623
- package/dist/clients/dependency-checker.js +15 -2
- package/dist/clients/deps/vscode-jsonrpc.js +1 -1
- package/dist/clients/dispatch/dispatcher.js +3 -3
- package/dist/clients/dispatch/fact-runner.js +9 -57
- package/dist/clients/dispatch/facts/comment-facts.js +12 -25
- package/dist/clients/dispatch/facts/function-facts.js +161 -132
- package/dist/clients/dispatch/facts/import-facts.js +164 -117
- package/dist/clients/dispatch/facts/tree-sitter-facts.js +34 -0
- package/dist/clients/dispatch/facts/try-catch-facts.js +62 -68
- package/dist/clients/dispatch/integration.js +19 -9
- package/dist/clients/dispatch/plan.js +0 -49
- package/dist/clients/dispatch/priorities.js +0 -1
- package/dist/clients/dispatch/rules/cors-wildcard.js +54 -0
- package/dist/clients/dispatch/rules/high-import-coupling.js +37 -0
- package/dist/clients/dispatch/rules/no-commented-credentials.js +52 -0
- package/dist/clients/dispatch/runners/index.js +2 -7
- package/dist/clients/dispatch/runners/lsp.js +1 -1
- package/dist/clients/dispatch/runners/tree-sitter.js +4 -62
- package/dist/clients/dispatch/runners/utils/runner-helpers.js +28 -33
- package/dist/clients/dispatch/tool-profile.js +0 -1
- package/dist/clients/dispatch/types.js +1 -1
- package/dist/clients/formatters.js +10 -0
- package/dist/clients/git-guard.js +1 -1
- package/dist/clients/grammar-source.js +65 -2
- package/dist/clients/jscpd-client.js +8 -2
- package/dist/clients/language-policy.js +2 -2
- package/dist/clients/lsp/client.js +74 -11
- package/dist/clients/lsp/launch.js +3 -32
- package/dist/clients/mcp/session.js +0 -1
- package/dist/clients/module-report.js +16 -5
- package/dist/clients/package-manager.js +65 -0
- package/dist/clients/project-diagnostics/extractors.js +85 -0
- package/dist/clients/project-diagnostics/runner-adapters/dead-code.js +44 -0
- package/dist/clients/project-diagnostics/runner-adapters/gitleaks.js +25 -0
- package/dist/clients/project-diagnostics/runner-adapters/govulncheck.js +34 -0
- package/dist/clients/project-diagnostics/runner-adapters/jscpd.js +38 -0
- package/dist/clients/project-diagnostics/runner-adapters/madge.js +47 -0
- package/dist/clients/project-diagnostics/runner-adapters/trivy.js +31 -0
- package/dist/clients/project-diagnostics/scanner.js +3 -8
- package/dist/clients/review-graph/builder.js +21 -12
- package/dist/clients/runtime-session.js +28 -2
- package/dist/clients/runtime-turn.js +4 -7
- package/dist/clients/sg-runner.js +15 -0
- package/dist/clients/test-runner-client.js +12 -6
- package/dist/clients/tree-sitter-cache.js +47 -14
- package/dist/clients/tree-sitter-client.js +9 -1
- package/dist/clients/tree-sitter-shared.js +113 -0
- package/dist/clients/tree-sitter-symbol-extractor.js +19 -12
- package/dist/index.js +4 -22
- package/dist/tools/lens-diagnostics.js +51 -8
- package/grammars/tree-sitter-yaml.wasm +0 -0
- package/grammars/tree-sitter-yaml.wasm.json +3 -3
- package/package.json +4 -3
- package/scripts/download-grammars.js +29 -4
- package/scripts/grammars.lock.json +14 -2
- package/scripts/install-selftest.mjs +8 -7
- package/dist/clients/deps/typescript.js +0 -15
- package/dist/clients/dispatch/rules/quality-rules.js +0 -297
- package/dist/clients/dispatch/rules/sonar-rules.js +0 -493
- package/dist/clients/dispatch/runners/biome.js +0 -69
- package/dist/clients/dispatch/runners/python-slop.js +0 -106
- package/dist/clients/dispatch/runners/ts-lsp.js +0 -109
- package/dist/clients/runner-tracker.js +0 -153
- package/dist/clients/ts-service.js +0 -130
- package/dist/clients/type-coverage-client.js +0 -128
- package/dist/clients/typescript-client.js +0 -509
- package/dist/commands/booboo.js +0 -1412
- package/rules/python-slop-rules/.sgconfig.yml +0 -4
- package/rules/python-slop-rules/rules/slop-rules.yml +0 -647
|
@@ -1,114 +1,371 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Complexity Metrics Client for pi-lens
|
|
2
|
+
* Complexity Metrics Client for pi-lens
|
|
3
3
|
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
4
|
+
* Language-agnostic AST-based code complexity metrics, computed over the shared
|
|
5
|
+
* tree-sitter client (#402 — no `typescript` compiler dependency). Supported
|
|
6
|
+
* grammars are keyed in LANGUAGE_NODES (JS/TS, Python, Go, Rust today; adding a
|
|
7
|
+
* language is one table entry).
|
|
6
8
|
*
|
|
7
|
-
* Tracks:
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
* - Cyclomatic Complexity: Independent code paths (M = E - N + 2P)
|
|
11
|
-
* - Cognitive Complexity: Human understanding difficulty
|
|
12
|
-
* - Halstead Volume: Vocabulary-based complexity
|
|
13
|
-
* - Maintainability Index: Composite score (0-100, higher is better)
|
|
14
|
-
*
|
|
15
|
-
* These are silent metrics shown in session summary.
|
|
9
|
+
* Tracks: max nesting depth, function length, cyclomatic + cognitive complexity,
|
|
10
|
+
* maintainability index (Halstead-free), LOC/comments, code entropy, and AI-slop
|
|
11
|
+
* indicators. These are silent metrics surfaced in the session summary.
|
|
16
12
|
*/
|
|
17
13
|
import * as fs from "node:fs";
|
|
18
14
|
import * as path from "node:path";
|
|
19
|
-
import {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
15
|
+
import { firstChildOfType, parseTreeSitterRoot, resolveTreeSitterLanguage, walk, } from "./tree-sitter-shared.js";
|
|
16
|
+
// JS/TS/JSX share one grammar shape.
|
|
17
|
+
const JSTS = {
|
|
18
|
+
functionLike: new Set([
|
|
19
|
+
"function_declaration",
|
|
20
|
+
"method_definition",
|
|
21
|
+
"function_expression",
|
|
22
|
+
"arrow_function",
|
|
23
|
+
"generator_function",
|
|
24
|
+
"generator_function_declaration",
|
|
25
|
+
]),
|
|
26
|
+
nesting: new Set([
|
|
27
|
+
"if_statement",
|
|
28
|
+
"while_statement",
|
|
29
|
+
"for_statement",
|
|
30
|
+
"for_in_statement",
|
|
31
|
+
"switch_statement",
|
|
32
|
+
"function_declaration",
|
|
33
|
+
"function_expression",
|
|
34
|
+
"arrow_function",
|
|
35
|
+
"method_definition",
|
|
36
|
+
"class_declaration",
|
|
37
|
+
"try_statement",
|
|
38
|
+
"catch_clause",
|
|
39
|
+
]),
|
|
40
|
+
decision: new Set([
|
|
41
|
+
"if_statement",
|
|
42
|
+
"while_statement",
|
|
43
|
+
"for_statement",
|
|
44
|
+
"for_in_statement",
|
|
45
|
+
"switch_case",
|
|
46
|
+
"ternary_expression",
|
|
47
|
+
]),
|
|
48
|
+
cognitive: new Set([
|
|
49
|
+
"if_statement",
|
|
50
|
+
"while_statement",
|
|
51
|
+
"for_statement",
|
|
52
|
+
"for_in_statement",
|
|
53
|
+
"switch_statement",
|
|
54
|
+
"switch_case",
|
|
55
|
+
"ternary_expression",
|
|
56
|
+
"catch_clause",
|
|
57
|
+
]),
|
|
58
|
+
logicalOpNodes: new Set(),
|
|
59
|
+
logicalBinaryOps: new Set(["&&", "||", "??"]),
|
|
60
|
+
tryNode: "try_statement",
|
|
61
|
+
nameChildTypes: ["identifier", "property_identifier"],
|
|
62
|
+
};
|
|
63
|
+
const PYTHON = {
|
|
64
|
+
functionLike: new Set(["function_definition", "lambda"]),
|
|
65
|
+
nesting: new Set([
|
|
66
|
+
"if_statement",
|
|
67
|
+
"for_statement",
|
|
68
|
+
"while_statement",
|
|
69
|
+
"match_statement",
|
|
70
|
+
"function_definition",
|
|
71
|
+
"class_definition",
|
|
72
|
+
"try_statement",
|
|
73
|
+
"except_clause",
|
|
74
|
+
"elif_clause",
|
|
75
|
+
]),
|
|
76
|
+
decision: new Set([
|
|
77
|
+
"if_statement",
|
|
78
|
+
"elif_clause",
|
|
79
|
+
"for_statement",
|
|
80
|
+
"while_statement",
|
|
81
|
+
"case_clause",
|
|
82
|
+
"conditional_expression",
|
|
83
|
+
]),
|
|
84
|
+
cognitive: new Set([
|
|
85
|
+
"if_statement",
|
|
86
|
+
"elif_clause",
|
|
87
|
+
"for_statement",
|
|
88
|
+
"while_statement",
|
|
89
|
+
"match_statement",
|
|
90
|
+
"case_clause",
|
|
91
|
+
"conditional_expression",
|
|
92
|
+
"except_clause",
|
|
93
|
+
]),
|
|
94
|
+
logicalOpNodes: new Set(["boolean_operator"]),
|
|
95
|
+
logicalBinaryOps: new Set(),
|
|
96
|
+
tryNode: "try_statement",
|
|
97
|
+
nameChildTypes: ["identifier"],
|
|
98
|
+
};
|
|
99
|
+
const GO = {
|
|
100
|
+
functionLike: new Set([
|
|
101
|
+
"function_declaration",
|
|
102
|
+
"method_declaration",
|
|
103
|
+
"func_literal",
|
|
104
|
+
]),
|
|
105
|
+
nesting: new Set([
|
|
106
|
+
"if_statement",
|
|
107
|
+
"for_statement",
|
|
108
|
+
"expression_switch_statement",
|
|
109
|
+
"type_switch_statement",
|
|
110
|
+
"select_statement",
|
|
111
|
+
"function_declaration",
|
|
112
|
+
"method_declaration",
|
|
113
|
+
]),
|
|
114
|
+
decision: new Set([
|
|
115
|
+
"if_statement",
|
|
116
|
+
"for_statement",
|
|
117
|
+
"expression_case",
|
|
118
|
+
"type_case",
|
|
119
|
+
"communication_case",
|
|
120
|
+
]),
|
|
121
|
+
cognitive: new Set([
|
|
122
|
+
"if_statement",
|
|
123
|
+
"for_statement",
|
|
124
|
+
"expression_switch_statement",
|
|
125
|
+
"type_switch_statement",
|
|
126
|
+
"select_statement",
|
|
127
|
+
"expression_case",
|
|
128
|
+
"type_case",
|
|
129
|
+
"communication_case",
|
|
130
|
+
]),
|
|
131
|
+
logicalOpNodes: new Set(),
|
|
132
|
+
logicalBinaryOps: new Set(["&&", "||"]),
|
|
133
|
+
tryNode: undefined, // Go has no try/catch (error returns)
|
|
134
|
+
nameChildTypes: ["identifier", "field_identifier"],
|
|
135
|
+
};
|
|
136
|
+
const RUST = {
|
|
137
|
+
functionLike: new Set(["function_item", "closure_expression"]),
|
|
138
|
+
nesting: new Set([
|
|
139
|
+
"if_expression",
|
|
140
|
+
"while_expression",
|
|
141
|
+
"for_expression",
|
|
142
|
+
"loop_expression",
|
|
143
|
+
"match_expression",
|
|
144
|
+
"function_item",
|
|
145
|
+
]),
|
|
146
|
+
decision: new Set([
|
|
147
|
+
"if_expression",
|
|
148
|
+
"while_expression",
|
|
149
|
+
"for_expression",
|
|
150
|
+
"loop_expression",
|
|
151
|
+
"match_arm",
|
|
152
|
+
]),
|
|
153
|
+
cognitive: new Set([
|
|
154
|
+
"if_expression",
|
|
155
|
+
"while_expression",
|
|
156
|
+
"for_expression",
|
|
157
|
+
"loop_expression",
|
|
158
|
+
"match_expression",
|
|
159
|
+
"match_arm",
|
|
160
|
+
]),
|
|
161
|
+
logicalOpNodes: new Set(),
|
|
162
|
+
logicalBinaryOps: new Set(["&&", "||"]),
|
|
163
|
+
tryNode: undefined, // Rust uses Result/? — no try/catch
|
|
164
|
+
nameChildTypes: ["identifier"],
|
|
165
|
+
};
|
|
166
|
+
const LANGUAGE_NODES = {
|
|
167
|
+
typescript: JSTS,
|
|
168
|
+
tsx: JSTS,
|
|
169
|
+
javascript: JSTS,
|
|
170
|
+
python: PYTHON,
|
|
171
|
+
go: GO,
|
|
172
|
+
rust: RUST,
|
|
173
|
+
};
|
|
174
|
+
const COMMENT_TYPES = new Set(["comment", "line_comment", "block_comment"]);
|
|
175
|
+
// --- Metric helpers (module-level, node-config-driven) ---
|
|
176
|
+
function isLogicalOp(node, nodes) {
|
|
177
|
+
if (nodes.logicalOpNodes.has(node.type))
|
|
178
|
+
return true;
|
|
179
|
+
if (nodes.logicalBinaryOps.size > 0 && node.type === "binary_expression") {
|
|
180
|
+
return (node.children ?? []).some((c) => c && nodes.logicalBinaryOps.has(c.type));
|
|
181
|
+
}
|
|
182
|
+
return false;
|
|
183
|
+
}
|
|
184
|
+
/** Cyclomatic contribution of a subtree: decision points + logical operators. */
|
|
185
|
+
function subtreeCyclomatic(root, nodes) {
|
|
186
|
+
let cc = 0;
|
|
187
|
+
walk(root, (n) => {
|
|
188
|
+
if (nodes.decision.has(n.type))
|
|
189
|
+
cc++;
|
|
190
|
+
if (isLogicalOp(n, nodes))
|
|
191
|
+
cc++;
|
|
192
|
+
});
|
|
193
|
+
return cc;
|
|
194
|
+
}
|
|
195
|
+
/** Cognitive complexity (SonarSource-style: base + nesting penalty). */
|
|
196
|
+
function subtreeCognitive(node, nesting, nodes) {
|
|
197
|
+
let complexity = 0;
|
|
198
|
+
if (nodes.cognitive.has(node.type))
|
|
199
|
+
complexity += 1 + nesting;
|
|
200
|
+
// Labeled break/continue add complexity.
|
|
201
|
+
if ((node.type === "break_statement" || node.type === "continue_statement") &&
|
|
202
|
+
(node.children ?? []).some((c) => c &&
|
|
203
|
+
(c.type === "statement_identifier" ||
|
|
204
|
+
c.type === "identifier" ||
|
|
205
|
+
c.type === "label_name" ||
|
|
206
|
+
c.type === "label"))) {
|
|
207
|
+
complexity += 1 + nesting;
|
|
208
|
+
}
|
|
209
|
+
if (isLogicalOp(node, nodes))
|
|
210
|
+
complexity += 1;
|
|
211
|
+
const childNesting = nodes.nesting.has(node.type) ? nesting + 1 : nesting;
|
|
212
|
+
for (const child of node.children ?? []) {
|
|
213
|
+
if (child)
|
|
214
|
+
complexity += subtreeCognitive(child, childNesting, nodes);
|
|
215
|
+
}
|
|
216
|
+
return complexity;
|
|
217
|
+
}
|
|
218
|
+
function subtreeMaxNesting(node, currentDepth, nodes) {
|
|
219
|
+
let maxDepth = currentDepth;
|
|
220
|
+
if (nodes.nesting.has(node.type)) {
|
|
221
|
+
currentDepth++;
|
|
222
|
+
maxDepth = Math.max(maxDepth, currentDepth);
|
|
223
|
+
}
|
|
224
|
+
for (const child of node.children ?? []) {
|
|
225
|
+
if (child) {
|
|
226
|
+
maxDepth = Math.max(maxDepth, subtreeMaxNesting(child, currentDepth, nodes));
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
return maxDepth;
|
|
230
|
+
}
|
|
231
|
+
function functionName(fnNode, nodes) {
|
|
232
|
+
for (const t of nodes.nameChildTypes) {
|
|
233
|
+
const id = firstChildOfType(fnNode, t);
|
|
234
|
+
if (id)
|
|
235
|
+
return id.text;
|
|
236
|
+
}
|
|
237
|
+
return undefined;
|
|
238
|
+
}
|
|
239
|
+
function collectFunctionMetrics(root, nodes) {
|
|
240
|
+
const functions = [];
|
|
241
|
+
const visit = (node, nestingLevel) => {
|
|
242
|
+
if (nodes.functionLike.has(node.type)) {
|
|
243
|
+
const startLine = node.startPosition.row;
|
|
244
|
+
const endLine = node.endPosition.row;
|
|
245
|
+
functions.push({
|
|
246
|
+
name: functionName(node, nodes) ?? `<anonymous@L${startLine + 1}>`,
|
|
247
|
+
line: startLine + 1,
|
|
248
|
+
length: endLine - startLine + 1,
|
|
249
|
+
cyclomatic: subtreeCyclomatic(node, nodes),
|
|
250
|
+
cognitive: subtreeCognitive(node, nestingLevel, nodes),
|
|
251
|
+
nestingDepth: subtreeMaxNesting(node, 0, nodes),
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
const newNesting = nodes.nesting.has(node.type)
|
|
255
|
+
? nestingLevel + 1
|
|
256
|
+
: nestingLevel;
|
|
257
|
+
for (const child of node.children ?? []) {
|
|
258
|
+
if (child)
|
|
259
|
+
visit(child, newNesting);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
visit(root, 0);
|
|
263
|
+
return functions;
|
|
264
|
+
}
|
|
265
|
+
function countTryCatch(root, nodes) {
|
|
266
|
+
if (!nodes.tryNode)
|
|
267
|
+
return 0;
|
|
268
|
+
let count = 0;
|
|
269
|
+
walk(root, (n) => {
|
|
270
|
+
if (n.type === nodes.tryNode)
|
|
271
|
+
count++;
|
|
272
|
+
});
|
|
273
|
+
return count;
|
|
274
|
+
}
|
|
275
|
+
function countLines(content, root) {
|
|
276
|
+
const lines = content.split(/\r?\n/);
|
|
277
|
+
const commentLineSet = new Set();
|
|
278
|
+
walk(root, (n) => {
|
|
279
|
+
if (COMMENT_TYPES.has(n.type)) {
|
|
280
|
+
for (let l = n.startPosition.row; l <= n.endPosition.row; l++) {
|
|
281
|
+
commentLineSet.add(l);
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
const codeLines = lines.filter((line, i) => {
|
|
286
|
+
if (line.trim().length === 0)
|
|
287
|
+
return false;
|
|
288
|
+
if (!commentLineSet.has(i))
|
|
289
|
+
return true;
|
|
290
|
+
// Line has a comment — keep it only if code remains after stripping it.
|
|
291
|
+
const stripped = line
|
|
292
|
+
.replace(/\/\/.*$/, "")
|
|
293
|
+
.replace(/\/\*[\s\S]*?\*\//g, "")
|
|
294
|
+
.replace(/#.*$/, "")
|
|
295
|
+
.trim();
|
|
296
|
+
return stripped.length > 0;
|
|
297
|
+
}).length;
|
|
298
|
+
return { codeLines, commentLines: commentLineSet.size };
|
|
299
|
+
}
|
|
300
|
+
const AI_COMMENT_PATTERNS = [
|
|
301
|
+
/(?:🔍|✅|📝|🔧|🐛|⚠️|🚀|💡|🎯|📌|🏷️|🔑|🏗️|🧪|🗑️|🔄|♻️|📋|🔖|📊|💬|🔥|💎|⭐|🌟|🎨|🛠️)/u,
|
|
302
|
+
/(?:\/\/|#)\s*(Initialize|Setup|Clean up|Create|Define|Check if|Handle|Process|Validate|Return|Get|Set|Add|Remove|Update|Fetch)\b/i,
|
|
303
|
+
/(?:\/\/|#)\s*(This function|This method|This code|Here we|Now we)\b/i,
|
|
304
|
+
/\/\*\*?\s*(Overview|Summary|Description|Example|Usage)\s*\*?\//i,
|
|
305
|
+
];
|
|
306
|
+
function countAICommentPatterns(sourceText) {
|
|
307
|
+
let count = 0;
|
|
308
|
+
for (const line of sourceText.split(/\r?\n/)) {
|
|
309
|
+
const trimmed = line.trim();
|
|
310
|
+
if (trimmed.startsWith("//") ||
|
|
311
|
+
trimmed.startsWith("/*") ||
|
|
312
|
+
trimmed.startsWith("*") ||
|
|
313
|
+
trimmed.startsWith("#")) {
|
|
314
|
+
for (const pattern of AI_COMMENT_PATTERNS) {
|
|
315
|
+
if (pattern.test(line)) {
|
|
316
|
+
count++;
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return count;
|
|
323
|
+
}
|
|
324
|
+
function calculateCodeEntropy(sourceText) {
|
|
325
|
+
const tokens = sourceText
|
|
326
|
+
.replace(/\/\/.*/g, "")
|
|
327
|
+
.replace(/\/\*[\s\S]*?\*\//g, "")
|
|
328
|
+
.replace(/["'`][^"'`]*["'`]/g, "STR")
|
|
329
|
+
.replace(/\b\d+(\.\d+)?\b/g, "NUM")
|
|
330
|
+
.split(/[\s\n\r\t,;:()[\]{}=<>!&|+\-*/%^~?]+/)
|
|
331
|
+
.filter((t) => t.length > 0);
|
|
332
|
+
if (tokens.length === 0)
|
|
333
|
+
return 0;
|
|
334
|
+
const freq = new Map();
|
|
335
|
+
for (const token of tokens)
|
|
336
|
+
freq.set(token, (freq.get(token) || 0) + 1);
|
|
337
|
+
let entropy = 0;
|
|
338
|
+
for (const count of freq.values()) {
|
|
339
|
+
const p = count / tokens.length;
|
|
340
|
+
if (p > 0)
|
|
341
|
+
entropy -= p * Math.log2(p);
|
|
342
|
+
}
|
|
343
|
+
return entropy;
|
|
344
|
+
}
|
|
345
|
+
/**
|
|
346
|
+
* Maintainability index, Halstead-free variant:
|
|
347
|
+
* MI = max(0, (171 - 0.23·Cyclomatic - 16.2·ln(LOC)) · 100/171) + comment bonus.
|
|
348
|
+
*/
|
|
349
|
+
function calculateMaintainabilityIndex(cyclomatic, loc, comments) {
|
|
350
|
+
if (loc === 0)
|
|
351
|
+
return 100;
|
|
352
|
+
const lnLOC = Math.log(loc);
|
|
353
|
+
let mi = ((171 - 0.23 * cyclomatic - 16.2 * lnLOC) * 100) / 171;
|
|
354
|
+
const commentBonus = Math.min(10, (comments / loc) * 50);
|
|
355
|
+
mi += commentBonus;
|
|
356
|
+
return Math.max(0, Math.min(100, mi));
|
|
357
|
+
}
|
|
358
|
+
function calculateMaxParams(functions) {
|
|
359
|
+
// Estimate from average function length (kept from the original heuristic).
|
|
360
|
+
return Math.min(10, Math.max(2, Math.round(functions.reduce((a, f) => a + f.length, 0) /
|
|
361
|
+
Math.max(1, functions.length) /
|
|
362
|
+
5)));
|
|
363
|
+
}
|
|
364
|
+
function countSingleUseFunctions(functions) {
|
|
365
|
+
return functions.filter((f) => f.length < 10 &&
|
|
366
|
+
f.cyclomatic <= 2 &&
|
|
367
|
+
/^(get|set|check|is|has|validate|format|parse|convert|create|make)/i.test(f.name)).length;
|
|
368
|
+
}
|
|
112
369
|
// --- Client ---
|
|
113
370
|
export class ComplexityClient {
|
|
114
371
|
log;
|
|
@@ -117,61 +374,46 @@ export class ComplexityClient {
|
|
|
117
374
|
? (msg) => console.error(`[complexity] ${msg}`)
|
|
118
375
|
: () => { };
|
|
119
376
|
}
|
|
120
|
-
/**
|
|
121
|
-
* Check if file is supported (TS/JS)
|
|
122
|
-
*/
|
|
377
|
+
/** True if the file's grammar has a complexity node mapping. */
|
|
123
378
|
isSupportedFile(filePath) {
|
|
124
|
-
|
|
379
|
+
const languageId = resolveTreeSitterLanguage(filePath);
|
|
380
|
+
return Boolean(languageId && languageId in LANGUAGE_NODES);
|
|
125
381
|
}
|
|
126
|
-
/**
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
const
|
|
131
|
-
if (!
|
|
382
|
+
/** Analyze complexity metrics for a file (null if unsupported / unparseable). */
|
|
383
|
+
async analyzeFile(filePath) {
|
|
384
|
+
const absolutePath = path.resolve(filePath);
|
|
385
|
+
const languageId = resolveTreeSitterLanguage(absolutePath);
|
|
386
|
+
const nodes = languageId ? LANGUAGE_NODES[languageId] : undefined;
|
|
387
|
+
if (!nodes)
|
|
132
388
|
return null;
|
|
389
|
+
let content;
|
|
390
|
+
let root;
|
|
133
391
|
try {
|
|
134
|
-
|
|
392
|
+
if (!fs.existsSync(absolutePath))
|
|
393
|
+
return null;
|
|
394
|
+
content = fs.readFileSync(absolutePath, "utf-8");
|
|
395
|
+
root = await parseTreeSitterRoot(absolutePath, content);
|
|
135
396
|
}
|
|
136
397
|
catch (err) {
|
|
137
|
-
this.log(`
|
|
398
|
+
this.log(`Read/parse error for ${filePath}: ${err.message}`);
|
|
138
399
|
return null;
|
|
139
400
|
}
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Read file and parse to TypeScript AST
|
|
143
|
-
*/
|
|
144
|
-
readAndParse(filePath) {
|
|
145
|
-
const absolutePath = path.resolve(filePath);
|
|
146
|
-
if (!fs.existsSync(absolutePath))
|
|
401
|
+
if (!root)
|
|
147
402
|
return null;
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
403
|
+
try {
|
|
404
|
+
return this.computeMetrics(absolutePath, content, root, nodes);
|
|
405
|
+
}
|
|
406
|
+
catch (err) {
|
|
407
|
+
this.log(`Analysis error for ${filePath}: ${err.message}`);
|
|
408
|
+
return null;
|
|
409
|
+
}
|
|
151
410
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
const
|
|
157
|
-
const lines = content.split(/\r?\n/);
|
|
158
|
-
// Line counts and function collection
|
|
159
|
-
const { codeLines, commentLines } = this.countLines(sourceFile, lines);
|
|
160
|
-
const functions = this.collectFunctionMetrics(sourceFile);
|
|
161
|
-
// File-level complexity metrics
|
|
162
|
-
const maxNestingDepth = this.calculateMaxNesting(sourceFile, 0);
|
|
163
|
-
const cognitive = this.calculateCognitiveComplexity(sourceFile);
|
|
164
|
-
const halstead = this.calculateHalsteadVolume(sourceFile);
|
|
165
|
-
// Aggregate function statistics
|
|
411
|
+
computeMetrics(absolutePath, content, root, nodes) {
|
|
412
|
+
const { codeLines, commentLines } = countLines(content, root);
|
|
413
|
+
const functions = collectFunctionMetrics(root, nodes);
|
|
414
|
+
const maxNestingDepth = subtreeMaxNesting(root, 0, nodes);
|
|
415
|
+
const cognitive = subtreeCognitive(root, 0, nodes);
|
|
166
416
|
const funcStats = this.aggregateFunctionStats(functions);
|
|
167
|
-
// Derived metrics
|
|
168
|
-
const maintainabilityIndex = this.calculateMaintainabilityIndex(halstead, funcStats.avgCyclomatic, codeLines, commentLines);
|
|
169
|
-
const codeEntropy = this.calculateCodeEntropy(content);
|
|
170
|
-
// AI slop indicators
|
|
171
|
-
const maxParamsInFunction = this.calculateMaxParams(functions);
|
|
172
|
-
const aiCommentPatterns = this.countAICommentPatterns(sourceFile);
|
|
173
|
-
const singleUseFunctions = this.countSingleUseFunctions(functions);
|
|
174
|
-
const tryCatchCount = this.countTryCatch(sourceFile);
|
|
175
417
|
return {
|
|
176
418
|
filePath: path.relative(process.cwd(), absolutePath),
|
|
177
419
|
maxNestingDepth,
|
|
@@ -181,20 +423,16 @@ export class ComplexityClient {
|
|
|
181
423
|
cyclomaticComplexity: funcStats.avgCyclomatic,
|
|
182
424
|
maxCyclomaticComplexity: funcStats.maxCyclomatic,
|
|
183
425
|
cognitiveComplexity: cognitive,
|
|
184
|
-
|
|
185
|
-
maintainabilityIndex: Math.round(maintainabilityIndex * 10) / 10,
|
|
426
|
+
maintainabilityIndex: Math.round(calculateMaintainabilityIndex(funcStats.avgCyclomatic, codeLines, commentLines) * 10) / 10,
|
|
186
427
|
linesOfCode: codeLines,
|
|
187
428
|
commentLines,
|
|
188
|
-
codeEntropy: Math.round(
|
|
189
|
-
maxParamsInFunction,
|
|
190
|
-
aiCommentPatterns,
|
|
191
|
-
singleUseFunctions,
|
|
192
|
-
tryCatchCount,
|
|
429
|
+
codeEntropy: Math.round(calculateCodeEntropy(content) * 100) / 100,
|
|
430
|
+
maxParamsInFunction: calculateMaxParams(functions),
|
|
431
|
+
aiCommentPatterns: countAICommentPatterns(content),
|
|
432
|
+
singleUseFunctions: countSingleUseFunctions(functions),
|
|
433
|
+
tryCatchCount: countTryCatch(root, nodes),
|
|
193
434
|
};
|
|
194
435
|
}
|
|
195
|
-
/**
|
|
196
|
-
* Aggregate function metrics into summary statistics
|
|
197
|
-
*/
|
|
198
436
|
aggregateFunctionStats(functions) {
|
|
199
437
|
if (functions.length === 0) {
|
|
200
438
|
return { avgLength: 0, maxLength: 0, avgCyclomatic: 1, maxCyclomatic: 1 };
|
|
@@ -209,468 +447,4 @@ export class ComplexityClient {
|
|
|
209
447
|
maxCyclomatic: Math.max(1, Math.max(...cyclomatics)),
|
|
210
448
|
};
|
|
211
449
|
}
|
|
212
|
-
/**
|
|
213
|
-
* Format metrics for display
|
|
214
|
-
*/
|
|
215
|
-
formatMetrics(metrics) {
|
|
216
|
-
const parts = [];
|
|
217
|
-
// Maintainability Index (most important)
|
|
218
|
-
let miLabel = "✗";
|
|
219
|
-
if (metrics.maintainabilityIndex >= 80)
|
|
220
|
-
miLabel = "✓";
|
|
221
|
-
else if (metrics.maintainabilityIndex >= 60)
|
|
222
|
-
miLabel = "⚠";
|
|
223
|
-
parts.push(`${miLabel} Maintainability: ${metrics.maintainabilityIndex}/100`);
|
|
224
|
-
// Complexity metrics
|
|
225
|
-
if (metrics.cyclomaticComplexity > 5 ||
|
|
226
|
-
metrics.maxCyclomaticComplexity > 10) {
|
|
227
|
-
const avg = metrics.cyclomaticComplexity;
|
|
228
|
-
const max = metrics.maxCyclomaticComplexity;
|
|
229
|
-
parts.push(` Cyclomatic: avg ${avg}, max ${max} (${metrics.functionCount} functions)`);
|
|
230
|
-
}
|
|
231
|
-
if (metrics.cognitiveComplexity > 15) {
|
|
232
|
-
parts.push(` Cognitive: ${metrics.cognitiveComplexity} (high mental complexity)`);
|
|
233
|
-
}
|
|
234
|
-
// Nesting depth
|
|
235
|
-
if (metrics.maxNestingDepth > 4) {
|
|
236
|
-
parts.push(` Max nesting: ${metrics.maxNestingDepth} levels (consider extracting)`);
|
|
237
|
-
}
|
|
238
|
-
// Code entropy (in bits, >5.5 = risky AI-induced complexity)
|
|
239
|
-
// Threshold increased from 3.5 to 5.5 to reduce false positives in tooling codebases
|
|
240
|
-
// where diverse method/variable names are naturally expected
|
|
241
|
-
if (metrics.codeEntropy > 5.5) {
|
|
242
|
-
parts.push(` Entropy: ${metrics.codeEntropy.toFixed(1)} bits (>5.5 — risky AI-induced complexity)`);
|
|
243
|
-
}
|
|
244
|
-
// Function length
|
|
245
|
-
if (metrics.maxFunctionLength > 50) {
|
|
246
|
-
parts.push(` Longest function: ${metrics.maxFunctionLength} lines (avg: ${metrics.avgFunctionLength})`);
|
|
247
|
-
}
|
|
248
|
-
// Halstead (only if notably high)
|
|
249
|
-
if (metrics.halsteadVolume > 500) {
|
|
250
|
-
parts.push(` Halstead volume: ${metrics.halsteadVolume} (high vocabulary)`);
|
|
251
|
-
}
|
|
252
|
-
return parts.length > 0
|
|
253
|
-
? `[Complexity] ${metrics.filePath}\n${parts.join("\n")}`
|
|
254
|
-
: "";
|
|
255
|
-
}
|
|
256
|
-
/**
|
|
257
|
-
* Calculate max parameters across all functions
|
|
258
|
-
*/
|
|
259
|
-
calculateMaxParams(functions) {
|
|
260
|
-
// We stored function params in the metrics during analysis
|
|
261
|
-
// For now, estimate based on function length (longer functions often have more params)
|
|
262
|
-
return Math.min(10, Math.max(2, Math.round(functions.reduce((a, f) => a + f.length, 0) /
|
|
263
|
-
Math.max(1, functions.length) /
|
|
264
|
-
5)));
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* Count AI comment patterns (emojis, boilerplate phrases)
|
|
268
|
-
*/
|
|
269
|
-
countAICommentPatterns(sourceFile) {
|
|
270
|
-
const sourceText = sourceFile.getText();
|
|
271
|
-
let count = 0;
|
|
272
|
-
const aiPatterns = [
|
|
273
|
-
/(?:🔍|✅|📝|🔧|🐛|⚠️|🚀|💡|🎯|📌|🏷️|🔑|🏗️|🧪|🗑️|🔄|♻️|📋|🔖|📊|💬|🔥|💎|⭐|🌟|🎨|🛠️)/u,
|
|
274
|
-
/\/\/\s*(Initialize|Setup|Clean up|Create|Define|Check if|Handle|Process|Validate|Return|Get|Set|Add|Remove|Update|Fetch)\b/i,
|
|
275
|
-
/\/\/\s*(This function|This method|This code|Here we|Now we)\b/i,
|
|
276
|
-
/\/\*\*?\s*(Overview|Summary|Description|Example|Usage)\s*\*?\//i,
|
|
277
|
-
];
|
|
278
|
-
const lines = sourceText.split(/\r?\n/);
|
|
279
|
-
for (const line of lines) {
|
|
280
|
-
// Only check comment lines
|
|
281
|
-
const trimmed = line.trim();
|
|
282
|
-
if (trimmed.startsWith("//") ||
|
|
283
|
-
trimmed.startsWith("/*") ||
|
|
284
|
-
trimmed.startsWith("*")) {
|
|
285
|
-
for (const pattern of aiPatterns) {
|
|
286
|
-
if (pattern.test(line)) {
|
|
287
|
-
count++;
|
|
288
|
-
break;
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
return count;
|
|
294
|
-
}
|
|
295
|
-
/**
|
|
296
|
-
* Count functions that appear to be single-use (helper patterns)
|
|
297
|
-
*/
|
|
298
|
-
countSingleUseFunctions(functions) {
|
|
299
|
-
// Heuristic: small functions (< 10 lines) with simple names are often single-use
|
|
300
|
-
const smallHelpers = functions.filter((f) => f.length < 10 &&
|
|
301
|
-
f.cyclomatic <= 2 &&
|
|
302
|
-
/^(get|set|check|is|has|validate|format|parse|convert|create|make)/i.test(f.name));
|
|
303
|
-
return smallHelpers.length;
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* Count try/catch blocks (generic error handling pattern)
|
|
307
|
-
*/
|
|
308
|
-
countTryCatch(sourceFile) {
|
|
309
|
-
let count = 0;
|
|
310
|
-
const visit = (node) => {
|
|
311
|
-
if (ts.isTryStatement(node)) {
|
|
312
|
-
count++;
|
|
313
|
-
}
|
|
314
|
-
ts.forEachChild(node, visit);
|
|
315
|
-
};
|
|
316
|
-
ts.forEachChild(sourceFile, visit);
|
|
317
|
-
return count;
|
|
318
|
-
}
|
|
319
|
-
/**
|
|
320
|
-
* Check thresholds and return actionable warnings
|
|
321
|
-
*/
|
|
322
|
-
checkThresholds(metrics) {
|
|
323
|
-
const warnings = [];
|
|
324
|
-
// TUNED: Only flag extreme cases to reduce noise
|
|
325
|
-
// MI < 30 is "critically poor" (was < 60, too aggressive)
|
|
326
|
-
if (metrics.maintainabilityIndex < 30) {
|
|
327
|
-
warnings.push(`Maintainability dropped to ${metrics.maintainabilityIndex} — extract logic into helper functions`);
|
|
328
|
-
}
|
|
329
|
-
// Cyclomatic > 20 is very high (was > 10)
|
|
330
|
-
if (metrics.cyclomaticComplexity > 20) {
|
|
331
|
-
warnings.push(`High complexity (${metrics.cyclomaticComplexity}) — use early returns or switch expressions`);
|
|
332
|
-
}
|
|
333
|
-
// Cognitive > 50 is high (was > 15, flagged almost everything)
|
|
334
|
-
if (metrics.cognitiveComplexity > 50) {
|
|
335
|
-
warnings.push(`Cognitive complexity (${metrics.cognitiveComplexity}) — simplify logic flow`);
|
|
336
|
-
}
|
|
337
|
-
// Nesting > 6 is deep (was > 4, normal for complex code)
|
|
338
|
-
if (metrics.maxNestingDepth > 6) {
|
|
339
|
-
warnings.push(`Deep nesting (${metrics.maxNestingDepth} levels) — extract nested logic into separate functions`);
|
|
340
|
-
}
|
|
341
|
-
// Entropy > 5.5 is high (was > 3.5 → 5.0, still too sensitive for tooling codebases)
|
|
342
|
-
if (metrics.codeEntropy > 5.5) {
|
|
343
|
-
warnings.push(`High entropy (${metrics.codeEntropy.toFixed(1)} bits) — follow project conventions`);
|
|
344
|
-
}
|
|
345
|
-
// Comments ratio (>60% = excessive, was > 40%)
|
|
346
|
-
const totalLines = metrics.linesOfCode + metrics.commentLines;
|
|
347
|
-
if (totalLines > 10 && metrics.commentLines / totalLines > 0.6) {
|
|
348
|
-
warnings.push(`Excessive comments (${Math.round((metrics.commentLines / totalLines) * 100)}%) — remove obvious comments`);
|
|
349
|
-
}
|
|
350
|
-
// Verbose code (long functions with low complexity = overly verbose)
|
|
351
|
-
if (metrics.avgFunctionLength > 30 && metrics.cyclomaticComplexity < 3) {
|
|
352
|
-
warnings.push(`Verbose code (avg ${Math.round(metrics.avgFunctionLength)} lines, low complexity) — simplify or extract`);
|
|
353
|
-
}
|
|
354
|
-
// AI slop: Emoji/boilerplate comments
|
|
355
|
-
if (metrics.aiCommentPatterns > 5) {
|
|
356
|
-
warnings.push(`AI-style comments (${metrics.aiCommentPatterns}) — remove hand-holding comments`);
|
|
357
|
-
}
|
|
358
|
-
// AI slop: Too many try/catch blocks (lazy error handling)
|
|
359
|
-
if (metrics.tryCatchCount > 15) {
|
|
360
|
-
warnings.push(`Many try/catch blocks (${metrics.tryCatchCount}) — consolidate error handling`);
|
|
361
|
-
}
|
|
362
|
-
// AI slop: Over-abstraction (many single-use helper functions)
|
|
363
|
-
if (metrics.singleUseFunctions > 3 && metrics.functionCount > 5) {
|
|
364
|
-
warnings.push(`Over-abstraction (${metrics.singleUseFunctions} single-use helpers) — inline or consolidate`);
|
|
365
|
-
}
|
|
366
|
-
// AI slop: Functions with too many parameters
|
|
367
|
-
if (metrics.maxParamsInFunction > 6) {
|
|
368
|
-
warnings.push(`Long parameter list (${metrics.maxParamsInFunction} params) — use options object`);
|
|
369
|
-
}
|
|
370
|
-
return warnings;
|
|
371
|
-
}
|
|
372
|
-
/**
|
|
373
|
-
* Format delta for session summary
|
|
374
|
-
*/
|
|
375
|
-
formatDelta(previous, current) {
|
|
376
|
-
const parts = [];
|
|
377
|
-
const miDelta = current.maintainabilityIndex - previous.maintainabilityIndex;
|
|
378
|
-
if (Math.abs(miDelta) > 1) {
|
|
379
|
-
const arrow = miDelta > 0 ? "↑" : "↓";
|
|
380
|
-
const sign = miDelta > 0 ? "+" : "";
|
|
381
|
-
parts.push(` ${arrow} ${current.filePath}: MI ${previous.maintainabilityIndex} → ${current.maintainabilityIndex} (${sign}${miDelta.toFixed(1)})`);
|
|
382
|
-
}
|
|
383
|
-
const cogDelta = current.cognitiveComplexity - previous.cognitiveComplexity;
|
|
384
|
-
if (Math.abs(cogDelta) > 3) {
|
|
385
|
-
const arrow = cogDelta > 0 ? "↑" : "↓";
|
|
386
|
-
const sign = cogDelta > 0 ? "+" : "";
|
|
387
|
-
parts.push(` ${arrow} ${current.filePath}: cognitive ${previous.cognitiveComplexity} → ${current.cognitiveComplexity} (${sign}${cogDelta})`);
|
|
388
|
-
}
|
|
389
|
-
return parts.join("\n");
|
|
390
|
-
}
|
|
391
|
-
// --- Private: Line Counting ---
|
|
392
|
-
countLines(sourceFile, lines) {
|
|
393
|
-
let commentLines = 0;
|
|
394
|
-
const commentPositions = new Set();
|
|
395
|
-
// Find comment positions
|
|
396
|
-
const _visitComments = (node) => {
|
|
397
|
-
ts.forEachChild(node, _visitComments);
|
|
398
|
-
};
|
|
399
|
-
// Scan for comments using text
|
|
400
|
-
const text = sourceFile.getFullText();
|
|
401
|
-
const commentRegex = /\/\/.*$|\/\*[\s\S]*?\*\//gm;
|
|
402
|
-
let match;
|
|
403
|
-
while ((match = commentRegex.exec(text)) !== null) {
|
|
404
|
-
const lineStart = text.lastIndexOf("\n", match.index) + 1;
|
|
405
|
-
const startLine = text.substring(0, lineStart).split(/\r?\n/).length - 1;
|
|
406
|
-
const endLine = text.substring(0, match.index + match[0].length).split(/\r?\n/).length -
|
|
407
|
-
1;
|
|
408
|
-
for (let i = startLine; i <= endLine; i++) {
|
|
409
|
-
commentPositions.add(i);
|
|
410
|
-
}
|
|
411
|
-
}
|
|
412
|
-
commentLines = commentPositions.size;
|
|
413
|
-
const codeLines = lines.filter((line, i) => {
|
|
414
|
-
const trimmed = line.trim();
|
|
415
|
-
if (trimmed.length === 0)
|
|
416
|
-
return false;
|
|
417
|
-
// If the line is not in commentPositions, it definitely has code
|
|
418
|
-
if (!commentPositions.has(i))
|
|
419
|
-
return true;
|
|
420
|
-
// If it IS in commentPositions, it might still have code (trailing comment)
|
|
421
|
-
// Remove the comment part and check if anything remains
|
|
422
|
-
const lineWithoutComments = line
|
|
423
|
-
.replace(/\/\/.*$/, "")
|
|
424
|
-
.replace(/\/\*[\s\S]*?\*\//g, "")
|
|
425
|
-
.trim();
|
|
426
|
-
return lineWithoutComments.length > 0;
|
|
427
|
-
}).length;
|
|
428
|
-
return { codeLines, commentLines };
|
|
429
|
-
}
|
|
430
|
-
// --- Private: Function Metrics Collection ---
|
|
431
|
-
/**
|
|
432
|
-
* Collect metrics for all functions in the source file
|
|
433
|
-
*/
|
|
434
|
-
collectFunctionMetrics(sourceFile) {
|
|
435
|
-
const functions = [];
|
|
436
|
-
this.visitFunctionMetrics(sourceFile, sourceFile, functions, 0);
|
|
437
|
-
return functions;
|
|
438
|
-
}
|
|
439
|
-
visitFunctionMetrics(node, sourceFile, functions, nestingLevel) {
|
|
440
|
-
if (FUNCTION_LIKE_NODES.has(node.kind)) {
|
|
441
|
-
const funcNode = node;
|
|
442
|
-
const startLine = sourceFile.getLineAndCharacterOfPosition(node.getStart()).line;
|
|
443
|
-
const endLine = sourceFile.getLineAndCharacterOfPosition(node.getEnd()).line;
|
|
444
|
-
const length = endLine - startLine + 1;
|
|
445
|
-
const cyclomatic = this.nodeCyclomaticComplexity(node, 0);
|
|
446
|
-
const cognitive = this.nodeCognitiveComplexity(node, nestingLevel);
|
|
447
|
-
const maxNesting = this.calculateMaxNesting(node, 0);
|
|
448
|
-
const name = funcNode.name
|
|
449
|
-
? funcNode.name.getText(sourceFile)
|
|
450
|
-
: `<anonymous@L${startLine + 1}>`;
|
|
451
|
-
functions.push({
|
|
452
|
-
name,
|
|
453
|
-
line: startLine + 1,
|
|
454
|
-
length,
|
|
455
|
-
cyclomatic,
|
|
456
|
-
cognitive,
|
|
457
|
-
nestingDepth: maxNesting,
|
|
458
|
-
});
|
|
459
|
-
}
|
|
460
|
-
// Track nesting depth changes
|
|
461
|
-
const newNesting = NESTING_NODES.has(node.kind)
|
|
462
|
-
? nestingLevel + 1
|
|
463
|
-
: nestingLevel;
|
|
464
|
-
ts.forEachChild(node, (child) => {
|
|
465
|
-
this.visitFunctionMetrics(child, sourceFile, functions, newNesting);
|
|
466
|
-
});
|
|
467
|
-
}
|
|
468
|
-
// --- Private: Max Nesting Depth ---
|
|
469
|
-
calculateMaxNesting(node, currentDepth) {
|
|
470
|
-
let maxDepth = currentDepth;
|
|
471
|
-
if (NESTING_NODES.has(node.kind)) {
|
|
472
|
-
currentDepth++;
|
|
473
|
-
maxDepth = Math.max(maxDepth, currentDepth);
|
|
474
|
-
}
|
|
475
|
-
ts.forEachChild(node, (child) => {
|
|
476
|
-
const childMax = this.calculateMaxNesting(child, currentDepth);
|
|
477
|
-
maxDepth = Math.max(maxDepth, childMax);
|
|
478
|
-
});
|
|
479
|
-
return maxDepth;
|
|
480
|
-
}
|
|
481
|
-
isLogicalOperator(node) {
|
|
482
|
-
if (node.kind === ts.SyntaxKind.BinaryExpression) {
|
|
483
|
-
const binary = node;
|
|
484
|
-
return (binary.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken ||
|
|
485
|
-
binary.operatorToken.kind === ts.SyntaxKind.BarBarToken);
|
|
486
|
-
}
|
|
487
|
-
return false;
|
|
488
|
-
}
|
|
489
|
-
nodeCyclomaticComplexity(node, complexity) {
|
|
490
|
-
// Base increment for branching nodes
|
|
491
|
-
if (CYCLOMAL_NODES.has(node.kind)) {
|
|
492
|
-
complexity++;
|
|
493
|
-
}
|
|
494
|
-
// Binary && and || add complexity
|
|
495
|
-
if (this.isLogicalOperator(node)) {
|
|
496
|
-
complexity++;
|
|
497
|
-
}
|
|
498
|
-
ts.forEachChild(node, (child) => {
|
|
499
|
-
complexity = this.nodeCyclomaticComplexity(child, complexity);
|
|
500
|
-
});
|
|
501
|
-
return complexity;
|
|
502
|
-
}
|
|
503
|
-
// --- Private: Cognitive Complexity ---
|
|
504
|
-
// Based on SonarSource's Cognitive Complexity specification
|
|
505
|
-
// Increment for: if, for, while, case, catch, conditional
|
|
506
|
-
// Additional increment for nesting
|
|
507
|
-
calculateCognitiveComplexity(node) {
|
|
508
|
-
return this.nodeCognitiveComplexity(node, 0);
|
|
509
|
-
}
|
|
510
|
-
nodeCognitiveComplexity(node, nestingDepth) {
|
|
511
|
-
let complexity = 0;
|
|
512
|
-
// Structures that contribute to cognitive complexity
|
|
513
|
-
if (COGNITIVE_NODES.has(node.kind)) {
|
|
514
|
-
// Base increment + nesting penalty
|
|
515
|
-
complexity += 1 + nestingDepth;
|
|
516
|
-
}
|
|
517
|
-
// Break/continue with label add to complexity
|
|
518
|
-
if (ts.isBreakStatement(node) || ts.isContinueStatement(node)) {
|
|
519
|
-
if (node.label) {
|
|
520
|
-
complexity += 1 + nestingDepth;
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
// Binary && and || contribute to complexity
|
|
524
|
-
if (this.isLogicalOperator(node)) {
|
|
525
|
-
complexity += 1;
|
|
526
|
-
}
|
|
527
|
-
// Calculate nesting for children
|
|
528
|
-
const increasesNesting = NESTING_NODES.has(node.kind);
|
|
529
|
-
const childNesting = increasesNesting ? nestingDepth + 1 : nestingDepth;
|
|
530
|
-
ts.forEachChild(node, (child) => {
|
|
531
|
-
complexity += this.nodeCognitiveComplexity(child, childNesting);
|
|
532
|
-
});
|
|
533
|
-
return complexity;
|
|
534
|
-
}
|
|
535
|
-
// --- Private: Halstead Volume ---
|
|
536
|
-
// V = N * log2(n) where N = total operators+operands, n = unique operators+operands
|
|
537
|
-
calculateHalsteadVolume(node) {
|
|
538
|
-
const operators = new Set();
|
|
539
|
-
const operands = new Set();
|
|
540
|
-
let totalOperators = 0;
|
|
541
|
-
let totalOperands = 0;
|
|
542
|
-
const visit = (n) => {
|
|
543
|
-
// Check if it's an operator
|
|
544
|
-
if (HALSTEAD_OPERATORS.has(n.kind)) {
|
|
545
|
-
const opText = ts.SyntaxKind[n.kind];
|
|
546
|
-
operators.add(opText);
|
|
547
|
-
totalOperators++;
|
|
548
|
-
}
|
|
549
|
-
// Check for identifiers (operands)
|
|
550
|
-
else if (ts.isIdentifier(n)) {
|
|
551
|
-
const text = n.getText();
|
|
552
|
-
// Skip keywords that are parsed as identifiers
|
|
553
|
-
if (!this.isKeyword(text)) {
|
|
554
|
-
operands.add(text);
|
|
555
|
-
totalOperands++;
|
|
556
|
-
}
|
|
557
|
-
}
|
|
558
|
-
// Check for literals (operands)
|
|
559
|
-
else if (ts.isNumericLiteral(n) ||
|
|
560
|
-
ts.isStringLiteral(n) ||
|
|
561
|
-
n.kind === ts.SyntaxKind.TrueKeyword ||
|
|
562
|
-
n.kind === ts.SyntaxKind.FalseKeyword ||
|
|
563
|
-
n.kind === ts.SyntaxKind.NullKeyword ||
|
|
564
|
-
n.kind === ts.SyntaxKind.UndefinedKeyword) {
|
|
565
|
-
const text = n.getText();
|
|
566
|
-
operands.add(text);
|
|
567
|
-
totalOperands++;
|
|
568
|
-
}
|
|
569
|
-
ts.forEachChild(n, visit);
|
|
570
|
-
};
|
|
571
|
-
visit(node);
|
|
572
|
-
const uniqueOps = operators.size + operands.size;
|
|
573
|
-
const totalOps = totalOperators + totalOperands;
|
|
574
|
-
if (uniqueOps === 0 || totalOps === 0)
|
|
575
|
-
return 0;
|
|
576
|
-
// V = N * log2(n)
|
|
577
|
-
return totalOps * Math.log2(uniqueOps);
|
|
578
|
-
}
|
|
579
|
-
/**
|
|
580
|
-
* Calculate Shannon entropy of code tokens (in bits)
|
|
581
|
-
* Uses log2 for entropy measured in bits
|
|
582
|
-
* Threshold: >5.5 bits indicates risky AI-induced complexity
|
|
583
|
-
* (Increased from 3.5 to reduce false positives in tooling codebases)
|
|
584
|
-
*/
|
|
585
|
-
calculateCodeEntropy(sourceText) {
|
|
586
|
-
// Tokenize by splitting on whitespace and common delimiters
|
|
587
|
-
const tokens = sourceText
|
|
588
|
-
.replace(/\/\/.*/g, "") // Remove single-line comments
|
|
589
|
-
.replace(/\/\*[\s\S]*?\*\//g, "") // Remove multi-line comments
|
|
590
|
-
.replace(/["'`][^"'`]*["'`]/g, "STR") // Normalize strings
|
|
591
|
-
.replace(/\b\d+(\.\d+)?\b/g, "NUM") // Normalize numbers
|
|
592
|
-
.split(/[\s\n\r\t,;:()[\]{}=<>!&|+\-*/%^~?]+/)
|
|
593
|
-
.filter((t) => t.length > 0);
|
|
594
|
-
if (tokens.length === 0)
|
|
595
|
-
return 0;
|
|
596
|
-
// Count token frequencies
|
|
597
|
-
const freq = new Map();
|
|
598
|
-
for (const token of tokens) {
|
|
599
|
-
freq.set(token, (freq.get(token) || 0) + 1);
|
|
600
|
-
}
|
|
601
|
-
// Calculate Shannon entropy in bits: H = -sum(p * log2(p))
|
|
602
|
-
let entropy = 0;
|
|
603
|
-
for (const count of Array.from(freq.values())) {
|
|
604
|
-
const p = count / tokens.length;
|
|
605
|
-
if (p > 0) {
|
|
606
|
-
entropy -= p * Math.log2(p);
|
|
607
|
-
}
|
|
608
|
-
}
|
|
609
|
-
return entropy; // Return in bits, not normalized
|
|
610
|
-
}
|
|
611
|
-
isKeyword(text) {
|
|
612
|
-
const keywords = new Set([
|
|
613
|
-
"if",
|
|
614
|
-
"else",
|
|
615
|
-
"for",
|
|
616
|
-
"while",
|
|
617
|
-
"do",
|
|
618
|
-
"switch",
|
|
619
|
-
"case",
|
|
620
|
-
"break",
|
|
621
|
-
"continue",
|
|
622
|
-
"return",
|
|
623
|
-
"throw",
|
|
624
|
-
"try",
|
|
625
|
-
"catch",
|
|
626
|
-
"finally",
|
|
627
|
-
"class",
|
|
628
|
-
"extends",
|
|
629
|
-
"super",
|
|
630
|
-
"import",
|
|
631
|
-
"export",
|
|
632
|
-
"default",
|
|
633
|
-
"from",
|
|
634
|
-
"as",
|
|
635
|
-
"const",
|
|
636
|
-
"let",
|
|
637
|
-
"var",
|
|
638
|
-
"function",
|
|
639
|
-
"new",
|
|
640
|
-
"delete",
|
|
641
|
-
"typeof",
|
|
642
|
-
"void",
|
|
643
|
-
"instanceof",
|
|
644
|
-
"in",
|
|
645
|
-
"of",
|
|
646
|
-
"this",
|
|
647
|
-
"true",
|
|
648
|
-
"false",
|
|
649
|
-
"null",
|
|
650
|
-
"undefined",
|
|
651
|
-
"async",
|
|
652
|
-
"await",
|
|
653
|
-
"yield",
|
|
654
|
-
"static",
|
|
655
|
-
"get",
|
|
656
|
-
"set",
|
|
657
|
-
]);
|
|
658
|
-
return keywords.has(text);
|
|
659
|
-
}
|
|
660
|
-
// --- Private: Maintainability Index ---
|
|
661
|
-
// Microsoft's formula: MI = max(0, (171 - 5.2 * ln(Halstead) - 0.23 * Cyclomatic - 16.2 * ln(LOC)) * 100 / 171)
|
|
662
|
-
// Adjusted for comment density bonus
|
|
663
|
-
calculateMaintainabilityIndex(halstead, cyclomatic, loc, comments) {
|
|
664
|
-
if (loc === 0)
|
|
665
|
-
return 100;
|
|
666
|
-
const lnHalstead = halstead > 0 ? Math.log(halstead) : 0;
|
|
667
|
-
const lnLOC = loc > 0 ? Math.log(loc) : 0;
|
|
668
|
-
// Base MI formula
|
|
669
|
-
let mi = ((171 - 5.2 * lnHalstead - 0.23 * cyclomatic - 16.2 * lnLOC) * 100) / 171;
|
|
670
|
-
// Comment density bonus (up to +10%)
|
|
671
|
-
const commentDensity = comments / loc;
|
|
672
|
-
const commentBonus = Math.min(10, commentDensity * 50);
|
|
673
|
-
mi += commentBonus;
|
|
674
|
-
return Math.max(0, Math.min(100, mi));
|
|
675
|
-
}
|
|
676
450
|
}
|