tldr-cli 0.1.3

CLI binary for TLDR code analysis tool
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
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
//! Explain Command - Comprehensive Function Analysis
//!
//! The explain command provides a complete analysis of a function including:
//! - Signature extraction (params, return type, decorators, docstring)
//! - Purity analysis (pure/impure/unknown with effects)
//! - Complexity metrics (cyclomatic, blocks, edges, loops)
//! - Call relationships (callers and callees)
//!
//! # Example
//!
//! ```bash
//! # Analyze a function
//! tldr explain src/utils.py calculate_total
//!
//! # With call graph depth
//! tldr explain src/utils.py calculate_total --depth 3
//!
//! # Text output
//! tldr explain src/utils.py calculate_total --format text
//! ```

use std::collections::HashSet;
use std::path::PathBuf;

use anyhow::Result;
use clap::Args;
use tree_sitter::{Node, Parser};

use super::error::RemainingError;
use super::types::{CallInfo, ComplexityInfo, ExplainReport, ParamInfo, PurityInfo, SignatureInfo};

use crate::output::{OutputFormat, OutputWriter};
use tldr_core::types::Language;

// =============================================================================
// CLI Arguments
// =============================================================================

/// Provide comprehensive function analysis.
#[derive(Debug, Clone, Args)]
pub struct ExplainArgs {
    /// Source file to analyze
    pub file: PathBuf,

    /// Function name to explain
    pub function: String,

    /// Call graph depth for callers/callees
    #[arg(long, default_value = "2")]
    pub depth: u32,

    /// Output file (stdout if not specified)
    #[arg(long, short = 'o')]
    pub output: Option<PathBuf>,
}

// =============================================================================
// Constants
// =============================================================================

/// Known I/O operations that make a function impure
const IO_OPERATIONS: &[&str] = &[
    "print",
    "open",
    "read",
    "write",
    "readline",
    "readlines",
    "writelines",
    "input",
    "system",
    "popen",
    "exec",
    "eval",
    "request",
    "fetch",
    "urlopen",
    "execute",
    "executemany",
    "fetchone",
    "fetchall",
];

/// Known impure calls (non-deterministic or side-effecting)
const IMPURE_CALLS: &[&str] = &[
    "random",
    "randint",
    "choice",
    "shuffle",
    "sample",
    "uniform",
    "random.random",
    "random.randint",
    "random.choice",
    "random.shuffle",
    "time",
    "time.time",
    "datetime.now",
    "datetime.datetime.now",
    "uuid4",
    "uuid1",
    "uuid.uuid4",
    "uuid.uuid1",
    "logging.info",
    "logging.debug",
    "logging.warning",
    "logging.error",
    "os.system",
    "os.popen",
    "os.getenv",
    "os.environ",
    "os.mkdir",
    "os.remove",
    "requests.get",
    "requests.post",
    "requests.put",
    "requests.delete",
    "subprocess.run",
    "subprocess.call",
    "subprocess.Popen",
];

/// Collection mutation methods
const COLLECTION_MUTATIONS: &[&str] = &[
    "append",
    "extend",
    "insert",
    "remove",
    "pop",
    "clear",
    "update",
    "add",
    "discard",
    "setdefault",
    "sort",
    "reverse",
];

/// Known pure builtins
const PURE_BUILTINS: &[&str] = &[
    "len",
    "range",
    "int",
    "float",
    "str",
    "bool",
    "list",
    "dict",
    "set",
    "tuple",
    "sorted",
    "reversed",
    "enumerate",
    "zip",
    "map",
    "filter",
    "min",
    "max",
    "sum",
    "abs",
    "round",
    "isinstance",
    "issubclass",
    "type",
    "id",
    "hash",
    "repr",
    "next",
    "iter",
    "all",
    "any",
    "chr",
    "ord",
    "hex",
    "oct",
    "bin",
    "pow",
    "divmod",
    "super",
    "property",
    "staticmethod",
    "classmethod",
];

// =============================================================================
// Tree-sitter Multi-Language Parsing
// =============================================================================

/// Get function node kinds for a given language
fn get_function_node_kinds(language: Language) -> &'static [&'static str] {
    match language {
        Language::Python => &["function_definition", "async_function_definition"],
        Language::TypeScript | Language::JavaScript => &[
            "function_declaration",
            "arrow_function",
            "method_definition",
            "function",
        ],
        Language::Go => &["function_declaration", "method_declaration"],
        Language::Rust => &["function_item"],
        Language::Java => &["method_declaration", "constructor_declaration"],
        Language::Kotlin => &["function_declaration"],
        Language::CSharp => &["method_declaration", "constructor_declaration"],
        Language::Ruby => &["method", "singleton_method"],
        Language::Php => &["function_definition", "method_declaration"],
        Language::Scala => &["function_definition"],
        Language::Swift => &["function_declaration"],
        Language::C | Language::Cpp => &["function_definition"],
        Language::Lua | Language::Luau => &["function_declaration", "function_definition"],
        Language::Elixir => &["call"], // Elixir def/defp are call nodes
        Language::Ocaml => &["value_definition"],
    }
}

