repotoire 0.3.47

Graph-powered code analysis CLI. 81 detectors for security, architecture, and code quality.
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
//! Python parser using tree-sitter
//!
//! Extracts functions, classes, imports, and call relationships from Python source code.

use crate::models::{Class, Function};
use crate::parsers::{ImportInfo, ParseResult};
use anyhow::{Context, Result};
use std::collections::HashMap;
use std::path::Path;
use tree_sitter::{Node, Parser, Query, QueryCursor};

/// Parse a Python file and extract all code entities
pub fn parse(path: &Path) -> Result<ParseResult> {
    let source = std::fs::read_to_string(path)
        .with_context(|| format!("Failed to read file: {}", path.display()))?;

    parse_source(&source, path)
}

/// Parse Python source code directly (useful for testing)
pub fn parse_source(source: &str, path: &Path) -> Result<ParseResult> {
    let mut parser = Parser::new();
    let language = tree_sitter_python::LANGUAGE;
    parser
        .set_language(&language.into())
        .context("Failed to set Python language")?;

    let tree = parser
        .parse(source, None)
        .context("Failed to parse Python source")?;

    let root = tree.root_node();
    let source_bytes = source.as_bytes();

    let mut result = ParseResult::default();

    // Extract all entities
    extract_functions(&root, source_bytes, path, &mut result)?;
    extract_classes(&root, source_bytes, path, &mut result)?;
    extract_imports(&root, source_bytes, &mut result)?;
    extract_calls(&root, source_bytes, path, &mut result)?;

    Ok(result)
}

/// Extract function definitions from the AST
fn extract_functions(
    root: &Node,
    source: &[u8],
    path: &Path,
    result: &mut ParseResult,
) -> Result<()> {
    // Query for function definitions at module level (handles both sync and async)
    let query_str = r#"
        (module
            (function_definition
                name: (identifier) @func_name
                parameters: (parameters) @params
                return_type: (type)? @return_type
            ) @func
        )
        (module
            (decorated_definition
                (function_definition
                    name: (identifier) @func_name
                    parameters: (parameters) @params
                    return_type: (type)? @return_type
                ) @func
            )
        )
    "#;

    let language = tree_sitter_python::LANGUAGE;
    let query = Query::new(&language.into(), query_str).context("Failed to create function query")?;

    let mut cursor = QueryCursor::new();
    let mut matches = cursor.matches(&query, *root, source);

    while let Some(m) = matches.next() {
        let mut func_node = None;
        let mut name = String::new();
        let mut params_node = None;
        let mut return_type_node = None;

        for capture in m.captures.iter() {
            let capture_name = query.capture_names()[capture.index as usize];
            match capture_name {
                "func" => func_node = Some(capture.node),
                "func_name" => {
                    name = capture.node.utf8_text(source).unwrap_or("").to_string();
                }
                "params" => params_node = Some(capture.node),
                "return_type" => return_type_node = Some(capture.node),
                _ => {}
            }
        }

        if let Some(node) = func_node {
            // Check for async: check if the line starts with "async def"
            let line_text = {
                let start = node.start_byte();
                let line_start = source[..start].iter().rposition(|&b| b == b'\n').map_or(0, |i| i + 1);
                std::str::from_utf8(&source[line_start..start + 10.min(source.len() - start)]).unwrap_or("")
            };
            let is_async = line_text.trim_start().starts_with("async");

            let parameters = extract_parameters(params_node, source);
            let return_type = return_type_node
                .map(|n| n.utf8_text(source).unwrap_or("").to_string());

            let line_start = node.start_position().row as u32 + 1;
            let line_end = node.end_position().row as u32 + 1;
            let qualified_name = format!("{}::{}:{}", path.display(), name, line_start);

            result.functions.push(Function {
                name: name.clone(),
                qualified_name,
                file_path: path.to_path_buf(),
                line_start,
                line_end,
                parameters,
                return_type,
                is_async,
                complexity: Some(calculate_complexity(&node, source)),
            });
        }
    }

    // Also handle async functions specifically
    extract_async_functions(root, source, path, result)?;

    Ok(())
}

