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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
use crate::core::{DebtType, Language};
use once_cell::sync::Lazy;
use regex::Regex;
use std::collections::HashMap;
use std::path::{Path, PathBuf};

#[derive(Debug, Clone)]
pub struct SuppressionContext {
    pub active_blocks: Vec<SuppressionBlock>,
    pub line_suppressions: HashMap<usize, SuppressionRule>,
    pub unclosed_blocks: Vec<UnclosedBlock>,
    /// Function-level suppressions using `debtmap:ignore[types] - reason`
    /// Maps the line number of the annotation to the suppression rule.
    /// The suppression applies to the next function definition after this line.
    pub function_allows: HashMap<usize, FunctionAllow>,
}

#[derive(Debug, Clone)]
pub struct SuppressionBlock {
    pub start_line: usize,
    pub end_line: Option<usize>,
    pub debt_types: Vec<DebtType>,
    pub reason: Option<String>,
}

#[derive(Debug, Clone)]
pub struct SuppressionRule {
    pub debt_types: Vec<DebtType>,
    pub reason: Option<String>,
    pub applies_to_next_line: bool,
}

#[derive(Debug, Clone)]
pub struct UnclosedBlock {
    pub file: PathBuf,
    pub start_line: usize,
}

/// Function-level suppression using `debtmap:ignore[types] - reason`
/// Applied to the function definition that follows the annotation.
#[derive(Debug, Clone)]
pub struct FunctionAllow {
    pub debt_types: Vec<DebtType>,
    pub reason: String,
    pub annotation_line: usize,
}

#[derive(Debug, Clone)]
pub struct SuppressionStats {
    pub total_suppressions: usize,
    pub suppressions_by_type: HashMap<DebtType, usize>,
    pub unclosed_blocks: Vec<UnclosedBlock>,
}

impl SuppressionContext {
    pub fn new() -> Self {
        Self {
            active_blocks: Vec::new(),
            line_suppressions: HashMap::new(),
            unclosed_blocks: Vec::new(),
            function_allows: HashMap::new(),
        }
    }

    /// Check if a function starting at the given line is suppressed via `debtmap:ignore`.
    /// Looks for ignore annotations in the lines immediately preceding the function.
    pub fn is_function_allowed(&self, function_start_line: usize, debt_type: &DebtType) -> bool {
        // Check up to 5 lines before the function for an allow annotation
        // (to account for doc comments between the annotation and function)
        for offset in 1..=5 {
            if function_start_line < offset {
                break;
            }
            let check_line = function_start_line - offset;
            if let Some(allow) = self.function_allows.get(&check_line) {
                return debt_type_matches(debt_type, &allow.debt_types);
            }
        }
        false
    }

    /// Get the reason for a function-level suppression, if any.
    pub fn get_function_allow_reason(
        &self,
        function_start_line: usize,
        debt_type: &DebtType,
    ) -> Option<&str> {
        for offset in 1..=5 {
            if function_start_line < offset {
                break;
            }
            let check_line = function_start_line - offset;
            if let Some(allow) = self.function_allows.get(&check_line) {
                if debt_type_matches(debt_type, &allow.debt_types) {
                    return Some(&allow.reason);
                }
            }
        }
        None
    }

    pub fn is_suppressed(&self, line: usize, debt_type: &DebtType) -> bool {
        // Check all suppression sources using lazy evaluation
        [
            self.is_in_suppression_block(line, debt_type),
            self.has_line_suppression(line, debt_type),
            self.has_next_line_suppression(line, debt_type),
        ]
        .into_iter()
        .any(|suppressed| suppressed)
    }

    fn is_in_suppression_block(&self, line: usize, debt_type: &DebtType) -> bool {
        self.active_blocks
            .iter()
            .filter(|block| line_within_block(line, block))
            .any(|block| debt_type_matches(debt_type, &block.debt_types))
    }

    fn has_line_suppression(&self, line: usize, debt_type: &DebtType) -> bool {
        self.line_suppressions
            .get(&line)
            .is_some_and(|rule| debt_type_matches(debt_type, &rule.debt_types))
    }

