debtmap 0.21.0

Code complexity and technical debt analyzer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# Language Configuration

Configure language-specific analysis behavior in debtmap.

## Overview

Debtmap analyzes source code for technical debt and complexity issues. Language configuration allows you to:

- Enable or disable specific languages
- Toggle analysis features per language
- Configure language-specific detection behavior

## Supported Languages

**Full Support (AST-Based Analysis):**
- **Rust** - Full AST parsing with `syn`
- **Python** - Full AST parsing with tree-sitter
- **JavaScript** - Tree-sitter parsing for modern JS, JSX, modules, callbacks, promises, and async workflows
- **TypeScript** - Tree-sitter parsing for TS/TSX syntax, type-oriented patterns, modules, and async workflows
- **Go** - Tree-sitter parsing for packages, imports, functions, receiver methods, complexity, generated-code handling, and advisory Go debt signals
- **Solidity** - Tree-sitter parsing for contracts, interfaces, libraries, modifiers, complexity metrics, import/inheritance extraction, Foundry test detection, and heuristic smart-contract security advisories

**Source**: `src/core/mod.rs` (Language enum) and `src/config/languages.rs` (language-specific configuration fields)

The `Language` enum in the codebase currently supports:

```rust
// From src/core/mod.rs
pub enum Language {
    Rust,
    Python,
    JavaScript,
    TypeScript,
    Go,
    Solidity,
    Unknown,
}
```

**Language Detection**: Debtmap determines file language by extension:
- `.rs` files are analyzed as Rust
- `.py` and `.pyw` files are analyzed as Python
- `.js`, `.mjs`, `.cjs`, and `.jsx` files are analyzed as JavaScript
- `.ts`, `.mts`, `.cts`, and `.tsx` files are analyzed as TypeScript
- `.go` files are analyzed as Go
- `.sol` files are analyzed as Solidity (including Foundry `*.t.sol` test files)

**Source**: `src/core/mod.rs`

```rust
pub fn from_extension(ext: &str) -> Self {
    match ext {
        "rs" => Language::Rust,
        "py" | "pyw" => Language::Python,
        "js" | "mjs" | "cjs" | "jsx" => Language::JavaScript,
        "ts" | "mts" | "cts" | "tsx" => Language::TypeScript,
        "go" => Language::Go,
        "sol" => Language::Solidity,
        _ => Language::Unknown,
    }
}
```

## Configuration Structure

The `[languages]` section in your `debtmap.toml` configures language analysis.

**Source**: `src/config/languages.rs:4-22` (LanguagesConfig struct)

```toml
[languages]
enabled = ["rust", "python", "javascript", "typescript", "go", "solidity"]

[languages.rust]
detect_dead_code = false       # Disabled by default for Rust
detect_complexity = true
detect_duplication = true

[languages.python]
detect_dead_code = true
detect_complexity = true
detect_duplication = true

[languages.javascript]
detect_dead_code = true
detect_complexity = true
detect_duplication = true

[languages.typescript]
detect_dead_code = true
detect_complexity = true
detect_duplication = true

[languages.go]
detect_dead_code = true
detect_complexity = true
detect_duplication = true
generated_code = "suppress_debt"

[languages.solidity]
detect_dead_code = false
detect_complexity = true
detect_duplication = true
vendor_code = "suppress_debt"
large_contract_threshold = 20

[languages.solidity.security]
tx_origin = true
reentrancy_heuristic = true
unchecked_calls = true
delegatecall = true
selfdestruct = true
assembly_blocks = true
unbounded_loops = true
missing_access_control = true
hardcoded_addresses = true
floating_pragma = true
large_contracts = true
unchecked_arithmetic = true
unsafe_erc20_transfer = true
push_without_length_cap = true
block_timestamp_dependency = true
tx_gas_price_dependency = true
encode_packed_collision = true
delegatecall_in_constructor = true
```

## Language-Specific Features

Each language supports three configurable feature toggles:

**Source**: `src/config/languages.rs:24-38` (LanguageFeatures struct)

| Feature | Description | Default |
|---------|-------------|---------|
| `detect_dead_code` | Identify unused code paths | `true` (except Rust) |
| `detect_complexity` | Calculate cyclomatic and cognitive complexity | `true` |
| `detect_duplication` | Find code duplication patterns | `true` |

### Rust Configuration

```toml
[languages.rust]
detect_dead_code = false        # Disabled: rustc already reports unused code
detect_complexity = true
detect_duplication = true
```

