debtmap 0.16.3

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
use debtmap::analyzers::rust_call_graph::extract_call_graph;
use std::path::PathBuf;

#[test]
fn test_basic_function_calls_with_resolution() {
    let code = r#"
fn main() {
    helper();
    process_data();
}

fn helper() {
    println!("Helper");
}

fn process_data() {
    validate();
}

fn validate() {
    // Some validation
}
"#;

    let parsed = syn::parse_file(code).unwrap();
    let path = PathBuf::from("test.rs");
    let call_graph = extract_call_graph(&parsed, &path);

    // Debug output to understand what's happening
    println!("\n=== Functions and their relationships ===");
    let functions = call_graph.find_all_functions();
    for func in &functions {
        let callers = call_graph.get_callers(func);
        let callees = call_graph.get_callees(func);
        println!("Function: {} (line {})", func.name, func.line);
        println!(
            "  Callers: {:?}",
            callers.iter().map(|f| &f.name).collect::<Vec<_>>()
        );
        println!(
            "  Callees: {:?}",
            callees.iter().map(|f| &f.name).collect::<Vec<_>>()
        );
    }

    // Check that functions are detected
    assert_eq!(functions.len(), 4, "Should find 4 functions");

    // Find the actual helper function
    let helper = functions
        .iter()
        .find(|f| f.name == "helper")
        .expect("Should find helper function");

    // Check helper's callers
    let helper_callers = call_graph.get_callers(helper);
    assert_eq!(helper_callers.len(), 1, "helper should have 1 caller");
    assert_eq!(
        helper_callers[0].name, "main",
        "helper should be called by main"
    );

    // Find main and check its callees
    let main_func = functions
        .iter()
        .find(|f| f.name == "main")
        .expect("Should find main function");

    let main_callees = call_graph.get_callees(main_func);
    assert_eq!(main_callees.len(), 2, "main should call 2 functions");
    assert!(
        main_callees.iter().any(|f| f.name == "helper"),
        "main should call helper"
    );
    assert!(
        main_callees.iter().any(|f| f.name == "process_data"),
        "main should call process_data"
    );
}

#[test]
fn test_method_calls_on_self() {
    let code = r#"
struct Processor;

impl Processor {
    fn process(&self) {
        self.validate();
        self.transform();
    }
    
    fn validate(&self) {
        // validation
    }
    
    fn transform(&self) {
        // transformation
    }
}
"#;

    let parsed = syn::parse_file(code).unwrap();
    let path = PathBuf::from("test.rs");
    let call_graph = extract_call_graph(&parsed, &path);

    // Check that impl methods are detected
    let functions = call_graph.find_all_functions();
    println!("\n=== Methods found ===");
    for func in &functions {
        println!("  {}", func.name);
    }

    assert!(
        functions.iter().any(|f| f.name == "Processor::process"),
        "Should find Processor::process"
    );
    assert!(
        functions.iter().any(|f| f.name == "Processor::validate"),
        "Should find Processor::validate"
    );
    assert!(
        functions.iter().any(|f| f.name == "Processor::transform"),
        "Should find Processor::transform"
    );

    // Check process's callees
    let process_fn = functions
        .iter()
        .find(|f| f.name == "Processor::process")
        .expect("Should find process method");

    let process_callees = call_graph.get_callees(process_fn);
    assert_eq!(process_callees.len(), 2, "process should call 2 methods");
    assert!(
        process_callees
            .iter()
            .any(|f| f.name == "Processor::validate"),
        "Should call validate"
    );
    assert!(
        process_callees
            .iter()
            .any(|f| f.name == "Processor::transform"),
        "Should call transform"
    );
}

#[test]
fn test_closure_calls() {
    let code = r#"
fn process_items(items: Vec<i32>) -> Vec<i32> {
    items.iter()
        .map(|x| transform(x))
        .filter(|x| validate(x))
        .collect()
}

fn transform(value: &i32) -> i32 {
    *value * 2
}

fn validate(value: &i32) -> bool {
    *value > 0
}
"#;

    let parsed = syn::parse_file(code).unwrap();
    let path = PathBuf::from("test.rs");
    let call_graph = extract_call_graph(&parsed, &path);

    // Find process_items
    let process_fn = call_graph
        .find_all_functions()
        .into_iter()
        .find(|f| f.name == "process_items")
        .expect("Should find process_items");

    let process_callees = call_graph.get_callees(&process_fn);
    println!("\n=== Calls from process_items ===");
    for callee in &process_callees {
        println!("  {}", callee.name);
    }

    assert!(
        process_callees.iter().any(|f| f.name == "transform"),
        "Should detect transform call in closure"
    );
    assert!(
        process_callees.iter().any(|f| f.name == "validate"),
        "Should detect validate call in closure"
    );
}

