Getting Started
Install tsmetrics and run your first analysis in under two minutes.
Prerequisites
- Rust 1.70+ (install via rustup.rs)
- TypeScript source files (
.tsor.tsx)
Installation
npm / npx (no Rust required)
bash
# Run directly without installing npx tsmetrics analyze ./src # Or install globally npm install -g tsmetrics
Cargo (from source)
bash
git clone https://github.com/gabrielrf97/tsmetrics.git cd tsmetrics cargo install --path . # Or build without installing: cargo build --release # Binary at target/release/tsmetrics
GitHub Releases
Pre-built binaries for Linux, macOS, and Windows are available on the GitHub releases page.
First Analysis
Point tsmetrics at a TypeScript file or directory:
bash
# Analyze a single file tsmetrics analyze src/utils.ts # Analyze an entire directory (recursive) tsmetrics analyze src/ # Analyze multiple paths tsmetrics analyze src/ lib/ types/
Example output (table format)
Output is organized into three sections. The tables below are scrollable on narrow screens.
Section 1 — Function Metrics
Analyzed 4 file(s) — 33 function(s) — 289 LOC total
=== Function Metrics ===
┌──────────────────────────────┬───────────────┬──────┬─────┬──────┬────────────┬─────────┬────────┬─────┬──────┬──────┐
│ File ┆ Function ┆ Line ┆ LOC ┆ SLOC ┆ Complexity ┆ Nesting ┆ Params ┆ HV ┆ MI ┆ CDep │
╞══════════════════════════════╪═══════════════╪══════╪═════╪══════╪════════════╪═════════╪════════╪═════╪══════╪══════╡
│ src/utils.ts ┆ parseConfig ┆ 4 ┆ 12 ┆ 12 ┆ 5 ┆ 2 ┆ 1 ┆ 220 ┆ 59.4 ┆ 1 │
│ src/utils.ts ┆ validateInput ┆ 17 ┆ 12 ┆ 12 ┆ 12 ┆ 2 ┆ 2 ┆ 375 ┆ 56.8 ┆ 1 │
│ src/processor.ts ┆ processData ┆ 8 ┆ 19 ┆ 19 ┆ 6 ┆ 3 ┆ 3 ┆ 291 ┆ 54.0 ┆ 1 │
└──────────────────────────────┴───────────────┴──────┴─────┴──────┴────────────┴─────────┴────────┴─────┴──────┴──────┘
Section 2 — React / FP Component Metrics
=== React / FP Component Metrics ===
┌──────────────────────────────┬───────────┬──────┬───────┬─────────┬────────────┬─────────────┬───────────┬──────┐
│ File ┆ Component ┆ Line ┆ Hooks ┆ Effects ┆ EffDensity ┆ RenderCmplx ┆ PropDrill ┆ CRS │
╞══════════════════════════════╪═══════════╪══════╪═══════╪═════════╪════════════╪═════════════╪═══════════╪══════╡
│ src/components/UserCard.tsx ┆ UserCard ┆ 51 ┆ 4 ┆ 1 ┆ 0.059 ┆ 0 ┆ 0 ┆ 8.0 │
│ src/components/Dashboard.tsx ┆ Dashboard ┆ 72 ┆ 8 ┆ 2 ┆ 0.062 ┆ 0 ┆ 0 ┆ 19.0 │
└──────────────────────────────┴───────────┴──────┴───────┴─────────┴────────────┴─────────────┴───────────┴──────┘
Section 3 — File Metrics
=== File Metrics ===
┌──────────────────────────────┬─────┬──────┬─────┬─────────┬──────────┬────────────┬──────────┬────────┬─────────────┐
│ File ┆ LOC ┆ SLOC ┆ Fns ┆ Classes ┆ TechDebt ┆ TD/100SLOC ┆ Cohesion ┆ FanOut ┆ PureFnRatio │
╞══════════════════════════════╪═════╪══════╪═════╪═════════╪══════════╪════════════╪══════════╪════════╪═════════════╡
│ src/utils.ts ┆ 60 ┆ 50 ┆ 2 ┆ 0 ┆ 12.40 ┆ 24.80 ┆ 0.85 ┆ 2 ┆ 0.50 │
│ src/processor.ts ┆ 107 ┆ 93 ┆ 19 ┆ 0 ┆ 53.78 ┆ 57.83 ┆ 0.10 ┆ 1 ┆ 0.63 │
│ src/components/UserCard.tsx ┆ 42 ┆ 37 ┆ 4 ┆ 0 ┆ 15.30 ┆ 41.35 ┆ 0.50 ┆ 1 ┆ 0.25 │
│ src/components/Dashboard.tsx ┆ 80 ┆ 68 ┆ 8 ┆ 0 ┆ 38.92 ┆ 57.24 ┆ 0.20 ┆ 2 ┆ 0.25 │
└──────────────────────────────┴─────┴──────┴─────┴─────────┴──────────┴────────────┴──────────┴────────┴─────────────┘
Understanding the output
Function Metrics
| Column | Description |
|---|---|
| File | Path to the TypeScript source file |
| Function | Function or method name (<anonymous> for unnamed) |
| Line | 1-based line number where the function is declared |
| LOC | Lines of Code (including blank and comment lines) |
| SLOC | Source Lines of Code (blank and comment lines excluded) |
| Complexity | Cyclomatic Complexity — color-coded green (<5) / yellow (5–9) / red (≥10) |
| Nesting | Maximum nesting depth |
| Params | Number of formal parameters |
| HV | Halstead Volume — token-based size estimate |
| MI | Maintainability Index (0–100, higher is better) |
| CDep | Closure Depth — maximum callback/closure nesting level |
React / FP Component Metrics
| Column | Description |
|---|---|
| Component | React component or FP function name |
| Hooks | Total hook calls (useState, useEffect, custom hooks, etc.) |
| Effects | Number of useEffect / useLayoutEffect calls |
| EffDensity | Effects per SLOC — how effect-heavy the component is |
| RenderCmplx | Render complexity — conditional branches in JSX |
| PropDrill | Prop drilling depth — levels of prop passing through intermediaries |
| CRS | Component Responsibility Score — composite size/complexity signal |
File Metrics
| Column | Description |
|---|---|
| LOC / SLOC | Total lines / source lines in the file |
| Fns | Number of functions (including anonymous) |
| Classes | Number of class declarations |
| TechDebt | Aggregate technical debt score |
| TD/100SLOC | Technical debt normalized per 100 source lines |
| Cohesion | Module cohesion (0–1, higher is better) |
| FanOut | Number of distinct external modules imported |
| PureFnRatio | Fraction of functions with no side effects (0–1) |
Filtering results
bash
# Only show functions with CC ≥ 10 tsmetrics analyze src/ --min-complexity 10 # Only show functions with ≥ 50 LOC tsmetrics analyze src/ --min-loc 50 # Show verbose output (skipped files reported to stderr) tsmetrics analyze src/ --verbose # Show timing information tsmetrics analyze src/ --timing
Next steps
- Configure thresholds in tsmetrics.yaml
- Explore all available metrics
- Learn about design smell detection