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
446
/// Graph building and management functionality for call graph extraction
use crate::priority::call_graph::{CallGraph, CallType, FunctionCall, FunctionId};
use std::path::PathBuf;
use syn::{ImplItemFn, ItemFn};

/// Expression categorization for special handling
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ExprCategory {
    Closure,
    Async,
    Await,
    Try,
    Unsafe,
    Regular,
}

/// Builds and manages the call graph
pub struct GraphBuilder {
    pub call_graph: CallGraph,
    current_file: PathBuf,
    module_path: Vec<String>,
}

impl GraphBuilder {
    pub fn new(file: PathBuf) -> Self {
        Self {
            call_graph: CallGraph::new(),
            current_file: file,
            module_path: Vec::new(),
        }
    }

    /// Set the current module path
    pub fn set_module_path(&mut self, path: Vec<String>) {
        self.module_path = path;
    }

    /// Get the current module path
    pub fn module_path(&self) -> &[String] {
        &self.module_path
    }

    /// Push a module to the path
    pub fn push_module(&mut self, module: String) {
        self.module_path.push(module);
    }

    /// Pop a module from the path
    pub fn pop_module(&mut self) {
        self.module_path.pop();
    }

    /// Add a function to the graph
    pub fn add_function(
        &mut self,
        name: String,
        line: usize,
        is_test: bool,
        _is_async: bool,
        module_path: String,
    ) -> FunctionId {
        let function_id = FunctionId::with_module_path(
            self.current_file.clone(),
            name.clone(),
            line,
            module_path,
        );

        // Add function with appropriate parameters
        // Using defaults for entry_point and complexity for now
        self.call_graph.add_function(
            function_id.clone(),
            false, // is_entry_point
            is_test,
            0, // complexity (to be calculated)
            0, // lines (to be calculated)
        );
        function_id
    }

    /// Add a function from an ItemFn
    pub fn add_function_from_item(
        &mut self,
        name: String,
        line: usize,
        item_fn: &ItemFn,
        module_path: String,
    ) -> FunctionId {
        let is_test = Self::has_test_attribute(&item_fn.attrs);
        let is_async = item_fn.sig.asyncness.is_some();

        self.add_function(name, line, is_test, is_async, module_path)
    }

    /// Add an impl method to the graph
    pub fn add_impl_method(
        &mut self,
        name: String,
        line: usize,
        impl_fn: &ImplItemFn,
        module_path: String,
    ) -> FunctionId {
        let is_test = Self::has_test_attribute(&impl_fn.attrs);
        let is_async = impl_fn.sig.asyncness.is_some();

        self.add_function(name, line, is_test, is_async, module_path)
    }

    /// Check if a function has a test attribute.
    ///
    /// Detects:
    /// - `#[test]` - standard Rust test
    /// - `#[tokio::test]` - async tokio test
    /// - `#[actix_rt::test]` - actix runtime test
    /// - `#[rstest]` - rstest parameterized test
    /// - `#[test_case]` - test_case attribute
    fn has_test_attribute(attrs: &[syn::Attribute]) -> bool {
        attrs.iter().any(|attr| {
            let path = attr.path();

            // Check single ident: #[test], #[rstest], #[test_case]
            if path.is_ident("test") || path.is_ident("rstest") || path.is_ident("test_case") {
                return true;
            }

            // Check path-based attributes: #[tokio::test], #[actix_rt::test]
            let segments: Vec<String> = path.segments.iter().map(|s| s.ident.to_string()).collect();

            if segments.len() == 2 {
                let first = segments[0].as_str();
                let second = segments[1].as_str();
                return (first == "actix_rt" || first == "tokio") && second == "test";
            }

            false
        })
    }

    /// Add a call edge to the graph
    pub fn add_call(&mut self, caller: FunctionId, callee: FunctionId, call_type: CallType) {
        self.call_graph.add_call(FunctionCall {
            caller,
            callee,
            call_type,
        });
    }

