{
  "schema": "leumas.docs.page/1",
  "id": "adapter:domain/code-metrics",
  "slug": "adapters/domain/code-metrics",
  "kind": "capabilities",
  "bucket": "package",
  "title": "code-metrics",
  "name": "Code Metrics",
  "eyebrow": null,
  "chip": null,
  "summary": "Static code-metrics pack: language-agnostic complexity and quality analysis by heuristic parsing (no compiler, no AST library). Counts cyclomatic complexity from branch/decision keywords...",
  "keywords": [
    "code-metrics",
    "language-agnostic",
    "complexity",
    "cyclomatic",
    "halstead",
    "operands",
    "maintainability",
    "duplicationestimate"
  ],
  "audience": "both",
  "funnel": {
    "product": null,
    "cta": null
  },
  "body": "# code-metrics\n\n**Language-agnostic static code metrics** by heuristic parsing — no compiler, no AST library. Feed it\nsource code and it measures complexity, size, and quality. Works on JavaScript/TypeScript, Python,\nJava, C/C++, C#, Go, Rust, Ruby, and PHP (comment syntax + keyword set per language, auto-detected).\n\nIt is an *intelligent microservice*: every tool has a deterministic heuristic core that runs fully\noffline; `summary` can optionally add a plain-English AI review and silently falls back to the\nscorecard. Results are tagged `{ mode: 'heuristic' | 'llm' }` where a model can contribute.\n\n## How it works\n\nComments and string/char literals are stripped **first** (length-preserving), so keyword counts never\nfire inside comments or strings. Then:\n\n- **cyclomatic** = `1 + decision points` (branch keywords + `&&`/`||` + ternary) — the McCabe approximation.\n- **halstead** — distinct/total operators & operands → volume, difficulty, effort, time, estimated bugs.\n- **maintainabilityIndex** — the SEI/Visual-Studio formula, normalized to `0..100` with a rating.\n- **loc** — SLOC vs comment vs blank, and comment ratio.\n- **duplicationEstimate** — k-line shingling: fraction of repeated line-windows (cheap clone density).\n- **functionCount** — multi-language signature patterns + anonymous callbacks + avg lines/function.\n- **summary** — all of the above plus a composite `0..100` health score, letter grade, and flags.\n\n## Tools\n\n| Tool | Output |\n|---|---|\n| `cyclomatic` | `{ complexity, decisionPoints, rating, breakdown }` |\n| `maintainabilityIndex` | `{ maintainabilityIndex, rating, inputs }` |\n| `halstead` | operators/operands, `volume`, `difficulty`, `effort`, `estimatedBugs` |\n| `loc` | `{ total, source, comment, blank, commentRatio }` |\n| `duplicationEstimate` | `{ duplicationRatio, duplicatedWindows, rating }` |\n| `functionCount` | `{ functions, anonymousCallbacks, avgLinesPerFunction }` |\n| `summary` | composite scorecard + `healthScore` + `grade` + `flags` (+ optional `review`) |\n\n## Usage\n\n```js\nimport metrics from './index.js';\n\nmetrics.adapters.cyclomatic({ code: 'function f(x){ if(x>0){for(;;){}} return x && 1; }' });\n// -> { complexity: 4, decisionPoints: 3, rating: 'simple', breakdown: { if:1, for:1, '&&/||':1 } }\n\nmetrics.adapters.summary({ code: sourceString, language: 'python' });\n// -> { healthScore: 72, grade: 'B', flags: [...], metrics: { loc, cyclomatic, halstead, ... } }\n```\n\n## DRY boundary\n\nMeasures **source code** (complexity/quality). Distinct from **`changelog`** (commit/release tooling)\nand the **`toolkit`** algorithm/graph utilities. Self-contained (Node built-ins + `../_shared/llm.js`).\n",
  "source": {
    "path": "shared/engines/adapters/domain/code-metrics/README.md",
    "blobSha": "",
    "commit": "",
    "committedAt": "",
    "provenance": "no-git",
    "bytes": 2902,
    "hash": "2c13238d944e50a91f393e126e0dc25268280ef3"
  },
  "urls": {
    "html": "/p/adapters/domain/code-metrics",
    "json": "/docs/adapters/domain/code-metrics.json",
    "md": "/docs/adapters/domain/code-metrics.md"
  },
  "links": {
    "composes": [],
    "usedBy": [],
    "product": [],
    "howTo": [],
    "skills": []
  }
}