/// Extract async function definitions
fn extract_async_functions(
    root: &Node,
    source: &[u8],
    path: &Path,
    result: &mut ParseResult,
) -> Result<()> {
    let mut cursor = root.walk();

    for node in root.children(&mut cursor) {
        if node.kind() == "async_function_definition" {
            if let Some(func) = parse_function_node(&node, source, path, true) {
                // Check if we already have this function (from the query)
                if !result.functions.iter().any(|f| f.qualified_name == func.qualified_name) {
                    result.functions.push(func);
                }
            }
        } else if node.kind() == "decorated_definition" {
            // Check if the decorated definition contains an async function
            for child in node.children(&mut node.walk()) {
                if child.kind() == "async_function_definition" {
                    if let Some(func) = parse_function_node(&child, source, path, true) {
                        if !result.functions.iter().any(|f| f.qualified_name == func.qualified_name) {
                            result.functions.push(func);
                        }
                    }
                }
            }
        }
    }

    Ok(())
}

/// Parse a single function node into a Function struct
fn parse_function_node(node: &Node, source: &[u8], path: &Path, is_async: bool) -> Option<Function> {
    let name_node = node.child_by_field_name("name")?;
    let name = name_node.utf8_text(source).ok()?.to_string();

    let params_node = node.child_by_field_name("parameters");
    let parameters = extract_parameters(params_node, source);

    let return_type = node
        .child_by_field_name("return_type")
        .and_then(|n| n.utf8_text(source).ok())
        .map(|s| s.to_string());

    let line_start = node.start_position().row as u32 + 1;
    let line_end = node.end_position().row as u32 + 1;
    let qualified_name = format!("{}::{}:{}", path.display(), name, line_start);

    Some(Function {
        name,
        qualified_name,
        file_path: path.to_path_buf(),
        line_start,
        line_end,
        parameters,
        return_type,
        is_async,
        complexity: Some(calculate_complexity(node, source)),
    })
}

/// Extract parameter names from a parameters node
fn extract_parameters(params_node: Option<Node>, source: &[u8]) -> Vec<String> {
    let Some(node) = params_node else {
        return vec![];
    };

    let mut params = Vec::new();
    let mut cursor = node.walk();

    for child in node.children(&mut cursor) {
        match child.kind() {
            "identifier" => {
                if let Ok(text) = child.utf8_text(source) {
                    params.push(text.to_string());
                }
            }
            "typed_parameter" | "default_parameter" | "typed_default_parameter" => {
                // Get the parameter name (first identifier child)
                if let Some(name_node) = child.child_by_field_name("name") {
                    if let Ok(text) = name_node.utf8_text(source) {
                        params.push(text.to_string());
                    }
                } else {
                    // Fallback: first identifier child
                    for grandchild in child.children(&mut child.walk()) {
                        if grandchild.kind() == "identifier" {
                            if let Ok(text) = grandchild.utf8_text(source) {
                                params.push(text.to_string());
                                break;
                            }
                        }
                    }
                }
            }
            "list_splat_pattern" | "dictionary_splat_pattern" => {
                // *args or **kwargs
                for grandchild in child.children(&mut child.walk()) {
                    if grandchild.kind() == "identifier" {
                        if let Ok(text) = grandchild.utf8_text(source) {
                            let prefix = if child.kind() == "list_splat_pattern" {
                                "*"
                            } else {
                                "**"
                            };
                            params.push(format!("{}{}", prefix, text));
                            break;
                        }
                    }
                }
            }
            _ => {}
        }
    }

    params
}

/// Extract class definitions from the AST
fn extract_classes(
    root: &Node,
    source: &[u8],
    path: &Path,
    result: &mut ParseResult,
) -> Result<()> {
    let mut cursor = root.walk();

    for node in root.children(&mut cursor) {
        let class_node = if node.kind() == "class_definition" {
            Some(node)
        } else if node.kind() == "decorated_definition" {
            // Find class_definition inside decorated_definition
            node.children(&mut node.walk())
                .find(|c| c.kind() == "class_definition")
        } else {
            None
        };

        if let Some(class_node) = class_node {
            if let Some(class) = parse_class_node(&class_node, source, path) {
                result.classes.push(class);
            }
        }
    }

    Ok(())
}

