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
//! Purity Analysis Module
//!
//! This module classifies functions on a purity spectrum (strictly pure, locally pure,
//! read-only, impure) using static analysis. It enables responsibility detection to
//! prefer pure computation classification and identify purity violations that indicate
//! mixed concerns.
//!
//! # Purity Levels
//!
//! - **Strictly Pure**: No I/O, no side effects, deterministic
//! - **Locally Pure**: Only mutates local variables, deterministic results
//! - **Read-Only**: Reads external state but doesn't modify it
//! - **Impure**: Performs I/O or modifies external state
//!
//! # Example
//!
//! ```ignore
//! use debtmap::analysis::purity_analysis::{PurityAnalyzer, PurityLevel};
//!
//! let analyzer = PurityAnalyzer::new();
//! let analysis = analyzer.analyze_code(code, Language::Rust);
//!
//! if analysis.purity == PurityLevel::StrictlyPure {
//!     println!("Function is strictly pure - ideal for testing!");
//! }
//! ```

use crate::analysis::io_detection::{IoDetector, IoProfile, Language, SideEffect};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Purity level classification
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum PurityLevel {
    /// No I/O, no side effects, deterministic
    StrictlyPure,
    /// Only local mutations, deterministic output
    LocallyPure,
    /// Reads external state, no mutations
    ReadOnly,
    /// Performs I/O or modifies external state
    Impure,
}

impl PurityLevel {
    /// Convert to a human-readable string
    pub fn as_str(&self) -> &'static str {
        match self {
            PurityLevel::StrictlyPure => "Strictly Pure",
            PurityLevel::LocallyPure => "Locally Pure",
            PurityLevel::ReadOnly => "Read-Only",
            PurityLevel::Impure => "Impure",
        }
    }
}

/// Purity violation types
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum PurityViolation {
    /// I/O operation performed
    IoOperation {
        description: String,
        line: Option<usize>,
    },
    /// External state mutation
    StateMutation { target: String, line: Option<usize> },
    /// Non-deterministic operation
    NonDeterministic {
        operation: String,
        line: Option<usize>,
    },
    /// Calls impure function
    ImpureCall { callee: String, line: Option<usize> },
}

impl PurityViolation {
    /// Get a description of this violation
    pub fn description(&self) -> String {
        match self {
            PurityViolation::IoOperation { description, .. } => {
                format!("I/O operation: {}", description)
            }
            PurityViolation::StateMutation { target, .. } => {
                format!("State mutation: {}", target)
            }
            PurityViolation::NonDeterministic { operation, .. } => {
                format!("Non-deterministic operation: {}", operation)
            }
            PurityViolation::ImpureCall { callee, .. } => {
                format!("Calls impure function: {}", callee)
            }
        }
    }

    /// Get the line number if available
    pub fn line(&self) -> Option<usize> {
        match self {
            PurityViolation::IoOperation { line, .. }
            | PurityViolation::StateMutation { line, .. }
            | PurityViolation::NonDeterministic { line, .. }
            | PurityViolation::ImpureCall { line, .. } => *line,
        }
    }
}

/// Refactoring opportunity type
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RefactoringType {
    /// Extract pure portion from impure function
    ExtractPureCore,
    /// Move I/O to function boundary
    SeparateIoFromLogic,
    /// Replace non-deterministic operation with parameter
    ParameterizeNonDeterminism,
    /// Extract single impure operation
    IsolateSingleViolation,
}

impl RefactoringType {
    /// Get a description of this refactoring type
    pub fn as_str(&self) -> &'static str {
        match self {
            RefactoringType::ExtractPureCore => "Extract Pure Core",
            RefactoringType::SeparateIoFromLogic => "Separate I/O from Logic",
            RefactoringType::ParameterizeNonDeterminism => "Parameterize Non-Determinism",
            RefactoringType::IsolateSingleViolation => "Isolate Single Violation",
        }
    }
}

/// Effort level for refactoring
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum EffortLevel {
    Low,
    Medium,
    High,
}