    fn has_next_line_suppression(&self, line: usize, debt_type: &DebtType) -> bool {
        (line > 0)
            .then(|| self.line_suppressions.get(&(line - 1)))
            .flatten()
            .is_some_and(|rule| {
                rule.applies_to_next_line && debt_type_matches(debt_type, &rule.debt_types)
            })
    }

    pub fn get_stats(&self) -> SuppressionStats {
        let completed_blocks = self.count_completed_blocks();
        let all_debt_types = self.collect_all_debt_types();
        let total_suppressions = completed_blocks + self.line_suppressions.len();
        let suppressions_by_type = self.count_suppressions_by_type(all_debt_types);

        SuppressionStats {
            total_suppressions,
            suppressions_by_type,
            unclosed_blocks: self.unclosed_blocks.clone(),
        }
    }

    fn count_completed_blocks(&self) -> usize {
        self.active_blocks
            .iter()
            .filter(|block| block.end_line.is_some())
            .count()
    }

    fn collect_all_debt_types(&self) -> Vec<DebtType> {
        let block_types = self
            .active_blocks
            .iter()
            .filter(|block| block.end_line.is_some())
            .flat_map(|block| block.debt_types.iter().cloned());

        let line_types = self
            .line_suppressions
            .values()
            .flat_map(|rule| rule.debt_types.iter().cloned());

        block_types.chain(line_types).collect()
    }

    fn count_suppressions_by_type(&self, debt_types: Vec<DebtType>) -> HashMap<DebtType, usize> {
        debt_types
            .into_iter()
            .fold(HashMap::new(), |mut acc, debt_type| {
                *acc.entry(debt_type).or_insert(0) += 1;
                acc
            })
    }
}

impl Default for SuppressionContext {
    fn default() -> Self {
        Self::new()
    }
}

// Helper functions extracted as pure functions for better testability
fn line_within_block(line: usize, block: &SuppressionBlock) -> bool {
    // Special case: line 0 means "unknown line" and should be suppressed if the block starts at line 1
    // This handles cases where the AST doesn't provide exact line numbers
    if line == 0 && block.start_line == 1 {
        return true;
    }
    line >= block.start_line && block.end_line.is_some_and(|end| line <= end)
}

fn debt_type_matches(debt_type: &DebtType, allowed_types: &[DebtType]) -> bool {
    // Empty allowed_types means wildcard - match all
    if allowed_types.is_empty() {
        return true;
    }
    // Compare by variant discriminant, ignoring field values
    // This allows `testing` suppression to match any TestingGap regardless of metrics
    allowed_types
        .iter()
        .any(|allowed| std::mem::discriminant(allowed) == std::mem::discriminant(debt_type))
}

struct SuppressionPatterns {
    block_start: Regex,
    block_end: Regex,
    line: Regex,
    next_line: Regex,
    /// Function-level ignore: `debtmap:ignore[types] - reason` (reason required)
    function_allow: Regex,
}

impl SuppressionPatterns {
    fn new(language: Language) -> Self {
        let comment_prefix = get_comment_prefix_pattern(language);

        Self {
            block_start: Regex::new(&format!(
                r"(?m)^\s*{comment_prefix}\s*debtmap:ignore-start(?:\s*\[([\w,*]+)\])?(?:\s*--\s*(.*))?$"
            )).unwrap(),
            block_end: Regex::new(&format!(
                r"(?m)^\s*{comment_prefix}\s*debtmap:ignore-end\s*$"
            )).unwrap(),
            line: Regex::new(&format!(
                r"(?m){comment_prefix}\s*debtmap:ignore(?:\s*\[([\w,*]+)\])?(?:\s*--\s*(.*))?$"
            )).unwrap(),
            next_line: Regex::new(&format!(
                r"(?m)^\s*{comment_prefix}\s*debtmap:ignore-next-line(?:\s*\[([\w,*]+)\])?(?:\s*--\s*(.*))?$"
            )).unwrap(),
            // Function-level ignore requires a reason (after - or --)
            function_allow: Regex::new(&format!(
                r"(?m)^\s*{comment_prefix}\s*debtmap:ignore\s*\[([\w,*]+)\]\s*-{{1,2}}\s*(.+)$"
            )).unwrap(),
        }
    }
}

