# Analyzer Types

## Overview

Debtmap provides deep, language-specific code analysis through specialized analyzers for each supported language. The architecture uses the `Analyzer` trait for extensibility across Rust, Python, JavaScript, TypeScript, and Go.

## Rust Analyzer

Debtmap provides comprehensive analysis for Rust codebases using the `syn` crate for native AST parsing.

### Core Capabilities

The Rust analyzer (`src/analyzers/rust/analyzer.rs`) provides:

- **Complexity Metrics**: Cyclomatic complexity, cognitive complexity, and entropy analysis
- **Purity Detection**: Identifies pure functions with confidence scoring
- **Call Graph Analysis**: Tracks upstream callers and downstream callees with transitive relationships
- **Trait Implementation Tracking**: Monitors trait implementations across the codebase
- **Macro Expansion Support**: Analyzes complexity within macros accurately
- **Pattern-Based Adjustments**: Recognizes and adjusts for code generation patterns
- **Visibility Tracking**: Distinguishes `pub`, `pub(crate)`, and private functions
- **Test Module Detection**: Identifies `#[cfg(test)]` modules and `#[test]` functions

**Source**: Capabilities verified in `src/analyzers/rust/analyzer.rs` and related modules under `src/analyzers/rust/`.

### Semantic Function Classification

The Rust analyzer automatically classifies functions by their role in the system. This classification feeds into the unified scoring system's role multiplier for accurate technical debt assessment.

**Classification Categories** (`src/analyzers/rust/`):

- **Entry Points**: Functions named `main`, `start`, or public functions in `bin/` modules
- **Business Logic**: Core domain functions containing complex algorithms and business rules
- **Data Access**: Functions performing database queries, file I/O, or network operations
- **Infrastructure**: Logging, configuration, monitoring, and error handling utilities
- **Utilities**: Helper functions, formatters, type converters, and validation functions
- **Test Code**: Functions in `#[cfg(test)]` modules or marked with `#[test]` attribute

These classifications are used to calculate role-based priority multipliers in the risk scoring system. See [Risk Scoring](risk-scoring.md) for details on how semantic classification affects debt prioritization.

## Language Support

### Supported Languages

Debtmap provides full analysis for the following languages:

| Language | Parser | File Extensions |
|----------|--------|-----------------|
| **Rust** | `syn` (native AST) | `.rs` |
| **Python** | tree-sitter | `.py` |
| **JavaScript** | tree-sitter | `.js`, `.mjs`, `.cjs`, `.jsx` |
| **TypeScript** | tree-sitter | `.ts`, `.mts`, `.cts`, `.tsx` |
| **Go** | tree-sitter | `.go` |

### Python Analyzer

The Python analyzer uses tree-sitter for AST parsing and provides:

- **Complexity Metrics**: Cyclomatic complexity, cognitive complexity, nesting depth
- **Class and Method Extraction**: Python classes, methods, and module-level functions
- **Decorator Awareness**: Handles decorated functions and class methods
- **Python-Specific Constructs**: Comprehensions, exception blocks, and idiomatic control flow
- **Entropy Analysis**: Pattern-based false positive reduction

### JavaScript/TypeScript Analyzer

The JavaScript and TypeScript analyzers use tree-sitter for AST parsing and provide:

- **Complexity Metrics**: Cyclomatic complexity, cognitive complexity, and nesting depth
- **Async Pattern Detection**: Promise chains, async/await, and callback-heavy flows
- **React Support**: JSX and TSX component analysis
- **Type-Oriented Patterns**: TypeScript-specific constructs such as assertions and broad type usage
- **Entropy Analysis**: Pattern-based false positive reduction

### Go Analyzer

The Go analyzer uses tree-sitter for AST parsing and provides:

- **Package and Import Extraction**: Package declarations and import dependencies
- **Function and Method Extraction**: Top-level functions and receiver methods with stable names
- **Complexity Metrics**: Cyclomatic complexity, cognitive complexity, nesting depth, and length
- **Call Relationships**: Same-file, same-package, and module-local package call resolution where statically inferable
- **Generated-Code Policy**: Configurable generated Go handling with debt suppression by default
- **Advisory Signals**: Repetitive error handling, swallowed errors, panic/recover risks, goroutine lifecycle hints, defer-in-loop patterns, channel operation risks, and mutation categories

Current limitations: Go analysis is syntax and lightweight symbol based. It does not perform full type checking, full interface dispatch, or complete external module resolution, and ambiguous receiver calls are left unresolved instead of creating likely-wrong internal edges.

### File Detection

During file discovery, debtmap detects files by extension and routes them to the appropriate analyzer:

```bash
# Analyze all supported files in current directory
debtmap analyze .

# Analyze specific languages only
debtmap analyze . --languages rust,python,javascript,typescript,go

# All supported languages are enabled by default
```

## Extensibility

The architecture supports adding new languages through the `Analyzer` trait.

### Analyzer Trait

The core `Analyzer` trait defines the interface for language-specific analyzers:

```rust
pub trait Analyzer: Send + Sync {
    fn parse(&self, content: &str, path: std::path::PathBuf) -> Result<Ast>;
    fn analyze(&self, ast: &Ast) -> FileMetrics;
    fn language(&self) -> crate::core::Language;
}
```

### Adding Language Support

To add support for a new language:

1. **Implement the `Analyzer` trait** with language-specific parsing and analysis
2. **Add the language variant** to the `Language` enum
3. **Update `from_extension()`** to recognize the file extension
4. **Register in `AnalyzerFactory`** to instantiate your analyzer

**Reference Implementation**: See `src/analyzers/rust/analyzer.rs`, `src/analyzers/python/analyzer.rs`, and `src/analyzers/typescript/analyzer.rs` for the current language-specific analyzer implementations.

## See Also

- [Overview](overview.md) - Analysis pipeline and workflow
- [Complexity Metrics](complexity-metrics.md) - Detailed metric calculations
- [Risk Scoring](risk-scoring.md) - How semantic classification affects prioritization