    /// Get all functions in the graph
    pub fn all_functions(&self) -> impl Iterator<Item = &FunctionId> {
        self.call_graph.get_all_functions()
    }

    /// Get the number of functions in the graph
    pub fn function_count(&self) -> usize {
        self.call_graph.node_count()
    }

    /// Merge another call graph into this one
    pub fn merge(&mut self, other: CallGraph) {
        self.call_graph.merge(other);
    }

    /// Extract a function name from a syn path
    pub fn extract_function_name_from_path(path: &syn::Path) -> Option<String> {
        // Get the full path as a string
        let segments: Vec<String> = path
            .segments
            .iter()
            .map(|seg| seg.ident.to_string())
            .collect();

        if segments.is_empty() {
            return None;
        }

        // Join with :: to get the full qualified name
        Some(segments.join("::"))
    }

    /// Get line number from a span (placeholder for actual implementation)
    pub fn get_line_number(&self, span: proc_macro2::Span) -> usize {
        span.start().line
    }

    /// Classify an expression for special handling
    pub fn classify_expr_category(expr: &syn::Expr) -> ExprCategory {
        match expr {
            syn::Expr::Closure(_) => ExprCategory::Closure,
            syn::Expr::Async(_) => ExprCategory::Async,
            syn::Expr::Await(_) => ExprCategory::Await,
            syn::Expr::Try(_) => ExprCategory::Try,
            syn::Expr::Unsafe(_) => ExprCategory::Unsafe,
            _ => ExprCategory::Regular,
        }
    }

    /// Check if an expression category needs special handling
    pub fn needs_special_handling(category: ExprCategory) -> bool {
        !matches!(category, ExprCategory::Regular)
    }

    /// Build a qualified function name with module path
    pub fn build_qualified_name(&self, base_name: &str) -> String {
        if self.module_path.is_empty() {
            base_name.to_string()
        } else {
            format!("{}::{}", self.module_path.join("::"), base_name)
        }
    }