/// Returns a regex pattern for matching comment prefixes.
/// For Rust, matches both `//` and `///` (doc comments).
fn get_comment_prefix_pattern(language: Language) -> &'static str {
    match language {
        Language::Python => "#",
        // Match both // and /// (doc comments)
        Language::Rust => r"///?",
        _ => "//",
    }
}

enum LineParseResult {
    BlockStart(usize, Vec<DebtType>, Option<String>),
    BlockEnd(usize),
    NextLineSuppression(usize, Vec<DebtType>, Option<String>),
    LineSuppression(usize, Vec<DebtType>, Option<String>),
    /// Function-level ignore: (line, debt_types, reason)
    FunctionAllowAnnotation(usize, Vec<DebtType>, String),
    None,
}

fn parse_line(line: &str, line_number: usize, patterns: &SuppressionPatterns) -> LineParseResult {
    // Try each pattern in order and return the first match
    // Note: function_allow is tried before line suppression to avoid partial matches
    try_parse_block_start(line, line_number, patterns)
        .or_else(|| try_parse_block_end(line, line_number, patterns))
        .or_else(|| try_parse_next_line(line, line_number, patterns))
        .or_else(|| try_parse_function_allow(line, line_number, patterns))
        .or_else(|| try_parse_line_suppression(line, line_number, patterns))
        .unwrap_or(LineParseResult::None)
}

fn try_parse_block_start(
    line: &str,
    line_number: usize,
    patterns: &SuppressionPatterns,
) -> Option<LineParseResult> {
    patterns.block_start.captures(line).map(|captures| {
        LineParseResult::BlockStart(
            line_number,
            parse_debt_types(captures.get(1).map(|m| m.as_str())),
            captures.get(2).map(|m| m.as_str().to_string()),
        )
    })
}

fn try_parse_block_end(
    line: &str,
    line_number: usize,
    patterns: &SuppressionPatterns,
) -> Option<LineParseResult> {
    patterns
        .block_end
        .is_match(line)
        .then_some(LineParseResult::BlockEnd(line_number))
}

fn try_parse_next_line(
    line: &str,
    line_number: usize,
    patterns: &SuppressionPatterns,
) -> Option<LineParseResult> {
    patterns.next_line.captures(line).map(|captures| {
        LineParseResult::NextLineSuppression(
            line_number,
            parse_debt_types(captures.get(1).map(|m| m.as_str())),
            captures.get(2).map(|m| m.as_str().to_string()),
        )
    })
}

fn try_parse_line_suppression(
    line: &str,
    line_number: usize,
    patterns: &SuppressionPatterns,
) -> Option<LineParseResult> {
    patterns.line.captures(line).map(|captures| {
        LineParseResult::LineSuppression(
            line_number,
            parse_debt_types(captures.get(1).map(|m| m.as_str())),
            captures.get(2).map(|m| m.as_str().to_string()),
        )
    })
}

fn try_parse_function_allow(
    line: &str,
    line_number: usize,
    patterns: &SuppressionPatterns,
) -> Option<LineParseResult> {
    patterns.function_allow.captures(line).map(|captures| {
        LineParseResult::FunctionAllowAnnotation(
            line_number,
            parse_debt_types(captures.get(1).map(|m| m.as_str())),
            captures
                .get(2)
                .map(|m| m.as_str().trim().to_string())
                .unwrap_or_default(),
        )
    })
}

fn process_parsed_line(
    result: LineParseResult,
    context: &mut SuppressionContext,
    open_blocks: &mut Vec<(usize, Vec<DebtType>, Option<String>)>,
) {
    use LineParseResult::*;

    match result {
        BlockStart(ln, types, reason) => open_blocks.push((ln, types, reason)),
        BlockEnd(end_line) => handle_block_end(context, open_blocks, end_line),
        NextLineSuppression(ln, types, reason) => {
            add_line_suppression(context, ln, types, reason, true)
        }
        LineSuppression(ln, types, reason) => {
            add_line_suppression(context, ln, types, reason, false)
        }
        FunctionAllowAnnotation(ln, types, reason) => {
            context.function_allows.insert(
                ln,
                FunctionAllow {
                    debt_types: types,
                    reason,
                    annotation_line: ln,
                },
            );
        }
        None => {}
    }
}

