pi-lens 3.6.3 → 3.6.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.
Files changed (206) hide show
  1. package/package.json +3 -3
  2. package/tsconfig.json +1 -1
  3. package/clients/__tests__/file-time.test.js +0 -216
  4. package/clients/__tests__/file-time.test.ts +0 -276
  5. package/clients/__tests__/format-service.test.js +0 -245
  6. package/clients/__tests__/format-service.test.ts +0 -339
  7. package/clients/__tests__/formatters.test.js +0 -271
  8. package/clients/__tests__/formatters.test.ts +0 -401
  9. package/clients/agent-behavior-client.js +0 -110
  10. package/clients/agent-behavior-client.test.js +0 -94
  11. package/clients/agent-behavior-client.test.ts +0 -116
  12. package/clients/amain-types.js +0 -164
  13. package/clients/architect-client.js +0 -291
  14. package/clients/ast-grep-client.js +0 -253
  15. package/clients/ast-grep-parser.js +0 -84
  16. package/clients/ast-grep-rule-manager.js +0 -89
  17. package/clients/ast-grep-types.js +0 -9
  18. package/clients/auto-loop.js +0 -131
  19. package/clients/biome-client.js +0 -420
  20. package/clients/biome-client.test.js +0 -144
  21. package/clients/biome-client.test.ts +0 -163
  22. package/clients/cache/rule-cache.js +0 -72
  23. package/clients/cache-manager.js +0 -245
  24. package/clients/cache-manager.test.js +0 -197
  25. package/clients/cache-manager.test.ts +0 -299
  26. package/clients/complexity-client.js +0 -675
  27. package/clients/complexity-client.test.js +0 -234
  28. package/clients/complexity-client.test.ts +0 -255
  29. package/clients/config-validator.js +0 -465
  30. package/clients/dependency-checker.js +0 -325
  31. package/clients/dependency-checker.test.js +0 -60
  32. package/clients/dependency-checker.test.ts +0 -71
  33. package/clients/dispatch/__tests__/autofix-integration.test.js +0 -245
  34. package/clients/dispatch/__tests__/autofix-integration.test.ts +0 -300
  35. package/clients/dispatch/__tests__/runner-registration.test.js +0 -234
  36. package/clients/dispatch/__tests__/runner-registration.test.ts +0 -286
  37. package/clients/dispatch/debug.log +0 -1
  38. package/clients/dispatch/dispatcher.edge.test.js +0 -82
  39. package/clients/dispatch/dispatcher.edge.test.ts +0 -100
  40. package/clients/dispatch/dispatcher.format.test.js +0 -46
  41. package/clients/dispatch/dispatcher.format.test.ts +0 -58
  42. package/clients/dispatch/dispatcher.inline.test.js +0 -74
  43. package/clients/dispatch/dispatcher.inline.test.ts +0 -93
  44. package/clients/dispatch/dispatcher.js +0 -381
  45. package/clients/dispatch/dispatcher.test.js +0 -116
  46. package/clients/dispatch/dispatcher.test.ts +0 -149
  47. package/clients/dispatch/integration.js +0 -108
  48. package/clients/dispatch/plan.js +0 -183
  49. package/clients/dispatch/runners/architect.js +0 -83
  50. package/clients/dispatch/runners/architect.test.js +0 -138
  51. package/clients/dispatch/runners/architect.test.ts +0 -162
  52. package/clients/dispatch/runners/ast-grep-napi.js +0 -405
  53. package/clients/dispatch/runners/ast-grep-napi.test.js +0 -107
  54. package/clients/dispatch/runners/ast-grep-napi.test.ts +0 -129
  55. package/clients/dispatch/runners/ast-grep.js +0 -157
  56. package/clients/dispatch/runners/biome.js +0 -55
  57. package/clients/dispatch/runners/config-validation.js +0 -67
  58. package/clients/dispatch/runners/go-vet.js +0 -48
  59. package/clients/dispatch/runners/index.js +0 -47
  60. package/clients/dispatch/runners/lsp.js +0 -102
  61. package/clients/dispatch/runners/oxlint.js +0 -67
  62. package/clients/dispatch/runners/oxlint.test.js +0 -230
  63. package/clients/dispatch/runners/oxlint.test.ts +0 -303
  64. package/clients/dispatch/runners/pyright.js +0 -100
  65. package/clients/dispatch/runners/pyright.test.js +0 -98
  66. package/clients/dispatch/runners/pyright.test.ts +0 -121
  67. package/clients/dispatch/runners/python-slop.js +0 -97
  68. package/clients/dispatch/runners/python-slop.test.js +0 -203
  69. package/clients/dispatch/runners/python-slop.test.ts +0 -298
  70. package/clients/dispatch/runners/ruff.js +0 -48
  71. package/clients/dispatch/runners/rust-clippy.js +0 -102
  72. package/clients/dispatch/runners/scan_codebase.test.js +0 -89
  73. package/clients/dispatch/runners/scan_codebase.test.ts +0 -105
  74. package/clients/dispatch/runners/shellcheck.js +0 -147
  75. package/clients/dispatch/runners/shellcheck.test.js +0 -98
  76. package/clients/dispatch/runners/shellcheck.test.ts +0 -129
  77. package/clients/dispatch/runners/similarity.js +0 -230
  78. package/clients/dispatch/runners/spellcheck.js +0 -106
  79. package/clients/dispatch/runners/spellcheck.test.js +0 -158
  80. package/clients/dispatch/runners/spellcheck.test.ts +0 -214
  81. package/clients/dispatch/runners/tree-sitter.js +0 -246
  82. package/clients/dispatch/runners/ts-lsp.js +0 -125
  83. package/clients/dispatch/runners/ts-slop.js +0 -113
  84. package/clients/dispatch/runners/type-safety.js +0 -142
  85. package/clients/dispatch/runners/utils/diagnostic-parsers.js +0 -134
  86. package/clients/dispatch/runners/utils/runner-helpers.js +0 -115
  87. package/clients/dispatch/runners/utils.js +0 -51
  88. package/clients/dispatch/runners/yaml-rule-parser.js +0 -360
  89. package/clients/dispatch/types.js +0 -16
  90. package/clients/dispatch/utils/format-utils.js +0 -44
  91. package/clients/dogfood.test.js +0 -201
  92. package/clients/dogfood.test.ts +0 -269
  93. package/clients/file-kinds.js +0 -177
  94. package/clients/file-kinds.test.js +0 -169
  95. package/clients/file-kinds.test.ts +0 -210
  96. package/clients/file-time.js +0 -152
  97. package/clients/file-utils.js +0 -40
  98. package/clients/fix-scanners.js +0 -204
  99. package/clients/format-service.js +0 -184
  100. package/clients/formatters.js +0 -488
  101. package/clients/go-client.js +0 -203
  102. package/clients/go-client.test.js +0 -127
  103. package/clients/go-client.test.ts +0 -143
  104. package/clients/installer/index.js +0 -403
  105. package/clients/interviewer-templates.js +0 -75
  106. package/clients/interviewer.js +0 -173
  107. package/clients/jscpd-client.js +0 -196
  108. package/clients/jscpd-client.test.js +0 -127
  109. package/clients/jscpd-client.test.ts +0 -145
  110. package/clients/knip-client.js +0 -239
  111. package/clients/knip-client.test.js +0 -112
  112. package/clients/knip-client.test.ts +0 -128
  113. package/clients/latency-logger.js +0 -40
  114. package/clients/lsp/__tests__/client.test.js +0 -310
  115. package/clients/lsp/__tests__/client.test.ts +0 -412
  116. package/clients/lsp/__tests__/config.test.js +0 -167
  117. package/clients/lsp/__tests__/config.test.ts +0 -217
  118. package/clients/lsp/__tests__/error-recovery.test.js +0 -213
  119. package/clients/lsp/__tests__/error-recovery.test.ts +0 -279
  120. package/clients/lsp/__tests__/integration.test.js +0 -127
  121. package/clients/lsp/__tests__/integration.test.ts +0 -160
  122. package/clients/lsp/__tests__/launch.test.js +0 -313
  123. package/clients/lsp/__tests__/launch.test.ts +0 -394
  124. package/clients/lsp/__tests__/server.test.js +0 -259
  125. package/clients/lsp/__tests__/server.test.ts +0 -332
  126. package/clients/lsp/__tests__/service.test.js +0 -438
  127. package/clients/lsp/__tests__/service.test.ts +0 -530
  128. package/clients/lsp/client.js +0 -350
  129. package/clients/lsp/config.js +0 -112
  130. package/clients/lsp/index.js +0 -318
  131. package/clients/lsp/installer/index.js +0 -391
  132. package/clients/lsp/interactive-install.js +0 -221
  133. package/clients/lsp/language.js +0 -170
  134. package/clients/lsp/launch.js +0 -329
  135. package/clients/lsp/lsp/launch.js +0 -116
  136. package/clients/lsp/lsp/server.js +0 -532
  137. package/clients/lsp/lsp-index.js +0 -10
  138. package/clients/lsp/path-utils.js +0 -5
  139. package/clients/lsp/server.js +0 -725
  140. package/clients/lsp/test-py-spawn/requirements.txt +0 -1
  141. package/clients/lsp/test-py-spawn/test.py +0 -3
  142. package/clients/lsp/test-py-svc/requirements.txt +0 -1
  143. package/clients/lsp/test-py-svc/test.py +0 -3
  144. package/clients/lsp/test-python-project/requirements.txt +0 -1
  145. package/clients/lsp/test-python-project/test.py +0 -5
  146. package/clients/metrics-client.js +0 -107
  147. package/clients/metrics-client.test.js +0 -128
  148. package/clients/metrics-client.test.ts +0 -163
  149. package/clients/metrics-history.js +0 -367
  150. package/clients/path-utils.js +0 -142
  151. package/clients/pipeline.js +0 -272
  152. package/clients/production-readiness.js +0 -522
  153. package/clients/project-index.js +0 -255
  154. package/clients/project-metadata.js +0 -531
  155. package/clients/ruff-client.js +0 -325
  156. package/clients/ruff-client.test.js +0 -132
  157. package/clients/ruff-client.test.ts +0 -153
  158. package/clients/rules-scanner.js +0 -97
  159. package/clients/runner-tracker.js +0 -152
  160. package/clients/rust-client.js +0 -205
  161. package/clients/rust-client.test.js +0 -108
  162. package/clients/rust-client.test.ts +0 -130
  163. package/clients/safe-spawn-async.js +0 -163
  164. package/clients/safe-spawn.js +0 -241
  165. package/clients/sanitize.js +0 -291
  166. package/clients/sanitize.test.js +0 -177
  167. package/clients/sanitize.test.ts +0 -223
  168. package/clients/scan-architectural-debt.js +0 -167
  169. package/clients/scan-utils.js +0 -83
  170. package/clients/secrets-scanner.js +0 -119
  171. package/clients/secrets-scanner.test.js +0 -100
  172. package/clients/secrets-scanner.test.ts +0 -113
  173. package/clients/sg-runner.js +0 -292
  174. package/clients/state-matrix.js +0 -160
  175. package/clients/subprocess-client.js +0 -65
  176. package/clients/symbol-types.js +0 -5
  177. package/clients/test-runner-client.js +0 -523
  178. package/clients/test-runner-client.test.js +0 -192
  179. package/clients/test-runner-client.test.ts +0 -253
  180. package/clients/test-utils.js +0 -27
  181. package/clients/test-utils.ts +0 -36
  182. package/clients/todo-scanner.js +0 -200
  183. package/clients/todo-scanner.test.js +0 -301
  184. package/clients/todo-scanner.test.ts +0 -352
  185. package/clients/tool-availability.js +0 -207
  186. package/clients/tree-sitter-client.js +0 -601
  187. package/clients/tree-sitter-query-loader.js +0 -355
  188. package/clients/tree-sitter-symbol-extractor.js +0 -289
  189. package/clients/ts-service.js +0 -129
  190. package/clients/type-coverage-client.js +0 -127
  191. package/clients/type-coverage-client.test.js +0 -105
  192. package/clients/type-coverage-client.test.ts +0 -125
  193. package/clients/type-safety-client.js +0 -138
  194. package/clients/types.js +0 -11
  195. package/clients/typescript-client.codefix.test.js +0 -157
  196. package/clients/typescript-client.codefix.test.ts +0 -186
  197. package/clients/typescript-client.js +0 -509
  198. package/clients/typescript-client.test.js +0 -105
  199. package/clients/typescript-client.test.ts +0 -126
  200. package/commands/booboo.js +0 -1007
  201. package/commands/fix-from-booboo.js +0 -398
  202. package/commands/fix-simplified.js +0 -618
  203. package/commands/rate.js +0 -281
  204. package/commands/rate.test.js +0 -119
  205. package/commands/rate.test.ts +0 -131
  206. package/commands/refactor.js +0 -130
