pi-lens 2.2.9 → 3.0.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.
- package/CHANGELOG.md +198 -0
- package/README.md +709 -519
- package/clients/__tests__/file-time.test.js +216 -0
- package/clients/__tests__/file-time.test.ts +276 -0
- package/clients/__tests__/format-service.test.js +245 -0
- package/clients/__tests__/format-service.test.ts +339 -0
- package/clients/__tests__/formatters.test.js +271 -0
- package/clients/__tests__/formatters.test.ts +401 -0
- package/clients/amain-types.js +164 -0
- package/clients/amain-types.ts +165 -0
- package/clients/architect-client.js +56 -12
- package/clients/architect-client.ts +81 -16
- package/clients/ast-grep-client.js +2 -2
- package/clients/ast-grep-client.ts +14 -39
- package/clients/ast-grep-parser.ts +1 -1
- package/clients/ast-grep-rule-manager.js +8 -0
- package/clients/ast-grep-rule-manager.ts +10 -1
- package/clients/ast-grep-types.js +9 -0
- package/clients/ast-grep-types.ts +106 -0
- package/clients/auto-loop.js +10 -0
- package/clients/auto-loop.ts +14 -1
- package/clients/biome-client.js +81 -19
- package/clients/biome-client.ts +103 -22
- package/clients/bus/bus.js +191 -0
- package/clients/bus/bus.ts +251 -0
- package/clients/bus/events.js +214 -0
- package/clients/bus/events.ts +279 -0
- package/clients/bus/index.js +8 -0
- package/clients/bus/index.ts +9 -0
- package/clients/bus/integration.js +158 -0
- package/clients/bus/integration.ts +214 -0
- package/clients/complexity-client.js +13 -7
- package/clients/complexity-client.ts +13 -7
- package/clients/config-validator.js +465 -0
- package/clients/config-validator.ts +558 -0
- package/clients/dependency-checker.js +4 -10
- package/clients/dependency-checker.ts +4 -10
- package/clients/dispatch/__tests__/autofix-integration.test.js +245 -0
- package/clients/dispatch/__tests__/autofix-integration.test.ts +300 -0
- package/clients/dispatch/__tests__/runner-registration.test.js +236 -0
- package/clients/dispatch/__tests__/runner-registration.test.ts +282 -0
- package/clients/dispatch/bus-dispatcher.js +177 -0
- package/clients/dispatch/bus-dispatcher.ts +251 -0
- package/clients/dispatch/dispatcher.edge.test.js +82 -0
- package/clients/dispatch/dispatcher.edge.test.ts +100 -0
- package/clients/dispatch/dispatcher.format.test.js +46 -0
- package/clients/dispatch/dispatcher.format.test.ts +58 -0
- package/clients/dispatch/dispatcher.inline.test.js +74 -0
- package/clients/dispatch/dispatcher.inline.test.ts +93 -0
- package/clients/dispatch/dispatcher.js +19 -53
- package/clients/dispatch/dispatcher.ts +20 -67
- package/clients/dispatch/plan.js +9 -4
- package/clients/dispatch/plan.ts +9 -4
- package/clients/dispatch/runners/architect.js +21 -7
- package/clients/dispatch/runners/architect.test.js +138 -0
- package/clients/dispatch/runners/architect.test.ts +162 -0
- package/clients/dispatch/runners/architect.ts +22 -7
- package/clients/dispatch/runners/ast-grep-napi.js +462 -0
- package/clients/dispatch/runners/ast-grep-napi.test.js +111 -0
- package/clients/dispatch/runners/ast-grep-napi.test.ts +133 -0
- package/clients/dispatch/runners/ast-grep-napi.ts +506 -0
- package/clients/dispatch/runners/ast-grep.js +62 -19
- package/clients/dispatch/runners/ast-grep.ts +70 -18
- package/clients/dispatch/runners/biome.js +29 -53
- package/clients/dispatch/runners/biome.ts +29 -63
- package/clients/dispatch/runners/config-validation.js +67 -0
- package/clients/dispatch/runners/config-validation.ts +82 -0
- package/clients/dispatch/runners/go-vet.js +4 -28
- package/clients/dispatch/runners/go-vet.ts +4 -32
- package/clients/dispatch/runners/index.js +30 -10
- package/clients/dispatch/runners/index.ts +30 -10
- package/clients/dispatch/runners/oxlint.js +141 -0
- package/clients/dispatch/runners/oxlint.test.js +230 -0
- package/clients/dispatch/runners/oxlint.test.ts +303 -0
- package/clients/dispatch/runners/oxlint.ts +175 -0
- package/clients/dispatch/runners/pyright.js +40 -70
- package/clients/dispatch/runners/pyright.test.js +16 -2
- package/clients/dispatch/runners/pyright.test.ts +14 -2
- package/clients/dispatch/runners/pyright.ts +48 -91
- package/clients/dispatch/runners/python-slop.js +97 -0
- package/clients/dispatch/runners/python-slop.test.js +203 -0
- package/clients/dispatch/runners/python-slop.test.ts +298 -0
- package/clients/dispatch/runners/python-slop.ts +124 -0
- package/clients/dispatch/runners/ruff.js +18 -71
- package/clients/dispatch/runners/ruff.ts +19 -79
- package/clients/dispatch/runners/rust-clippy.js +28 -32
- package/clients/dispatch/runners/rust-clippy.ts +29 -31
- package/clients/dispatch/runners/scan_codebase.test.js +89 -0
- package/clients/dispatch/runners/scan_codebase.test.ts +105 -0
- package/clients/dispatch/runners/shellcheck.js +147 -0
- package/clients/dispatch/runners/shellcheck.test.js +98 -0
- package/clients/dispatch/runners/shellcheck.test.ts +129 -0
- package/clients/dispatch/runners/shellcheck.ts +188 -0
- package/clients/dispatch/runners/similarity.js +230 -0
- package/clients/dispatch/runners/similarity.ts +339 -0
- package/clients/dispatch/runners/spellcheck.js +106 -0
- package/clients/dispatch/runners/spellcheck.test.js +158 -0
- package/clients/dispatch/runners/spellcheck.test.ts +214 -0
- package/clients/dispatch/runners/spellcheck.ts +136 -0
- package/clients/dispatch/runners/tree-sitter.js +107 -0
- package/clients/dispatch/runners/tree-sitter.ts +135 -0
- package/clients/dispatch/runners/ts-lsp.js +104 -33
- package/clients/dispatch/runners/ts-lsp.ts +120 -38
- package/clients/dispatch/runners/ts-slop.js +113 -0
- package/clients/dispatch/runners/ts-slop.test.js +180 -0
- package/clients/dispatch/runners/ts-slop.test.ts +230 -0
- package/clients/dispatch/runners/ts-slop.ts +142 -0
- package/clients/dispatch/runners/utils/diagnostic-parsers.js +134 -0
- package/clients/dispatch/runners/utils/diagnostic-parsers.ts +186 -0
- package/clients/dispatch/runners/utils/runner-helpers.js +115 -0
- package/clients/dispatch/runners/utils/runner-helpers.ts +167 -0
- package/clients/dispatch/runners/utils.js +2 -4
- package/clients/dispatch/runners/utils.ts +2 -4
- package/clients/dispatch/types.ts +1 -1
- package/clients/dispatch/utils/format-utils.js +49 -0
- package/clients/dispatch/utils/format-utils.ts +60 -0
- package/clients/dogfood.test.js +201 -0
- package/clients/dogfood.test.ts +269 -0
- package/clients/file-time.js +152 -0
- package/clients/file-time.ts +208 -0
- package/clients/file-utils.js +40 -0
- package/clients/file-utils.ts +44 -0
- package/clients/fix-scanners.js +10 -20
- package/clients/fix-scanners.ts +10 -22
- package/clients/format-service.js +172 -0
- package/clients/format-service.ts +254 -0
- package/clients/formatters.js +435 -0
- package/clients/formatters.ts +508 -0
- package/clients/go-client.js +5 -14
- package/clients/go-client.ts +5 -13
- package/clients/installer/index.js +356 -0
- package/clients/installer/index.ts +426 -0
- package/clients/jscpd-client.js +11 -9
- package/clients/jscpd-client.ts +12 -8
- package/clients/knip-client.js +3 -7
- package/clients/knip-client.ts +3 -6
- package/clients/lsp/__tests__/client.test.js +325 -0
- package/clients/lsp/__tests__/client.test.ts +434 -0
- package/clients/lsp/__tests__/config.test.js +166 -0
- package/clients/lsp/__tests__/config.test.ts +209 -0
- package/clients/lsp/__tests__/error-recovery.test.js +213 -0
- package/clients/lsp/__tests__/error-recovery.test.ts +279 -0
- package/clients/lsp/__tests__/integration.test.js +127 -0
- package/clients/lsp/__tests__/integration.test.ts +160 -0
- package/clients/lsp/__tests__/launch.test.js +260 -0
- package/clients/lsp/__tests__/launch.test.ts +329 -0
- package/clients/lsp/__tests__/server.test.js +259 -0
- package/clients/lsp/__tests__/server.test.ts +332 -0
- package/clients/lsp/__tests__/service.test.js +417 -0
- package/clients/lsp/__tests__/service.test.ts +499 -0
- package/clients/lsp/client.js +235 -0
- package/clients/lsp/client.ts +328 -0
- package/clients/lsp/config.js +115 -0
- package/clients/lsp/config.ts +149 -0
- package/clients/lsp/index.js +222 -0
- package/clients/lsp/index.ts +280 -0
- package/clients/lsp/installer/index.js +391 -0
- package/clients/lsp/interactive-install.js +210 -0
- package/clients/lsp/interactive-install.ts +251 -0
- package/clients/lsp/language.js +170 -0
- package/clients/lsp/language.ts +216 -0
- package/clients/lsp/launch.js +174 -0
- package/clients/lsp/launch.ts +240 -0
- package/clients/lsp/lsp/launch.js +116 -0
- package/clients/lsp/lsp/server.js +532 -0
- package/clients/lsp/lsp-index.js +10 -0
- package/clients/lsp/lsp-index.ts +11 -0
- package/clients/lsp/path-utils.js +48 -0
- package/clients/lsp/path-utils.ts +52 -0
- package/clients/lsp/server.js +615 -0
- package/clients/lsp/server.ts +800 -0
- package/clients/lsp/test-py-spawn/requirements.txt +1 -0
- package/clients/lsp/test-py-spawn/test.py +3 -0
- package/clients/lsp/test-py-svc/requirements.txt +1 -0
- package/clients/lsp/test-py-svc/test.py +3 -0
- package/clients/lsp/test-python-project/requirements.txt +1 -0
- package/clients/lsp/test-python-project/test.py +5 -0
- package/clients/metrics-history.js +2 -2
- package/clients/metrics-history.ts +2 -2
- package/clients/production-readiness.js +522 -0
- package/clients/production-readiness.ts +556 -0
- package/clients/project-index.js +255 -0
- package/clients/project-index.ts +383 -0
- package/clients/project-metadata.js +531 -0
- package/clients/project-metadata.ts +624 -0
- package/clients/ruff-client.js +56 -16
- package/clients/ruff-client.ts +72 -15
- package/clients/runner-tracker.js +152 -0
- package/clients/runner-tracker.ts +213 -0
- package/clients/rust-client.js +4 -11
- package/clients/rust-client.ts +5 -11
- package/clients/safe-spawn.js +96 -0
- package/clients/safe-spawn.ts +128 -0
- package/clients/scan-architectural-debt.js +3 -6
- package/clients/scan-architectural-debt.ts +3 -6
- package/clients/scan-utils.js +5 -20
- package/clients/scan-utils.ts +5 -29
- package/clients/secrets-scanner.js +3 -17
- package/clients/secrets-scanner.ts +4 -20
- package/clients/services/__tests__/effect-integration.test.js +86 -0
- package/clients/services/__tests__/effect-integration.test.ts +111 -0
- package/clients/services/effect-integration.js +194 -0
- package/clients/services/effect-integration.ts +268 -0
- package/clients/services/index.js +7 -0
- package/clients/services/index.ts +8 -0
- package/clients/services/runner-service.js +105 -0
- package/clients/services/runner-service.ts +179 -0
- package/clients/sg-runner.js +87 -13
- package/clients/sg-runner.ts +97 -13
- package/clients/state-matrix.js +160 -0
- package/clients/state-matrix.ts +202 -0
- package/clients/subprocess-client.js +10 -9
- package/clients/subprocess-client.ts +10 -8
- package/clients/test-runner-client.js +3 -7
- package/clients/test-runner-client.ts +3 -6
- package/clients/tool-availability.js +4 -10
- package/clients/tool-availability.ts +4 -9
- package/clients/tree-sitter-client.js +564 -0
- package/clients/tree-sitter-client.ts +797 -0
- package/clients/tree-sitter-query-loader.js +355 -0
- package/clients/tree-sitter-query-loader.ts +425 -0
- package/clients/type-coverage-client.js +3 -7
- package/clients/type-coverage-client.ts +3 -6
- package/clients/typescript-client.codefix.test.js +157 -0
- package/clients/typescript-client.codefix.test.ts +186 -0
- package/clients/typescript-client.js +43 -0
- package/clients/typescript-client.ts +98 -0
- package/commands/booboo.js +799 -219
- package/commands/booboo.ts +1004 -225
- package/commands/clients/ast-grep-client.js +250 -0
- package/commands/clients/ast-grep-parser.js +86 -0
- package/commands/clients/ast-grep-rule-manager.js +91 -0
- package/commands/clients/ast-grep-types.js +9 -0
- package/commands/clients/biome-client.js +380 -0
- package/commands/clients/complexity-client.js +667 -0
- package/commands/clients/file-kinds.js +177 -0
- package/commands/clients/file-utils.js +40 -0
- package/commands/clients/jscpd-client.js +169 -0
- package/commands/clients/knip-client.js +211 -0
- package/commands/clients/ruff-client.js +297 -0
- package/commands/clients/safe-spawn.js +88 -0
- package/commands/clients/scan-utils.js +83 -0
- package/commands/clients/sg-runner.js +190 -0
- package/commands/clients/types.js +11 -0
- package/commands/clients/typescript-client.js +505 -0
- package/commands/fix-from-booboo.js +398 -0
- package/commands/fix-from-booboo.ts +485 -0
- package/commands/fix-simplified.js +618 -0
- package/commands/fix-simplified.ts +768 -0
- package/commands/rate.js +10 -14
- package/commands/rate.ts +9 -16
- package/default-architect.yaml +59 -15
- package/index.ts +342 -429
- package/package.json +16 -3
- package/rules/ast-grep-rules/rules/empty-catch.yml +38 -13
- package/rules/ast-grep-rules/rules/no-array-constructor.yml +1 -0
- package/rules/ast-grep-rules/rules/no-debugger.yml +2 -0
- package/rules/python-slop-rules/.sgconfig.yml +4 -0
- package/rules/python-slop-rules/rules/slop-rules.yml +647 -0
- package/rules/tree-sitter-queries/python/bare-except.yml +54 -0
- package/rules/tree-sitter-queries/python/eval-exec.yml +50 -0
- package/rules/tree-sitter-queries/python/is-vs-equals.yml +60 -0
- package/rules/tree-sitter-queries/python/mutable-default-arg.yml +57 -0
- package/rules/tree-sitter-queries/python/unreachable-except.yml +60 -0
- package/rules/tree-sitter-queries/python/wildcard-import.yml +46 -0
- package/rules/tree-sitter-queries/tsx/dangerously-set-inner-html.yml +63 -0
- package/rules/tree-sitter-queries/typescript/await-in-loop.yml +56 -0
- package/rules/tree-sitter-queries/typescript/console-statement.yml +47 -0
- package/rules/tree-sitter-queries/typescript/debugger.yml +47 -0
- package/rules/tree-sitter-queries/typescript/deep-nesting.yml +117 -0
- package/rules/tree-sitter-queries/typescript/deep-promise-chain.yml +73 -0
- package/rules/tree-sitter-queries/typescript/empty-catch.yml +64 -0
- package/rules/tree-sitter-queries/typescript/eval.yml +48 -0
- package/rules/tree-sitter-queries/typescript/hardcoded-secrets.yml +78 -0
- package/rules/tree-sitter-queries/typescript/long-parameter-list.yml +62 -0
- package/rules/tree-sitter-queries/typescript/mixed-async-styles.yml +49 -0
- package/rules/tree-sitter-queries/typescript/nested-ternary.yml +45 -0
- package/rules/ts-slop-rules/.sgconfig.yml +4 -0
- package/rules/ts-slop-rules/rules/in-correct-optional-input-type.yml +10 -0
- package/rules/ts-slop-rules/rules/jwt-no-verify.yml +13 -0
- package/rules/ts-slop-rules/rules/no-architecture-violation.yml +10 -0
- package/rules/ts-slop-rules/rules/no-case-declarations.yml +10 -0
- package/rules/ts-slop-rules/rules/no-dangerously-set-inner-html.yml +10 -0
- package/rules/ts-slop-rules/rules/no-debugger.yml +10 -0
- package/rules/ts-slop-rules/rules/no-dupe-args.yml +10 -0
- package/rules/ts-slop-rules/rules/no-dupe-class-members.yml +10 -0
- package/rules/ts-slop-rules/rules/no-dupe-keys.yml +10 -0
- package/rules/ts-slop-rules/rules/no-eval.yml +13 -0
- package/rules/ts-slop-rules/rules/no-hardcoded-secrets.yml +12 -0
- package/rules/ts-slop-rules/rules/no-implied-eval.yml +12 -0
- package/rules/ts-slop-rules/rules/no-inner-html.yml +13 -0
- package/rules/ts-slop-rules/rules/no-javascript-url.yml +10 -0
- package/rules/ts-slop-rules/rules/no-mutable-default.yml +10 -0
- package/rules/ts-slop-rules/rules/no-nested-links.yml +12 -0
- package/rules/ts-slop-rules/rules/no-new-symbol.yml +10 -0
- package/rules/ts-slop-rules/rules/no-new-wrappers.yml +13 -0
- package/rules/ts-slop-rules/rules/no-open-redirect.yml +16 -0
- package/rules/ts-slop-rules/rules/slop-rules.yml +455 -0
- package/rules/ts-slop-rules/rules/weak-rsa-key.yml +12 -0
- package/skills/ast-grep/SKILL.md +182 -0
- package/clients/dispatch/runners/secrets.js +0 -109
- package/commands/fix.js +0 -244
- package/commands/fix.ts +0 -373
- package/rules/ast-grep-rules/rules/no-lonely-if.yml +0 -13
|
@@ -0,0 +1,667 @@
|
|
|
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
|
+
}
|