fn handle_block_end(
    context: &mut SuppressionContext,
    open_blocks: &mut Vec<(usize, Vec<DebtType>, Option<String>)>,
    end_line: usize,
) {
    if let Some((start_line, debt_types, reason)) = open_blocks.pop() {
        context.active_blocks.push(SuppressionBlock {
            start_line,
            end_line: Some(end_line),
            debt_types,
            reason,
        });
    }
}

fn add_line_suppression(
    context: &mut SuppressionContext,
    line: usize,
    debt_types: Vec<DebtType>,
    reason: Option<String>,
    applies_to_next_line: bool,
) {
    context.line_suppressions.insert(
        line,
        SuppressionRule {
            debt_types,
            reason,
            applies_to_next_line,
        },
    );
}

/// Transforms open blocks into unclosed block markers
fn create_unclosed_blocks(
    open_blocks: Vec<(usize, Vec<DebtType>, Option<String>)>,
    file: &Path,
) -> Vec<UnclosedBlock> {
    open_blocks
        .into_iter()
        .map(|(start_line, _, _)| UnclosedBlock {
            file: file.to_path_buf(),
            start_line,
        })
        .collect()
}

pub fn parse_suppression_comments(
    content: &str,
    language: Language,
    file: &Path,
) -> SuppressionContext {
    let patterns = SuppressionPatterns::new(language);
    let mut context = SuppressionContext::new();
    let mut open_blocks: Vec<(usize, Vec<DebtType>, Option<String>)> = Vec::new();

    for (line_number, line) in content
        .lines()
        .enumerate()
        .map(|(idx, line)| (idx + 1, line))
    {
        let result = parse_line(line, line_number, &patterns);
        process_parsed_line(result, &mut context, &mut open_blocks);
    }

    // Record any unclosed blocks
    context.unclosed_blocks = create_unclosed_blocks(open_blocks, file);

    context
}

/// Map of keyword to list of debt types it matches.
/// Some keywords match multiple variants (e.g., "complexity" matches both Complexity and ComplexityHotspot).
static DEBT_TYPE_MAP: Lazy<HashMap<&'static str, Vec<DebtType>>> = Lazy::new(|| {
    let mut map = HashMap::new();
    map.insert("todo", vec![DebtType::Todo { reason: None }]);
    map.insert("fixme", vec![DebtType::Fixme { reason: None }]);
    map.insert("smell", vec![DebtType::CodeSmell { smell_type: None }]);
    map.insert("codesmell", vec![DebtType::CodeSmell { smell_type: None }]);
    map.insert(
        "duplication",
        vec![DebtType::Duplication {
            instances: 0,
            total_lines: 0,
        }],
    );
    map.insert(
        "duplicate",
        vec![DebtType::Duplication {
            instances: 0,
            total_lines: 0,
        }],
    );
    // "complexity" matches both Complexity (legacy) and ComplexityHotspot (priority-based)
    map.insert(
        "complexity",
        vec![
            DebtType::Complexity {
                cyclomatic: 0,
                cognitive: 0,
            },
            DebtType::ComplexityHotspot {
                cyclomatic: 0,
                cognitive: 0,
            },
        ],
    );
    // Explicit alias for hotspot-only suppression
    map.insert(
        "hotspot",
        vec![DebtType::ComplexityHotspot {
            cyclomatic: 0,
            cognitive: 0,
        }],
    );
    map.insert(
        "dependency",
        vec![DebtType::Dependency {
            dependency_type: None,
        }],
    );
    // Testing/coverage debt types - for suppressing TestingGap items
    map.insert(
        "testing",
        vec![DebtType::TestingGap {
            coverage: 0.0,
            cyclomatic: 0,
            cognitive: 0,
        }],
    );
    map.insert(
        "coverage",
        vec![DebtType::TestingGap {
            coverage: 0.0,
            cyclomatic: 0,
            cognitive: 0,
        }],
    );
    map.insert(
        "untested",
        vec![DebtType::TestingGap {
            coverage: 0.0,
            cyclomatic: 0,
            cognitive: 0,
        }],
    );
    // God Object debt type - for suppressing file-level God Object detections
    map.insert(
        "god_object",
        vec![DebtType::GodObject {
            methods: 0,
            fields: None,
            responsibilities: 0,
            god_object_score: 0.0,
            lines: 0,
        }],
    );
    map.insert(
        "godobject",
        vec![DebtType::GodObject {
            methods: 0,
            fields: None,
            responsibilities: 0,
            god_object_score: 0.0,
            lines: 0,
        }],
    );
    map
});

