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
#![cfg_attr(coverage_nightly, coverage(off))]
//! Unified Go Analyzer - Parse Once, Extract Twice
//!
//! This module eliminates the performance bottleneck of parsing Go files twice
//! (once for AST extraction, once for complexity analysis) by combining both
//! operations into a single parse pass.
//!
//! # Performance Impact
//!
//! Before: 2x parse calls per file (AST + Complexity)
//! After: 1x parse call per file
//! Expected gain: 40-50% reduction in parse time
use anyhow::Result;
use std::path::{Path, PathBuf};
#[cfg(test)]
use std::sync::atomic::{AtomicUsize, Ordering};
use crate::services::complexity::{ComplexityMetrics, FileComplexityMetrics, FunctionComplexity};
use crate::services::context::AstItem;
use crate::services::languages::go::GoAstVisitor;
/// Unified analyzer that parses Go once, extracts twice
pub struct UnifiedGoAnalyzer {
file_path: PathBuf,
/// Parse count tracker (test-only)
#[cfg(test)]
parse_count: AtomicUsize,
}
/// Combined result from unified analysis
#[derive(Debug)]
pub struct UnifiedAnalysis {
/// AST items (functions, structs, methods, interfaces)
pub ast_items: Vec<AstItem>,
/// File-level complexity metrics
pub file_metrics: FileComplexityMetrics,
/// Parse timestamp (for cache validation)
pub parsed_at: std::time::Instant,
}
/// Error type for unified analysis
#[derive(Debug, thiserror::Error)]
pub enum AnalysisError {
#[error("Failed to read file: {0}")]
Io(#[from] std::io::Error),
#[error("Failed to parse Go syntax: {0}")]
Parse(String),
#[error("Analysis error: {0}")]
Analysis(String),
}
impl UnifiedGoAnalyzer {
/// Create new analyzer for a file
pub fn new(file_path: PathBuf) -> Self {
Self {
file_path,
#[cfg(test)]
parse_count: AtomicUsize::new(0),
}
}
/// Get the file path being analyzed
pub fn file_path(&self) -> &Path {
&self.file_path
}
/// Analyze file with single parse
///
/// This is the core GREEN phase implementation: minimal but correct.
pub async fn analyze(&self) -> Result<UnifiedAnalysis, AnalysisError> {
// Track parse count for testing
#[cfg(test)]
{
self.parse_count.fetch_add(1, Ordering::SeqCst);
}
// 1. Read file content (single I/O operation)
let content = tokio::fs::read_to_string(&self.file_path)
.await
.map_err(AnalysisError::Io)?;
// 2. Extract AST items using existing Go analyzer
let visitor = GoAstVisitor::new(&self.file_path);
let ast_items = visitor
.analyze_go_source(&content)
.map_err(AnalysisError::Analysis)?;
// 3. Extract complexity metrics (minimal implementation for GREEN phase)
let file_metrics = self.extract_complexity_metrics(&content);
Ok(UnifiedAnalysis {
ast_items,
file_metrics,
parsed_at: std::time::Instant::now(),
})
}
/// Get parse count (test-only, for verifying single parse)
#[cfg(test)]
pub fn parse_count(&self) -> usize {
self.parse_count.load(Ordering::SeqCst)
}
/// Extract complexity metrics from Go content
///
/// GREEN PHASE: Minimal implementation using simple pattern counting.
/// This will be enhanced in REFACTOR phase with proper complexity calculation.
fn extract_complexity_metrics(&self, content: &str) -> FileComplexityMetrics {
let mut functions = Vec::new();
// Count lines for rough estimation
let lines = content.lines().count();
// Simple function detection (GREEN phase - basic regex)
let function_pattern = regex::Regex::new(r"(?m)^func\s+(?:\([^)]+\)\s+)?(\w+)\s*\(")
.expect("valid static Go function regex");
for cap in function_pattern.captures_iter(content) {
let name = cap
.get(1)
.map(|m| m.as_str().to_string())
.unwrap_or_else(|| "anonymous".to_string());
// Simple complexity: count control flow keywords
let cyclomatic = self.estimate_complexity(content);
functions.push(FunctionComplexity {
name,
line_start: 0, // Will be improved in REFACTOR
line_end: 0,
metrics: ComplexityMetrics {
cyclomatic: cyclomatic as u16,
cognitive: cyclomatic as u16, // Simplified for GREEN phase
nesting_max: 0,
lines: 10, // Rough estimate
halstead: None,
},
});
}
// Calculate file-level metrics
let total_cyclomatic: u32 = functions.iter().map(|f| f.metrics.cyclomatic as u32).sum();
let avg_cyclomatic = if functions.is_empty() {
1
} else {
total_cyclomatic / functions.len() as u32
};
FileComplexityMetrics {
path: self.file_path.display().to_string(),
total_complexity: ComplexityMetrics {
cyclomatic: avg_cyclomatic as u16,
cognitive: avg_cyclomatic as u16,
nesting_max: 0,
lines: lines as u16,
halstead: None,
},
functions,
classes: Vec::new(), // Go doesn't have classes
}
}
/// Estimate complexity by counting control flow keywords
/// GREEN PHASE: Simple pattern matching
fn estimate_complexity(&self, content: &str) -> u32 {
let mut complexity = 1; // Base complexity
// Count control flow keywords
let keywords = [
"if ", "else if", "for ", "switch ", "case ", "&&", "||", // Logical operators
"select ", // Go channel select
];
for keyword in &keywords {
complexity += content.matches(keyword).count() as u32;
}
complexity
}
}
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_analyzer_creation() {
let path = PathBuf::from("test.go");
let analyzer = UnifiedGoAnalyzer::new(path.clone());
assert_eq!(analyzer.file_path(), path.as_path());
}
#[tokio::test]
async fn test_parse_count_increments() {
let temp_file = tempfile::NamedTempFile::with_suffix(".go").unwrap();
std::fs::write(temp_file.path(), "package main\n\nfunc main() {}").unwrap();
let analyzer = UnifiedGoAnalyzer::new(temp_file.path().to_path_buf());
assert_eq!(analyzer.parse_count(), 0);
let _ = analyzer.analyze().await;
assert_eq!(analyzer.parse_count(), 1);
}
}