pi-lens 3.1.2 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (154) hide show
  1. package/CHANGELOG.md +55 -0
  2. package/README.md +16 -12
  3. package/clients/ast-grep-client.js +8 -1
  4. package/clients/ast-grep-client.ts +9 -1
  5. package/clients/biome-client.js +51 -38
  6. package/clients/biome-client.ts +60 -58
  7. package/clients/dependency-checker.js +30 -1
  8. package/clients/dependency-checker.ts +35 -1
  9. package/clients/dispatch/__tests__/runner-registration.test.ts +286 -282
  10. package/clients/dispatch/bus-dispatcher.js +15 -14
  11. package/clients/dispatch/bus-dispatcher.ts +32 -25
  12. package/clients/dispatch/dispatcher.js +18 -25
  13. package/clients/dispatch/dispatcher.test.ts +2 -1
  14. package/clients/dispatch/dispatcher.ts +17 -28
  15. package/clients/dispatch/plan.js +77 -32
  16. package/clients/dispatch/plan.ts +78 -32
  17. package/clients/dispatch/runners/ast-grep-napi.js +36 -376
  18. package/clients/dispatch/runners/ast-grep-napi.ts +60 -433
  19. package/clients/dispatch/runners/index.js +8 -4
  20. package/clients/dispatch/runners/index.ts +8 -4
  21. package/clients/dispatch/runners/lsp.js +65 -0
  22. package/clients/dispatch/runners/lsp.ts +125 -0
  23. package/clients/dispatch/runners/oxlint.js +2 -2
  24. package/clients/dispatch/runners/oxlint.ts +2 -2
  25. package/clients/dispatch/runners/pyright.js +24 -8
  26. package/clients/dispatch/runners/pyright.ts +28 -14
  27. package/clients/dispatch/runners/rust-clippy.js +2 -2
  28. package/clients/dispatch/runners/rust-clippy.ts +2 -4
  29. package/clients/dispatch/runners/tree-sitter.js +14 -2
  30. package/clients/dispatch/runners/tree-sitter.ts +15 -2
  31. package/clients/dispatch/runners/ts-lsp.js +3 -3
  32. package/clients/dispatch/runners/ts-lsp.ts +8 -5
  33. package/clients/dispatch/runners/yaml-rule-parser.js +292 -0
  34. package/clients/dispatch/runners/yaml-rule-parser.ts +338 -0
  35. package/clients/dispatch/types.js +3 -0
  36. package/clients/dispatch/types.ts +3 -0
  37. package/clients/formatters.js +67 -14
  38. package/clients/formatters.ts +68 -15
  39. package/clients/installer/index.js +78 -10
  40. package/clients/installer/index.ts +519 -426
  41. package/clients/jscpd-client.js +28 -0
  42. package/clients/jscpd-client.ts +41 -3
  43. package/clients/knip-client.js +30 -1
  44. package/clients/knip-client.ts +34 -2
  45. package/clients/lsp/__tests__/client.test.ts +64 -41
  46. package/clients/lsp/__tests__/config.test.ts +25 -17
  47. package/clients/lsp/__tests__/launch.test.ts +108 -43
  48. package/clients/lsp/__tests__/service.test.ts +76 -48
  49. package/clients/lsp/client.js +87 -2
  50. package/clients/lsp/client.ts +150 -6
  51. package/clients/lsp/config.js +8 -11
  52. package/clients/lsp/config.ts +24 -21
  53. package/clients/lsp/index.js +69 -0
  54. package/clients/lsp/index.ts +82 -0
  55. package/clients/lsp/interactive-install.js +19 -8
  56. package/clients/lsp/interactive-install.ts +52 -27
  57. package/clients/lsp/launch.js +182 -32
  58. package/clients/lsp/launch.ts +241 -38
  59. package/clients/lsp/path-utils.js +3 -46
  60. package/clients/lsp/path-utils.ts +11 -51
  61. package/clients/lsp/server.js +93 -71
  62. package/clients/lsp/server.ts +173 -131
  63. package/clients/path-utils.js +142 -0
  64. package/clients/path-utils.ts +153 -0
  65. package/clients/ruff-client.js +33 -4
  66. package/clients/ruff-client.ts +44 -13
  67. package/clients/safe-spawn.js +3 -1
  68. package/clients/safe-spawn.ts +3 -1
  69. package/clients/services/effect-integration.js +11 -7
  70. package/clients/services/effect-integration.ts +34 -26
  71. package/clients/sg-runner.js +51 -9
  72. package/clients/sg-runner.ts +58 -15
  73. package/clients/tree-sitter-client.js +12 -0
  74. package/clients/tree-sitter-client.ts +12 -0
  75. package/clients/typescript-client.js +6 -2
  76. package/clients/typescript-client.ts +9 -2
  77. package/commands/booboo.js +2 -4
  78. package/commands/booboo.ts +2 -4
  79. package/index.ts +377 -93
  80. package/package.json +2 -1
  81. package/rules/tree-sitter-queries/tsx/no-nested-links.yml +45 -0
  82. package/rules/tree-sitter-queries/typescript/constructor-super.yml +55 -0
  83. package/rules/tree-sitter-queries/typescript/debugger.yml +1 -1
  84. package/rules/tree-sitter-queries/typescript/no-dupe-class-members.yml +47 -0
  85. package/tsconfig.json +1 -1
  86. package/clients/__tests__/file-time.test.js +0 -216
  87. package/clients/__tests__/format-service.test.js +0 -245
  88. package/clients/__tests__/formatters.test.js +0 -271
  89. package/clients/agent-behavior-client.test.js +0 -94
  90. package/clients/ast-grep-client.test.js +0 -129
  91. package/clients/ast-grep-client.test.ts +0 -155
  92. package/clients/biome-client.test.js +0 -144
  93. package/clients/cache-manager.test.js +0 -197
  94. package/clients/complexity-client.test.js +0 -234
  95. package/clients/dependency-checker.test.js +0 -60
  96. package/clients/dispatch/__tests__/autofix-integration.test.js +0 -245
  97. package/clients/dispatch/__tests__/runner-registration.test.js +0 -236
  98. package/clients/dispatch/dispatcher.edge.test.js +0 -82
  99. package/clients/dispatch/dispatcher.format.test.js +0 -46
  100. package/clients/dispatch/dispatcher.inline.test.js +0 -74
  101. package/clients/dispatch/dispatcher.test.js +0 -115
  102. package/clients/dispatch/runners/architect.test.js +0 -138
  103. package/clients/dispatch/runners/ast-grep-napi.test.js +0 -106
  104. package/clients/dispatch/runners/oxlint.test.js +0 -230
  105. package/clients/dispatch/runners/pyright.test.js +0 -98
  106. package/clients/dispatch/runners/python-slop.test.js +0 -203
  107. package/clients/dispatch/runners/scan_codebase.test.js +0 -89
  108. package/clients/dispatch/runners/shellcheck.test.js +0 -98
  109. package/clients/dispatch/runners/spellcheck.test.js +0 -158
  110. package/clients/dispatch/runners/ts-slop.test.js +0 -180
  111. package/clients/dispatch/runners/ts-slop.test.ts +0 -230
  112. package/clients/dogfood.test.js +0 -201
  113. package/clients/file-kinds.test.js +0 -169
  114. package/clients/go-client.test.js +0 -127
  115. package/clients/jscpd-client.test.js +0 -127
  116. package/clients/knip-client.test.js +0 -112
  117. package/clients/lsp/__tests__/client.test.js +0 -325
  118. package/clients/lsp/__tests__/config.test.js +0 -166
  119. package/clients/lsp/__tests__/error-recovery.test.js +0 -213
  120. package/clients/lsp/__tests__/integration.test.js +0 -127
  121. package/clients/lsp/__tests__/launch.test.js +0 -260
  122. package/clients/lsp/__tests__/server.test.js +0 -259
  123. package/clients/lsp/__tests__/service.test.js +0 -417
  124. package/clients/metrics-client.test.js +0 -141
  125. package/clients/ruff-client.test.js +0 -132
  126. package/clients/rust-client.test.js +0 -108
  127. package/clients/sanitize.test.js +0 -177
  128. package/clients/secrets-scanner.test.js +0 -100
  129. package/clients/services/__tests__/effect-integration.test.js +0 -86
  130. package/clients/test-runner-client.test.js +0 -192
  131. package/clients/todo-scanner.test.js +0 -301
  132. package/clients/type-coverage-client.test.js +0 -105
  133. package/clients/typescript-client.codefix.test.js +0 -157
  134. package/clients/typescript-client.test.js +0 -105
  135. package/commands/clients/ast-grep-client.js +0 -250
  136. package/commands/clients/ast-grep-parser.js +0 -86
  137. package/commands/clients/ast-grep-rule-manager.js +0 -91
  138. package/commands/clients/ast-grep-types.js +0 -9
  139. package/commands/clients/biome-client.js +0 -380
  140. package/commands/clients/complexity-client.js +0 -667
  141. package/commands/clients/file-kinds.js +0 -177
  142. package/commands/clients/file-utils.js +0 -40
  143. package/commands/clients/jscpd-client.js +0 -169
  144. package/commands/clients/knip-client.js +0 -211
  145. package/commands/clients/ruff-client.js +0 -297
  146. package/commands/clients/safe-spawn.js +0 -88
  147. package/commands/clients/scan-utils.js +0 -83
  148. package/commands/clients/sg-runner.js +0 -190
  149. package/commands/clients/types.js +0 -11
  150. package/commands/clients/typescript-client.js +0 -505
  151. package/commands/rate.test.js +0 -119
  152. package/rules/ast-grep-rules/rules/no-dangerously-set-inner-html.yml +0 -13
  153. package/rules/ast-grep-rules/rules/no-debugger.yml +0 -12
  154. package/rules/ast-grep-rules/rules/no-eval.yml +0 -13