**Why dead code detection is disabled for Rust**: The Rust compiler (`rustc`) already provides excellent unused code warnings via `#[warn(dead_code)]`. Enabling debtmap's dead code detection would duplicate these warnings without adding value.

**Source**: `src/config/accessors.rs:104-111`

```rust
Language::Rust => {
    languages_config
        .and_then(|lc| lc.rust.clone())
        .unwrap_or(LanguageFeatures {
            detect_dead_code: false, // Rust dead code detection disabled by default
            detect_complexity: true,
            detect_duplication: true,
        })
}
```

### Python Configuration

```toml
[languages.python]
detect_dead_code = true
detect_complexity = true
detect_duplication = true
```

All features enabled by default for Python. Dead code detection is valuable since Python lacks a compiler phase that catches unused code.

**Source**: `src/config/accessors.rs:113-115`

### JavaScript Configuration

```toml
[languages.javascript]
detect_dead_code = true
detect_complexity = true
detect_duplication = true
```

JavaScript analysis covers modern ES syntax, JSX, callbacks, promises, and async workflow patterns.

### TypeScript Configuration

```toml
[languages.typescript]
detect_dead_code = true
detect_complexity = true
detect_duplication = true
```

TypeScript analysis covers TS/TSX syntax, modules, type-oriented patterns, and async workflow patterns.

### Go Configuration

```toml
[languages.go]
detect_dead_code = true
detect_complexity = true
detect_duplication = true
generated_code = "suppress_debt"
```

Go analysis covers packages, imports, top-level functions, receiver methods, complexity, same-package call relationships, and advisory Go debt signals. Generated Go files are parsed by default, but generated-file debt is suppressed unless `generated_code = "analyze"` is configured. Use `generated_code = "exclude"` to skip generated Go files during config-driven batch analysis.

### Solidity Configuration

```toml
[languages.solidity]
detect_dead_code = false
detect_complexity = true
detect_duplication = true
vendor_code = "suppress_debt"
large_contract_threshold = 20

[languages.solidity.security]
tx_origin = true
reentrancy_heuristic = true
unchecked_calls = true
delegatecall = true
selfdestruct = true
assembly_blocks = true
unbounded_loops = true
missing_access_control = true
hardcoded_addresses = true
floating_pragma = true
large_contracts = true
unchecked_arithmetic = true
unsafe_erc20_transfer = true
push_without_length_cap = true
block_timestamp_dependency = true
tx_gas_price_dependency = true
encode_packed_collision = true
delegatecall_in_constructor = true
```

Solidity analysis covers contracts, interfaces, libraries, functions, modifiers, constructors, complexity metrics, imports, inheritance, and heuristic security advisories (for example `tx.origin` usage, unchecked low-level calls, assembly blocks, and reentrancy heuristics). Foundry test files (`*.t.sol`), Hardhat-style `*.test.sol` files, and contracts importing `forge-std/Test.sol` or `hardhat/console.sol` are detected as tests; complexity debt is suppressed for test functions.

Generated/vendor Solidity handling uses the same modes as Go:

| `vendor_code` | Behavior |
|---------------|----------|
| `"analyze"` | Analyze generated/vendor files and emit debt normally |
| `"suppress_debt"` | Preserve metrics but suppress debt for generated/vendor files |
| `"exclude"` | Skip generated/vendor Solidity files during config-driven batch analysis |

Solidity supports standard suppression comments:

```solidity
// debtmap:ignore-next-line[smell] -- reviewed low-level call
target.call(data);

// debtmap:ignore[complexity] -- compact state machine
function settle(uint256 state) public { ... }
```

## Enabling Languages

Specify which languages to analyze with the `enabled` array:

```toml
[languages]
enabled = ["rust", "python", "javascript", "typescript", "go", "solidity"]
```

The documented and implemented user-facing language set is Rust, Python, JavaScript, TypeScript, Go, and Solidity.

## Feature Defaults

**Source**: `src/config/languages.rs:40-61`

```rust
impl Default for LanguageFeatures {
    fn default() -> Self {
        Self {
            detect_dead_code: true,   // default_detect_dead_code()
            detect_complexity: true,  // default_detect_complexity()
            detect_duplication: true, // default_detect_duplication()
        }
    }
}
```

When no language-specific configuration is provided, debtmap uses these defaults (with the Rust dead code exception handled at the accessor level).

## Using Language Configuration

### Analyze Only Rust Code

```toml
[languages]
enabled = ["rust"]

[languages.rust]
detect_dead_code = false
detect_complexity = true
detect_duplication = true
```

### Full Python Analysis