@@ -1,675 +0,0 @@
1
- /**
2
- * Complexity Metrics Client for pi-lens (cache test)
3
- *
4
- * Calculates AST-based code complexity metrics for TypeScript/JavaScript files.
5
- * Uses the TypeScript compiler API for parsing.
6
- *
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.
16
- */
17
- import * as fs from "node:fs";
18
- import * as path from "node:path";
19
- import * as ts from "typescript";
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
- ]);
112
- // --- Client ---
113
- export class ComplexityClient {
114
- constructor(verbose = false) {
115
- this.log = verbose
116
- ? (msg) => console.error(`[complexity] ${msg}`)
117
- : () => { };
118
- }
119
- /**
120
- * Check if file is supported (TS/JS)
121
- */
122
- isSupportedFile(filePath) {
123
- return isFileKind(filePath, "jsts");
124
- }
125
- /**
126
- * Analyze complexity metrics for a file
127
- */
128
- analyzeFile(filePath) {
129
- const parsed = this.readAndParse(filePath);
130
- if (!parsed)
131
- return null;
132
- try {
133
- return this.computeMetrics(parsed);
134
- }
135
- catch (err) {
136
- this.log(`Analysis error for ${filePath}: ${err.message}`);
137
- return null;
138
- }
139
- }
140
- /**
141
- * Read file and parse to TypeScript AST
142
- */
143
- readAndParse(filePath) {
144
- const absolutePath = path.resolve(filePath);
145
- if (!fs.existsSync(absolutePath))
146
- return null;
147
- const content = fs.readFileSync(absolutePath, "utf-8");
148
- const sourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true);
149
- return { absolutePath, content, sourceFile };
150
- }
151
- /**
152
- * Compute all metrics from parsed source
153
- */
154
- computeMetrics(parsed) {
155
- const { absolutePath, content, sourceFile } = parsed;
156
- const lines = content.split("\n");
157
- // Line counts and function collection
158
- const { codeLines, commentLines } = this.countLines(sourceFile, lines);
159
- const functions = this.collectFunctionMetrics(sourceFile);
160
- // File-level complexity metrics
161
- const maxNestingDepth = this.calculateMaxNesting(sourceFile, 0);
162
- const cognitive = this.calculateCognitiveComplexity(sourceFile);
163
- const halstead = this.calculateHalsteadVolume(sourceFile);
164
- // Aggregate function statistics
165
- const funcStats = this.aggregateFunctionStats(functions);
166
- // Derived metrics
167
- const maintainabilityIndex = this.calculateMaintainabilityIndex(halstead, funcStats.avgCyclomatic, codeLines, commentLines);
168
- const codeEntropy = this.calculateCodeEntropy(content);
169
- // AI slop indicators
170
- const maxParamsInFunction = this.calculateMaxParams(functions);
171
- const aiCommentPatterns = this.countAICommentPatterns(sourceFile);
172
- const singleUseFunctions = this.countSingleUseFunctions(functions);
173
- const tryCatchCount = this.countTryCatch(sourceFile);
174
- return {
175
- filePath: path.relative(process.cwd(), absolutePath),
176
- maxNestingDepth,
177
- avgFunctionLength: funcStats.avgLength,
178
- maxFunctionLength: funcStats.maxLength,
179
- functionCount: functions.length,
180
- cyclomaticComplexity: funcStats.avgCyclomatic,
181
- maxCyclomaticComplexity: funcStats.maxCyclomatic,
182
- cognitiveComplexity: cognitive,
183
- halsteadVolume: Math.round(halstead * 10) / 10,
184
- maintainabilityIndex: Math.round(maintainabilityIndex * 10) / 10,
185
- linesOfCode: codeLines,
186
- commentLines,
187
- codeEntropy: Math.round(codeEntropy * 100) / 100,
188
- maxParamsInFunction,
189
- aiCommentPatterns,
190
- singleUseFunctions,
191
- tryCatchCount,
192
- };
193
- }
194
- /**
195
- * Aggregate function metrics into summary statistics
196
- */
197
- aggregateFunctionStats(functions) {
198
- if (functions.length === 0) {
199
- return { avgLength: 0, maxLength: 0, avgCyclomatic: 1, maxCyclomatic: 1 };
200
- }
201
- const lengths = functions.map((f) => f.length);
202
- const cyclomatics = functions.map((f) => f.cyclomatic);
203
- const sum = (arr) => arr.reduce((a, b) => a + b, 0);
204
- return {
205
- avgLength: Math.round(sum(lengths) / lengths.length),
206
- maxLength: Math.max(...lengths),
207
- avgCyclomatic: Math.max(1, Math.round(sum(cyclomatics) / cyclomatics.length)),
208
- maxCyclomatic: Math.max(1, Math.max(...cyclomatics)),
209
- };
210
- }
211
- /**
212
- * Format metrics for display
213
- */
214
- formatMetrics(metrics) {
215
- const parts = [];
216
- // Maintainability Index (most important)
217
- let miLabel = "✗";
218
- if (metrics.maintainabilityIndex >= 80)
219
- miLabel = "✓";
220
- else if (metrics.maintainabilityIndex >= 60)
221
- miLabel = "⚠";
222
- parts.push(`${miLabel} Maintainability: ${metrics.maintainabilityIndex}/100`);
223
- // Complexity metrics
224
- if (metrics.cyclomaticComplexity > 5 ||
225
- metrics.maxCyclomaticComplexity > 10) {
226
- const avg = metrics.cyclomaticComplexity;
227
- const max = metrics.maxCyclomaticComplexity;
228
- parts.push(` Cyclomatic: avg ${avg}, max ${max} (${metrics.functionCount} functions)`);
229
- }
230
- if (metrics.cognitiveComplexity > 15) {
231
- parts.push(` Cognitive: ${metrics.cognitiveComplexity} (high mental complexity)`);
232
- }
233
- // Nesting depth
234
- if (metrics.maxNestingDepth > 4) {
235
- parts.push(` Max nesting: ${metrics.maxNestingDepth} levels (consider extracting)`);
236
- }
237
- // Code entropy (in bits, >5.5 = risky AI-induced complexity)
238
- // Threshold increased from 3.5 to 5.5 to reduce false positives in tooling codebases
239
- // where diverse method/variable names are naturally expected
240
- if (metrics.codeEntropy > 5.5) {
241
- parts.push(` Entropy: ${metrics.codeEntropy.toFixed(1)} bits (>5.5 — risky AI-induced complexity)`);
242
- }
243
- // Function length
244
- if (metrics.maxFunctionLength > 50) {
245
- parts.push(` Longest function: ${metrics.maxFunctionLength} lines (avg: ${metrics.avgFunctionLength})`);
246
- }
247
- // Halstead (only if notably high)
248
- if (metrics.halsteadVolume > 500) {
249
- parts.push(` Halstead volume: ${metrics.halsteadVolume} (high vocabulary)`);
250
- }
251
- return parts.length > 0
252
- ? `[Complexity] ${metrics.filePath}\n${parts.join("\n")}`
253
- : "";
254
- }
255
- /**
256
- * Calculate max parameters across all functions
257
- */
258
- calculateMaxParams(functions) {
259
- const _maxParams = 0;
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("\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("\n").length - 1;
406
- const endLine = text.substring(0, match.index + match[0].length).split("\n").length - 1;
407
- for (let i = startLine; i <= endLine; i++) {
408
- commentPositions.add(i);
409
- }
410
- }
411
- commentLines = commentPositions.size;
412
- const codeLines = lines.filter((line, i) => {
413
- const trimmed = line.trim();
414
- if (trimmed.length === 0)
415
- return false;
416
- // If the line is not in commentPositions, it definitely has code
417
- if (!commentPositions.has(i))
418
- return true;
419
- // If it IS in commentPositions, it might still have code (trailing comment)
420
- // Remove the comment part and check if anything remains
421
- const lineWithoutComments = line
422
- .replace(/\/\/.*$/, "")
423
- .replace(/\/\*[\s\S]*?\*\//g, "")
424
- .trim();
425
- return lineWithoutComments.length > 0;
426
- }).length;
427
- return { codeLines, commentLines };
428
- }
429
- // --- Private: Function Metrics Collection ---
430
- /**
431
- * Collect metrics for all functions in the source file
432
- */
433
- collectFunctionMetrics(sourceFile) {
434
- const functions = [];
435
- this.visitFunctionMetrics(sourceFile, sourceFile, functions, 0);
436
- return functions;
437
- }
438
- visitFunctionMetrics(node, sourceFile, functions, nestingLevel) {
439
- if (FUNCTION_LIKE_NODES.has(node.kind)) {
440
- const funcNode = node;
441
- const startLine = sourceFile.getLineAndCharacterOfPosition(node.getStart()).line;
442
- const endLine = sourceFile.getLineAndCharacterOfPosition(node.getEnd()).line;
443
- const length = endLine - startLine + 1;
444
- const cyclomatic = this.nodeCyclomaticComplexity(node, 0);
445
- const cognitive = this.nodeCognitiveComplexity(node, nestingLevel);
446
- const maxNesting = this.calculateMaxNesting(node, 0);
447
- const name = funcNode.name
448
- ? funcNode.name.getText(sourceFile)
449
- : `<anonymous@L${startLine + 1}>`;
450
- functions.push({
451
- name,
452
- line: startLine + 1,
453
- length,
454
- cyclomatic,
455
- cognitive,
456
- nestingDepth: maxNesting,
457
- });
458
- }
459
- // Track nesting depth changes
460
- const newNesting = NESTING_NODES.has(node.kind)
461
- ? nestingLevel + 1
462
- : nestingLevel;
463
- ts.forEachChild(node, (child) => {
464
- this.visitFunctionMetrics(child, sourceFile, functions, newNesting);
465
- });
466
- }
467
- // --- Private: Max Nesting Depth ---
468
- calculateMaxNesting(node, currentDepth) {
469
- let maxDepth = currentDepth;
470
- if (NESTING_NODES.has(node.kind)) {
471
- currentDepth++;
472
- maxDepth = Math.max(maxDepth, currentDepth);
473
- }
474
- ts.forEachChild(node, (child) => {
475
- const childMax = this.calculateMaxNesting(child, currentDepth);
476
- maxDepth = Math.max(maxDepth, childMax);
477
- });
478
- return maxDepth;
479
- }
480
- isLogicalOperator(node) {
481
- if (node.kind === ts.SyntaxKind.BinaryExpression) {
482
- const binary = node;
483
- return (binary.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken ||
484
- binary.operatorToken.kind === ts.SyntaxKind.BarBarToken);
485
- }
486
- return false;
487
- }
488
- nodeCyclomaticComplexity(node, complexity) {
489
- // Base increment for branching nodes
490
- if (CYCLOMAL_NODES.has(node.kind)) {
491
- complexity++;
492
- }
493
- // Binary && and || add complexity
494
- if (this.isLogicalOperator(node)) {
495
- complexity++;
496
- }
497
- ts.forEachChild(node, (child) => {
498
- complexity = this.nodeCyclomaticComplexity(child, complexity);
499
- });
500
- return complexity;
501
- }
502
- // --- Private: Cognitive Complexity ---
503
- // Based on SonarSource's Cognitive Complexity specification
504
- // Increment for: if, for, while, case, catch, conditional
505
- // Additional increment for nesting
506
- calculateCognitiveComplexity(node) {
507
- return this.nodeCognitiveComplexity(node, 0);
508
- }
509
- nodeCognitiveComplexity(node, nestingDepth) {
510
- let complexity = 0;
511
- // Structures that contribute to cognitive complexity
512
- if (COGNITIVE_NODES.has(node.kind)) {
513
- // Base increment + nesting penalty
514
- complexity += 1 + nestingDepth;
515
- }
516
- // Break/continue with label add to complexity
517
- if (ts.isBreakStatement(node) || ts.isContinueStatement(node)) {
518
- if (node.label) {
519
- complexity += 1 + nestingDepth;
520
- }
521
- }
522
- // Binary && and || contribute to complexity
523
- if (this.isLogicalOperator(node)) {
524
- complexity += 1;
525
- }
526
- // Calculate nesting for children
527
- const increasesNesting = NESTING_NODES.has(node.kind);
528
- const childNesting = increasesNesting ? nestingDepth + 1 : nestingDepth;
529
- ts.forEachChild(node, (child) => {
530
- complexity += this.nodeCognitiveComplexity(child, childNesting);
531
- });
532
- return complexity;
533
- }
534
- // --- Private: Halstead Volume ---
535
- // V = N * log2(n) where N = total operators+operands, n = unique operators+operands
536
- calculateHalsteadVolume(node) {
537
- const operators = new Set();
538
- const operands = new Set();
539
- let totalOperators = 0;
540
- let totalOperands = 0;
541
- const visit = (n) => {
542
- // Check if it's an operator
543
- if (HALSTEAD_OPERATORS.has(n.kind)) {
544
- const opText = ts.SyntaxKind[n.kind];
545
- operators.add(opText);
546
- totalOperators++;
547
- }
548
- // Check for identifiers (operands)
549
- else if (ts.isIdentifier(n)) {
550
- const text = n.getText();
551
- // Skip keywords that are parsed as identifiers
552
- if (!this.isKeyword(text)) {
553
- operands.add(text);
554
- totalOperands++;
555
- }
556
- }
557
- // Check for literals (operands)
558
- else if (ts.isNumericLiteral(n) ||
559
- ts.isStringLiteral(n) ||
560
- n.kind === ts.SyntaxKind.TrueKeyword ||
561
- n.kind === ts.SyntaxKind.FalseKeyword ||
562
- n.kind === ts.SyntaxKind.NullKeyword ||
563
- n.kind === ts.SyntaxKind.UndefinedKeyword) {
564
- const text = n.getText();
565
- operands.add(text);
566
- totalOperands++;
567
- }
568
- ts.forEachChild(n, visit);
569
- };
570
- visit(node);
571
- const uniqueOps = operators.size + operands.size;
572
- const totalOps = totalOperators + totalOperands;
573
- if (uniqueOps === 0 || totalOps === 0)
574
- return 0;
575
- // V = N * log2(n)
576
- return totalOps * Math.log2(uniqueOps);
577
- }
578
- /**
579
- * Calculate Shannon entropy of code tokens (in bits)
580
- * Uses log2 for entropy measured in bits
581
- * Threshold: >5.5 bits indicates risky AI-induced complexity
582
- * (Increased from 3.5 to reduce false positives in tooling codebases)
583
- */
584
- calculateCodeEntropy(sourceText) {
585
- // Tokenize by splitting on whitespace and common delimiters
586
- const tokens = sourceText
587
- .replace(/\/\/.*/g, "") // Remove single-line comments
588
- .replace(/\/\*[\s\S]*?\*\//g, "") // Remove multi-line comments
589
- .replace(/["'`][^"'`]*["'`]/g, "STR") // Normalize strings
590
- .replace(/\b\d+(\.\d+)?\b/g, "NUM") // Normalize numbers
591
- .split(/[\s\n\r\t,;:()[\]{}=<>!&|+\-*/%^~?]+/)
592
- .filter((t) => t.length > 0);
593
- if (tokens.length === 0)
594
- return 0;
595
- // Count token frequencies
596
- const freq = new Map();
597
- for (const token of tokens) {
598
- freq.set(token, (freq.get(token) || 0) + 1);
599
- }
600
- // Calculate Shannon entropy in bits: H = -sum(p * log2(p))
601
- let entropy = 0;
602
- for (const count of Array.from(freq.values())) {
603
- const p = count / tokens.length;
604
- if (p > 0) {
605
- entropy -= p * Math.log2(p);
606
- }
607
- }
608
- return entropy; // Return in bits, not normalized
609
- }
610
- isKeyword(text) {
611
- const keywords = new Set([
612
- "if",
613
- "else",
614
- "for",
615
- "while",
616
- "do",
617
- "switch",
618
- "case",
619
- "break",
620
- "continue",
621
- "return",
622
- "throw",
623
- "try",
624
- "catch",
625
- "finally",
626
- "class",
627
- "extends",
628
- "super",
629
- "import",
630
- "export",
631
- "default",
632
- "from",
633
- "as",
634
- "const",
635
- "let",
636
- "var",
637
- "function",
638
- "new",
639
- "delete",
640
- "typeof",
641
- "void",
642
- "instanceof",
643
- "in",
644
- "of",
645
- "this",
646
- "true",
647
- "false",
648
- "null",
649
- "undefined",
650
- "async",
651
- "await",
652
- "yield",
653
- "static",
654
- "get",
655
- "set",
656
- ]);
657
- return keywords.has(text);
658
- }
659
- // --- Private: Maintainability Index ---
660
- // Microsoft's formula: MI = max(0, (171 - 5.2 * ln(Halstead) - 0.23 * Cyclomatic - 16.2 * ln(LOC)) * 100 / 171)
661
- // Adjusted for comment density bonus
662
- calculateMaintainabilityIndex(halstead, cyclomatic, loc, comments) {
663
- if (loc === 0)
664
- return 100;
665
- const lnHalstead = halstead > 0 ? Math.log(halstead) : 0;
666
- const lnLOC = loc > 0 ? Math.log(loc) : 0;
667
- // Base MI formula
668
- let mi = ((171 - 5.2 * lnHalstead - 0.23 * cyclomatic - 16.2 * lnLOC) * 100) / 171;
669
- // Comment density bonus (up to +10%)
670
- const commentDensity = comments / loc;
671
- const commentBonus = Math.min(10, commentDensity * 50);
672
- mi += commentBonus;
673
- return Math.max(0, Math.min(100, mi));
674
- }
675
- }