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
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
use debtmap::complexity::calculate_cognitive_for_block;
use debtmap::complexity::pattern_adjustments::{
    calculate_cognitive_adjusted, PatternMatchRecognizer, PatternRecognizer, PatternType,
    SimpleDelegationRecognizer,
};

#[test]
fn test_pattern_matching_reduces_complexity() {
    // Test with a typical file type detection function
    let code = r#"
        fn detect_file_type(path: &str) -> FileType {
            if path.ends_with(".rs") {
                return FileType::Rust;
            }
            if path.ends_with(".py") {
                return FileType::Python;
            }
            if path.ends_with(".js") {
                return FileType::JavaScript;
            }
            if path.ends_with(".ts") {
                return FileType::TypeScript;
            }
            if path.ends_with(".go") {
                return FileType::Go;
            }
            if path.ends_with(".java") {
                return FileType::Java;
            }
            if path.ends_with(".cpp") || path.ends_with(".cc") {
                return FileType::Cpp;
            }
            if path.ends_with(".c") {
                return FileType::C;
            }
            FileType::Unknown
        }
    "#;

    let file = syn::parse_file(code).unwrap();
    if let syn::Item::Fn(func) = &file.items[0] {
        // calculate_cognitive_for_block already includes pattern adjustments now
        let complexity = calculate_cognitive_for_block(&func.block);

        // With pattern adjustments, complexity should be manageable
        assert!(
            complexity <= 10,
            "Adjusted complexity should be reasonable: got {}",
            complexity
        );
    } else {
        panic!("Expected function");
    }
}

#[test]
fn test_simple_delegation_low_complexity() {
    let code = r#"
        fn process_data(input: Data) -> Result<Output> {
            let validated = validate(input)?;
            let transformed = transform(validated);
            finalize(transformed)
        }
    "#;

    let file = syn::parse_file(code).unwrap();
    if let syn::Item::Fn(func) = &file.items[0] {
        let recognizer = SimpleDelegationRecognizer::new();
        let info = recognizer.detect(&func.block);

        assert!(info.is_some(), "Should detect simple delegation");

        // Simple delegation should have minimal complexity
        let adjusted = recognizer.adjust_complexity(&info.unwrap(), 10);
        assert_eq!(adjusted, 1, "Simple delegation should have complexity of 1");
    } else {
        panic!("Expected function");
    }
}

#[test]
fn test_non_pattern_matching_unchanged() {
    let code = r#"
        fn complex_logic(x: i32, y: i32) -> i32 {
            if x > 0 {
                if y > 0 {
                    return x + y;
                } else {
                    return x - y;
                }
            } else {
                if y > 0 {
                    return y - x;
                } else {
                    return -(x + y);
                }
            }
        }
    "#;

    let file = syn::parse_file(code).unwrap();
    if let syn::Item::Fn(func) = &file.items[0] {
        let base_complexity = 10; // Example base
        let adjusted = calculate_cognitive_adjusted(&func.block, base_complexity);

        // Non-pattern matching should not be adjusted
        assert_eq!(
            adjusted, base_complexity,
            "Non-pattern complexity should be unchanged"
        );
    } else {
        panic!("Expected function");
    }
}

#[test]
fn test_pattern_with_else_default() {
    let code = r#"
        fn classify_number(n: i32) -> Classification {
            if n == 0 {
                return Classification::Zero;
            } else if n == 1 {
                return Classification::One;
            } else if n == 2 {
                return Classification::Two;
            } else if n == 3 {
                return Classification::Three;
            } else {
                return Classification::Other;
            }
        }
    "#;

    let file = syn::parse_file(code).unwrap();
    if let syn::Item::Fn(func) = &file.items[0] {
        let recognizer = PatternMatchRecognizer::new();
        let info = recognizer.detect(&func.block);

        assert!(info.is_some(), "Should detect pattern matching");
        let info = info.unwrap();
        assert!(info.has_default, "Should detect else clause as default");
        assert_eq!(info.condition_count, 4, "Should count 4 conditions");
    } else {
        panic!("Expected function");
    }
}

