# SunLint Dart Analyzer

Dart language analyzer for SunLint. Provides AST-based code analysis and rule checking via JSON-RPC communication.

## Architecture

```
┌─────────────────────────────────────────────────────────────────┐
│  SunLint (Node.js)                                               │
│  └── DartAnalyzer adapter                                        │
└────────┬───────────────────────────────────────────────────────┘
         │ JSON-RPC over STDIO
         ▼
┌─────────────────────────────────────────────────────────────────┐
│  SunLint Dart Analyzer (This package)                            │
│  ├── JsonRpcServer      - Handles communication                  │
│  ├── AnalyzerService    - Orchestrates analysis                  │
│  ├── SymbolTableExtractor - Extracts symbols from AST            │
│  └── Analyzers/         - Rule implementations                   │
│      ├── C001 - Code Complexity                                  │
│      ├── C008 - Deep Nesting                                     │
│      ├── N001 - Naming Conventions                               │
│      ├── E001 - Error Handling                                   │
│      └── S003 - Security (Sensitive Data)                        │
└─────────────────────────────────────────────────────────────────┘
```

## Development

### Prerequisites

- Dart SDK >= 3.0.0

### Setup

```bash
cd dart_analyzer
dart pub get
```

### Run in standalone mode (for testing)

```bash
# Analyze a single file
dart run bin/sunlint_dart_analyzer.dart --standalone --file test/fixtures/sample.dart

# Analyze with specific rules
dart run bin/sunlint_dart_analyzer.dart --standalone --file lib/main.dart --rules C001,N001
```

### Run as JSON-RPC server

```bash
dart run bin/sunlint_dart_analyzer.dart
```

### Build executable

```bash
# Compile to native executable
dart compile exe bin/sunlint_dart_analyzer.dart -o bin/sunlint-dart-macos

# For different platforms (requires cross-compilation):
# Linux: dart compile exe bin/sunlint_dart_analyzer.dart -o bin/sunlint-dart-linux
# Windows: dart compile exe bin/sunlint_dart_analyzer.dart -o bin/sunlint-dart-windows.exe
```

## JSON-RPC Protocol

### Methods

#### `initialize`

Initialize the analyzer with a project.

**Request:**
```json
{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "initialize",
  "params": {
    "projectPath": "/path/to/project",
    "targetFiles": ["lib/main.dart"]
  }
}
```

**Response:**
```json
{
  "jsonrpc": "2.0",
  "id": 1,
  "result": {
    "success": true,
    "version": "1.0.0",
    "capabilities": {
      "analyze": true,
      "getSymbolTable": true,
      "rules": ["C001", "C008", "N001", "E001", "S003"]
    }
  }
}
```

#### `analyze`

Analyze a file with specified rules.

**Request:**
```json
{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "analyze",
  "params": {
    "filePath": "lib/main.dart",
    "rules": [
      {"id": "C001", "config": {"threshold": 10}},
      {"id": "N001"}
    ]
  }
}
```

**Response:**
```json
{
  "jsonrpc": "2.0",
  "id": 2,
  "result": {
    "violations": [
      {
        "ruleId": "C001",
        "filePath": "/path/to/lib/main.dart",
        "line": 15,
        "column": 3,
        "message": "Method \"processData\" has cyclomatic complexity of 12 (threshold: 10)",
        "severity": "warning",
        "analysisMethod": "ast"
      }
    ],
    "fileAnalyzed": "lib/main.dart"
  }
}
```

#### `getSymbolTable`

Get symbol table for a file.

**Request:**
```json
{
  "jsonrpc": "2.0",
  "id": 3,
  "method": "getSymbolTable",
  "params": {
    "filePath": "lib/main.dart"
  }
}
```

**Response:**
```json
{
  "jsonrpc": "2.0",
  "id": 3,
  "result": {
    "filePath": "/path/to/lib/main.dart",
    "fileName": "main.dart",
    "imports": [...],
    "classes": [...],
    "functions": [...],
    "variables": [...]
  }
}
```

#### `shutdown`

Gracefully shut down the analyzer.

**Request:**
```json
{
  "jsonrpc": "2.0",
  "id": 4,
  "method": "shutdown",
  "params": {}
}
```

#### `ping`

Health check.

**Request:**
```json
{
  "jsonrpc": "2.0",
  "id": 5,
  "method": "ping",
  "params": {}
}
```

## Supported Rules

| Rule ID | Name | Description |
|---------|------|-------------|
| C001 | Code Complexity | Detects functions with high cyclomatic complexity |
| C008 | Deep Nesting | Detects code with excessive nesting depth |
| N001 | Naming Conventions | Enforces Dart naming conventions |
| E001 | Error Handling | Detects empty catch blocks and ignored exceptions |
| S003 | Security | Detects logging of sensitive data |

## Testing

```bash
dart test
```

## License

MIT
