debtmap 0.16.4

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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//! Rust-specific module structure analysis
//!
//! Provides detailed analysis of Rust source files including:
//! - Accurate function counting (module-level, impl methods, trait methods)
//! - Component detection (structs, enums, impl blocks)
//! - Responsibility identification
//!
//! ## Line Range Extraction
//!
//! Line ranges are extracted from `syn::Span` information using the `Spanned` trait.
//! All line numbers are 1-based, consistent with syn's conventions.

use std::collections::HashMap;
use std::collections::HashSet;
use std::path::Path;
use syn::spanned::Spanned;
use syn::visit::Visit;

use super::facade::detect_module_facade;
use super::types::{
    ComponentDependencyGraph, FunctionCounts, FunctionGroup, ModuleComponent, ModuleStructure,
};

/// Analyze a Rust source file to extract detailed module structure
///
/// Spec 202: Resets SourceMap after analysis (not after parsing) to ensure
/// span lookups in the AST remain valid during analysis.
pub fn analyze_rust_file(content: &str, _file_path: &Path) -> ModuleStructure {
    let result = match syn::parse_file(content) {
        Ok(ast) => {
            // Analyze while AST spans are still valid
            let structure = analyze_rust_ast_with_path(&ast, Some(_file_path));
            // Reset SourceMap after all span lookups are done (spec 202)
            crate::core::parsing::reset_span_locations();
            structure
        }
        Err(_) => ModuleStructure {
            total_lines: content.lines().count(),
            components: vec![],
            function_counts: FunctionCounts::new(),
            responsibility_count: 0,
            public_api_surface: 0,
            dependencies: ComponentDependencyGraph::new(),
            facade_info: None,
        },
    };
    result
}

/// Analyze a parsed Rust AST
pub fn analyze_rust_ast(ast: &syn::File) -> ModuleStructure {
    analyze_rust_ast_with_path(ast, None)
}

fn analyze_rust_ast_with_path(ast: &syn::File, file_path: Option<&Path>) -> ModuleStructure {
    let total_lines = estimate_line_count(ast);
    let components = extract_components(ast);
    let function_counts = count_functions(&components);
    let responsibility_count = detect_responsibilities(&components);
    let public_api_surface = count_public_api(&components);
    let dependencies = build_dependency_graph(ast, &components, file_path);
    let facade_info = Some(detect_module_facade(ast));

    ModuleStructure {
        total_lines,
        components,
        function_counts,
        responsibility_count,
        public_api_surface,
        dependencies,
        facade_info,
    }
}

fn build_dependency_graph(
    ast: &syn::File,
    components: &[ModuleComponent],
    file_path: Option<&Path>,
) -> ComponentDependencyGraph {
    let module_name = file_path
        .and_then(|path| path.file_stem())
        .and_then(|stem| stem.to_str())
        .unwrap_or("module")
        .to_string();
    let local_component_names = component_names(components);
    let import_names = collect_import_roots(ast);
    let mut graph_components = vec![module_name.clone()];
    graph_components.extend(components.iter().map(ModuleComponent::name));
    graph_components.extend(import_names.iter().cloned());

    let mut edges = collect_component_edges(ast, &local_component_names);
    edges.extend(
        import_names
            .iter()
            .map(|dependency| (module_name.clone(), dependency.clone())),
    );

    let edge_set: HashSet<(String, String)> =
        edges.into_iter().filter(|(from, to)| from != to).collect();
    let edges: Vec<(String, String)> = edge_set.into_iter().collect();

    graph_components.sort();
    graph_components.dedup();

    let mut coupling_scores = HashMap::new();
    let outgoing_counts = build_outgoing_counts(&edges);

    for component in &graph_components {
        let fan_out = outgoing_counts.get(component).copied().unwrap_or(0) as f64;
        let score = if fan_out == 0.0 {
            1.0
        } else {
            1.0 / (1.0 + fan_out)
        };
        coupling_scores.insert(component.clone(), score);
    }

    ComponentDependencyGraph {
        components: graph_components,
        edges,
        coupling_scores,
    }
}