/// Parse a single class node into a Class struct
fn parse_class_node(node: &Node, source: &[u8], path: &Path) -> Option<Class> {
    let name_node = node.child_by_field_name("name")?;
    let name = name_node.utf8_text(source).ok()?.to_string();

    let line_start = node.start_position().row as u32 + 1;
    let line_end = node.end_position().row as u32 + 1;
    let qualified_name = format!("{}::{}:{}", path.display(), name, line_start);

    // Extract base classes
    let bases = extract_bases(node, source);

    // Extract method names
    let methods = extract_methods(node, source);

    Some(Class {
        name,
        qualified_name,
        file_path: path.to_path_buf(),
        line_start,
        line_end,
        methods,
        bases,
    })
}

/// Extract base class names from a class definition
fn extract_bases(class_node: &Node, source: &[u8]) -> Vec<String> {
    let mut bases = Vec::new();

    // Find the argument_list (superclasses) or superclasses node
    for child in class_node.children(&mut class_node.walk()) {
        if child.kind() == "argument_list" {
            // Each argument is a base class
            for arg in child.children(&mut child.walk()) {
                if let Some(base_name) = extract_base_name(&arg, source) {
                    bases.push(base_name);
                }
            }
        }
    }

    bases
}

/// Extract a base class name from various node types
fn extract_base_name(node: &Node, source: &[u8]) -> Option<String> {
    match node.kind() {
        "identifier" => node.utf8_text(source).ok().map(|s| s.to_string()),
        "attribute" => {
            // module.ClassName
            node.utf8_text(source).ok().map(|s| s.to_string())
        }
        "subscript" => {
            // Generic[T] - just get the base
            node.child_by_field_name("value")
                .and_then(|n| extract_base_name(&n, source))
        }
        "keyword_argument" => None, // Skip keyword args like metaclass=...
        "(" | ")" | "," => None,    // Skip punctuation
        _ => None,
    }
}

/// Extract method names from a class body
fn extract_methods(class_node: &Node, source: &[u8]) -> Vec<String> {
    let mut methods = Vec::new();

    // Find the block (class body)
    let body = class_node
        .child_by_field_name("body")
        .or_else(|| {
            class_node
                .children(&mut class_node.walk())
                .find(|c| c.kind() == "block")
        });

    if let Some(body) = body {
        for child in body.children(&mut body.walk()) {
            let func_node = if child.kind() == "function_definition"
                || child.kind() == "async_function_definition"
            {
                Some(child)
            } else if child.kind() == "decorated_definition" {
                child.children(&mut child.walk()).find(|c| {
                    c.kind() == "function_definition" || c.kind() == "async_function_definition"
                })
            } else {
                None
            };

            if let Some(func) = func_node {
                if let Some(name_node) = func.child_by_field_name("name") {
                    if let Ok(name) = name_node.utf8_text(source) {
                        methods.push(name.to_string());
                    }
                }
            }
        }
    }

    methods
}

/// Extract import statements from the AST
fn extract_imports(root: &Node, source: &[u8], result: &mut ParseResult) -> Result<()> {
    let mut cursor = root.walk();

    for node in root.children(&mut cursor) {
        match node.kind() {
            "import_statement" => {
                // import module1, module2
                for child in node.children(&mut node.walk()) {
                    if child.kind() == "dotted_name" {
                        if let Ok(text) = child.utf8_text(source) {
                            result.imports.push(ImportInfo::runtime(text.to_string()));
                        }
                    } else if child.kind() == "aliased_import" {
                        // import module as alias
                        if let Some(name_node) = child.child_by_field_name("name") {
                            if let Ok(text) = name_node.utf8_text(source) {
                                result.imports.push(ImportInfo::runtime(text.to_string()));
                            }
                        }
                    }
                }
            }
            "import_from_statement" => {
                // from module import name1, name2
                // Get the module name
                if let Some(module_node) = node.child_by_field_name("module_name") {
                    if let Ok(module) = module_node.utf8_text(source) {
                        result.imports.push(ImportInfo::runtime(module.to_string()));
                    }
                } else {
                    // Try to find dotted_name directly
                    for child in node.children(&mut node.walk()) {
                        if child.kind() == "dotted_name" || child.kind() == "relative_import" {
                            if let Ok(text) = child.utf8_text(source) {
                                result.imports.push(ImportInfo::runtime(text.to_string()));
                            }
                            break;
                        }
                    }
                }
            }
            _ => {}
        }
    }

    Ok(())
}