    /// Build a qualified name for an impl method
    pub fn build_impl_method_name(&self, impl_type: &str, method_name: &str) -> String {
        if self.module_path.is_empty() {
            format!("{}::{}", impl_type, method_name)
        } else {
            format!(
                "{}::{}::{}",
                self.module_path.join("::"),
                impl_type,
                method_name
            )
        }
    }
}

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

    #[test]
    fn test_module_path_operations() {
        let mut builder = GraphBuilder::new(PathBuf::from("test.rs"));

        assert!(builder.module_path().is_empty());

        builder.push_module("mod1".to_string());
        assert_eq!(builder.module_path(), &["mod1"]);

        builder.push_module("mod2".to_string());
        assert_eq!(builder.module_path(), &["mod1", "mod2"]);

        builder.pop_module();
        assert_eq!(builder.module_path(), &["mod1"]);
    }

    #[test]
    fn test_build_qualified_name() {
        let mut builder = GraphBuilder::new(PathBuf::from("test.rs"));

        assert_eq!(builder.build_qualified_name("func"), "func");

        builder.push_module("module".to_string());
        assert_eq!(builder.build_qualified_name("func"), "module::func");

        builder.push_module("submodule".to_string());
        assert_eq!(
            builder.build_qualified_name("func"),
            "module::submodule::func"
        );
    }

    #[test]
    fn test_build_impl_method_name() {
        let mut builder = GraphBuilder::new(PathBuf::from("test.rs"));

        assert_eq!(
            builder.build_impl_method_name("MyStruct", "method"),
            "MyStruct::method"
        );

        builder.push_module("module".to_string());
        assert_eq!(
            builder.build_impl_method_name("MyStruct", "method"),
            "module::MyStruct::method"
        );
    }

    #[test]
    fn test_classify_expr_category() {
        use syn::parse_quote;

        let closure: syn::Expr = parse_quote! { |x| x + 1 };
        assert_eq!(
            GraphBuilder::classify_expr_category(&closure),
            ExprCategory::Closure
        );

        let async_block: syn::Expr = parse_quote! { async { foo().await } };
        assert_eq!(
            GraphBuilder::classify_expr_category(&async_block),
            ExprCategory::Async
        );

        let regular: syn::Expr = parse_quote! { foo() };
        assert_eq!(
            GraphBuilder::classify_expr_category(&regular),
            ExprCategory::Regular
        );
    }

    #[test]
    fn test_needs_special_handling() {
        assert!(GraphBuilder::needs_special_handling(ExprCategory::Closure));
        assert!(GraphBuilder::needs_special_handling(ExprCategory::Async));
        assert!(GraphBuilder::needs_special_handling(ExprCategory::Await));
        assert!(!GraphBuilder::needs_special_handling(ExprCategory::Regular));
    }

    // Unit tests for test function detection (Spec 267)

    #[test]
    fn test_detect_basic_test_attribute() {
        use syn::parse_quote;

        let test_fn: ItemFn = parse_quote! {
            #[test]
            fn my_test() {
                assert!(true);
            }
        };

        assert!(
            GraphBuilder::has_test_attribute(&test_fn.attrs),
            "#[test] attribute should be detected"
        );
    }

    #[test]
    fn test_detect_production_function() {
        use syn::parse_quote;

        let prod_fn: ItemFn = parse_quote! {
            fn production_function() {
                println!("hello");
            }
        };

        assert!(
            !GraphBuilder::has_test_attribute(&prod_fn.attrs),
            "Function without #[test] should NOT be detected as test"
        );
    }

    #[test]
    fn test_detect_tokio_test_attribute() {
        use syn::parse_quote;

        let tokio_test_fn: ItemFn = parse_quote! {
            #[tokio::test]
            async fn async_test() {
                assert!(true);
            }
        };

        assert!(
            GraphBuilder::has_test_attribute(&tokio_test_fn.attrs),
            "#[tokio::test] attribute should be detected"
        );
    }

    #[test]
    fn test_detect_actix_test_attribute() {
        use syn::parse_quote;

        let actix_test_fn: ItemFn = parse_quote! {
            #[actix_rt::test]
            async fn actix_test() {
                assert!(true);
            }
        };

        assert!(
            GraphBuilder::has_test_attribute(&actix_test_fn.attrs),
            "#[actix_rt::test] attribute should be detected"
        );
    }

    #[test]
    fn test_detect_rstest_attribute() {
        use syn::parse_quote;

        let rstest_fn: ItemFn = parse_quote! {
            #[rstest]
            fn parameterized_test() {
                assert!(true);
            }
        };

        assert!(
            GraphBuilder::has_test_attribute(&rstest_fn.attrs),
            "#[rstest] attribute should be detected"
        );
    }

    #[test]
    fn test_detect_test_case_attribute() {
        use syn::parse_quote;

        let test_case_fn: ItemFn = parse_quote! {
            #[test_case]
            fn test_case_test() {
                assert!(true);
            }
        };

        assert!(
            GraphBuilder::has_test_attribute(&test_case_fn.attrs),
            "#[test_case] attribute should be detected"
        );
    }

    #[test]
    fn test_helper_function_without_test_attr_not_detected() {
        use syn::parse_quote;

        // Helper functions in test modules don't have #[test]
        let helper_fn: ItemFn = parse_quote! {
            fn create_test_fixture() -> i32 {
                42
            }
        };

        assert!(
            !GraphBuilder::has_test_attribute(&helper_fn.attrs),
            "Helper functions without #[test] should NOT be detected as tests"
        );
    }

    #[test]
    fn test_function_with_other_attributes_not_detected() {
        use syn::parse_quote;

        let other_fn: ItemFn = parse_quote! {
            #[inline]
            #[must_use]
            fn some_function() -> i32 {
                42
            }
        };

        assert!(
            !GraphBuilder::has_test_attribute(&other_fn.attrs),
            "Function with non-test attributes should NOT be detected as test"
        );
    }
}