fn component_names(components: &[ModuleComponent]) -> HashSet<String> {
    components.iter().map(ModuleComponent::name).collect()
}

fn collect_import_roots(ast: &syn::File) -> HashSet<String> {
    let mut imports = HashSet::new();

    for item in &ast.items {
        if let syn::Item::Use(item_use) = item {
            collect_use_tree_roots(&item_use.tree, &mut imports);
        }
    }

    imports
}

fn collect_use_tree_roots(tree: &syn::UseTree, imports: &mut HashSet<String>) {
    match tree {
        syn::UseTree::Path(path) => {
            imports.insert(path.ident.to_string());
        }
        syn::UseTree::Name(name) => {
            imports.insert(name.ident.to_string());
        }
        syn::UseTree::Rename(rename) => {
            imports.insert(rename.ident.to_string());
        }
        syn::UseTree::Group(group) => {
            for item in &group.items {
                collect_use_tree_roots(item, imports);
            }
        }
        syn::UseTree::Glob(_) => {}
    }
}

fn collect_component_edges(
    ast: &syn::File,
    local_component_names: &HashSet<String>,
) -> Vec<(String, String)> {
    ast.items
        .iter()
        .flat_map(|item| component_edges_for_item(item, local_component_names))
        .collect()
}

fn component_edges_for_item(
    item: &syn::Item,
    local_component_names: &HashSet<String>,
) -> Vec<(String, String)> {
    let Some(component_name) = component_name_for_item(item) else {
        return Vec::new();
    };

    let mut visitor = RustDependencyVisitor::new(local_component_names);
    visitor.visit_item(item);

    visitor
        .references
        .into_iter()
        .map(|dependency| (component_name.clone(), dependency))
        .collect()
}

fn component_name_for_item(item: &syn::Item) -> Option<String> {
    match item {
        syn::Item::Struct(item_struct) => Some(item_struct.ident.to_string()),
        syn::Item::Enum(item_enum) => Some(item_enum.ident.to_string()),
        syn::Item::Fn(item_fn) => Some(item_fn.sig.ident.to_string()),
        syn::Item::Impl(item_impl) => {
            let target = extract_type_name(&item_impl.self_ty)?;
            Some(
                item_impl
                    .trait_
                    .as_ref()
                    .and_then(|(_, path, _)| {
                        path.segments
                            .last()
                            .map(|segment| segment.ident.to_string())
                    })
                    .map(|trait_name| format!("{} for {}", trait_name, target))
                    .unwrap_or_else(|| format!("{} impl", target)),
            )
        }
        _ => None,
    }
}

fn build_outgoing_counts(edges: &[(String, String)]) -> HashMap<String, usize> {
    let mut outgoing = HashMap::new();

    for (from, _) in edges {
        *outgoing.entry(from.clone()).or_insert(0) += 1;
    }

    outgoing
}

struct RustDependencyVisitor<'a> {
    _local_component_names: &'a HashSet<String>,
    references: HashSet<String>,
}

impl<'a> RustDependencyVisitor<'a> {
    fn new(local_component_names: &'a HashSet<String>) -> Self {
        Self {
            _local_component_names: local_component_names,
            references: HashSet::new(),
        }
    }

    fn record_path(&mut self, path: &syn::Path) {
        let Some(first) = path.segments.first() else {
            return;
        };
        let root = first.ident.to_string();

        if is_ignored_dependency(&root) {
            return;
        }

        self.references.insert(root);
    }
}

impl<'ast> Visit<'ast> for RustDependencyVisitor<'_> {
    fn visit_type_path(&mut self, type_path: &'ast syn::TypePath) {
        self.record_path(&type_path.path);
        syn::visit::visit_type_path(self, type_path);
    }

    fn visit_expr_path(&mut self, expr_path: &'ast syn::ExprPath) {
        self.record_path(&expr_path.path);
        syn::visit::visit_expr_path(self, expr_path);
    }

    fn visit_macro(&mut self, mac: &'ast syn::Macro) {
        self.record_path(&mac.path);
        syn::visit::visit_macro(self, mac);
    }
}

