pi-lens 3.8.65 → 3.8.67

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/CHANGELOG.md +59 -0
  2. package/README.md +0 -1
  3. package/dist/clients/actionable-warnings-logger.js +11 -36
  4. package/dist/clients/ast-grep-tool-logger.js +11 -36
  5. package/dist/clients/biome-client.js +10 -2
  6. package/dist/clients/bootstrap.js +1 -3
  7. package/dist/clients/cascade-logger.js +7 -12
  8. package/dist/clients/complexity-client.js +397 -623
  9. package/dist/clients/dead-code-logger.js +11 -22
  10. package/dist/clients/dependency-checker.js +15 -2
  11. package/dist/clients/deps/vscode-jsonrpc.js +1 -1
  12. package/dist/clients/diagnostic-logger.js +7 -31
  13. package/dist/clients/dispatch/auxiliary-lsp.js +38 -0
  14. package/dist/clients/dispatch/dispatcher.js +4 -39
  15. package/dist/clients/dispatch/fact-runner.js +9 -57
  16. package/dist/clients/dispatch/facts/comment-facts.js +12 -25
  17. package/dist/clients/dispatch/facts/function-facts.js +161 -132
  18. package/dist/clients/dispatch/facts/import-facts.js +164 -117
  19. package/dist/clients/dispatch/facts/tree-sitter-facts.js +34 -0
  20. package/dist/clients/dispatch/facts/try-catch-facts.js +62 -68
  21. package/dist/clients/dispatch/inline-suppressions.js +61 -0
  22. package/dist/clients/dispatch/integration.js +94 -32
  23. package/dist/clients/dispatch/plan.js +0 -49
  24. package/dist/clients/dispatch/priorities.js +0 -1
  25. package/dist/clients/dispatch/rules/cors-wildcard.js +54 -0
  26. package/dist/clients/dispatch/rules/high-import-coupling.js +37 -0
  27. package/dist/clients/dispatch/rules/no-commented-credentials.js +52 -0
  28. package/dist/clients/dispatch/runners/index.js +2 -7
  29. package/dist/clients/dispatch/runners/lsp.js +18 -6
  30. package/dist/clients/dispatch/runners/tree-sitter.js +11 -64
  31. package/dist/clients/dispatch/runners/utils/runner-helpers.js +28 -33
  32. package/dist/clients/dispatch/tool-profile.js +0 -1
  33. package/dist/clients/dispatch/types.js +1 -1
  34. package/dist/clients/formatters.js +10 -0
  35. package/dist/clients/git-guard.js +1 -1
  36. package/dist/clients/grammar-source.js +65 -2
  37. package/dist/clients/jscpd-client.js +8 -2
  38. package/dist/clients/language-policy.js +2 -2
  39. package/dist/clients/latency-logger.js +10 -15
  40. package/dist/clients/lsp/client.js +74 -11
  41. package/dist/clients/lsp/config.js +61 -3
  42. package/dist/clients/lsp/index.js +43 -3
  43. package/dist/clients/lsp/launch.js +3 -32
  44. package/dist/clients/mcp/session.js +0 -1
  45. package/dist/clients/module-report.js +16 -5
  46. package/dist/clients/ndjson-logger.js +150 -0
  47. package/dist/clients/package-manager.js +65 -0
  48. package/dist/clients/pipeline.js +25 -41
  49. package/dist/clients/project-diagnostics/extractors.js +85 -0
  50. package/dist/clients/project-diagnostics/runner-adapters/dead-code.js +44 -0
  51. package/dist/clients/project-diagnostics/runner-adapters/gitleaks.js +25 -0
  52. package/dist/clients/project-diagnostics/runner-adapters/govulncheck.js +34 -0
  53. package/dist/clients/project-diagnostics/runner-adapters/jscpd.js +38 -0
  54. package/dist/clients/project-diagnostics/runner-adapters/madge.js +47 -0
  55. package/dist/clients/project-diagnostics/runner-adapters/trivy.js +31 -0
  56. package/dist/clients/project-diagnostics/scanner.js +11 -10
  57. package/dist/clients/read-guard-logger.js +11 -36
  58. package/dist/clients/review-graph/builder.js +337 -27
  59. package/dist/clients/review-graph/git-identity.js +150 -0
  60. package/dist/clients/review-graph/service.js +3 -3
  61. package/dist/clients/runtime-coordinator.js +83 -0
  62. package/dist/clients/runtime-session.js +28 -2
  63. package/dist/clients/runtime-tool-result.js +10 -2
  64. package/dist/clients/runtime-turn.js +25 -7
  65. package/dist/clients/sg-runner.js +15 -0
  66. package/dist/clients/test-runner-client.js +12 -6
  67. package/dist/clients/tree-sitter-cache.js +47 -14
  68. package/dist/clients/tree-sitter-client.js +9 -1
  69. package/dist/clients/tree-sitter-logger.js +7 -12
  70. package/dist/clients/tree-sitter-query-loader.js +1 -0
  71. package/dist/clients/tree-sitter-shared.js +113 -0
  72. package/dist/clients/tree-sitter-symbol-extractor.js +19 -12
  73. package/dist/index.js +4 -22
  74. package/dist/tools/lens-diagnostics.js +254 -20
  75. package/grammars/tree-sitter-yaml.wasm +0 -0
  76. package/grammars/tree-sitter-yaml.wasm.json +3 -3
  77. package/package.json +4 -3
  78. package/rules/ast-grep-rules/rule-tests/no-init-return-test.yml +15 -0
  79. package/rules/ast-grep-rules/rules/no-init-return.yml +11 -5
  80. package/rules/tree-sitter-queries/python/python-assert-production.yml +4 -0
  81. package/scripts/download-grammars.js +29 -4
  82. package/scripts/grammars.lock.json +14 -2
  83. package/scripts/install-selftest.mjs +8 -7
  84. package/dist/clients/deps/typescript.js +0 -15
  85. package/dist/clients/dispatch/rules/quality-rules.js +0 -297
  86. package/dist/clients/dispatch/rules/sonar-rules.js +0 -493
  87. package/dist/clients/dispatch/runners/biome.js +0 -69
  88. package/dist/clients/dispatch/runners/python-slop.js +0 -106
  89. package/dist/clients/dispatch/runners/ts-lsp.js +0 -109
  90. package/dist/clients/runner-tracker.js +0 -153
  91. package/dist/clients/ts-service.js +0 -130
  92. package/dist/clients/type-coverage-client.js +0 -128
  93. package/dist/clients/typescript-client.js +0 -509
  94. package/dist/commands/booboo.js +0 -1412
  95. package/rules/python-slop-rules/.sgconfig.yml +0 -4
  96. package/rules/python-slop-rules/rules/slop-rules.yml +0 -647