/// Extract function calls from the AST
fn extract_calls(
    root: &Node,
    source: &[u8],
    path: &Path,
    result: &mut ParseResult,
) -> Result<()> {
    // Build a map of function/method locations for call extraction
    let mut scope_map: HashMap<(u32, u32), String> = HashMap::new();

    // Add all functions to the scope map
    for func in &result.functions {
        scope_map.insert(
            (func.line_start, func.line_end),
            func.qualified_name.clone(),
        );
    }

    // Add all methods to the scope map
    for class in &result.classes {
        // Re-parse to get method line numbers
        let class_methods = extract_method_ranges(root, source, path, &class.name);
        for (method_name, start, end) in class_methods {
            let qualified = format!(
                "{}::{}:{}.{}:{}",
                path.display(),
                class.name,
                class.line_start,
                method_name,
                start
            );
            scope_map.insert((start, end), qualified);
        }
    }

    // Now walk the tree to find all calls
    extract_calls_recursive(root, source, path, &scope_map, result);

    Ok(())
}

/// Extract method ranges from a class for call tracking
fn extract_method_ranges(
    root: &Node,
    source: &[u8],
    _path: &Path,
    class_name: &str,
) -> Vec<(String, u32, u32)> {
    let mut methods = Vec::new();

    // Find the class
    fn find_class<'a>(node: &Node<'a>, source: &[u8], class_name: &str) -> Option<Node<'a>> {
        if node.kind() == "class_definition" {
            if let Some(name_node) = node.child_by_field_name("name") {
                if let Ok(name) = name_node.utf8_text(source) {
                    if name == class_name {
                        return Some(*node);
                    }
                }
            }
        }

        for child in node.children(&mut node.walk()) {
            if let Some(found) = find_class(&child, source, class_name) {
                return Some(found);
            }
        }

        None
    }

    if let Some(class_node) = find_class(root, source, class_name) {
        if let Some(body) = class_node.child_by_field_name("body") {
            for child in body.children(&mut body.walk()) {
                let func_node = if child.kind() == "function_definition"
                    || child.kind() == "async_function_definition"
                {
                    Some(child)
                } else if child.kind() == "decorated_definition" {
                    child.children(&mut child.walk()).find(|c| {
                        c.kind() == "function_definition"
                            || c.kind() == "async_function_definition"
                    })
                } else {
                    None
                };

                if let Some(func) = func_node {
                    if let Some(name_node) = func.child_by_field_name("name") {
                        if let Ok(name) = name_node.utf8_text(source) {
                            let start = func.start_position().row as u32 + 1;
                            let end = func.end_position().row as u32 + 1;
                            methods.push((name.to_string(), start, end));
                        }
                    }
                }
            }
        }
    }

    methods
}

/// Recursively extract calls from the AST
fn extract_calls_recursive(
    node: &Node,
    source: &[u8],
    path: &Path,
    scope_map: &HashMap<(u32, u32), String>,
    result: &mut ParseResult,
) {
    if node.kind() == "call" {
        // Get the line number of this call
        let call_line = node.start_position().row as u32 + 1;

        // Find which function/method contains this call
        // For top-level calls (outside any function), use the file path as the caller
        let caller = find_containing_scope(call_line, scope_map)
            .unwrap_or_else(|| path.display().to_string());

        // Get the function being called
        if let Some(func_node) = node.child_by_field_name("function") {
            if let Some(callee) = extract_call_target(&func_node, source) {
                // Skip self.method calls where caller and callee are in same class
                // (these are tracked differently in the Python version)
                if !callee.starts_with("self.") || !caller.contains(&callee.replace("self.", "")) {
                    result.calls.push((caller, callee));
                }
            }
        }
    }

    // Recurse into children
    for child in node.children(&mut node.walk()) {
        extract_calls_recursive(&child, source, path, scope_map, result);
    }
}