fn is_ignored_dependency(name: &str) -> bool {
    matches!(
        name,
        "Self"
            | "self"
            | "super"
            | "crate"
            | "i8"
            | "i16"
            | "i32"
            | "i64"
            | "i128"
            | "isize"
            | "u8"
            | "u16"
            | "u32"
            | "u64"
            | "u128"
            | "usize"
            | "f32"
            | "f64"
            | "bool"
            | "str"
            | "String"
            | "Option"
            | "Result"
            | "Vec"
            | "Box"
    )
}

/// Extract all components from a Rust AST
fn extract_components(ast: &syn::File) -> Vec<ModuleComponent> {
    ast.items
        .iter()
        .filter_map(extract_item_component)
        .collect()
}

/// Extract a component from a single AST item
fn extract_item_component(item: &syn::Item) -> Option<ModuleComponent> {
    match item {
        syn::Item::Struct(s) => Some(extract_struct_component(s)),
        syn::Item::Enum(e) => Some(extract_enum_component(e)),
        syn::Item::Impl(i) => extract_impl_component(i),
        syn::Item::Fn(f) => Some(extract_function_component(f)),
        syn::Item::Mod(_) => None, // Handle nested modules separately if needed
        _ => None,
    }
}

fn extract_struct_component(s: &syn::ItemStruct) -> ModuleComponent {
    let name = s.ident.to_string();
    let fields = match &s.fields {
        syn::Fields::Named(f) => f.named.len(),
        syn::Fields::Unnamed(f) => f.unnamed.len(),
        syn::Fields::Unit => 0,
    };
    let public = matches!(s.vis, syn::Visibility::Public(_));

    ModuleComponent::Struct {
        name,
        fields,
        methods: 0, // Will be counted from impl blocks
        public,
        line_range: extract_line_range(s),
    }
}

fn extract_enum_component(e: &syn::ItemEnum) -> ModuleComponent {
    let name = e.ident.to_string();
    let variants = e.variants.len();
    let public = matches!(e.vis, syn::Visibility::Public(_));

    ModuleComponent::Enum {
        name,
        variants,
        methods: 0,
        public,
        line_range: extract_line_range(e),
    }
}

fn extract_impl_component(i: &syn::ItemImpl) -> Option<ModuleComponent> {
    let target = if let Some((_, path, _)) = &i.trait_ {
        path.segments.last()?.ident.to_string()
    } else {
        extract_type_name(&i.self_ty)?
    };

    let trait_impl = i.trait_.as_ref().map(|(_, path, _)| {
        path.segments
            .last()
            .map(|s| s.ident.to_string())
            .unwrap_or_default()
    });

    let methods = i
        .items
        .iter()
        .filter(|item| matches!(item, syn::ImplItem::Fn(_)))
        .count();

    Some(ModuleComponent::ImplBlock {
        target,
        methods,
        trait_impl,
        line_range: extract_line_range(i),
    })
}

fn extract_function_component(f: &syn::ItemFn) -> ModuleComponent {
    let name = f.sig.ident.to_string();
    let public = matches!(f.vis, syn::Visibility::Public(_));

    ModuleComponent::ModuleLevelFunction {
        name,
        public,
        lines: estimate_function_lines(f),
        complexity: 1, // Simplified
    }
}

