README.md

# Elixir Complexity Analyzer

A powerful CLI tool for analyzing cyclomatic and cognitive complexity in Elixir projects. Perfect for CI/CD pipelines and code quality monitoring.

## Quick Start

```bash
# Analyze current directory (default: cyclomatic complexity)
mix complexity

# Analyze cognitive complexity with custom thresholds
mix complexity ./lib --type cognitive --error-threshold 15 --warning-threshold 10

# Generate detailed report
mix complexity ./lib --report complexity-report.json

# Or use the --default-report option
mix complexity ./lib --default-report
```

## Features

- **Single Complexity Focus**: Analyze either cyclomatic or cognitive complexity
- **CI/CD Ready**: Exit codes for pipeline integration
- **Configurable Thresholds**: Custom warning and error levels
- **Smart Filtering**: Ignore patterns with glob support
- **Configuration File**: JSON-based project configuration
- **Detailed Reports**: JSON output for tracking over time
- **Fast Analysis**: Parallel processing with Task.async_stream
- **AST-Powered**: Uses Elixir's built-in Code.string_to_quoted for accurate parsing


## CLI Options

| Option | Description | Default |
|--------|-------------|---------|
| `--type <type>` | Complexity type to analyze (cyclomatic or cognitive) | cyclomatic |
| `--error-threshold <number>` | Error threshold for complexity | 20 |
| `--warning-threshold <number>` | Warning threshold for complexity | 10 |
| `--ignore <patterns>` | Comma-separated glob patterns to ignore | "" |
| `--report <file>` | Save detailed JSON report to file | - |
| `--default-report` | Save report to default location (complexity-report.json) | - |

## Usage Examples

### CI/CD Integration
```yaml
# GitHub Actions example
- name: Check code complexity
  run: mix complexity ./lib --type cognitive --error-threshold 20
```

### Local Development
```bash
# Quick check with default settings (cyclomatic complexity)
mix complexity ./lib

# Analyze cognitive complexity with strict thresholds
mix complexity ./lib --type cognitive --error-threshold 15 --warning-threshold 8

# Generate report for tracking
mix complexity ./lib --report "reports/complexity-$(date +%Y%m%d).json"

# Generate report to default location
mix complexity ./lib --default-report
```

### Advanced Filtering
```bash
# Ignore test files and external libraries
mix complexity ./lib --ignore "**/test/**,**/*.spec.ex,**/vendor/**"

# Focus on specific file types (ignore test files)
mix complexity ./lib --ignore "**/*_test.exs,**/test_helper.exs"
```

## Configuration File

Create a `complexity.config.json` file in the root of your analyzed directory:

```json
{
  "type": "cognitive",
  "errorThreshold": 25,
  "warningThreshold": 15,
  "ignore": [
    "**/test/**",
    "**/*_test.exs",
    "**/vendor/**"
  ]
}
```

CLI options will override configuration file settings.

## Exit Codes

- `0` - Success (no violations)
- `1` - Complexity thresholds exceeded
- `2` - Tool error (file not found, parse errors)

## Output Examples

**Success:**
```
✓ No complexity threshold violations found
```

**Warnings and Errors:**
```
🟠 [12/10] parseComplexConfig @ lib/utils/parser.ex:45
🟠 [16/10] render_rows @ lib/components/data_table.ex:23
🔴 [25/20] process_data @ lib/legacy/processor.ex:156
🔴 [28/20] convert_legacy_data @ lib/legacy/converter.ex:89
```

The format shows: `[actual_complexity/threshold] function_name @ file_path:line`
- 🟠 indicates warnings (complexity ≥ warning threshold but < error threshold)
- 🔴 indicates errors (complexity ≥ error threshold)

*Note: All violations are collected and displayed, then exits with code 1 if any errors exist*