```toml
[languages]
enabled = ["python"]

[languages.python]
detect_dead_code = true
detect_complexity = true
detect_duplication = true
```

### Multi-Language Projects

```toml
[languages]
enabled = ["rust", "python", "javascript", "typescript"]

# Different settings per language
[languages.rust]
detect_dead_code = false        # Trust rustc
detect_complexity = true
detect_duplication = true

[languages.python]
detect_dead_code = true         # Python needs this
detect_complexity = true
detect_duplication = false      # Skip if not needed

[languages.javascript]
detect_dead_code = true
detect_complexity = true
detect_duplication = true

[languages.typescript]
detect_dead_code = true
detect_complexity = true
detect_duplication = true
```

## Complexity Calculations by Language

Debtmap uses language-specific complexity analyzers:

**Source**: `src/complexity/languages/rust.rs` and `src/analyzers/implementations.rs`

### Rust Complexity

- Uses `syn` for AST parsing
- Calculates cyclomatic complexity from control flow
- Tracks cognitive complexity with nesting depth penalties
- Detects pattern match complexity with entropy analysis

### Python Complexity

- Uses tree-sitter for AST parsing
- Handles Python-specific constructs (decorators, comprehensions)
- Tracks class method complexity
- Analyzes exception handling patterns

### JavaScript Complexity

- Uses tree-sitter for AST parsing
- Handles ES modules, JSX, callbacks, promises, and async workflows
- Tracks function and class method complexity
- Detects JavaScript-specific functional and async patterns

### TypeScript Complexity

- Uses tree-sitter for TS/TSX parsing
- Handles modern module syntax and frontend/server patterns
- Tracks type-oriented complexity and async workflows
- Detects broad type-safety and promise-related patterns

## Integration with Other Configuration

Language configuration interacts with other debtmap settings:

### Ignore Patterns

File exclusions in `[ignore]` apply before language detection:

```toml
[ignore]
patterns = [
    "target/**",       # Rust build output
    "venv/**",         # Python virtual environment
    "out/**",          # Foundry build output
    "cache/**",        # Foundry/Hardhat cache
    "artifacts/**",    # Hardhat build output
    "*.min.js",        # Minified frontend artifacts
]
```

See [Thresholds Configuration](thresholds.md) for complexity thresholds that can be language-aware.

### Classification

Semantic classification (function roles like "orchestrator", "pure_logic") operates independently of language but uses language-specific patterns for detection.

See [Advanced Options](advanced.md) for classification configuration.

## API Reference

### LanguagesConfig

**Source**: `src/config/languages.rs:4-22`

| Field | Type | Description |
|-------|------|-------------|
| `enabled` | `Vec<String>` | Languages to analyze |
| `rust` | `Option<LanguageFeatures>` | Rust-specific settings |
| `python` | `Option<LanguageFeatures>` | Python-specific settings |
| `javascript` | `Option<LanguageFeatures>` | JavaScript-specific settings |
| `typescript` | `Option<LanguageFeatures>` | TypeScript-specific settings |

### LanguageFeatures

**Source**: `src/config/languages.rs:24-38`

| Field | Type | Default | Description |
|-------|------|---------|-------------|
| `detect_dead_code` | `bool` | `true` | Enable dead code detection |
| `detect_complexity` | `bool` | `true` | Enable complexity analysis |
| `detect_duplication` | `bool` | `true` | Enable duplication detection |

## Troubleshooting

### Language Not Detected

If files aren't being analyzed:

1. Check the file extension is recognized (`.rs`, `.py`, `.pyw`, `.js`, `.mjs`, `.cjs`, `.jsx`, `.ts`, `.mts`, `.cts`, `.tsx`)
2. Verify the language is in `enabled` array
3. Check ignore patterns aren't excluding the files

### Missing Dead Code Warnings

For Rust: Enable dead code detection explicitly if you want debtmap to report it:

```toml
[languages.rust]
detect_dead_code = true  # Override default
```

For Python, JavaScript, and TypeScript: Ensure `detect_dead_code = true` (the default).

### Unexpected Complexity Scores

Language-specific complexity varies due to:
- Different control flow constructs
- Pattern matching complexity (Rust)
- Exception handling (Python)
- Async and callback flow (JavaScript/TypeScript)

See [Entropy Analysis](../entropy-analysis.md) for how entropy affects complexity scoring.

## See Also

- [Scoring Configuration]scoring.md - Configure how complexity translates to debt scores
- [Thresholds Configuration]thresholds.md - Set complexity thresholds
- [Advanced Options]advanced.md - Classification and detection settings