/// Count functions by category from components
fn count_functions(components: &[ModuleComponent]) -> FunctionCounts {
    components
        .iter()
        .fold(FunctionCounts::new(), |mut counts, component| {
            match component {
                ModuleComponent::ImplBlock {
                    methods,
                    trait_impl,
                    ..
                } => {
                    if trait_impl.is_some() {
                        counts.trait_methods += methods;
                    } else {
                        counts.impl_methods += methods;
                    }
                }
                ModuleComponent::ModuleLevelFunction { public, .. } => {
                    counts.module_level_functions += 1;
                    if *public {
                        counts.public_functions += 1;
                    } else {
                        counts.private_functions += 1;
                    }
                }
                _ => {}
            }
            counts
        })
}

/// Detect distinct responsibilities in a module
fn detect_responsibilities(components: &[ModuleComponent]) -> usize {
    let impl_count = components
        .iter()
        .filter(|c| match c {
            ModuleComponent::ImplBlock { .. } => true,
            ModuleComponent::Struct { methods, .. } => *methods > 0,
            ModuleComponent::Enum { methods, .. } => *methods > 0,
            _ => false,
        })
        .count();

    let function_groups = group_module_functions(components);
    let total = impl_count + function_groups.len();

    total.max(1)
}

/// Group module-level functions by prefix
fn group_module_functions(components: &[ModuleComponent]) -> Vec<FunctionGroup> {
    let mut groups: HashMap<String, Vec<String>> = HashMap::new();

    for component in components {
        if let ModuleComponent::ModuleLevelFunction { name, .. } = component {
            let prefix = extract_function_prefix(name);
            groups.entry(prefix).or_default().push(name.clone());
        }
    }

    groups
        .into_iter()
        .map(|(prefix, functions)| FunctionGroup { prefix, functions })
        .collect()
}

/// Count public API surface
fn count_public_api(components: &[ModuleComponent]) -> usize {
    components
        .iter()
        .filter(|c| match c {
            ModuleComponent::Struct { public, .. } => *public,
            ModuleComponent::Enum { public, .. } => *public,
            ModuleComponent::ModuleLevelFunction { public, .. } => *public,
            _ => false,
        })
        .count()
}

// Pure helper functions

/// Extract line range from any syn AST node that implements Spanned.
///
/// Returns a tuple of (start_line, end_line) using 1-based line numbering
/// consistent with syn's conventions.
fn extract_line_range<T: Spanned>(node: &T) -> (usize, usize) {
    let span = node.span();
    let start = span.start().line;
    let end = span.end().line;
    (start, end)
}

fn extract_type_name(ty: &syn::Type) -> Option<String> {
    match ty {
        syn::Type::Path(type_path) => type_path.path.segments.last().map(|s| s.ident.to_string()),
        _ => None,
    }
}

fn estimate_line_count(ast: &syn::File) -> usize {
    ast.items.len() * 10 // Rough estimate
}

fn estimate_function_lines(_f: &syn::ItemFn) -> usize {
    10 // Simplified - would need actual span info
}