#[test]
fn test_mixed_conditions_not_pattern() {
    // Different variables being tested - not pattern matching
    let code = r#"
        fn check_conditions(x: i32, y: i32, z: i32) -> bool {
            if x > 0 {
                return true;
            }
            if y < 0 {
                return false;
            }
            if z == 0 {
                return true;
            }
            false
        }
    "#;

    let file = syn::parse_file(code).unwrap();
    if let syn::Item::Fn(func) = &file.items[0] {
        let recognizer = PatternMatchRecognizer::new();
        let info = recognizer.detect(&func.block);

        assert!(
            info.is_none(),
            "Should not detect pattern matching for different variables"
        );
    } else {
        panic!("Expected function");
    }
}

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

    // Test with 16 conditions
    let block: Block = parse_quote! {{
        if x == 1 { return A; }
        if x == 2 { return B; }
        if x == 3 { return C; }
        if x == 4 { return D; }
        if x == 5 { return E; }
        if x == 6 { return F; }
        if x == 7 { return G; }
        if x == 8 { return H; }
        if x == 9 { return I; }
        if x == 10 { return J; }
        if x == 11 { return K; }
        if x == 12 { return L; }
        if x == 13 { return M; }
        if x == 14 { return N; }
        if x == 15 { return O; }
        if x == 16 { return P; }
    }};

    let recognizer = PatternMatchRecognizer::new();
    let info = recognizer.detect(&block);

    assert!(info.is_some());
    let info = info.unwrap();
    assert_eq!(info.condition_count, 16);

    // log2(16) = 4, plus 1 for no default = 5
    let adjusted = recognizer.adjust_complexity(&info, 16);
    assert_eq!(
        adjusted, 5,
        "16 conditions should have log2(16) + 1 = 5 complexity"
    );
}

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

    // Test simple if-else chain with same variable
    let if_expr: ExprIf = parse_quote! {
        if name.starts_with("test_") {
            return ItemType::Test;
        } else if name.contains("_impl") {
            return ItemType::Implementation;
        } else {
            return ItemType::Regular;
        }
    };

    let recognizer = PatternMatchRecognizer::new();
    let mut tracked_var = None;
    let result = recognizer.analyze_if_chain(&if_expr, &mut tracked_var);

    assert!(result.is_some(), "Should recognize if-else chain pattern");
    let (conditions, _pattern_types, has_else) = result.unwrap();
    assert_eq!(conditions.len(), 2, "Should detect 2 conditions");
    assert!(has_else, "Should detect else branch");
    assert_eq!(
        tracked_var,
        Some("name".to_string()),
        "Should track variable name"
    );
}

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

    // Test if-else chain without immediate returns
    let if_expr: ExprIf = parse_quote! {
        if x == 1 {
            let y = 2;
            println!("Processing");
            return y;
        } else if x == 2 {
            return 3;
        }
    };

    let recognizer = PatternMatchRecognizer::new();
    let mut tracked_var = None;
    let result = recognizer.analyze_if_chain(&if_expr, &mut tracked_var);

    assert!(
        result.is_none(),
        "Should not recognize pattern without immediate returns"
    );
}

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

    // Test if-else chain with different variables (not pattern matching)
    let if_expr: ExprIf = parse_quote! {
        if x > 0 {
            return true;
        } else if y < 0 {
            return false;
        }
    };

    let recognizer = PatternMatchRecognizer::new();
    let mut tracked_var = None;
    let result = recognizer.analyze_if_chain(&if_expr, &mut tracked_var);

    assert!(
        result.is_none(),
        "Should not recognize pattern with different variables"
    );
}

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

    // Test if-else chain without final else
    let if_expr: ExprIf = parse_quote! {
        if status == "pending" {
            return Status::Pending;
        } else if status == "active" {
            return Status::Active;
        } else if status == "done" {
            return Status::Done;
        }
    };

    let recognizer = PatternMatchRecognizer::new();
    let mut tracked_var = None;
    let result = recognizer.analyze_if_chain(&if_expr, &mut tracked_var);

    assert!(result.is_some(), "Should recognize pattern without else");
    let (_conditions, _pattern_types, has_else) = result.unwrap();
    assert!(!has_else, "Should detect no else branch");
}

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

    // Test with method calls on same variable
    let if_expr: ExprIf = parse_quote! {
        if path.ends_with(".rs") {
            return FileType::Rust;
        } else if path.ends_with(".py") {
            return FileType::Python;
        } else if path.starts_with("/tmp") {
            return FileType::Temp;
        }
    };

    let recognizer = PatternMatchRecognizer::new();
    let mut tracked_var = None;
    let result = recognizer.analyze_if_chain(&if_expr, &mut tracked_var);

    assert!(result.is_some(), "Should recognize method call patterns");
    let (conditions, pattern_types, _) = result.unwrap();
    assert_eq!(conditions.len(), 3, "Should detect 3 conditions");
    assert!(pattern_types
        .iter()
        .any(|pt| matches!(pt, PatternType::StringMatching)));
}

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

    // Test with field access patterns
    let if_expr: ExprIf = parse_quote! {
        if self.state == State::Init {
            return Action::Start;
        } else if self.state == State::Running {
            return Action::Continue;
        } else {
            return Action::Stop;
        }
    };

    let recognizer = PatternMatchRecognizer::new();
    let mut tracked_var = None;
    let result = recognizer.analyze_if_chain(&if_expr, &mut tracked_var);

    // Current implementation doesn't handle field access in binary expressions
    assert!(
        result.is_none(),
        "Field access in binary expressions not currently supported"
    );
}

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

    // Test with single expressions in branches (not returns)
    let if_expr: ExprIf = parse_quote! {
        if mode == 1 {
            Mode::Fast
        } else if mode == 2 {
            Mode::Normal
        } else {
            Mode::Slow
        }
    };

    let recognizer = PatternMatchRecognizer::new();
    let mut tracked_var = None;
    let result = recognizer.analyze_if_chain(&if_expr, &mut tracked_var);

    // This should be recognized as it has single expressions
    assert!(
        result.is_some(),
        "Should recognize single expression patterns"
    );
}

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

    let recognizer = PatternMatchRecognizer::new();

    // Test method call extraction
    let expr: Expr = parse_quote! { path.ends_with(".rs") };
    assert_eq!(
        recognizer.extract_tested_variable(&expr),
        Some("path".to_string())
    );

    // Test binary comparison
    let expr: Expr = parse_quote! { x == 5 };
    assert_eq!(
        recognizer.extract_tested_variable(&expr),
        Some("x".to_string())
    );

    // Test field access in method call - extracts base only
    let expr: Expr = parse_quote! { self.field.method() };
    assert_eq!(
        recognizer.extract_tested_variable(&expr),
        Some("self".to_string())
    );

    // Test unary expression
    let expr: Expr = parse_quote! { !is_valid };
    assert_eq!(
        recognizer.extract_tested_variable(&expr),
        Some("is_valid".to_string())
    );

    // Test parenthesized expression
    let expr: Expr = parse_quote! { (value) };
    assert_eq!(
        recognizer.extract_tested_variable(&expr),
        Some("value".to_string())
    );

    // Test direct path
    let expr: Expr = parse_quote! { flag };
    assert_eq!(
        recognizer.extract_tested_variable(&expr),
        Some("flag".to_string())
    );

    // Test field in binary expression - current behavior
    let expr: Expr = parse_quote! { self.state == 5 };
    // Binary expressions with field access don't extract the field properly
    assert_eq!(recognizer.extract_tested_variable(&expr), None);
}

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

    let recognizer = PatternMatchRecognizer::new();

    // Test string matching methods
    let expr: Expr = parse_quote! { name.ends_with("_test") };
    assert!(matches!(
        recognizer.detect_pattern_type(&expr),
        PatternType::StringMatching
    ));

    let expr: Expr = parse_quote! { path.starts_with("/usr") };
    assert!(matches!(
        recognizer.detect_pattern_type(&expr),
        PatternType::StringMatching
    ));

    let expr: Expr = parse_quote! { text.contains("error") };
    assert!(matches!(
        recognizer.detect_pattern_type(&expr),
        PatternType::StringMatching
    ));

    // Test simple comparisons
    let expr: Expr = parse_quote! { x == 10 };
    assert!(matches!(
        recognizer.detect_pattern_type(&expr),
        PatternType::SimpleComparison
    ));

    let expr: Expr = parse_quote! { y != 0 };
    assert!(matches!(
        recognizer.detect_pattern_type(&expr),
        PatternType::SimpleComparison
    ));

    // Test range matching
    let expr: Expr = parse_quote! { value > 100 };
    assert!(matches!(
        recognizer.detect_pattern_type(&expr),
        PatternType::RangeMatching
    ));

    let expr: Expr = parse_quote! { count <= 5 };
    assert!(matches!(
        recognizer.detect_pattern_type(&expr),
        PatternType::RangeMatching
    ));
}

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

    let recognizer = PatternMatchRecognizer::new();

    // Test block with immediate return
    let block: Block = parse_quote! {{
        return value;
    }};
    assert!(recognizer.has_immediate_return(&block));

    // Test block with two statements (second is return)
    let block: Block = parse_quote! {{
        let x = 5;
        return x;
    }};
    assert!(recognizer.has_immediate_return(&block));

    // Test block without return
    let block: Block = parse_quote! {{
        let x = 5;
        let y = 10;
        x + y
    }};
    assert!(!recognizer.has_immediate_return(&block));

    // Test empty block
    let block: Block = parse_quote! {{}};
    assert!(!recognizer.has_immediate_return(&block));

    // Test block with too many statements
    let block: Block = parse_quote! {{
        let x = 1;
        let y = 2;
        let z = 3;
        return x + y + z;
    }};
    assert!(!recognizer.has_immediate_return(&block));
}

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

    let recognizer = PatternMatchRecognizer::new();

    // Test simple field access
    let expr: Expr = parse_quote! { self.state };
    assert_eq!(
        recognizer.extract_field_path(&expr),
        Some("self.state".to_string())
    );

    // Test nested field access
    let expr: Expr = parse_quote! { self.inner.value };
    assert_eq!(
        recognizer.extract_field_path(&expr),
        Some("self.inner.value".to_string())
    );

    // Test object field access
    let expr: Expr = parse_quote! { obj.field };
    assert_eq!(
        recognizer.extract_field_path(&expr),
        Some("obj.field".to_string())
    );

    // Test simple path without fields
    let expr: Expr = parse_quote! { variable };
    assert_eq!(
        recognizer.extract_field_path(&expr),
        Some("variable".to_string())
    );

    // Test expression that's not a field access
    let expr: Expr = parse_quote! { 42 };
    assert_eq!(recognizer.extract_field_path(&expr), None);
}