@@ -1,114 +1,371 @@
1
1
  /**
2
- * Complexity Metrics Client for pi-lens (cache test)
2
+ * Complexity Metrics Client for pi-lens
3
3
  *
4
- * Calculates AST-based code complexity metrics for TypeScript/JavaScript files.
5
- * Uses the TypeScript compiler API for parsing.
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
- * - Max Nesting Depth: Deepest control flow nesting
9
- * - Avg/Max Function Length: Lines per function
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 { ts } from "./deps/typescript.js";
20
- import { isFileKind } from "./file-kinds.js";
21
- // --- Constants ---
22
- // Nodes that increase cyclomatic complexity
23
- const CYCLOMAL_NODES = new Set([
24
- ts.SyntaxKind.IfStatement,
25
- ts.SyntaxKind.WhileStatement,
26
- ts.SyntaxKind.ForStatement,
27
- ts.SyntaxKind.ForInStatement,
28
- ts.SyntaxKind.ForOfStatement,
29
- ts.SyntaxKind.CaseClause,
30
- ts.SyntaxKind.ConditionalExpression,
31
- ts.SyntaxKind.BinaryExpression, // && and ||
32
- ]);
33
- // Nodes that increase cognitive complexity (with nesting penalty)
34
- const COGNITIVE_NODES = new Set([
35
- ts.SyntaxKind.IfStatement,
36
- ts.SyntaxKind.WhileStatement,
37
- ts.SyntaxKind.ForStatement,
38
- ts.SyntaxKind.ForInStatement,
39
- ts.SyntaxKind.ForOfStatement,
40
- ts.SyntaxKind.SwitchStatement,
41
- ts.SyntaxKind.CaseClause,
42
- ts.SyntaxKind.ConditionalExpression,
43
- ts.SyntaxKind.CatchClause,
44
- ]);
45
- // Nesting-increasing nodes
46
- const NESTING_NODES = new Set([
47
- ts.SyntaxKind.IfStatement,
48
- ts.SyntaxKind.WhileStatement,
49
- ts.SyntaxKind.ForStatement,
50
- ts.SyntaxKind.ForInStatement,
51
- ts.SyntaxKind.ForOfStatement,
52
- ts.SyntaxKind.SwitchStatement,
53
- ts.SyntaxKind.FunctionDeclaration,
54
- ts.SyntaxKind.FunctionExpression,
55
- ts.SyntaxKind.ArrowFunction,
56
- ts.SyntaxKind.ClassDeclaration,
57
- ts.SyntaxKind.MethodDeclaration,
58
- ts.SyntaxKind.TryStatement,
59
- ts.SyntaxKind.CatchClause,
60
- ]);
61
- // Function-like nodes
62
- const FUNCTION_LIKE_NODES = new Set([
63
- ts.SyntaxKind.FunctionDeclaration,
64
- ts.SyntaxKind.FunctionExpression,
65
- ts.SyntaxKind.ArrowFunction,
66
- ts.SyntaxKind.MethodDeclaration,
67
- ts.SyntaxKind.Constructor,
68
- ts.SyntaxKind.GetAccessor,
69
- ts.SyntaxKind.SetAccessor,
70
- ]);
71
- // Halstead operators (common operators)
72
- const HALSTEAD_OPERATORS = new Set([
73
- ts.SyntaxKind.PlusToken,
74
- ts.SyntaxKind.MinusToken,
75
- ts.SyntaxKind.AsteriskToken,
76
- ts.SyntaxKind.SlashToken,
77
- ts.SyntaxKind.PercentToken,
78
- ts.SyntaxKind.AmpersandToken,
79
- ts.SyntaxKind.BarToken,
80
- ts.SyntaxKind.CaretToken,
81
- ts.SyntaxKind.LessThanToken,
82
- ts.SyntaxKind.GreaterThanToken,
83
- ts.SyntaxKind.LessThanEqualsToken,
84
- ts.SyntaxKind.GreaterThanEqualsToken,
85
- ts.SyntaxKind.EqualsEqualsToken,
86
- ts.SyntaxKind.ExclamationEqualsToken,
87
- ts.SyntaxKind.EqualsEqualsEqualsToken,
88
- ts.SyntaxKind.ExclamationEqualsEqualsToken,
89
- ts.SyntaxKind.PlusPlusToken,
90
- ts.SyntaxKind.MinusMinusToken,
91
- ts.SyntaxKind.PlusEqualsToken,
92
- ts.SyntaxKind.MinusEqualsToken,
93
- ts.SyntaxKind.AsteriskEqualsToken,
94
- ts.SyntaxKind.SlashEqualsToken,
95
- ts.SyntaxKind.AmpersandEqualsToken,
96
- ts.SyntaxKind.BarEqualsToken,
97
- ts.SyntaxKind.LessThanLessThanToken,
98
- ts.SyntaxKind.GreaterThanGreaterThanToken,
99
- ts.SyntaxKind.QuestionToken,
100
- ts.SyntaxKind.ColonToken,
101
- ts.SyntaxKind.EqualsToken,
102
- ts.SyntaxKind.EqualsGreaterThanToken,
103
- ts.SyntaxKind.AmpersandAmpersandToken,
104
- ts.SyntaxKind.BarBarToken,
105
- ts.SyntaxKind.ExclamationToken,
106
- ts.SyntaxKind.TildeToken,
107
- ts.SyntaxKind.CommaToken,
108
- ts.SyntaxKind.SemicolonToken,
109
- ts.SyntaxKind.DotToken,
110
- ts.SyntaxKind.QuestionDotToken,
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
- return isFileKind(filePath, "jsts");
379
+ const languageId = resolveTreeSitterLanguage(filePath);
380
+ return Boolean(languageId && languageId in LANGUAGE_NODES);
125
381
  }
126
- /**
127
- * Analyze complexity metrics for a file
128
- */
129
- analyzeFile(filePath) {
130
- const parsed = this.readAndParse(filePath);
131
- if (!parsed)
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
- return this.computeMetrics(parsed);
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(`Analysis error for ${filePath}: ${err.message}`);
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
- const content = fs.readFileSync(absolutePath, "utf-8");
149
- const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true);
150
- return { absolutePath, content, sourceFile };
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
- * Compute all metrics from parsed source
154
- */
155
- computeMetrics(parsed) {
156
- const { absolutePath, content, sourceFile } = parsed;
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
- halsteadVolume: Math.round(halstead * 10) / 10,
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(codeEntropy * 100) / 100,
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
  }