/// Initialize tree-sitter parser for the detected language
fn get_parser(language: Language) -> Result<Parser, RemainingError> {
    let mut parser = Parser::new();

    let ts_language = match language {
        Language::Python => tree_sitter_python::LANGUAGE.into(),
        Language::TypeScript => tree_sitter_typescript::LANGUAGE_TSX.into(),
        Language::JavaScript => tree_sitter_typescript::LANGUAGE_TSX.into(),
        Language::Go => tree_sitter_go::LANGUAGE.into(),
        Language::Rust => tree_sitter_rust::LANGUAGE.into(),
        Language::Java => tree_sitter_java::LANGUAGE.into(),
        Language::C => tree_sitter_c::LANGUAGE.into(),
        Language::Cpp => tree_sitter_cpp::LANGUAGE.into(),
        Language::CSharp => tree_sitter_c_sharp::LANGUAGE.into(),
        Language::Kotlin => tree_sitter_kotlin_ng::LANGUAGE.into(),
        Language::Scala => tree_sitter_scala::LANGUAGE.into(),
        Language::Php => tree_sitter_php::LANGUAGE_PHP.into(),
        Language::Ruby => tree_sitter_ruby::LANGUAGE.into(),
        Language::Lua => tree_sitter_lua::LANGUAGE.into(),
        Language::Luau => tree_sitter_luau::LANGUAGE.into(),
        Language::Elixir => tree_sitter_elixir::LANGUAGE.into(),
        Language::Ocaml => tree_sitter_ocaml::LANGUAGE_OCAML.into(),
        Language::Swift => tree_sitter_swift::LANGUAGE.into(),
    };

    parser.set_language(&ts_language).map_err(|e| {
        RemainingError::parse_error(PathBuf::new(), format!("Failed to set language: {}", e))
    })?;
    Ok(parser)
}

/// Get text for a node from source
fn node_text<'a>(node: Node, source: &'a [u8]) -> &'a str {
    node.utf8_text(source).unwrap_or("")
}

/// Get the line number (1-indexed) for a node
fn get_line_number(node: Node) -> u32 {
    node.start_position().row as u32 + 1
}

/// Get the end line number (1-indexed) for a node
fn get_end_line_number(node: Node) -> u32 {
    node.end_position().row as u32 + 1
}

// =============================================================================
// Function Finding
// =============================================================================

/// Find a function definition by name in the AST
fn find_function_node<'a>(
    root: Node<'a>,
    source: &[u8],
    function_name: &str,
    func_kinds: &[&str],
) -> Option<Node<'a>> {
    find_function_recursive(root, source, function_name, func_kinds)
}