#[test]
fn test_async_function_calls() {
    let code = r#"
async fn main() {
    fetch_data().await;
    process_async().await;
}

async fn fetch_data() -> String {
    // fetch data
    "data".to_string()
}

async fn process_async() {
    validate_async().await;
}

async fn validate_async() {
    // async validation
}
"#;

    let parsed = syn::parse_file(code).unwrap();
    let path = PathBuf::from("test.rs");
    let call_graph = extract_call_graph(&parsed, &path);

    // Check async function calls
    let main_func = call_graph
        .find_all_functions()
        .into_iter()
        .find(|f| f.name == "main")
        .expect("Should find main");

    let main_callees = call_graph.get_callees(&main_func);
    assert_eq!(main_callees.len(), 2, "main should call 2 async functions");
    assert!(
        main_callees.iter().any(|f| f.name == "fetch_data"),
        "Should call fetch_data"
    );
    assert!(
        main_callees.iter().any(|f| f.name == "process_async"),
        "Should call process_async"
    );
}

#[test]
fn test_associated_function_calls() {
    let code = r#"
struct Calculator;

impl Calculator {
    fn new() -> Self {
        Calculator
    }
    
    fn calculate(value: i32) -> i32 {
        Self::validate_input(value);
        Self::process(value)
    }
    
    fn validate_input(value: i32) {
        // validation
    }
    
    fn process(value: i32) -> i32 {
        value * 2
    }
}

fn main() {
    let calc = Calculator::new();
    let result = Calculator::calculate(42);
}
"#;

    let parsed = syn::parse_file(code).unwrap();
    let path = PathBuf::from("test.rs");
    let call_graph = extract_call_graph(&parsed, &path);

    // Check associated function calls
    let calculate_fn = call_graph
        .find_all_functions()
        .into_iter()
        .find(|f| f.name == "Calculator::calculate")
        .expect("Should find calculate function");

    let calculate_callees = call_graph.get_callees(&calculate_fn);
    println!("\n=== Calls from Calculator::calculate ===");
    for callee in &calculate_callees {
        println!("  {}", callee.name);
    }

    // The calculate function should call validate_input and process
    // Note: Self::method calls may be resolved as Calculator::method
    assert!(
        calculate_callees.len() >= 2,
        "calculate should call at least 2 functions"
    );
}

#[test]
fn test_trait_impl_calls() {
    let code = r#"
trait Handler {
    fn handle(&self);
}

struct MyHandler;

impl Handler for MyHandler {
    fn handle(&self) {
        self.pre_process();
        process_internal();
        self.post_process();
    }
}

impl MyHandler {
    fn pre_process(&self) {
        // pre-processing
    }
    
    fn post_process(&self) {
        // post-processing
    }
}

fn process_internal() {
    // internal processing
}
"#;

    let parsed = syn::parse_file(code).unwrap();
    let path = PathBuf::from("test.rs");
    let call_graph = extract_call_graph(&parsed, &path);

    // Check that trait impl methods are detected
    let functions = call_graph.find_all_functions();
    let handle_fn = functions
        .iter()
        .find(|f| f.name.contains("handle"))
        .expect("Should find handle method");

    let handle_callees = call_graph.get_callees(handle_fn);
    println!("\n=== Calls from handle ===");
    for callee in &handle_callees {
        println!("  {}", callee.name);
    }

    assert!(
        handle_callees.len() >= 2,
        "handle should call at least 2 functions"
    );
    assert!(
        handle_callees.iter().any(|f| f.name == "process_internal"),
        "Should call process_internal"
    );
}

#[test]
fn test_cross_file_resolution_simulation() {
    // This simulates how cross-file resolution would work
    // In a real scenario, we'd have multiple files
    let code = r#"
mod utils {
    pub fn helper() {
        println!("Helper");
    }
}

fn main() {
    utils::helper();
    process();
}

fn process() {
    // processing
}
"#;

    let parsed = syn::parse_file(code).unwrap();
    let path = PathBuf::from("test.rs");
    let call_graph = extract_call_graph(&parsed, &path);

    let main_func = call_graph
        .find_all_functions()
        .into_iter()
        .find(|f| f.name == "main")
        .expect("Should find main");

    let main_callees = call_graph.get_callees(&main_func);
    println!("\n=== Calls from main ===");
    for callee in &main_callees {
        println!("  {}", callee.name);
    }

    // Should detect at least the process call
    assert!(
        main_callees.iter().any(|f| f.name == "process"),
        "Should call process"
    );
    // Note: utils::helper might not be resolved if it's in a different module
}

#[test]
fn test_generic_function_calls() {
    let code = r#"
fn process<T: Clone>(value: T) -> T {
    validate(&value);
    transform(value)
}

fn validate<T>(value: &T) {
    // validation
}

fn transform<T: Clone>(value: T) -> T {
    value.clone()
}

fn main() {
    process(42);
    process("hello");
}
"#;

    let parsed = syn::parse_file(code).unwrap();
    let path = PathBuf::from("test.rs");
    let call_graph = extract_call_graph(&parsed, &path);

    // Check generic function calls
    let process_fn = call_graph
        .find_all_functions()
        .into_iter()
        .find(|f| f.name == "process")
        .expect("Should find process");

    let process_callees = call_graph.get_callees(&process_fn);
    assert_eq!(
        process_callees.len(),
        2,
        "process should call validate and transform"
    );
    assert!(
        process_callees.iter().any(|f| f.name == "validate"),
        "Should call validate"
    );
    assert!(
        process_callees.iter().any(|f| f.name == "transform"),
        "Should call transform"
    );
}