impl EffortLevel {
    /// Convert to a human-readable string
    pub fn as_str(&self) -> &'static str {
        match self {
            EffortLevel::Low => "Low",
            EffortLevel::Medium => "Medium",
            EffortLevel::High => "High",
        }
    }
}

/// Purity refactoring opportunity
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PurityRefactoringOpportunity {
    pub opportunity_type: RefactoringType,
    pub description: String,
    pub estimated_effort: EffortLevel,
}

/// Complete purity analysis result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PurityAnalysis {
    pub purity: PurityLevel,
    pub violations: Vec<PurityViolation>,
    pub is_deterministic: bool,
    pub can_be_pure: bool,
    pub refactoring_opportunity: Option<PurityRefactoringOpportunity>,
}

impl PurityAnalysis {
    /// Create a new purity analysis for a strictly pure function
    pub fn strictly_pure() -> Self {
        Self {
            purity: PurityLevel::StrictlyPure,
            violations: Vec::new(),
            is_deterministic: true,
            can_be_pure: false,
            refactoring_opportunity: None,
        }
    }

    /// Create a new purity analysis for an impure function
    pub fn impure(violations: Vec<PurityViolation>) -> Self {
        let is_deterministic = !violations
            .iter()
            .any(|v| matches!(v, PurityViolation::NonDeterministic { .. }));

        Self {
            purity: PurityLevel::Impure,
            violations,
            is_deterministic,
            can_be_pure: false,
            refactoring_opportunity: None,
        }
    }
}

/// Purity analyzer
pub struct PurityAnalyzer {
    io_detector: IoDetector,
    non_determinism_patterns: HashMap<Language, Vec<String>>,
}

impl PurityAnalyzer {
    /// Create a new purity analyzer
    pub fn new() -> Self {
        Self {
            io_detector: IoDetector::new(),
            non_determinism_patterns: Self::build_non_determinism_patterns(),
        }
    }

    /// Analyze code for purity
    pub fn analyze_code(&self, code: &str, language: Language) -> PurityAnalysis {
        // Get I/O profile from Spec 141
        let io_profile = self.io_detector.detect_io(code, language);

        // Collect violations
        let mut violations = Vec::new();

        // Check for I/O operations
        violations.extend(self.analyze_io_operations(&io_profile, code, language));

        // Check for side effects
        violations.extend(self.analyze_side_effects(&io_profile, code));

        // Check for non-deterministic operations
        violations.extend(self.detect_non_determinism(code, language));

        // Classify purity level
        let purity = self.classify_purity(&violations, &io_profile, code, language);

        // Check determinism
        let is_deterministic = !violations
            .iter()
            .any(|v| matches!(v, PurityViolation::NonDeterministic { .. }));

        // Check if function can be made pure with refactoring
        let can_be_pure = self.can_be_made_pure(&violations);

        // Generate refactoring opportunity if applicable
        let refactoring_opportunity = self.suggest_refactoring(&violations);

        PurityAnalysis {
            purity,
            violations,
            is_deterministic,
            can_be_pure,
            refactoring_opportunity,
        }
    }

    /// Analyze I/O operations from the I/O profile
    fn analyze_io_operations(
        &self,
        profile: &IoProfile,
        _code: &str,
        _language: Language,
    ) -> Vec<PurityViolation> {
        let mut violations = Vec::new();

        // File operations
        for _ in &profile.file_operations {
            violations.push(PurityViolation::IoOperation {
                description: "File I/O operation".to_string(),
                line: None,
            });
        }

        // Network operations
        for _ in &profile.network_operations {
            violations.push(PurityViolation::IoOperation {
                description: "Network I/O operation".to_string(),
                line: None,
            });
        }

        // Console operations
        for _ in &profile.console_operations {
            violations.push(PurityViolation::IoOperation {
                description: "Console I/O operation".to_string(),
                line: None,
            });
        }

        // Database operations
        for _ in &profile.database_operations {
            violations.push(PurityViolation::IoOperation {
                description: "Database I/O operation".to_string(),
                line: None,
            });
        }

        // Environment operations
        for _ in &profile.environment_operations {
            violations.push(PurityViolation::IoOperation {
                description: "Environment variable access".to_string(),
                line: None,
            });
        }

        violations
    }