@@ -1,667 +0,0 @@
1
- /**
2
- * Complexity Metrics Client for pi-lens
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
- log;
115
- constructor(verbose = false) {
116
- this.log = verbose
117
- ? (msg) => console.error(`[complexity] ${msg}`)
118
- : () => { };
119
- }
120
- /**
121
- * Check if file is supported (TS/JS)
122
- */
123
- isSupportedFile(filePath) {
124
- return isFileKind(filePath, "jsts");
125
- }
126
- /**
127
- * Analyze complexity metrics for a file
128
- */
129
- analyzeFile(filePath) {
130
- const parsed = this.readAndParse(filePath);
131
- if (!parsed)
132
- return null;
133
- try {
134
- return this.computeMetrics(parsed);
135
- }
136
- catch (err) {
137
- this.log(`Analysis error for ${filePath}: ${err.message}`);
138
- return null;
139
- }
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))
147
- 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 };
151
- }
152
- /**
153
- * Compute all metrics from parsed source
154
- */
155
- computeMetrics(parsed) {
156
- const { absolutePath, content, sourceFile } = parsed;
157
- const lines = content.split("\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
166
- 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
- return {
176
- filePath: path.relative(process.cwd(), absolutePath),
177
- maxNestingDepth,
178
- avgFunctionLength: funcStats.avgLength,
179
- maxFunctionLength: funcStats.maxLength,
180
- functionCount: functions.length,
181
- cyclomaticComplexity: funcStats.avgCyclomatic,
182
- maxCyclomaticComplexity: funcStats.maxCyclomatic,
183
- cognitiveComplexity: cognitive,
184
- halsteadVolume: Math.round(halstead * 10) / 10,
185
- maintainabilityIndex: Math.round(maintainabilityIndex * 10) / 10,
186
- linesOfCode: codeLines,
187
- commentLines,
188
- codeEntropy: Math.round(codeEntropy * 100) / 100,
189
- maxParamsInFunction,
190
- aiCommentPatterns,
191
- singleUseFunctions,
192
- tryCatchCount,
193
- };
194
- }
195
- /**
196
- * Aggregate function metrics into summary statistics
197
- */
198
- aggregateFunctionStats(functions) {
199
- if (functions.length === 0) {
200
- return { avgLength: 0, maxLength: 0, avgCyclomatic: 1, maxCyclomatic: 1 };
201
- }
202
- const lengths = functions.map((f) => f.length);
203
- const cyclomatics = functions.map((f) => f.cyclomatic);
204
- const sum = (arr) => arr.reduce((a, b) => a + b, 0);
205
- return {
206
- avgLength: Math.round(sum(lengths) / lengths.length),
207
- maxLength: Math.max(...lengths),
208
- avgCyclomatic: Math.max(1, Math.round(sum(cyclomatics) / cyclomatics.length)),
209
- maxCyclomatic: Math.max(1, Math.max(...cyclomatics)),
210
- };
211
- }
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, >3.5 = risky AI-induced complexity)
239
- if (metrics.codeEntropy > 3.5) {
240
- parts.push(` Entropy: ${metrics.codeEntropy.toFixed(1)} bits (>3.5 — risky AI-induced complexity)`);
241
- }
242
- // Function length
243
- if (metrics.maxFunctionLength > 50) {
244
- parts.push(` Longest function: ${metrics.maxFunctionLength} lines (avg: ${metrics.avgFunctionLength})`);
245
- }
246
- // Halstead (only if notably high)
247
- if (metrics.halsteadVolume > 500) {
248
- parts.push(` Halstead volume: ${metrics.halsteadVolume} (high vocabulary)`);
249
- }
250
- return parts.length > 0
251
- ? `[Complexity] ${metrics.filePath}\n${parts.join("\n")}`
252
- : "";
253
- }
254
- /**
255
- * Calculate max parameters across all functions
256
- */
257
- calculateMaxParams(functions) {
258
- const _maxParams = 0;
259
- // We stored function params in the metrics during analysis
260
- // For now, estimate based on function length (longer functions often have more params)
261
- return Math.min(10, Math.max(2, Math.round(functions.reduce((a, f) => a + f.length, 0) /
262
- Math.max(1, functions.length) /
263
- 5)));
264
- }
265
- /**
266
- * Count AI comment patterns (emojis, boilerplate phrases)
267
- */
268
- countAICommentPatterns(sourceFile) {
269
- const sourceText = sourceFile.getText();
270
- let count = 0;
271
- const aiPatterns = [
272
- /[🔍✅📝🔧🐛⚠️🚀💡🎯📌🏷️🔑🏗️🧪🗑️🔄♻️📋🔖📊💬🔥💎⭐🌟🎯🎨🔧🛠️]/u,
273
- /\/\/\s*(Initialize|Setup|Clean up|Create|Define|Check if|Handle|Process|Validate|Return|Get|Set|Add|Remove|Update|Fetch)\b/i,
274
- /\/\/\s*(This function|This method|This code|Here we|Now we)\b/i,
275
- /\/\*\*?\s*(Overview|Summary|Description|Example|Usage)\s*\*?\//i,
276
- ];
277
- const lines = sourceText.split("\n");
278
- for (const line of lines) {
279
- // Only check comment lines
280
- const trimmed = line.trim();
281
- if (trimmed.startsWith("//") ||
282
- trimmed.startsWith("/*") ||
283
- trimmed.startsWith("*")) {
284
- for (const pattern of aiPatterns) {
285
- if (pattern.test(line)) {
286
- count++;
287
- break;
288
- }
289
- }
290
- }
291
- }
292
- return count;
293
- }
294
- /**
295
- * Count functions that appear to be single-use (helper patterns)
296
- */
297
- countSingleUseFunctions(functions) {
298
- // Heuristic: small functions (< 10 lines) with simple names are often single-use
299
- const smallHelpers = functions.filter((f) => f.length < 10 &&
300
- f.cyclomatic <= 2 &&
301
- /^(get|set|check|is|has|validate|format|parse|convert|create|make)/i.test(f.name));
302
- return smallHelpers.length;
303
- }
304
- /**
305
- * Count try/catch blocks (generic error handling pattern)
306
- */
307
- countTryCatch(sourceFile) {
308
- let count = 0;
309
- const visit = (node) => {
310
- if (ts.isTryStatement(node)) {
311
- count++;
312
- }
313
- ts.forEachChild(node, visit);
314
- };
315
- ts.forEachChild(sourceFile, visit);
316
- return count;
317
- }
318
- /**
319
- * Check thresholds and return actionable warnings
320
- */
321
- checkThresholds(metrics) {
322
- const warnings = [];
323
- if (metrics.maintainabilityIndex < 60) {
324
- warnings.push(`Maintainability dropped to ${metrics.maintainabilityIndex} — extract logic into helper functions`);
325
- }
326
- if (metrics.cyclomaticComplexity > 10) {
327
- warnings.push(`High complexity (${metrics.cyclomaticComplexity}) — use early returns or switch expressions`);
328
- }
329
- if (metrics.cognitiveComplexity > 15) {
330
- warnings.push(`Cognitive complexity (${metrics.cognitiveComplexity}) — simplify logic flow`);
331
- }
332
- if (metrics.maxNestingDepth > 4) {
333
- warnings.push(`Deep nesting (${metrics.maxNestingDepth} levels) — extract nested logic into separate functions`);
334
- }
335
- if (metrics.codeEntropy > 3.5) {
336
- warnings.push(`High entropy (${metrics.codeEntropy.toFixed(1)} bits) — follow project conventions`);
337
- }
338
- // Comments ratio (>40% = excessive comments, AI slop signal)
339
- const totalLines = metrics.linesOfCode + metrics.commentLines;
340
- if (totalLines > 10 && metrics.commentLines / totalLines > 0.4) {
341
- warnings.push(`Excessive comments (${Math.round((metrics.commentLines / totalLines) * 100)}%) — remove obvious comments`);
342
- }
343
- // Verbose code (long functions with low complexity = overly verbose)
344
- if (metrics.avgFunctionLength > 30 && metrics.cyclomaticComplexity < 3) {
345
- warnings.push(`Verbose code (avg ${Math.round(metrics.avgFunctionLength)} lines, low complexity) — simplify or extract`);
346
- }
347
- // AI slop: Emoji/boilerplate comments
348
- if (metrics.aiCommentPatterns > 5) {
349
- warnings.push(`AI-style comments (${metrics.aiCommentPatterns}) — remove hand-holding comments`);
350
- }
351
- // AI slop: Too many try/catch blocks (lazy error handling)
352
- if (metrics.tryCatchCount > 15) {
353
- warnings.push(`Many try/catch blocks (${metrics.tryCatchCount}) — consolidate error handling`);
354
- }
355
- // AI slop: Over-abstraction (many single-use helper functions)
356
- if (metrics.singleUseFunctions > 3 && metrics.functionCount > 5) {
357
- warnings.push(`Over-abstraction (${metrics.singleUseFunctions} single-use helpers) — inline or consolidate`);
358
- }
359
- // AI slop: Functions with too many parameters
360
- if (metrics.maxParamsInFunction > 6) {
361
- warnings.push(`Long parameter list (${metrics.maxParamsInFunction} params) — use options object`);
362
- }
363
- return warnings;
364
- }
365
- /**
366
- * Format delta for session summary
367
- */
368
- formatDelta(previous, current) {
369
- const parts = [];
370
- const miDelta = current.maintainabilityIndex - previous.maintainabilityIndex;
371
- if (Math.abs(miDelta) > 1) {
372
- const arrow = miDelta > 0 ? "↑" : "↓";
373
- const sign = miDelta > 0 ? "+" : "";
374
- parts.push(` ${arrow} ${current.filePath}: MI ${previous.maintainabilityIndex} → ${current.maintainabilityIndex} (${sign}${miDelta.toFixed(1)})`);
375
- }
376
- const cogDelta = current.cognitiveComplexity - previous.cognitiveComplexity;
377
- if (Math.abs(cogDelta) > 3) {
378
- const arrow = cogDelta > 0 ? "↑" : "↓";
379
- const sign = cogDelta > 0 ? "+" : "";
380
- parts.push(` ${arrow} ${current.filePath}: cognitive ${previous.cognitiveComplexity} → ${current.cognitiveComplexity} (${sign}${cogDelta})`);
381
- }
382
- return parts.join("\n");
383
- }
384
- // --- Private: Line Counting ---
385
- countLines(sourceFile, lines) {
386
- let commentLines = 0;
387
- const commentPositions = new Set();
388
- // Find comment positions
389
- const _visitComments = (node) => {
390
- ts.forEachChild(node, _visitComments);
391
- };
392
- // Scan for comments using text
393
- const text = sourceFile.getFullText();
394
- const commentRegex = /\/\/.*$|\/\*[\s\S]*?\*\//gm;
395
- let match;
396
- while ((match = commentRegex.exec(text)) !== null) {
397
- const lineStart = text.lastIndexOf("\n", match.index) + 1;
398
- const startLine = text.substring(0, lineStart).split("\n").length - 1;
399
- const endLine = text.substring(0, match.index + match[0].length).split("\n").length - 1;
400
- for (let i = startLine; i <= endLine; i++) {
401
- commentPositions.add(i);
402
- }
403
- }
404
- commentLines = commentPositions.size;
405
- const codeLines = lines.filter((line, i) => {
406
- const trimmed = line.trim();
407
- if (trimmed.length === 0)
408
- return false;
409
- // If the line is not in commentPositions, it definitely has code
410
- if (!commentPositions.has(i))
411
- return true;
412
- // If it IS in commentPositions, it might still have code (trailing comment)
413
- // Remove the comment part and check if anything remains
414
- const lineWithoutComments = line
415
- .replace(/\/\/.*$/, "")
416
- .replace(/\/\*[\s\S]*?\*\//g, "")
417
- .trim();
418
- return lineWithoutComments.length > 0;
419
- }).length;
420
- return { codeLines, commentLines };
421
- }
422
- // --- Private: Function Metrics Collection ---
423
- /**
424
- * Collect metrics for all functions in the source file
425
- */
426
- collectFunctionMetrics(sourceFile) {
427
- const functions = [];
428
- this.visitFunctionMetrics(sourceFile, sourceFile, functions, 0);
429
- return functions;
430
- }
431
- visitFunctionMetrics(node, sourceFile, functions, nestingLevel) {
432
- if (FUNCTION_LIKE_NODES.has(node.kind)) {
433
- const funcNode = node;
434
- const startLine = sourceFile.getLineAndCharacterOfPosition(node.getStart()).line;
435
- const endLine = sourceFile.getLineAndCharacterOfPosition(node.getEnd()).line;
436
- const length = endLine - startLine + 1;
437
- const cyclomatic = this.nodeCyclomaticComplexity(node, 0);
438
- const cognitive = this.nodeCognitiveComplexity(node, nestingLevel);
439
- const maxNesting = this.calculateMaxNesting(node, 0);
440
- const name = funcNode.name
441
- ? funcNode.name.getText(sourceFile)
442
- : `<anonymous@L${startLine + 1}>`;
443
- functions.push({
444
- name,
445
- line: startLine + 1,
446
- length,
447
- cyclomatic,
448
- cognitive,
449
- nestingDepth: maxNesting,
450
- });
451
- }
452
- // Track nesting depth changes
453
- const newNesting = NESTING_NODES.has(node.kind)
454
- ? nestingLevel + 1
455
- : nestingLevel;
456
- ts.forEachChild(node, (child) => {
457
- this.visitFunctionMetrics(child, sourceFile, functions, newNesting);
458
- });
459
- }
460
- // --- Private: Max Nesting Depth ---
461
- calculateMaxNesting(node, currentDepth) {
462
- let maxDepth = currentDepth;
463
- if (NESTING_NODES.has(node.kind)) {
464
- currentDepth++;
465
- maxDepth = Math.max(maxDepth, currentDepth);
466
- }
467
- ts.forEachChild(node, (child) => {
468
- const childMax = this.calculateMaxNesting(child, currentDepth);
469
- maxDepth = Math.max(maxDepth, childMax);
470
- });
471
- return maxDepth;
472
- }
473
- isLogicalOperator(node) {
474
- if (node.kind === ts.SyntaxKind.BinaryExpression) {
475
- const binary = node;
476
- return (binary.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken ||
477
- binary.operatorToken.kind === ts.SyntaxKind.BarBarToken);
478
- }
479
- return false;
480
- }
481
- nodeCyclomaticComplexity(node, complexity) {
482
- // Base increment for branching nodes
483
- if (CYCLOMAL_NODES.has(node.kind)) {
484
- complexity++;
485
- }
486
- // Binary && and || add complexity
487
- if (this.isLogicalOperator(node)) {
488
- complexity++;
489
- }
490
- ts.forEachChild(node, (child) => {
491
- complexity = this.nodeCyclomaticComplexity(child, complexity);
492
- });
493
- return complexity;
494
- }
495
- // --- Private: Cognitive Complexity ---
496
- // Based on SonarSource's Cognitive Complexity specification
497
- // Increment for: if, for, while, case, catch, conditional
498
- // Additional increment for nesting
499
- calculateCognitiveComplexity(node) {
500
- return this.nodeCognitiveComplexity(node, 0);
501
- }
502
- nodeCognitiveComplexity(node, nestingDepth) {
503
- let complexity = 0;
504
- // Structures that contribute to cognitive complexity
505
- if (COGNITIVE_NODES.has(node.kind)) {
506
- // Base increment + nesting penalty
507
- complexity += 1 + nestingDepth;
508
- }
509
- // Break/continue with label add to complexity
510
- if (ts.isBreakStatement(node) || ts.isContinueStatement(node)) {
511
- if (node.label) {
512
- complexity += 1 + nestingDepth;
513
- }
514
- }
515
- // Binary && and || contribute to complexity
516
- if (this.isLogicalOperator(node)) {
517
- complexity += 1;
518
- }
519
- // Calculate nesting for children
520
- const increasesNesting = NESTING_NODES.has(node.kind);
521
- const childNesting = increasesNesting ? nestingDepth + 1 : nestingDepth;
522
- ts.forEachChild(node, (child) => {
523
- complexity += this.nodeCognitiveComplexity(child, childNesting);
524
- });
525
- return complexity;
526
- }
527
- // --- Private: Halstead Volume ---
528
- // V = N * log2(n) where N = total operators+operands, n = unique operators+operands
529
- calculateHalsteadVolume(node) {
530
- const operators = new Set();
531
- const operands = new Set();
532
- let totalOperators = 0;
533
- let totalOperands = 0;
534
- const visit = (n) => {
535
- // Check if it's an operator
536
- if (HALSTEAD_OPERATORS.has(n.kind)) {
537
- const opText = ts.SyntaxKind[n.kind];
538
- operators.add(opText);
539
- totalOperators++;
540
- }
541
- // Check for identifiers (operands)
542
- else if (ts.isIdentifier(n)) {
543
- const text = n.getText();
544
- // Skip keywords that are parsed as identifiers
545
- if (!this.isKeyword(text)) {
546
- operands.add(text);
547
- totalOperands++;
548
- }
549
- }
550
- // Check for literals (operands)
551
- else if (ts.isNumericLiteral(n) ||
552
- ts.isStringLiteral(n) ||
553
- n.kind === ts.SyntaxKind.TrueKeyword ||
554
- n.kind === ts.SyntaxKind.FalseKeyword ||
555
- n.kind === ts.SyntaxKind.NullKeyword ||
556
- n.kind === ts.SyntaxKind.UndefinedKeyword) {
557
- const text = n.getText();
558
- operands.add(text);
559
- totalOperands++;
560
- }
561
- ts.forEachChild(n, visit);
562
- };
563
- visit(node);
564
- const uniqueOps = operators.size + operands.size;
565
- const totalOps = totalOperators + totalOperands;
566
- if (uniqueOps === 0 || totalOps === 0)
567
- return 0;
568
- // V = N * log2(n)
569
- return totalOps * Math.log2(uniqueOps);
570
- }
571
- /**
572
- * Calculate Shannon entropy of code tokens (in bits)
573
- * Uses log2 for entropy measured in bits
574
- * Threshold: >3.5 bits indicates risky AI-induced complexity
575
- */
576
- calculateCodeEntropy(sourceText) {
577
- // Tokenize by splitting on whitespace and common delimiters
578
- const tokens = sourceText
579
- .replace(/\/\/.*/g, "") // Remove single-line comments
580
- .replace(/\/\*[\s\S]*?\*\//g, "") // Remove multi-line comments
581
- .replace(/["'`][^"'`]*["'`]/g, "STR") // Normalize strings
582
- .replace(/\b\d+(\.\d+)?\b/g, "NUM") // Normalize numbers
583
- .split(/[\s\n\r\t,;:()[\]{}=<>!&|+\-*/%^~?]+/)
584
- .filter((t) => t.length > 0);
585
- if (tokens.length === 0)
586
- return 0;
587
- // Count token frequencies
588
- const freq = new Map();
589
- for (const token of tokens) {
590
- freq.set(token, (freq.get(token) || 0) + 1);
591
- }
592
- // Calculate Shannon entropy in bits: H = -sum(p * log2(p))
593
- let entropy = 0;
594
- for (const count of Array.from(freq.values())) {
595
- const p = count / tokens.length;
596
- if (p > 0) {
597
- entropy -= p * Math.log2(p);
598
- }
599
- }
600
- return entropy; // Return in bits, not normalized
601
- }
602
- isKeyword(text) {
603
- const keywords = new Set([
604
- "if",
605
- "else",
606
- "for",
607
- "while",
608
- "do",
609
- "switch",
610
- "case",
611
- "break",
612
- "continue",
613
- "return",
614
- "throw",
615
- "try",
616
- "catch",
617
- "finally",
618
- "class",
619
- "extends",
620
- "super",
621
- "import",
622
- "export",
623
- "default",
624
- "from",
625
- "as",
626
- "const",
627
- "let",
628
- "var",
629
- "function",
630
- "new",
631
- "delete",
632
- "typeof",
633
- "void",
634
- "instanceof",
635
- "in",
636
- "of",
637
- "this",
638
- "true",
639
- "false",
640
- "null",
641
- "undefined",
642
- "async",
643
- "await",
644
- "yield",
645
- "static",
646
- "get",
647
- "set",
648
- ]);
649
- return keywords.has(text);
650
- }
651
- // --- Private: Maintainability Index ---
652
- // Microsoft's formula: MI = max(0, (171 - 5.2 * ln(Halstead) - 0.23 * Cyclomatic - 16.2 * ln(LOC)) * 100 / 171)
653
- // Adjusted for comment density bonus
654
- calculateMaintainabilityIndex(halstead, cyclomatic, loc, comments) {
655
- if (loc === 0)
656
- return 100;
657
- const lnHalstead = halstead > 0 ? Math.log(halstead) : 0;
658
- const lnLOC = loc > 0 ? Math.log(loc) : 0;
659
- // Base MI formula
660
- let mi = ((171 - 5.2 * lnHalstead - 0.23 * cyclomatic - 16.2 * lnLOC) * 100) / 171;
661
- // Comment density bonus (up to +10%)
662
- const commentDensity = comments / loc;
663
- const commentBonus = Math.min(10, commentDensity * 50);
664
- mi += commentBonus;
665
- return Math.max(0, Math.min(100, mi));
666
- }
667
- }