/// Find which scope (function/method) contains a given line
fn find_containing_scope(line: u32, scope_map: &HashMap<(u32, u32), String>) -> Option<String> {
    // Find the smallest scope that contains this line
    let mut best_match: Option<(&(u32, u32), &String)> = None;

    for (range, name) in scope_map {
        if line >= range.0 && line <= range.1 {
            match best_match {
                None => best_match = Some((range, name)),
                Some((best_range, _)) => {
                    // Prefer smaller (more specific) scope
                    if (range.1 - range.0) < (best_range.1 - best_range.0) {
                        best_match = Some((range, name));
                    }
                }
            }
        }
    }

    best_match.map(|(_, name)| name.clone())
}

/// Extract the target of a function call
fn extract_call_target(node: &Node, source: &[u8]) -> Option<String> {
    match node.kind() {
        "identifier" => node.utf8_text(source).ok().map(|s| s.to_string()),
        "attribute" => {
            // obj.method or module.func
            node.utf8_text(source).ok().map(|s| s.to_string())
        }
        "subscript" => {
            // func[T]() - get the function name
            node.child_by_field_name("value")
                .and_then(|n| extract_call_target(&n, source))
        }
        "call" => {
            // Chained call: func()() - get the inner function
            node.child_by_field_name("function")
                .and_then(|n| extract_call_target(&n, source))
        }
        _ => None,
    }
}