    /// Analyze side effects from the I/O profile
    fn analyze_side_effects(&self, profile: &IoProfile, code: &str) -> Vec<PurityViolation> {
        let mut violations = Vec::new();

        for side_effect in &profile.side_effects {
            // Check if mutation is local or external
            if !self.is_local_mutation(side_effect, code) {
                match side_effect {
                    SideEffect::FieldMutation { target, field } => {
                        violations.push(PurityViolation::StateMutation {
                            target: format!("{}.{}", target, field),
                            line: None,
                        });
                    }
                    SideEffect::GlobalMutation { name } => {
                        violations.push(PurityViolation::StateMutation {
                            target: name.clone(),
                            line: None,
                        });
                    }
                    SideEffect::CollectionMutation { .. } => {
                        // Collection mutations are considered local unless proven otherwise
                        // This is a simplification - a more sophisticated analysis would
                        // track whether the collection is local or external
                    }
                    SideEffect::ExternalState { description } => {
                        violations.push(PurityViolation::StateMutation {
                            target: description.clone(),
                            line: None,
                        });
                    }
                }
            }
        }

        violations
    }

    /// Check if a mutation is local to the function
    fn is_local_mutation(&self, side_effect: &SideEffect, code: &str) -> bool {
        match side_effect {
            SideEffect::FieldMutation { target, .. } => {
                // If target is "self", it's a field mutation (not local)
                target == "unknown" || !code.contains("self.")
            }
            SideEffect::GlobalMutation { .. } => false, // Global mutations are never local
            SideEffect::CollectionMutation { .. } => {
                // Assume collection mutations are local for now
                // A more sophisticated analysis would track variable scope
                true
            }
            SideEffect::ExternalState { .. } => false,
        }
    }

    /// Detect non-deterministic operations
    fn detect_non_determinism(&self, code: &str, language: Language) -> Vec<PurityViolation> {
        let mut violations = Vec::new();

        if let Some(patterns) = self.non_determinism_patterns.get(&language) {
            for pattern in patterns {
                if code.contains(pattern) {
                    violations.push(PurityViolation::NonDeterministic {
                        operation: pattern.clone(),
                        line: None,
                    });
                }
            }
        }

        violations
    }

    /// Classify the purity level based on violations
    fn classify_purity(
        &self,
        violations: &[PurityViolation],
        profile: &IoProfile,
        code: &str,
        language: Language,
    ) -> PurityLevel {
        if violations.is_empty() {
            return PurityLevel::StrictlyPure;
        }

        // Check if all violations are local mutations
        let only_local_mutations = violations
            .iter()
            .all(|v| matches!(v, PurityViolation::StateMutation { .. }))
            && !violations.is_empty();

        if only_local_mutations {
            return PurityLevel::LocallyPure;
        }

        // Check if function only reads state (no writes)
        let only_reads = self.only_has_read_operations(profile, code, language);

        if only_reads
            && !violations
                .iter()
                .any(|v| matches!(v, PurityViolation::StateMutation { .. }))
        {
            return PurityLevel::ReadOnly;
        }

        PurityLevel::Impure
    }

    /// Check if the I/O profile only contains read operations
    fn only_has_read_operations(
        &self,
        profile: &IoProfile,
        code: &str,
        language: Language,
    ) -> bool {
        // Check if we have file reads but no other I/O
        let has_file_ops = !profile.file_operations.is_empty();
        let has_network = !profile.network_operations.is_empty();
        let has_console = !profile.console_operations.is_empty();
        let has_db = !profile.database_operations.is_empty();
        let has_mutations = !profile.side_effects.is_empty();

        // If we have network, console, db, or mutations, it's not read-only
        if has_network || has_console || has_db || has_mutations {
            return false;
        }

        // Check if we have file write patterns
        if has_file_ops && self.has_write_operations(code, language) {
            return false;
        }

        // If we only have file operations and no write patterns, consider it read-only
        has_file_ops
    }