fn find_function_recursive<'a>(
    node: Node<'a>,
    source: &[u8],
    function_name: &str,
    func_kinds: &[&str],
) -> Option<Node<'a>> {
    if func_kinds.contains(&node.kind()) {
        // Check if this function has the name we're looking for
        // Try field name first (most reliable)
        if let Some(name_node) = node.child_by_field_name("name") {
            let name = node_text(name_node, source);
            if name == function_name {
                return Some(node);
            }
        }
        // C/C++: function_definition -> declarator -> function_declarator -> identifier
        if let Some(declarator) = node.child_by_field_name("declarator") {
            if let Some(name) = extract_c_declarator_name_explain(declarator, source) {
                if name == function_name {
                    return Some(node);
                }
            }
        }
        // Fallback: search for identifier child (Python, etc.)
        for child in node.children(&mut node.walk()) {
            if child.kind() == "identifier" {
                let name = node_text(child, source);
                if name == function_name {
                    return Some(node);
                }
                break;
            }
        }
    }

    // Check for arrow functions in variable declarations (TS/JS pattern):
    // lexical_declaration / variable_declaration -> variable_declarator -> name + value(arrow_function)
    if matches!(node.kind(), "lexical_declaration" | "variable_declaration") {
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            if child.kind() == "variable_declarator" {
                if let Some(name_node) = child.child_by_field_name("name") {
                    let var_name = node_text(name_node, source);
                    if var_name == function_name {
                        if let Some(value_node) = child.child_by_field_name("value") {
                            if matches!(
                                value_node.kind(),
                                "arrow_function"
                                    | "function"
                                    | "function_expression"
                                    | "generator_function"
                            ) {
                                return Some(value_node);
                            }
                        }
                    }
                }
            }
        }
    }

    // Elixir: def/defp are `call` nodes where the first child identifier is "def"/"defp"
    // and the function name is in the arguments
    if node.kind() == "call" && func_kinds.contains(&"call") {
        for child in node.children(&mut node.walk()) {
            if child.kind() == "identifier" {
                let text = node_text(child, source);
                if text == "def" || text == "defp" {
                    if let Some(args) = child.next_sibling() {
                        if args.kind() == "arguments" || args.kind() == "call" {
                            if let Some(name_node) = args.child(0) {
                                let fname = if name_node.kind() == "call" {
                                    name_node
                                        .child(0)
                                        .map(|n| node_text(n, source))
                                        .unwrap_or("")
                                } else {
                                    node_text(name_node, source)
                                };
                                if fname == function_name {
                                    return Some(node);
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    // OCaml: value_definition -> let_binding -> pattern field contains the function name
    if node.kind() == "value_definition" {
        for child in node.children(&mut node.walk()) {
            if child.kind() == "let_binding" {
                if let Some(pattern_node) = child.child_by_field_name("pattern") {
                    let name = node_text(pattern_node, source);
                    if name == function_name {
                        return Some(node);
                    }
                }
            }
        }
    }

    // Recurse into children
    for child in node.children(&mut node.walk()) {
        if let Some(found) = find_function_recursive(child, source, function_name, func_kinds) {
            return Some(found);
        }
    }

    None
}

/// Recursively extract function name from C/C++ nested declarator chain
fn extract_c_declarator_name_explain(declarator: Node, source: &[u8]) -> Option<String> {
    match declarator.kind() {
        "identifier" | "field_identifier" => {
            let name = node_text(declarator, source).to_string();
            if !name.is_empty() {
                Some(name)
            } else {
                None
            }
        }
        "function_declarator"
        | "pointer_declarator"
        | "reference_declarator"
        | "parenthesized_declarator" => declarator
            .child_by_field_name("declarator")
            .and_then(|inner| extract_c_declarator_name_explain(inner, source)),
        _ => None,
    }
}

// =============================================================================
// Signature Extraction
// =============================================================================

/// Extract signature information from a function node
fn extract_signature(func_node: Node, source: &[u8], language: Language) -> SignatureInfo {
    let mut sig = SignatureInfo::new();

    // Check if async (language-specific)
    sig.is_async = match language {
        Language::Python => func_node.kind() == "async_function_definition",
        Language::TypeScript | Language::JavaScript => {
            // Check for async modifier
            let mut is_async = false;
            for child in func_node.children(&mut func_node.walk()) {
                if child.kind() == "async" {
                    is_async = true;
                    break;
                }
            }
            is_async
        }
        Language::Rust => {
            // Check for async keyword
            node_text(func_node, source).contains("async")
        }
        _ => false,
    };

    // Extract parameters
    if let Some(params_node) = func_node.child_by_field_name("parameters") {
        sig.params = extract_params(params_node, source);
    }

    // Extract return type
    if let Some(return_node) = func_node.child_by_field_name("return_type") {
        sig.return_type = Some(node_text(return_node, source).to_string());
    }

    // Extract decorators (look for decorated_definition parent or decorator children)
    sig.decorators = extract_decorators(func_node, source);

    // Extract docstring
    sig.docstring = extract_docstring(func_node, source);

    sig
}

/// Extract parameters from a parameters node
fn extract_params(params_node: Node, source: &[u8]) -> Vec<ParamInfo> {
    let mut params = Vec::new();

    for child in params_node.children(&mut params_node.walk()) {
        match child.kind() {
            "identifier" => {
                // Simple parameter without annotation
                let name = node_text(child, source);
                if name != "self" && name != "cls" {
                    params.push(ParamInfo::new(name));
                }
            }
            "typed_parameter" | "typed_default_parameter" => {
                // Parameter with type annotation
                let mut param = ParamInfo::new("");
                for part in child.children(&mut child.walk()) {
                    match part.kind() {
                        "identifier" => {
                            let name = node_text(part, source);
                            if name != "self" && name != "cls" && param.name.is_empty() {
                                param.name = name.to_string();
                            }
                        }
                        "type" => {
                            param.type_hint = Some(node_text(part, source).to_string());
                        }
                        _ => {}
                    }
                }
                // Only add if we got a name
                if !param.name.is_empty() {
                    params.push(param);
                }
            }
            "default_parameter" => {
                // Parameter with default value
                let mut param = ParamInfo::new("");
                let mut got_name = false;
                for part in child.children(&mut child.walk()) {
                    if part.kind() == "identifier" && !got_name {
                        let name = node_text(part, source);
                        if name != "self" && name != "cls" {
                            param.name = name.to_string();
                            got_name = true;
                        }
                    } else if got_name && param.default.is_none() && part.kind() != "=" {
                        param.default = Some(node_text(part, source).to_string());
                    }
                }
                if !param.name.is_empty() {
                    params.push(param);
                }
            }
            _ => {}
        }
    }

    params
}

/// Extract decorators
fn extract_decorators(func_node: Node, source: &[u8]) -> Vec<String> {
    let mut decorators = Vec::new();

    // Check if parent is decorated_definition
    if let Some(parent) = func_node.parent() {
        if parent.kind() == "decorated_definition" {
            for child in parent.children(&mut parent.walk()) {
                if child.kind() == "decorator" {
                    let text = node_text(child, source);
                    decorators.push(text.trim_start_matches('@').to_string());
                }
            }
        }
    }

    decorators
}

/// Extract docstring from function body
fn extract_docstring(func_node: Node, source: &[u8]) -> Option<String> {
    // Look for the function body (block)
    if let Some(body) = func_node.child_by_field_name("body") {
        // First statement in body might be a docstring
        if let Some(first_stmt) = body.child(0) {
            if first_stmt.kind() == "expression_statement" {
                if let Some(expr) = first_stmt.child(0) {
                    if expr.kind() == "string" {
                        let text = node_text(expr, source);
                        // Remove quotes
                        let cleaned = text
                            .trim_start_matches("\"\"\"")
                            .trim_start_matches("'''")
                            .trim_start_matches('"')
                            .trim_start_matches('\'')
                            .trim_end_matches("\"\"\"")
                            .trim_end_matches("'''")
                            .trim_end_matches('"')
                            .trim_end_matches('\'')
                            .trim();
                        return Some(cleaned.to_string());
                    }
                }
            }
        }
    }
    None
}

// =============================================================================
// Purity Analysis
// =============================================================================

/// Analyze purity of a function
fn analyze_purity(func_node: Node, source: &[u8]) -> PurityInfo {
    let mut effects = Vec::new();
    let mut has_unknown_calls = false;
    let mut has_any_calls = false;

    analyze_purity_recursive(
        func_node,
        source,
        &mut effects,
        &mut has_unknown_calls,
        &mut has_any_calls,
    );

    if !effects.is_empty() {
        // Has side effects -> impure
        PurityInfo::impure(effects)
    } else if has_unknown_calls {
        // No known side effects, but calls unknown functions -> unknown
        PurityInfo::unknown().with_confidence("medium")
    } else if has_any_calls {
        // All calls resolved to known-pure builtins -> pure
        PurityInfo::pure()
    } else {
        // No calls detected at all (empty body or pure computation like a+b).
        // Absence of evidence is not evidence of purity — classify as unknown
        // with low confidence since we have nothing to base a purity claim on.
        PurityInfo::unknown().with_confidence("low")
    }
}

fn analyze_purity_recursive(
    node: Node,
    source: &[u8],
    effects: &mut Vec<String>,
    has_unknown_calls: &mut bool,
    has_any_calls: &mut bool,
) {
    match node.kind() {
        "global_statement" | "nonlocal_statement" => {
            if !effects.contains(&"global_write".to_string()) {
                effects.push("global_write".to_string());
            }
        }
        "assignment" | "augmented_assignment" => {
            // Check for attribute writes (self.x = ...)
            if let Some(left) = node.child_by_field_name("left") {
                if left.kind() == "attribute"
                    && !effects.contains(&"attribute_write".to_string())
                {
                    effects.push("attribute_write".to_string());
                }
            }
        }
        "call" => {
            *has_any_calls = true;
            let call_name = extract_call_name(node, source);
            if let Some(name) = &call_name {
                // Check for I/O operations
                for &io_op in IO_OPERATIONS {
                    if name == io_op || name.ends_with(&format!(".{}", io_op)) {
                        if !effects.contains(&"io".to_string()) {
                            effects.push("io".to_string());
                        }
                        return;
                    }
                }

                // Check for impure calls
                for &impure in IMPURE_CALLS {
                    if name == impure || name.ends_with(impure) {
                        if !effects.contains(&"io".to_string()) {
                            effects.push("io".to_string());
                        }
                        return;
                    }
                }

                // Check for collection mutations
                let method_name = name.split('.').next_back().unwrap_or(name);
                for &mutation in COLLECTION_MUTATIONS {
                    if method_name == mutation {
                        if !effects.contains(&"collection_modify".to_string()) {
                            effects.push("collection_modify".to_string());
                        }
                        return;
                    }
                }

                // Check if it's a known pure builtin
                let base = name.split('.').next_back().unwrap_or(name);
                if !PURE_BUILTINS.contains(&name.as_str()) && !PURE_BUILTINS.contains(&base) {
                    *has_unknown_calls = true;
                }
            }
        }
        _ => {}
    }

    // Recurse into children
    for child in node.children(&mut node.walk()) {
        analyze_purity_recursive(child, source, effects, has_unknown_calls, has_any_calls);
    }
}

/// Extract call name from a call node
fn extract_call_name(node: Node, source: &[u8]) -> Option<String> {
    if let Some(func) = node.child_by_field_name("function") {
        return Some(extract_name_from_expr(func, source));
    }

    for child in node.children(&mut node.walk()) {
        match child.kind() {
            "identifier" => return Some(node_text(child, source).to_string()),
            "attribute" => return Some(extract_name_from_expr(child, source)),
            _ => continue,
        }
    }
    None
}

/// Extract a dotted name from an expression
fn extract_name_from_expr(node: Node, source: &[u8]) -> String {
    match node.kind() {
        "identifier" => node_text(node, source).to_string(),
        "attribute" => {
            let mut parts = Vec::new();
            let mut current = node;

            loop {
                if let Some(attr) = current.child_by_field_name("attribute") {
                    parts.push(node_text(attr, source).to_string());
                }

                if let Some(obj) = current.child_by_field_name("object") {
                    if obj.kind() == "attribute" {
                        current = obj;
                    } else if obj.kind() == "identifier" {
                        parts.push(node_text(obj, source).to_string());
                        break;
                    } else {
                        break;
                    }
                } else {
                    break;
                }
            }

            parts.reverse();
            parts.join(".")
        }
        _ => node_text(node, source).to_string(),
    }
}

// =============================================================================
// Complexity Analysis
// =============================================================================

/// Compute complexity metrics for a function
fn compute_complexity(func_node: Node) -> ComplexityInfo {
    let mut cyclomatic = 1; // Base complexity
    let mut num_blocks = 1;
    let mut num_edges = 0;
    let mut has_loops = false;

    count_complexity_recursive(
        func_node,
        &mut cyclomatic,
        &mut num_blocks,
        &mut num_edges,
        &mut has_loops,
    );

    ComplexityInfo::new(cyclomatic, num_blocks, num_edges, has_loops)
}

fn count_complexity_recursive(
    node: Node,
    cyclomatic: &mut u32,
    num_blocks: &mut u32,
    num_edges: &mut u32,
    has_loops: &mut bool,
) {
    match node.kind() {
        "if_statement" | "elif_clause" => {
            *cyclomatic += 1;
            *num_blocks += 1;
            *num_edges += 2;
        }
        "for_statement" | "while_statement" => {
            *cyclomatic += 1;
            *num_blocks += 1;
            *num_edges += 2;
            *has_loops = true;
        }
        "try_statement" => {
            *cyclomatic += 1;
            *num_blocks += 1;
            *num_edges += 1;
        }
        "except_clause" => {
            *cyclomatic += 1;
            *num_blocks += 1;
            *num_edges += 1;
        }
        "and_operator" | "or_operator" => {
            *cyclomatic += 1;
        }
        "conditional_expression" => {
            // Ternary: x if cond else y
            *cyclomatic += 1;
            *num_edges += 1;
        }
        "list_comprehension"
        | "set_comprehension"
        | "dictionary_comprehension"
        | "generator_expression" => {
            *cyclomatic += 1;
            *has_loops = true;
        }
        _ => {}
    }

    for child in node.children(&mut node.walk()) {
        count_complexity_recursive(child, cyclomatic, num_blocks, num_edges, has_loops);
    }
}

// =============================================================================
// Call Graph Analysis
// =============================================================================

/// Find callees (functions called by this function)
fn find_callees(
    func_node: Node,
    source: &[u8],
    file_path: &str,
    local_functions: &HashSet<String>,
) -> Vec<CallInfo> {
    let mut callees = Vec::new();
    find_callees_recursive(func_node, source, file_path, local_functions, &mut callees);
    callees
}

fn find_callees_recursive(
    node: Node,
    source: &[u8],
    file_path: &str,
    local_functions: &HashSet<String>,
    callees: &mut Vec<CallInfo>,
) {
    if node.kind() == "call" {
        if let Some(name) = extract_call_name(node, source) {
            // Get base name for local function check
            let base_name = name.split('.').next().unwrap_or(&name);

            // Add if it's a local function or a known call
            let file = if local_functions.contains(base_name) {
                file_path.to_string()
            } else {
                "<external>".to_string()
            };

            // Avoid duplicates
            if !callees.iter().any(|c| c.name == name) {
                callees.push(CallInfo::new(name, file, get_line_number(node)));
            }
        }
    }

    for child in node.children(&mut node.walk()) {
        find_callees_recursive(child, source, file_path, local_functions, callees);
    }
}

/// Find callers (functions that call this function) - searches the entire file
fn find_callers(
    root: Node,
    source: &[u8],
    target_function: &str,
    file_path: &str,
    func_kinds: &[&str],
) -> Vec<CallInfo> {
    let mut callers = Vec::new();
    find_callers_in_file(
        root,
        source,
        target_function,
        file_path,
        &mut callers,
        None,
        func_kinds,
    );
    callers
}

fn find_callers_in_file(
    node: Node,
    source: &[u8],
    target_function: &str,
    file_path: &str,
    callers: &mut Vec<CallInfo>,
    current_function: Option<&str>,
    func_kinds: &[&str],
) {
    if func_kinds.contains(&node.kind()) {
        // Get this function's name
        let mut func_name = None;

        // Try field name first
        if let Some(name_node) = node.child_by_field_name("name") {
            func_name = Some(node_text(name_node, source));
        } else {
            // Fallback: search for identifier child
            for child in node.children(&mut node.walk()) {
                if child.kind() == "identifier" {
                    func_name = Some(node_text(child, source));
                    break;
                }
            }
        }

        // Recurse with this function as current
        for child in node.children(&mut node.walk()) {
            find_callers_in_file(
                child,
                source,
                target_function,
                file_path,
                callers,
                func_name,
                func_kinds,
            );
        }
        return;
    } else if node.kind() == "call" {
        if let Some(name) = extract_call_name(node, source) {
            // Check if this call is to our target function
            let base = name.split('.').next_back().unwrap_or(&name);
            if base == target_function || name == target_function {
                if let Some(caller_name) = current_function {
                    // Avoid duplicates and self-references
                    if caller_name != target_function
                        && !callers.iter().any(|c| c.name == caller_name)
                    {
                        callers.push(CallInfo::new(caller_name, file_path, get_line_number(node)));
                    }
                }
            }
        }
    }

    for child in node.children(&mut node.walk()) {
        find_callers_in_file(
            child,
            source,
            target_function,
            file_path,
            callers,
            current_function,
            func_kinds,
        );
    }
}

/// Collect all function names in a file
fn collect_function_names(root: Node, source: &[u8], func_kinds: &[&str]) -> HashSet<String> {
    let mut names = HashSet::new();
    collect_function_names_recursive(root, source, &mut names, func_kinds);
    names
}

fn collect_function_names_recursive(
    node: Node,
    source: &[u8],
    names: &mut HashSet<String>,
    func_kinds: &[&str],
) {
    if func_kinds.contains(&node.kind()) {
        // Try field name first
        if let Some(name_node) = node.child_by_field_name("name") {
            names.insert(node_text(name_node, source).to_string());
        } else {
            // Fallback: search for identifier child
            for child in node.children(&mut node.walk()) {
                if child.kind() == "identifier" {
                    names.insert(node_text(child, source).to_string());
                    break;
                }
            }
        }
    }

    for child in node.children(&mut node.walk()) {
        collect_function_names_recursive(child, source, names, func_kinds);
    }
}

// =============================================================================
// Text Formatting
// =============================================================================

/// Format an ExplainReport as human-readable text
fn format_explain_text(report: &ExplainReport) -> String {
    let mut lines = Vec::new();

    lines.push(format!("Function: {}", report.function_name));
    lines.push(format!("File: {}", report.file));
    lines.push(format!("Lines: {}-{}", report.line_start, report.line_end));
    lines.push(format!("Language: {}", report.language));
    lines.push(String::new());

    // Signature
    lines.push("Signature:".to_string());
    if report.signature.is_async {
        lines.push("  async: yes".to_string());
    }
    lines.push(format!("  Parameters: {}", report.signature.params.len()));
    for param in &report.signature.params {
        let type_str = param.type_hint.as_deref().unwrap_or("untyped");
        lines.push(format!("    - {}: {}", param.name, type_str));
    }
    if let Some(ref ret) = report.signature.return_type {
        lines.push(format!("  Returns: {}", ret));
    }
    if !report.signature.decorators.is_empty() {
        lines.push(format!(
            "  Decorators: {}",
            report.signature.decorators.join(", ")
        ));
    }
    if let Some(ref doc) = report.signature.docstring {
        let preview = if doc.len() > 100 {
            format!("{}...", &doc[..100])
        } else {
            doc.clone()
        };
        lines.push(format!("  Docstring: {}", preview));
    }
    lines.push(String::new());

    // Purity
    lines.push("Purity:".to_string());
    lines.push(format!(
        "  Classification: {}",
        report.purity.classification
    ));
    lines.push(format!("  Confidence: {}", report.purity.confidence));
    if !report.purity.effects.is_empty() {
        lines.push(format!("  Effects: {}", report.purity.effects.join(", ")));
    }
    lines.push(String::new());

    // Complexity
    if let Some(ref cx) = report.complexity {
        lines.push("Complexity:".to_string());
        lines.push(format!("  Cyclomatic: {}", cx.cyclomatic));
        lines.push(format!("  Blocks: {}", cx.num_blocks));
        lines.push(format!("  Edges: {}", cx.num_edges));
        lines.push(format!("  Has loops: {}", cx.has_loops));
        lines.push(String::new());
    }

    // Callers
    if !report.callers.is_empty() {
        lines.push(format!("Callers ({}):", report.callers.len()));
        for caller in &report.callers {
            lines.push(format!(
                "  - {} ({}:{})",
                caller.name, caller.file, caller.line
            ));
        }
        lines.push(String::new());
    }

    // Callees
    if !report.callees.is_empty() {
        lines.push(format!("Callees ({}):", report.callees.len()));
        for callee in &report.callees {
            lines.push(format!(
                "  - {} ({}:{})",
                callee.name, callee.file, callee.line
            ));
        }
    }

    lines.join("\n")
}

// =============================================================================
// Entry Point
// =============================================================================

impl ExplainArgs {
    /// Run the explain command
    pub fn run(&self, format: OutputFormat, quiet: bool) -> Result<()> {
        let writer = OutputWriter::new(format, quiet);

        writer.progress(&format!(
            "Analyzing function {} in {}...",
            self.function,
            self.file.display()
        ));

        // Check file exists
        if !self.file.exists() {
            return Err(RemainingError::file_not_found(&self.file).into());
        }

        // Detect language from file extension
        let language = Language::from_path(&self.file)
            .ok_or_else(|| RemainingError::parse_error(&self.file, "Unsupported language"))?;

        // Get function node kinds for this language
        let func_kinds = get_function_node_kinds(language);

        // Read source
        let source = std::fs::read_to_string(&self.file)
            .map_err(|e| RemainingError::parse_error(&self.file, e.to_string()))?;
        let source_bytes = source.as_bytes();

        // Parse with tree-sitter
        let mut parser = get_parser(language)?;
        let tree = parser
            .parse(&source, None)
            .ok_or_else(|| RemainingError::parse_error(&self.file, "Failed to parse file"))?;

        let root = tree.root_node();

        // Find the function
        let func_node = find_function_node(root, source_bytes, &self.function, func_kinds)
            .ok_or_else(|| RemainingError::symbol_not_found(&self.function, &self.file))?;

        // Get file path string
        let file_path = self.file.to_string_lossy().to_string();

        // Get language name for report
        let language_name = match language {
            Language::Python => "python",
            Language::TypeScript => "typescript",
            Language::JavaScript => "javascript",
            Language::Go => "go",
            Language::Rust => "rust",
            Language::Java => "java",
            Language::C => "c",
            Language::Cpp => "cpp",
            Language::CSharp => "csharp",
            Language::Kotlin => "kotlin",
            Language::Scala => "scala",
            Language::Php => "php",
            Language::Ruby => "ruby",
            Language::Lua => "lua",
            Language::Luau => "luau",
            Language::Elixir => "elixir",
            Language::Ocaml => "ocaml",
            Language::Swift => "swift",
        };

        // Build report
        let mut report = ExplainReport::new(
            &self.function,
            &file_path,
            get_line_number(func_node),
            get_end_line_number(func_node),
            language_name,
        );

        // Extract signature
        report.signature = extract_signature(func_node, source_bytes, language);

        // Analyze purity
        report.purity = analyze_purity(func_node, source_bytes);

        // Compute complexity
        report.complexity = Some(compute_complexity(func_node));

        // Collect local function names for call graph analysis
        let local_functions = collect_function_names(root, source_bytes, func_kinds);

        // Find callees
        report.callees = find_callees(func_node, source_bytes, &file_path, &local_functions);

        // Find callers
        report.callers = find_callers(root, source_bytes, &self.function, &file_path, func_kinds);

        // Output based on format
        if writer.is_text() {
            let text = format_explain_text(&report);
            writer.write_text(&text)?;
        } else {
            writer.write(&report)?;
        }

        // Write to output file if specified
        if let Some(ref output_path) = self.output {
            let output_str = if format == OutputFormat::Text {
                format_explain_text(&report)
            } else {
                serde_json::to_string_pretty(&report)?
            };
            std::fs::write(output_path, &output_str)?;
        }

        Ok(())
    }
}

// =============================================================================
// Tests
// =============================================================================

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

    const SAMPLE_CODE: &str = r#"
def calculate_total(items: list[dict], tax_rate: float = 0.1) -> float:
    """Calculate total price with tax.

    Args:
        items: List of items with 'price' key
        tax_rate: Tax rate as decimal (default 10%)

    Returns:
        Total price including tax
    """
    subtotal = sum(item['price'] for item in items)
    return subtotal * (1 + tax_rate)

def helper_function(x):
    return x * 2

def main():
    items = [{'price': 10}, {'price': 20}]
    total = calculate_total(items)
    doubled = helper_function(total)
    print(doubled)
"#;

    #[test]
    fn test_find_function() {
        let language = Language::Python;
        let func_kinds = get_function_node_kinds(language);
        let mut parser = get_parser(language).unwrap();
        let tree = parser.parse(SAMPLE_CODE, None).unwrap();
        let root = tree.root_node();

        let func = find_function_node(root, SAMPLE_CODE.as_bytes(), "calculate_total", func_kinds);
        assert!(func.is_some());

        let func = find_function_node(root, SAMPLE_CODE.as_bytes(), "nonexistent", func_kinds);
        assert!(func.is_none());
    }

    #[test]
    fn test_extract_signature() {
        let language = Language::Python;
        let func_kinds = get_function_node_kinds(language);
        let mut parser = get_parser(language).unwrap();
        let tree = parser.parse(SAMPLE_CODE, None).unwrap();
        let root = tree.root_node();

        let func = find_function_node(root, SAMPLE_CODE.as_bytes(), "calculate_total", func_kinds)
            .unwrap();
        let sig = extract_signature(func, SAMPLE_CODE.as_bytes(), language);

        assert_eq!(sig.params.len(), 2);
        assert_eq!(sig.params[0].name, "items");
        assert_eq!(sig.params[1].name, "tax_rate");
        assert!(sig.return_type.is_some());
        assert!(sig.docstring.is_some());
    }

    #[test]
    fn test_purity_analysis() {
        let language = Language::Python;
        let func_kinds = get_function_node_kinds(language);
        let mut parser = get_parser(language).unwrap();
        let tree = parser.parse(SAMPLE_CODE, None).unwrap();
        let root = tree.root_node();

        // calculate_total should be pure
        let func = find_function_node(root, SAMPLE_CODE.as_bytes(), "calculate_total", func_kinds)
            .unwrap();
        let purity = analyze_purity(func, SAMPLE_CODE.as_bytes());
        assert_eq!(purity.classification, "pure");

        // main calls print, so impure
        let func = find_function_node(root, SAMPLE_CODE.as_bytes(), "main", func_kinds).unwrap();
        let purity = analyze_purity(func, SAMPLE_CODE.as_bytes());
        assert_eq!(purity.classification, "impure");
        assert!(purity.effects.contains(&"io".to_string()));
    }

    #[test]
    fn test_complexity_analysis() {
        let code = r#"
def complex_func(x, y):
    if x > 0:
        if y > 0:
            return x + y
        else:
            return x
    else:
        for i in range(10):
            x += i
        return x
"#;
        let language = Language::Python;
        let func_kinds = get_function_node_kinds(language);
        let mut parser = get_parser(language).unwrap();
        let tree = parser.parse(code, None).unwrap();
        let root = tree.root_node();

        let func = find_function_node(root, code.as_bytes(), "complex_func", func_kinds).unwrap();
        let cx = compute_complexity(func);

        assert!(cx.cyclomatic > 1);
        assert!(cx.has_loops);
    }

    #[test]
    fn test_find_callees() {
        let language = Language::Python;
        let func_kinds = get_function_node_kinds(language);
        let mut parser = get_parser(language).unwrap();
        let tree = parser.parse(SAMPLE_CODE, None).unwrap();
        let root = tree.root_node();

        let local_funcs = collect_function_names(root, SAMPLE_CODE.as_bytes(), func_kinds);
        let func = find_function_node(root, SAMPLE_CODE.as_bytes(), "main", func_kinds).unwrap();
        let callees = find_callees(func, SAMPLE_CODE.as_bytes(), "test.py", &local_funcs);

        assert!(callees.iter().any(|c| c.name == "calculate_total"));
        assert!(callees.iter().any(|c| c.name == "helper_function"));
    }

    #[test]
    fn test_find_callers() {
        let language = Language::Python;
        let func_kinds = get_function_node_kinds(language);
        let mut parser = get_parser(language).unwrap();
        let tree = parser.parse(SAMPLE_CODE, None).unwrap();
        let root = tree.root_node();

        let callers = find_callers(
            root,
            SAMPLE_CODE.as_bytes(),
            "calculate_total",
            "test.py",
            func_kinds,
        );
        assert!(callers.iter().any(|c| c.name == "main"));
    }

    #[test]
    fn test_find_ts_arrow_function() {
        let ts_source = r#"
const getDuration = (start: Date, end: Date): number => {
    return end.getTime() - start.getTime();
};

function regularFunc(x: number): number {
    return x * 2;
}

export const processItems = (items: string[]) => {
    return items.map(i => i.trim());
};
"#;
        let language = Language::TypeScript;
        let func_kinds = get_function_node_kinds(language);
        let mut parser = get_parser(language).unwrap();
        let tree = parser.parse(ts_source, None).unwrap();
        let root = tree.root_node();

        // Regular function should always work
        let regular = find_function_node(root, ts_source.as_bytes(), "regularFunc", func_kinds);
        assert!(regular.is_some(), "Should find regular TS function");

        // Arrow function assigned to const should also work
        let arrow = find_function_node(root, ts_source.as_bytes(), "getDuration", func_kinds);
        assert!(
            arrow.is_some(),
            "Should find TS arrow function 'getDuration'"
        );

        // Exported arrow function should also work
        let exported = find_function_node(root, ts_source.as_bytes(), "processItems", func_kinds);
        assert!(
            exported.is_some(),
            "Should find exported TS arrow function 'processItems'"
        );
    }

    // =========================================================================
    // Bug: analyze_purity returns "pure" when it should return "unknown"
    // =========================================================================

    /// A function with no function body content (empty/pass) should classify
    /// as "unknown", not "pure". We have no evidence of purity -- the analysis
    /// simply found nothing.
    #[test]
    fn test_empty_function_is_unknown_not_pure() {
        let source = r#"
def empty_func():
    pass
"#;
        let language = Language::Python;
        let func_kinds = get_function_node_kinds(language);
        let mut parser = get_parser(language).unwrap();
        let tree = parser.parse(source, None).unwrap();
        let root = tree.root_node();

        let func_node = find_function_node(root, source.as_bytes(), "empty_func", func_kinds);
        assert!(func_node.is_some(), "Should find empty_func");

        let purity = analyze_purity(func_node.unwrap(), source.as_bytes());

        // The buggy code returns "pure" because no effects and no unknown calls.
        // But "pass" means we found nothing -- not that we proved purity.
        // A truly empty function (just `pass`) has no evidence to support "pure".
        assert_ne!(
            purity.classification, "pure",
            "A function with only `pass` (no calls, no computation) should NOT be classified as \
             'pure' with high confidence. We have no evidence to support a purity claim. \
             Got classification='{}', confidence='{}'. Expected 'unknown'.",
            purity.classification, purity.confidence
        );
    }

    /// A function that calls other user-defined functions (not builtins, not IO)
    /// where those calls are unresolved should classify as "unknown", not "pure".
    ///
    /// The bug: when a call doesn't match IO_OPERATIONS, IMPURE_CALLS,
    /// COLLECTION_MUTATIONS, or PURE_BUILTINS, it sets has_unknown_calls=true.
    /// This case is actually handled correctly for unknown calls, BUT if the
    /// call name happens to match a PURE_BUILTIN substring, it incorrectly
    /// passes as pure. This test verifies the general "unknown calls" path works.
    #[test]
    fn test_function_with_unknown_calls_is_unknown() {
        let source = r#"
def my_func(x):
    result = compute_something(x)
    return transform_result(result)
"#;
        let language = Language::Python;
        let func_kinds = get_function_node_kinds(language);
        let mut parser = get_parser(language).unwrap();
        let tree = parser.parse(source, None).unwrap();
        let root = tree.root_node();

        let func_node = find_function_node(root, source.as_bytes(), "my_func", func_kinds);
        assert!(func_node.is_some(), "Should find my_func");

        let purity = analyze_purity(func_node.unwrap(), source.as_bytes());

        // compute_something and transform_result are NOT in PURE_BUILTINS,
        // so has_unknown_calls should be true -> classification = "unknown"
        assert_eq!(
            purity.classification, "unknown",
            "Function calling unknown user functions should be 'unknown', got '{}'",
            purity.classification
        );
        assert_ne!(
            purity.confidence, "high",
            "Unknown classification should not have high confidence, got '{}'",
            purity.confidence
        );
    }

    /// A function that ONLY calls known-pure builtins should classify as "pure".
    /// This is the legitimate pure case.
    #[test]
    fn test_only_pure_builtins_is_pure() {
        let source = r#"
def pure_func(items):
    return len(items) + sum(items)
"#;
        let language = Language::Python;
        let func_kinds = get_function_node_kinds(language);
        let mut parser = get_parser(language).unwrap();
        let tree = parser.parse(source, None).unwrap();
        let root = tree.root_node();

        let func_node = find_function_node(root, source.as_bytes(), "pure_func", func_kinds);
        assert!(func_node.is_some(), "Should find pure_func");

        let purity = analyze_purity(func_node.unwrap(), source.as_bytes());

        assert_eq!(
            purity.classification, "pure",
            "Function calling only pure builtins (len, sum) should be 'pure', got '{}'",
            purity.classification
        );
        assert_eq!(
            purity.confidence, "high",
            "Pure classification should have high confidence"
        );
    }

    /// A function with IO operations should classify as "impure".
    #[test]
    fn test_io_operations_is_impure() {
        let source = r#"
def impure_func(msg):
    print(msg)
    return True
"#;
        let language = Language::Python;
        let func_kinds = get_function_node_kinds(language);
        let mut parser = get_parser(language).unwrap();
        let tree = parser.parse(source, None).unwrap();
        let root = tree.root_node();

        let func_node = find_function_node(root, source.as_bytes(), "impure_func", func_kinds);
        assert!(func_node.is_some(), "Should find impure_func");

        let purity = analyze_purity(func_node.unwrap(), source.as_bytes());

        assert_eq!(
            purity.classification, "impure",
            "Function with print() should be 'impure', got '{}'",
            purity.classification
        );
        assert_eq!(
            purity.confidence, "high",
            "Impure classification should have high confidence"
        );
        assert!(
            purity.effects.contains(&"io".to_string()),
            "Effects should contain 'io', got {:?}",
            purity.effects
        );
    }

    /// A function with only arithmetic (no calls at all) should be "unknown"
    /// because we have no positive evidence of purity -- the analysis simply
    /// didn't find any calls to classify.
    #[test]
    fn test_no_calls_arithmetic_only_is_unknown() {
        let source = r#"
def add(a, b):
    return a + b
"#;
        let language = Language::Python;
        let func_kinds = get_function_node_kinds(language);
        let mut parser = get_parser(language).unwrap();
        let tree = parser.parse(source, None).unwrap();
        let root = tree.root_node();

        let func_node = find_function_node(root, source.as_bytes(), "add", func_kinds);
        assert!(func_node.is_some(), "Should find add");

        let purity = analyze_purity(func_node.unwrap(), source.as_bytes());

        // The bug: analyze_purity returns "pure" because no effects and
        // no unknown calls. But we have no positive evidence -- we just
        // didn't find any calls. The correct answer is "unknown" with
        // low confidence, or at minimum not "pure/high".
        assert_ne!(
            purity.classification, "pure",
            "A simple arithmetic function with no calls should NOT confidently be 'pure'. \
             The analysis found no calls to evaluate -- absence of evidence is not evidence \
             of purity. Got classification='{}', confidence='{}'. \
             Expected 'unknown' since no calls were analyzed.",
            purity.classification, purity.confidence
        );
    }
}