fn parse_debt_types(types_str: Option<&str>) -> Vec<DebtType> {
    // Early return for special cases
    let Some(types) = types_str else {
        return vec![]; // No types specified means all types
    };

    if types == "*" {
        return vec![]; // Empty vector means all types
    }

    // Use a static mapping for type conversion
    // Each keyword can map to multiple debt types, so we flatten the results
    types
        .split(',')
        .flat_map(|t| parse_single_debt_type(t.trim()))
        .collect()
}

fn parse_single_debt_type(type_str: &str) -> Vec<DebtType> {
    DEBT_TYPE_MAP
        .get(type_str.to_lowercase().as_str())
        .cloned()
        .unwrap_or_default()
}

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

    #[test]
    fn test_parse_block_suppression() {
        // debtmap:ignore-start -- Test fixture data
        let content = r#"
// debtmap:ignore-start
// TODO: This should be suppressed
// FIXME: This too
// debtmap:ignore-end
// TODO: This should not be suppressed
"#;
        // debtmap:ignore-end
        let file = PathBuf::from("test.rs");
        let context = parse_suppression_comments(content, Language::Rust, &file);

        assert_eq!(context.active_blocks.len(), 1);
        assert_eq!(context.active_blocks[0].start_line, 2);
        assert_eq!(context.active_blocks[0].end_line, Some(5));
        assert!(context.is_suppressed(3, &DebtType::Todo { reason: None }));
        assert!(context.is_suppressed(4, &DebtType::Fixme { reason: None }));
        assert!(!context.is_suppressed(6, &DebtType::Todo { reason: None }));
    }

    #[test]
    fn test_parse_line_suppression() {
        // debtmap:ignore-start -- Test fixture data
        let content = r#"
// TODO: Not suppressed
// TODO: Suppressed // debtmap:ignore
// FIXME: Also not suppressed
"#;
        // debtmap:ignore-end
        let file = PathBuf::from("test.rs");
        let context = parse_suppression_comments(content, Language::Rust, &file);

        assert!(!context.is_suppressed(2, &DebtType::Todo { reason: None }));
        assert!(context.is_suppressed(3, &DebtType::Todo { reason: None }));
        assert!(!context.is_suppressed(4, &DebtType::Fixme { reason: None }));
    }

    #[test]
    fn test_parse_next_line_suppression() {
        // debtmap:ignore-start -- Test fixture data
        let content = r#"
// debtmap:ignore-next-line
// TODO: This should be suppressed
// TODO: This should not be suppressed
"#;
        // debtmap:ignore-end
        let file = PathBuf::from("test.rs");
        let context = parse_suppression_comments(content, Language::Rust, &file);

        assert!(context.is_suppressed(3, &DebtType::Todo { reason: None }));
        assert!(!context.is_suppressed(4, &DebtType::Todo { reason: None }));
    }

    #[test]
    fn test_type_specific_suppression() {
        // debtmap:ignore-start -- Test fixture data
        let content = r#"
// debtmap:ignore-start[todo]
// TODO: Suppressed
// FIXME: Not suppressed
// debtmap:ignore-end
"#;
        // debtmap:ignore-end
        let file = PathBuf::from("test.rs");
        let context = parse_suppression_comments(content, Language::Rust, &file);

        assert!(context.is_suppressed(3, &DebtType::Todo { reason: None }));
        assert!(!context.is_suppressed(4, &DebtType::Fixme { reason: None }));
    }

    #[test]
    fn test_suppression_with_reason() {
        // debtmap:ignore-start -- Test fixture data
        let content = r#"
// debtmap:ignore-start -- Test fixture
// TODO: Suppressed with reason
// debtmap:ignore-end
"#;
        // debtmap:ignore-end
        let file = PathBuf::from("test.rs");
        let context = parse_suppression_comments(content, Language::Rust, &file);

        assert_eq!(
            context.active_blocks[0].reason,
            Some("Test fixture".to_string())
        );
    }

    #[test]
    fn test_unclosed_block_detection() {
        // Test content with intentionally unclosed block for testing
        let content = format!(
            "{}{}{}",
            "// debtmap:", "ignore-start\n", "// TODO: In unclosed block\n"
        );
        let file = PathBuf::from("test.rs");
        let context = parse_suppression_comments(&content, Language::Rust, &file);

        assert_eq!(context.unclosed_blocks.len(), 1);
        assert_eq!(context.unclosed_blocks[0].start_line, 1);
    }

    #[test]
    fn test_python_comment_syntax() {
        // debtmap:ignore-start -- Test fixture data
        let content = r#"
# debtmap:ignore-start
# TODO: Python TODO
# debtmap:ignore-end
"#;
        // debtmap:ignore-end
        let file = PathBuf::from("test.py");
        let context = parse_suppression_comments(content, Language::Python, &file);

        assert_eq!(context.active_blocks.len(), 1);
        assert!(context.is_suppressed(3, &DebtType::Todo { reason: None }));
    }

    #[test]
    fn test_wildcard_suppression() {
        let content = "// TODO: Test // debtmap:ignore[*]";
        let file = PathBuf::from("test.rs");
        let context = parse_suppression_comments(content, Language::Rust, &file);

        // Line 1 has the wildcard suppression that applies to the same line
        assert!(context.is_suppressed(1, &DebtType::Todo { reason: None }));
        assert!(context.is_suppressed(1, &DebtType::Fixme { reason: None }));
        assert!(context.is_suppressed(1, &DebtType::CodeSmell { smell_type: None }));
    }

    #[test]
    fn test_create_unclosed_blocks() {
        let open_blocks = vec![
            (
                10,
                vec![DebtType::Todo { reason: None }],
                Some("reason1".to_string()),
            ),
            (25, vec![DebtType::Fixme { reason: None }], None),
            (
                42,
                vec![
                    DebtType::CodeSmell { smell_type: None },
                    DebtType::Complexity {
                        cyclomatic: 10,
                        cognitive: 8,
                    },
                ],
                Some("reason2".to_string()),
            ),
        ];
        let file = Path::new("test_file.rs");

        let unclosed = create_unclosed_blocks(open_blocks, file);

        assert_eq!(unclosed.len(), 3);
        assert_eq!(unclosed[0].start_line, 10);
        assert_eq!(unclosed[0].file, PathBuf::from("test_file.rs"));
        assert_eq!(unclosed[1].start_line, 25);
        assert_eq!(unclosed[2].start_line, 42);
    }

    #[test]
    fn test_create_unclosed_blocks_empty() {
        let open_blocks = vec![];
        let file = Path::new("empty.rs");

        let unclosed = create_unclosed_blocks(open_blocks, file);

        assert!(unclosed.is_empty());
    }

    #[test]
    fn test_testing_debt_type_suppression() {
        let content = r#"
// debtmap:ignore-start[testing]
fn untested_function() {}
// debtmap:ignore-end
"#;
        let file = PathBuf::from("test.rs");
        let context = parse_suppression_comments(content, Language::Rust, &file);

        let testing_gap = DebtType::TestingGap {
            coverage: 0.25,
            cyclomatic: 10,
            cognitive: 15,
        };
        assert!(context.is_suppressed(3, &testing_gap));
    }

    #[test]
    fn test_coverage_alias_suppression() {
        let content = "fn foo() {} // debtmap:ignore[coverage]";
        let file = PathBuf::from("test.rs");
        let context = parse_suppression_comments(content, Language::Rust, &file);

        let testing_gap = DebtType::TestingGap {
            coverage: 0.0,
            cyclomatic: 5,
            cognitive: 8,
        };
        assert!(context.is_suppressed(1, &testing_gap));
    }

    #[test]
    fn test_function_allow_basic() {
        let content = r#"
// debtmap:ignore[testing] -- Orchestration function; callees are tested
async fn run_loop() {}
"#;
        let file = PathBuf::from("test.rs");
        let context = parse_suppression_comments(content, Language::Rust, &file);

        assert_eq!(context.function_allows.len(), 1);
        let allow = context.function_allows.get(&2).unwrap();
        assert_eq!(allow.reason, "Orchestration function; callees are tested");
        assert!(!allow.debt_types.is_empty());
    }

    #[test]
    fn test_function_allow_is_function_allowed() {
        let content = r#"
// debtmap:ignore[testing] -- Pure logic extracted and tested
async fn orchestration_function() {}
"#;
        let file = PathBuf::from("test.rs");
        let context = parse_suppression_comments(content, Language::Rust, &file);

        let testing_gap = DebtType::TestingGap {
            coverage: 0.25,
            cyclomatic: 11,
            cognitive: 19,
        };
        // Function starts at line 3, annotation is at line 2
        assert!(context.is_function_allowed(3, &testing_gap));
        // Different function at line 10 should not be allowed
        assert!(!context.is_function_allowed(10, &testing_gap));
    }

    #[test]
    fn test_function_allow_get_reason() {
        let content = r#"
// debtmap:ignore[complexity,testing] -- State machine with exhaustive matching
fn complex_match() {}
"#;
        let file = PathBuf::from("test.rs");
        let context = parse_suppression_comments(content, Language::Rust, &file);

        let testing_gap = DebtType::TestingGap {
            coverage: 0.5,
            cyclomatic: 15,
            cognitive: 20,
        };
        let reason = context.get_function_allow_reason(3, &testing_gap);
        assert_eq!(reason, Some("State machine with exhaustive matching"));
    }

    #[test]
    fn test_function_allow_requires_reason() {
        // Without a reason, the pattern should not match
        let content = r#"
// debtmap:ignore[testing]
fn no_reason() {}
"#;
        let file = PathBuf::from("test.rs");
        let context = parse_suppression_comments(content, Language::Rust, &file);

        // Should not parse without the required reason
        assert!(context.function_allows.is_empty());
    }

    #[test]
    fn test_function_allow_multiple_types() {
        let content = r#"
// debtmap:ignore[testing,complexity] -- Async orchestration with inherent complexity
async fn run_loop() {}
"#;
        let file = PathBuf::from("test.rs");
        let context = parse_suppression_comments(content, Language::Rust, &file);

        let testing_gap = DebtType::TestingGap {
            coverage: 0.25,
            cyclomatic: 11,
            cognitive: 19,
        };
        let complexity = DebtType::Complexity {
            cyclomatic: 11,
            cognitive: 19,
        };

        assert!(context.is_function_allowed(3, &testing_gap));
        assert!(context.is_function_allowed(3, &complexity));
    }

    #[test]
    fn test_function_allow_wildcard() {
        let content = r#"
// debtmap:ignore[*] -- Legacy code pending refactor
fn legacy_function() {}
"#;
        let file = PathBuf::from("test.rs");
        let context = parse_suppression_comments(content, Language::Rust, &file);

        let testing_gap = DebtType::TestingGap {
            coverage: 0.0,
            cyclomatic: 20,
            cognitive: 30,
        };
        let complexity = DebtType::Complexity {
            cyclomatic: 20,
            cognitive: 30,
        };
        let todo = DebtType::Todo { reason: None };

        // Wildcard should allow all debt types
        assert!(context.is_function_allowed(3, &testing_gap));
        assert!(context.is_function_allowed(3, &complexity));
        assert!(context.is_function_allowed(3, &todo));
    }

    #[test]
    fn test_function_allow_doc_comment() {
        // Test that /// doc comments are also recognized for ignore annotations
        let content = r#"
    /// debtmap:ignore[complexity,coverage] -- I/O shell function
    async fn invoke() {}
"#;
        let file = PathBuf::from("test.rs");
        let context = parse_suppression_comments(content, Language::Rust, &file);

        let testing_gap = DebtType::TestingGap {
            coverage: 0.0,
            cyclomatic: 6,
            cognitive: 28,
        };
        let complexity = DebtType::Complexity {
            cyclomatic: 6,
            cognitive: 28,
        };

        // Function starts at line 3, annotation is at line 2
        assert!(context.is_function_allowed(3, &testing_gap));
        assert!(context.is_function_allowed(3, &complexity));

        // Check the reason was captured
        let reason = context.get_function_allow_reason(3, &testing_gap);
        assert_eq!(reason, Some("I/O shell function"));
    }

    #[test]
    fn test_suppression_block_doc_comment() {
        // Test that /// doc comments work for block suppressions too
        let content = r#"
/// debtmap:ignore-start[testing]
fn untested() {}
/// debtmap:ignore-end
"#;
        let file = PathBuf::from("test.rs");
        let context = parse_suppression_comments(content, Language::Rust, &file);

        let testing_gap = DebtType::TestingGap {
            coverage: 0.0,
            cyclomatic: 5,
            cognitive: 10,
        };
        assert!(context.is_suppressed(3, &testing_gap));
    }

    #[test]
    fn test_function_ignore_single_dash() {
        // Test that single dash works as reason separator (not just double dash)
        let content = r#"
// debtmap:ignore[testing] - I/O dispatcher with no extractable pure logic
async fn run_command() {}
"#;
        let file = PathBuf::from("test.rs");
        let context = parse_suppression_comments(content, Language::Rust, &file);

        let testing_gap = DebtType::TestingGap {
            coverage: 0.0,
            cyclomatic: 6,
            cognitive: 25,
        };

        // Function starts at line 3, annotation is at line 2
        assert!(context.is_function_allowed(3, &testing_gap));

        // Check the reason was captured
        let reason = context.get_function_allow_reason(3, &testing_gap);
        assert_eq!(
            reason,
            Some("I/O dispatcher with no extractable pure logic")
        );
    }

    #[test]
    fn test_god_object_suppression() {
        let content = r#"
// debtmap:ignore-start[god_object]
// Large file with many related functions
fn function1() {}
fn function2() {}
// debtmap:ignore-end
"#;
        let file = PathBuf::from("classifier.rs");
        let context = parse_suppression_comments(content, Language::Rust, &file);

        let god_object = DebtType::GodObject {
            methods: 122,
            fields: None,
            responsibilities: 13,
            god_object_score: 37.79,
            lines: 3057,
        };
        assert!(context.is_suppressed(4, &god_object));
        assert!(context.is_suppressed(5, &god_object));
    }

    #[test]
    fn test_god_object_function_allow() {
        // File-level annotation at the top of file
        let content = r#"
// debtmap:ignore[god_object] - Related pure classification functions for method analysis
// Module documentation...
"#;
        let file = PathBuf::from("classifier.rs");
        let context = parse_suppression_comments(content, Language::Rust, &file);

        let god_object = DebtType::GodObject {
            methods: 122,
            fields: None,
            responsibilities: 13,
            god_object_score: 37.79,
            lines: 3057,
        };

        // Check the function allow works
        assert!(context.is_function_allowed(3, &god_object));

        // Check reason was captured
        let reason = context.get_function_allow_reason(3, &god_object);
        assert_eq!(
            reason,
            Some("Related pure classification functions for method analysis")
        );
    }

    #[test]
    fn test_godobject_alias() {
        // Test that "godobject" (no underscore) also works
        let content = "fn big_module() {} // debtmap:ignore[godobject]";
        let file = PathBuf::from("test.rs");
        let context = parse_suppression_comments(content, Language::Rust, &file);

        let god_object = DebtType::GodObject {
            methods: 50,
            fields: Some(10),
            responsibilities: 5,
            god_object_score: 25.0,
            lines: 1000,
        };
        assert!(context.is_suppressed(1, &god_object));
    }
}