/// Calculate cyclomatic complexity of a function
fn calculate_complexity(node: &Node, _source: &[u8]) -> u32 {
    let mut complexity = 1; // Base complexity

    fn count_branches(node: &Node, complexity: &mut u32) {
        match node.kind() {
            // Control flow
            "if_statement" | "elif_clause" | "while_statement" | "for_statement" => {
                *complexity += 1;
            }
            // Exception handling
            "except_clause" => {
                *complexity += 1;
            }
            // Boolean operators (each 'and'/'or' adds a branch)
            "boolean_operator" => {
                *complexity += 1;
            }
            // Ternary/conditional expression
            "conditional_expression" => {
                *complexity += 1;
            }
            // List/dict/set comprehensions with conditions
            "list_comprehension" | "dictionary_comprehension" | "set_comprehension" => {
                // Count 'if' clauses inside
                for child in node.children(&mut node.walk()) {
                    if child.kind() == "if_clause" {
                        *complexity += 1;
                    }
                }
            }
            // Match statement (Python 3.10+)
            "match_statement" => {
                // Each case adds to complexity
                for child in node.children(&mut node.walk()) {
                    if child.kind() == "case_clause" {
                        *complexity += 1;
                    }
                }
            }
            // Try blocks don't add complexity themselves, but except clauses do
            "try_statement" => {}
            // With statement
            "with_statement" => {
                *complexity += 1;
            }
            // Assert
            "assert_statement" => {
                *complexity += 1;
            }
            _ => {}
        }

        // Recurse into children
        for child in node.children(&mut node.walk()) {
            count_branches(&child, complexity);
        }
    }

    count_branches(node, &mut complexity);
    complexity
}

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

    #[test]
    fn test_parse_simple_function() {
        let source = r#"
def hello(name: str) -> str:
    """Greet someone."""
    return f"Hello, {name}!"
"#;
        let path = PathBuf::from("test.py");
        let result = parse_source(source, &path).unwrap();

        assert_eq!(result.functions.len(), 1);
        let func = &result.functions[0];
        assert_eq!(func.name, "hello");
        assert_eq!(func.parameters, vec!["name"]);
        assert!(!func.is_async);
        assert_eq!(func.line_start, 2);
    }

    #[test]
    fn test_parse_async_function() {
        let source = r#"
async def fetch_data(url: str) -> bytes:
    return await http.get(url)
"#;
        let path = PathBuf::from("test.py");
        let result = parse_source(source, &path).unwrap();

        assert_eq!(result.functions.len(), 1);
        let func = &result.functions[0];
        assert_eq!(func.name, "fetch_data");
        assert!(func.is_async);
    }

    #[test]
    fn test_parse_class() {
        let source = r#"
class MyClass(BaseClass, Mixin):
    def __init__(self):
        pass

    def method(self, x):
        return x * 2
"#;
        let path = PathBuf::from("test.py");
        let result = parse_source(source, &path).unwrap();

        assert_eq!(result.classes.len(), 1);
        let class = &result.classes[0];
        assert_eq!(class.name, "MyClass");
        assert_eq!(class.bases, vec!["BaseClass", "Mixin"]);
        assert_eq!(class.methods, vec!["__init__", "method"]);
    }

    #[test]
    fn test_parse_imports() {
        let source = r#"
import os
import sys
from pathlib import Path
from typing import List, Optional
"#;
        let path = PathBuf::from("test.py");
        let result = parse_source(source, &path).unwrap();

        assert!(result.imports.iter().any(|i| i.path == "os"));
        assert!(result.imports.iter().any(|i| i.path == "sys"));
        assert!(result.imports.iter().any(|i| i.path == "pathlib"));
        assert!(result.imports.iter().any(|i| i.path == "typing"));
    }

    #[test]
    fn test_parse_calls() {
        let source = r#"
def caller():
    result = some_function()
    other_function(result)
    return result

def some_function():
    return 42

def other_function(x):
    print(x)
"#;
        let path = PathBuf::from("test.py");
        let result = parse_source(source, &path).unwrap();

        // Should have calls from caller to some_function and other_function
        assert!(!result.calls.is_empty());

        let call_targets: Vec<&str> = result.calls.iter().map(|(_, t)| t.as_str()).collect();
        assert!(call_targets.contains(&"some_function"));
        assert!(call_targets.contains(&"other_function"));
    }

    #[test]
    fn test_complexity_calculation() {
        let source = r#"
def complex_function(x):
    if x > 0:
        if x > 10:
            return "big"
        else:
            return "small positive"
    elif x < 0:
        return "negative"
    else:
        return "zero"
"#;
        let path = PathBuf::from("test.py");
        let result = parse_source(source, &path).unwrap();

        let func = &result.functions[0];
        // Base (1) + if (1) + if (1) + elif (1) = 4
        assert!(func.complexity.unwrap() >= 4);
    }

    #[test]
    fn test_parse_decorated_function() {
        let source = r#"
@decorator
def decorated():
    pass

@property
def prop(self):
    return self._value
"#;
        let path = PathBuf::from("test.py");
        let result = parse_source(source, &path).unwrap();

        assert_eq!(result.functions.len(), 2);
    }

    #[test]
    fn test_parse_star_args() {
        let source = r#"
def varargs(*args, **kwargs):
    pass
"#;
        let path = PathBuf::from("test.py");
        let result = parse_source(source, &path).unwrap();

        let func = &result.functions[0];
        assert!(func.parameters.contains(&"*args".to_string()));
        assert!(func.parameters.contains(&"**kwargs".to_string()));
    }

    #[test]
    fn test_method_count_excludes_nested() {
        // Issue #18: Parser should not count nested functions/lambdas as class methods
        let source = r#"
class DataProcessor:
    def __init__(self):
        self.handlers = []
    
    def process(self, items):
        # These should NOT be counted as methods:
        inner_helper = lambda x: x * 2
        results = list(map(lambda item: item.strip(), items))
        
        def local_transform(val):
            return val.upper()
        
        return [local_transform(r) for r in results]
    
    def register(self, handler):
        self.handlers.append(handler)
"#;
        let path = PathBuf::from("test.py");
        let result = parse_source(source, &path).unwrap();

        assert_eq!(result.classes.len(), 1);
        let class = &result.classes[0];
        assert_eq!(class.name, "DataProcessor");
        
        // Should have exactly 3 methods: __init__, process, register
        // NOT: inner_helper lambda, map lambda, local_transform
        assert_eq!(
            class.methods.len(),
            3,
            "Expected 3 methods (__init__, process, register), got {:?}",
            class.methods
        );
        assert!(class.methods.contains(&"__init__".to_string()));
        assert!(class.methods.contains(&"process".to_string()));
        assert!(class.methods.contains(&"register".to_string()));
    }

    #[test]
    fn test_decorated_methods_counted_correctly() {
        let source = r#"
class MyClass:
    @property
    def value(self):
        return self._value
    
    @staticmethod
    def create():
        return MyClass()
    
    @classmethod
    def from_string(cls, s):
        return cls()
"#;
        let path = PathBuf::from("test.py");
        let result = parse_source(source, &path).unwrap();

        let class = &result.classes[0];
        assert_eq!(
            class.methods.len(),
            3,
            "Expected 3 methods (value, create, from_string), got {:?}",
            class.methods
        );
    }
}