    /// Check if code contains write operations
    fn has_write_operations(&self, code: &str, language: Language) -> bool {
        match language {
            Language::Rust => {
                code.contains("::write")
                    || code.contains("File::create")
                    || code.contains("OpenOptions")
                    || code.contains("write_all")
            }
            Language::Python => {
                code.contains("write_text")
                    || code.contains("write_bytes")
                    || code.contains("open(") && code.contains("'w'")
                    || code.contains("open(") && code.contains("\"w\"")
            }
            Language::JavaScript | Language::TypeScript => {
                code.contains("writeFile")
                    || code.contains("createWriteStream")
                    || code.contains("appendFile")
            }
        }
    }

    /// Check if function can be made pure with refactoring
    fn can_be_made_pure(&self, violations: &[PurityViolation]) -> bool {
        // Single violation: Easy to extract
        if violations.len() == 1 {
            return true;
        }

        // All violations are I/O: Can separate I/O from logic
        let all_io = violations
            .iter()
            .all(|v| matches!(v, PurityViolation::IoOperation { .. }));

        if all_io && violations.len() <= 3 {
            return true;
        }

        false
    }

    /// Suggest refactoring opportunities
    fn suggest_refactoring(
        &self,
        violations: &[PurityViolation],
    ) -> Option<PurityRefactoringOpportunity> {
        // Single violation: Easy to extract
        if violations.len() == 1 {
            let description = format!(
                "Function has single purity violation: {}. Extract to make core logic pure.",
                violations[0].description()
            );
            return Some(PurityRefactoringOpportunity {
                opportunity_type: RefactoringType::IsolateSingleViolation,
                description,
                estimated_effort: EffortLevel::Low,
            });
        }

        // All violations are I/O: Separate I/O from logic
        let all_io = violations
            .iter()
            .all(|v| matches!(v, PurityViolation::IoOperation { .. }));

        if all_io {
            return Some(PurityRefactoringOpportunity {
                opportunity_type: RefactoringType::SeparateIoFromLogic,
                description: "Separate I/O operations from business logic. Make computation pure."
                    .to_string(),
                estimated_effort: EffortLevel::Medium,
            });
        }

        // Non-deterministic: Parameterize
        let has_non_determinism = violations
            .iter()
            .any(|v| matches!(v, PurityViolation::NonDeterministic { .. }));

        if has_non_determinism {
            return Some(PurityRefactoringOpportunity {
                opportunity_type: RefactoringType::ParameterizeNonDeterminism,
                description: "Replace non-deterministic operations (time, random) with parameters for testability.".to_string(),
                estimated_effort: EffortLevel::Low,
            });
        }

        None
    }