fn extract_function_prefix(name: &str) -> String {
    name.find('_')
        .map(|idx| name[..idx].to_string())
        .unwrap_or_else(|| "other".to_string())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_extract_function_prefix() {
        assert_eq!(extract_function_prefix("format_output"), "format");
        assert_eq!(extract_function_prefix("parse_input"), "parse");
        assert_eq!(extract_function_prefix("simple"), "other");
    }

    #[test]
    fn test_analyze_simple_rust_file() {
        let code = r#"
            pub struct Foo {
                field: u32,
            }

            impl Foo {
                pub fn new() -> Self {
                    Self { field: 0 }
                }
            }

            pub fn helper() {}
        "#;

        let structure = analyze_rust_file(code, Path::new("test.rs"));

        assert_eq!(structure.function_counts.impl_methods, 1);
        assert_eq!(structure.function_counts.module_level_functions, 1);
        assert!(structure.responsibility_count >= 1);
    }

    #[test]
    fn test_extract_struct_line_range() {
        let code = r#"
pub struct Foo {
    field1: u32,
    field2: String,
    field3: bool,
}
        "#;

        let ast = syn::parse_file(code).unwrap();
        let structure = analyze_rust_ast(&ast);

        let struct_comp = structure
            .components
            .iter()
            .find(|c| matches!(c, ModuleComponent::Struct { name, .. } if name == "Foo"))
            .expect("Foo struct should exist");

        let line_count = struct_comp.line_count();
        assert!(
            line_count >= 4,
            "Struct should span 4+ lines, got {}",
            line_count
        );
        assert!(
            line_count <= 6,
            "Struct should span at most 6 lines, got {}",
            line_count
        );
    }

    #[test]
    fn test_extract_enum_line_range() {
        let code = r#"
pub enum Status {
    Active,
    Inactive,
    Pending,
}
        "#;

        let ast = syn::parse_file(code).unwrap();
        let structure = analyze_rust_ast(&ast);

        let enum_comp = structure
            .components
            .iter()
            .find(|c| matches!(c, ModuleComponent::Enum { name, .. } if name == "Status"))
            .expect("Status enum should exist");

        let line_count = enum_comp.line_count();
        assert!(
            line_count >= 4,
            "Enum should span 4+ lines, got {}",
            line_count
        );
    }

    #[test]
    fn test_extract_impl_line_range() {
        let code = r#"
impl Foo {
    pub fn new() -> Self {
        Self { field1: 0, field2: String::new(), field3: false }
    }

    pub fn process(&self) -> u32 {
        self.field1 * 2
    }

    fn helper(&self) -> String {
        self.field2.clone()
    }
}
        "#;

        let ast = syn::parse_file(code).unwrap();
        let structure = analyze_rust_ast(&ast);

        let impl_comp = structure
            .components
            .iter()
            .find(|c| matches!(c, ModuleComponent::ImplBlock { target, .. } if target == "Foo"))
            .expect("Foo impl should exist");

        let line_count = impl_comp.line_count();
        assert!(
            line_count >= 12,
            "Impl block should span 12+ lines, got {}",
            line_count
        );
    }

    #[test]
    fn test_component_sorting_by_line_count() {
        let code = r#"
pub struct Small { x: u32 }

pub struct Large {
    field1: u32,
    field2: String,
    field3: bool,
    field4: Vec<u8>,
    field5: Option<usize>,
}

impl Large {
    pub fn new() -> Self {
        Self {
            field1: 0,
            field2: String::new(),
            field3: false,
            field4: Vec::new(),
            field5: None,
        }
    }
}
        "#;

        let ast = syn::parse_file(code).unwrap();
        let structure = analyze_rust_ast(&ast);

        let mut sorted = structure.components.clone();
        sorted.sort_by_key(|c| std::cmp::Reverse(c.line_count()));

        // Verify Large impl is first (longest)
        if let ModuleComponent::ImplBlock { target, .. } = &sorted[0] {
            assert_eq!(target, "Large", "Largest component should be Large impl");
            assert!(sorted[0].line_count() > sorted[1].line_count());
        } else {
            panic!("First component should be impl block");
        }
    }

    #[test]
    fn test_analyze_rust_ast_populates_dependency_graph() {
        let code = r#"
use serde::Serialize;

pub struct Config {
    helper: Helper,
}

pub struct Helper;

impl Config {
    pub fn build(helper: Helper) -> Self {
        log::info!("building");
        Self { helper }
    }
}

pub fn render(config: Config) -> Helper {
    Helper
}
"#;

        let ast = syn::parse_file(code).unwrap();
        let structure = analyze_rust_ast(&ast);

        assert!(structure
            .dependencies
            .edges
            .iter()
            .any(|(from, to)| from == "Config" && to == "Helper"));
        assert!(structure
            .dependencies
            .edges
            .iter()
            .any(|(from, to)| from == "Config impl" && to == "log"));
        assert!(structure
            .dependencies
            .edges
            .iter()
            .any(|(from, to)| from == "module" && to == "serde"));
        assert!(structure
            .dependencies
            .coupling_scores
            .contains_key("Config impl"));
    }
}