    /// Build non-determinism patterns for each language
    fn build_non_determinism_patterns() -> HashMap<Language, Vec<String>> {
        let mut patterns = HashMap::new();

        // Rust patterns
        patterns.insert(
            Language::Rust,
            vec![
                "std::time::Instant::now".to_string(),
                "std::time::SystemTime::now".to_string(),
                "Instant::now".to_string(),
                "SystemTime::now".to_string(),
                "rand::".to_string(),
                "thread_rng".to_string(),
                "uuid::Uuid::new_v4".to_string(),
                "Uuid::new_v4".to_string(),
                "HashMap::new".to_string(), // Uses random seed
                "HashSet::new".to_string(), // Uses random seed
            ],
        );

        // Python patterns
        patterns.insert(
            Language::Python,
            vec![
                "random.".to_string(),
                "datetime.now".to_string(),
                "time.time".to_string(),
                "uuid.uuid4".to_string(),
                "time.monotonic".to_string(),
            ],
        );

        // JavaScript patterns
        patterns.insert(
            Language::JavaScript,
            vec![
                "Math.random".to_string(),
                "Date.now".to_string(),
                "new Date()".to_string(),
                "crypto.randomUUID".to_string(),
                "performance.now".to_string(),
            ],
        );

        // TypeScript has same patterns as JavaScript
        patterns.insert(
            Language::TypeScript,
            patterns[&Language::JavaScript].clone(),
        );

        patterns
    }
}

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

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

    #[test]
    fn strictly_pure_function() {
        let code = r#"
        fn add(a: i32, b: i32) -> i32 {
            a + b
        }
        "#;

        let analyzer = PurityAnalyzer::new();
        let analysis = analyzer.analyze_code(code, Language::Rust);

        assert_eq!(analysis.purity, PurityLevel::StrictlyPure);
        assert!(analysis.violations.is_empty());
        assert!(analysis.is_deterministic);
    }

    #[test]
    fn read_only_function() {
        let code = r#"
        fn read_config() -> String {
            std::fs::read_to_string("config.toml").unwrap()
        }
        "#;

        let analyzer = PurityAnalyzer::new();
        let analysis = analyzer.analyze_code(code, Language::Rust);

        assert_eq!(analysis.purity, PurityLevel::ReadOnly);
        assert!(!analysis.violations.is_empty());
        assert!(analysis
            .violations
            .iter()
            .any(|v| { matches!(v, PurityViolation::IoOperation { .. }) }));
    }

    #[test]
    fn impure_function() {
        let code = r#"
        fn save_data(data: &str) {
            std::fs::write("output.txt", data).unwrap();
        }
        "#;

        let analyzer = PurityAnalyzer::new();
        let analysis = analyzer.analyze_code(code, Language::Rust);

        assert_eq!(analysis.purity, PurityLevel::Impure);
        assert!(!analysis.violations.is_empty());
    }

    #[test]
    fn non_deterministic_detection() {
        let code = r#"
        fn generate_id() -> String {
            uuid::Uuid::new_v4().to_string()
        }
        "#;

        let analyzer = PurityAnalyzer::new();
        let analysis = analyzer.analyze_code(code, Language::Rust);

        assert!(!analysis.is_deterministic);
        assert!(analysis
            .violations
            .iter()
            .any(|v| { matches!(v, PurityViolation::NonDeterministic { .. }) }));
    }

    #[test]
    fn almost_pure_refactoring_opportunity() {
        let code = r#"
        fn calculate_with_logging(a: i32, b: i32) -> i32 {
            let result = a * b + a / b;
            println!("Result: {}", result);
            result
        }
        "#;

        let analyzer = PurityAnalyzer::new();
        let analysis = analyzer.analyze_code(code, Language::Rust);

        assert!(analysis.can_be_pure);
        assert!(analysis.refactoring_opportunity.is_some());

        if let Some(opportunity) = &analysis.refactoring_opportunity {
            assert!(matches!(
                opportunity.opportunity_type,
                RefactoringType::IsolateSingleViolation
            ));
        }
    }

    #[test]
    fn python_non_deterministic() {
        let code = r#"
def generate_timestamp():
    return datetime.now()
        "#;

        let analyzer = PurityAnalyzer::new();
        let analysis = analyzer.analyze_code(code, Language::Python);

        assert!(!analysis.is_deterministic);
        assert!(analysis
            .violations
            .iter()
            .any(|v| { matches!(v, PurityViolation::NonDeterministic { .. }) }));
    }

    #[test]
    fn javascript_random() {
        let code = r#"
function randomNumber() {
    return Math.random();
}
        "#;

        let analyzer = PurityAnalyzer::new();
        let analysis = analyzer.analyze_code(code, Language::JavaScript);

        assert!(!analysis.is_deterministic);
        assert!(analysis
            .violations
            .iter()
            .any(|v| { matches!(v, PurityViolation::NonDeterministic { .. }) }));
    }

    #[test]
    fn separate_io_refactoring() {
        let code = r#"
        fn process_file(path: &str) -> Result<i32, Error> {
            let content = std::fs::read_to_string(path)?;
            let data = parse_content(&content);
            let result = calculate(&data);
            std::fs::write("output.txt", &result.to_string())?;
            Ok(result)
        }
        "#;

        let analyzer = PurityAnalyzer::new();
        let analysis = analyzer.analyze_code(code, Language::Rust);

        assert_eq!(analysis.purity, PurityLevel::Impure);

        if let Some(opportunity) = &analysis.refactoring_opportunity {
            assert!(matches!(
                opportunity.opportunity_type,
                RefactoringType::SeparateIoFromLogic
            ));
        }
    }
}