tldr-core 0.1.2

Core analysis engine 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
//! Shared function finder utilities for locating functions in tree-sitter ASTs.
//!
//! This module provides the canonical implementations of function-finding logic
//! used across CFG, DFG, metrics, and quality modules. All languages supported
//! by tree-sitter grammars are handled here.

use crate::types::Language;
use tree_sitter::Node;

/// Helper to recursively search for function_definition inside a node (e.g., wrapped in function_call).
/// Searches up to `max_depth` levels deep to handle patterns like `socket.protect(function() end)`.
fn find_function_in_node<'a>(node: Node<'a>, max_depth: usize) -> Option<Node<'a>> {
    if max_depth == 0 {
        return None;
    }

    // Direct function_definition
    if node.kind() == "function_definition" {
        return Some(node);
    }

    // Recurse into children (especially for function_call -> arguments -> function_definition)
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if let Some(func) = find_function_in_node(child, max_depth - 1) {
            return Some(func);
        }
    }

    None
}

/// Find a function node by name in the AST
pub fn find_function_node<'a>(
    root: Node<'a>,
    function_name: &str,
    language: Language,
    source: &str,
) -> Option<Node<'a>> {
    let func_kinds = get_function_node_kinds(language);

    let mut stack = vec![root];

    while let Some(node) = stack.pop() {
        // Check direct function nodes
        if func_kinds.contains(&node.kind()) {
            if let Some(name) = get_function_name(node, language, source) {
                if name == function_name
                    || name
                        .strip_prefix('#')
                        .is_some_and(|stripped| stripped == function_name)
                    // Lua/Luau: match short name for dot-indexed functions
                    // e.g. "Kong.init" matches search for "init"
                    || (matches!(language, Language::Lua | Language::Luau)
                        && name.contains('.')
                        && name
                            .rsplit('.')
                            .next()
                            .is_some_and(|short| short == function_name))
                {
                    return Some(node);
                }
            }
        }

        // Check for variable declarations with arrow functions (TypeScript/JavaScript pattern)
        // Pattern: const foo = () => {}  or  const foo = function() {}
        if matches!(language, Language::TypeScript | Language::JavaScript)
            && matches!(node.kind(), "lexical_declaration" | "variable_declaration")
        {
            let mut child_cursor = node.walk();
            for child in node.children(&mut child_cursor) {
                if child.kind() == "variable_declarator" {
                    if let Some(name_node) = child.child_by_field_name("name") {
                        let var_name = name_node.utf8_text(source.as_bytes()).unwrap_or("");
                        if var_name == function_name {
                            // Check if the value is a function
                            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);
                                }
                            }
                        }
                    }
                }
            }
        }

        // Check for Lua/Luau assignment-based functions: M.request = function() end
        if matches!(language, Language::Lua | Language::Luau)
            && matches!(node.kind(), "assignment_statement" | "variable_assignment")
        {
            let mut child_cursor = node.walk();
            let children: Vec<_> = node.children(&mut child_cursor).collect();
            // Look for field_expression or dot_index_expression on LHS, function on RHS
            for child in &children {
                if matches!(child.kind(), "variable_list" | "assignment_variable_list") {
                    let mut inner_cursor = child.walk();
                    for inner in child.children(&mut inner_cursor) {
                        if matches!(inner.kind(), "field_expression" | "dot_index_expression") {
                            let lhs_text = inner.utf8_text(source.as_bytes()).unwrap_or("");
                            // Check if the field name matches (e.g. "M.request" -> "request")
                            if let Some(field_name) = lhs_text.rsplit('.').next() {
                                if field_name == function_name || lhs_text == function_name {
                                    // Find function_definition in RHS (handles both direct and wrapped)
                                    for rhs in &children {
                                        if matches!(
                                            rhs.kind(),
                                            "expression_list" | "assignment_expression_list"
                                        ) {
                                            let mut rhs_cursor = rhs.walk();
                                            for rhs_child in rhs.children(&mut rhs_cursor) {
                                                if let Some(func) = find_function_in_node(rhs_child, 3)
                                                {
                                                    return Some(func);
                                                }
                                            }
                                        }
                                        if let Some(func) = find_function_in_node(*rhs, 3) {
                                            return Some(func);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        // Add children to stack (reverse order for depth-first)
        let mut cursor = node.walk();
        let children: Vec<_> = node.children(&mut cursor).collect();
        for child in children.into_iter().rev() {
            stack.push(child);
        }
    }

    None
}

/// Get the node kinds that represent functions in each language
pub fn get_function_node_kinds(language: Language) -> &'static [&'static str] {
    match language {
        Language::Python => &["function_definition"],
        Language::TypeScript | Language::JavaScript => &[
            "function_declaration",
            "arrow_function",
            "method_definition",
            "function",
            "generator_function_declaration",
            "generator_function",
        ],
        Language::Go => &["function_declaration", "method_declaration"],
        Language::Rust => &["function_item"],
        Language::Java => &["method_declaration", "constructor_declaration"],
        Language::C | Language::Cpp => &["function_definition"],
        Language::Ruby => &["method", "singleton_method"],
        Language::Php => &["function_definition", "method_declaration"],
        Language::CSharp => &["method_declaration", "constructor_declaration"],
        Language::Kotlin => &["function_declaration"],
        Language::Scala => &["function_definition", "function_declaration"],
        Language::Elixir => &["call"], // Elixir uses def/defp which are calls
        Language::Lua | Language::Luau => &[
            "function_declaration",
            "function_definition",
            "local_function",
        ],
        Language::Swift => &["function_declaration", "init_declaration"],
        Language::Ocaml => &["let_binding", "value_definition"],
    }
}

/// Recursively extract the function name from a C/C++ declarator chain.
/// Handles: function_declarator, pointer_declarator, parenthesized_declarator,
/// identifier, field_identifier, qualified_identifier, destructor_name,
/// template_function.
fn extract_c_declarator_name(declarator: Node, source: &str) -> Option<String> {
    match declarator.kind() {
        "identifier" | "field_identifier" => {
            // field_identifier is used for methods defined inline in class bodies
            Some(
                declarator
                    .utf8_text(source.as_bytes())
                    .unwrap_or("")
                    .to_string(),
            )
        }
        "destructor_name" => {
            // ~ClassName - return as-is
            Some(
                declarator
                    .utf8_text(source.as_bytes())
                    .unwrap_or("")
                    .to_string(),
            )
        }
        "qualified_identifier" => {
            // C++ qualified name: Namespace::Class::method
            // Tree-sitter nests these recursively:
            //   qualified_identifier(Luau::Analysis::Normalizer::normalize)
            //     -> namespace_identifier(Luau), qualified_identifier(Analysis::...)
            //       -> ... -> identifier(normalize)
            // We need to find the deepest rightmost identifier.
            let mut cursor = declarator.walk();
            for child in declarator.children(&mut cursor) {
                // If there's a nested qualified_identifier, recurse into it
                if child.kind() == "qualified_identifier" {
                    return extract_c_declarator_name(child, source);
                }
            }
            // No nested qualified_identifier: look for terminal name nodes
            let mut cursor2 = declarator.walk();
            for child in declarator.children(&mut cursor2) {
                if matches!(child.kind(), "identifier" | "destructor_name") {
                    return Some(child.utf8_text(source.as_bytes()).unwrap_or("").to_string());
                }
                if child.kind() == "template_function" {
                    return child
                        .child_by_field_name("name")
                        .or_else(|| child.named_child(0))
                        .map(|n| n.utf8_text(source.as_bytes()).unwrap_or("").to_string());
                }
            }
            None
        }
        "template_function" => {
            // template<T> void foo() - extract identifier from template_function
            declarator
                .child_by_field_name("name")
                .or_else(|| declarator.named_child(0))
                .map(|n| n.utf8_text(source.as_bytes()).unwrap_or("").to_string())
        }
        "function_declarator" => {
            // function_declarator has a "declarator" field which is the name (identifier)
            if let Some(inner) = declarator.child_by_field_name("declarator") {
                return extract_c_declarator_name(inner, source);
            }
            None
        }
        "pointer_declarator" | "reference_declarator" => {
            // pointer_declarator wraps: * <inner_declarator>
            // reference_declarator wraps: & <inner_declarator>
            if let Some(inner) = declarator.child_by_field_name("declarator") {
                return extract_c_declarator_name(inner, source);
            }
            // Fallback: search children for function_declarator or identifier
            let mut cursor = declarator.walk();
            for child in declarator.children(&mut cursor) {
                if matches!(
                    child.kind(),
                    "function_declarator" | "identifier" | "field_identifier"
                ) {
                    return extract_c_declarator_name(child, source);
                }
            }
            None
        }
        "parenthesized_declarator" => {
            // parenthesized_declarator wraps: ( <inner_declarator> )
            let mut cursor = declarator.walk();
            for child in declarator.children(&mut cursor) {
                if child.is_named() {
                    if let Some(name) = extract_c_declarator_name(child, source) {
                        return Some(name);
                    }
                }
            }
            None
        }
        _ => None,
    }
}

/// Extract function name from a function node
pub fn get_function_name(node: Node, language: Language, source: &str) -> Option<String> {
    match language {
        Language::C | Language::Cpp => {
            // C/C++: function_definition -> declarator -> ... -> identifier
            // The declarator chain can be:
            //   function_declarator -> identifier (simple: int foo())
            //   pointer_declarator -> function_declarator -> identifier (pointer return: int *foo())
            //   identifier (rare, no parens)
            if let Some(declarator) = node.child_by_field_name("declarator") {
                return extract_c_declarator_name(declarator, source);
            }
            None
        }
        Language::Ruby => {
            // Ruby: method node has "name" field
            node.child_by_field_name("name")
                .map(|n| n.utf8_text(source.as_bytes()).unwrap_or("").to_string())
        }
        Language::Php => {
            // PHP function_definition has "name" field
            node.child_by_field_name("name")
                .map(|n| n.utf8_text(source.as_bytes()).unwrap_or("").to_string())
        }
        Language::Elixir => {
            // Elixir: def/defp are calls. The first argument after "def" is the function clause
            // Structure: (call (identifier "def") (arguments (call (identifier "func_name") ...)))
            if node.kind() == "call" {
                // First child should be "def" or "defp"
                let first_child = node.child(0)?;
                let first_text = first_child.utf8_text(source.as_bytes()).unwrap_or("");
                if first_text == "def" || first_text == "defp" {
                    // Second child: arguments containing the function name
                    if let Some(args) = node.child(1) {
                        // Could be directly an identifier or a call node
                        if args.kind() == "identifier" {
                            return Some(
                                args.utf8_text(source.as_bytes()).unwrap_or("").to_string(),
                            );
                        }
                        if args.kind() == "arguments" || args.kind() == "call" {
                            // Find the first identifier
                            let mut cursor = args.walk();
                            for child in args.children(&mut cursor) {
                                if child.kind() == "identifier" {
                                    return Some(
                                        child
                                            .utf8_text(source.as_bytes())
                                            .unwrap_or("")
                                            .to_string(),
                                    );
                                }
                                if child.kind() == "call" {
                                    if let Some(name) = child.child(0) {
                                        if name.kind() == "identifier" {
                                            return Some(
                                                name.utf8_text(source.as_bytes())
                                                    .unwrap_or("")
                                                    .to_string(),
                                            );
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                None
            } else {
                None
            }
        }
        Language::Ocaml => {
            // OCaml: let_binding has "pattern" field (not "name")
            // Structure: (let_binding pattern: (value_name) body: ...)
            // For value_definition, it wraps let_binding(s)
            if node.kind() == "value_definition" {
                // Find the first let_binding child and recurse
                let mut cursor = node.walk();
                for child in node.children(&mut cursor) {
                    if child.kind() == "let_binding" {
                        return get_function_name(child, language, source);
                    }
                }
                None
            } else {
                // let_binding: pattern field contains the name
                node.child_by_field_name("pattern")
                    .map(|n| n.utf8_text(source.as_bytes()).unwrap_or("").to_string())
            }
        }
        Language::Swift => {
            // Swift: function_declaration has "name" field, but init_declaration does not --
            // the keyword "init" IS the name.
            if node.kind() == "init_declaration" {
                return Some("init".to_string());
            }
            node.child_by_field_name("name")
                .map(|n| n.utf8_text(source.as_bytes()).unwrap_or("").to_string())
        }
        Language::Lua | Language::Luau => {
            // Lua/Luau: function_declaration may have a dot_index_expression as name
            // e.g. function Kong.init() -> name is dot_index_expression "Kong.init"
            if let Some(name_node) = node.child_by_field_name("name") {
                let name_text = name_node
                    .utf8_text(source.as_bytes())
                    .unwrap_or("")
                    .to_string();
                return Some(name_text);
            }
            // Fallback for local_function or other variants: search named children
            // for identifier or dot_index_expression
            if node.kind() == "local_function" || node.kind() == "function_declaration" {
                let mut cursor = node.walk();
                for child in node.children(&mut cursor) {
                    if matches!(child.kind(), "identifier" | "dot_index_expression") {
                        return Some(child.utf8_text(source.as_bytes()).unwrap_or("").to_string());
                    }
                }
            }
            // For function_definition (anonymous), no name
            None
        }
        _ => {
            // Most languages use "name" field
            node.child_by_field_name("name")
                .map(|n| n.utf8_text(source.as_bytes()).unwrap_or("").to_string())
        }
    }
}

/// Get the body node of a function
pub fn get_function_body(func_node: Node, language: Language) -> Option<Node> {
    match language {
        Language::Python => func_node.child_by_field_name("body"),
        Language::TypeScript | Language::JavaScript => func_node.child_by_field_name("body"),
        Language::Go => func_node.child_by_field_name("body"),
        Language::Rust => func_node.child_by_field_name("body"),
        Language::Java => func_node.child_by_field_name("body"),
        Language::C | Language::Cpp => func_node.child_by_field_name("body"),
        Language::Ruby => func_node.child_by_field_name("body"),
        Language::Php => func_node.child_by_field_name("body"),
        Language::CSharp => func_node.child_by_field_name("body"),
        Language::Kotlin => {
            // Kotlin: function_declaration has function_body as a named child (not a field).
            // function_body contains a block with the actual statements.
            func_node.child_by_field_name("body").or_else(|| {
                let mut cursor = func_node.walk();
                for child in func_node.children(&mut cursor) {
                    if child.kind() == "function_body" {
                        // function_body may contain a block or a direct expression
                        let mut inner = child.walk();
                        for inner_child in child.children(&mut inner) {
                            if inner_child.kind() == "block" {
                                return Some(inner_child);
                            }
                        }
                        return Some(child);
                    }
                }
                None
            })
        }
        Language::Scala => func_node.child_by_field_name("body"),
        Language::Elixir => {
            // Elixir def body is inside a "do" block
            // Structure: (call "def" (arguments ...) (do_block (body)))
            let mut cursor = func_node.walk();
            for child in func_node.children(&mut cursor) {
                if child.kind() == "do_block" {
                    return Some(child);
                }
            }
            func_node.child_by_field_name("body")
        }
        Language::Lua | Language::Luau => func_node.child_by_field_name("body"),
        Language::Ocaml => {
            // OCaml: func_node may be value_definition or let_binding.
            // For value_definition, drill down to let_binding first.
            // For let_binding, the body field contains the expression.
            if func_node.kind() == "value_definition" {
                // Find let_binding child, then get its body
                let child_count = func_node.child_count();
                let mut binding_body = None;
                for i in 0..child_count {
                    if let Some(child) = func_node.child(i) {
                        if child.kind() == "let_binding" {
                            binding_body = child.child_by_field_name("body");
                            break;
                        }
                    }
                }
                binding_body.or(Some(func_node))
            } else {
                // Already a let_binding
                func_node.child_by_field_name("body").or(Some(func_node))
            }
        }
        _ => func_node.child_by_field_name("body"),
    }
}

/// Convenience: get function node kinds as a Vec (for callers that need Vec<&'static str>)
pub fn get_function_node_kinds_vec(language: Language) -> Vec<&'static str> {
    get_function_node_kinds(language).to_vec()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ast::parser::parse;

    // -- TypeScript generator function tests --

    #[test]
    fn test_ts_generator_function_declaration() {
        let source = r#"
function* genNumbers(): Generator<number> {
    yield 1;
    yield 2;
    yield 3;
}
"#;
        let tree = parse(source, Language::TypeScript).unwrap();
        let root = tree.root_node();
        let node = find_function_node(root, "genNumbers", Language::TypeScript, source);
        assert!(node.is_some(), "Should find generator function declaration");
        let node = node.unwrap();
        assert_eq!(node.kind(), "generator_function_declaration");
        let name = get_function_name(node, Language::TypeScript, source);
        assert_eq!(name.as_deref(), Some("genNumbers"));
        let body = get_function_body(node, Language::TypeScript);
        assert!(body.is_some(), "Should find body of generator function");
    }

    #[test]
    fn test_ts_async_generator_function() {
        let source = r#"
async function* asyncGen(): AsyncGenerator<string> {
    yield "hello";
    yield "world";
}
"#;
        let tree = parse(source, Language::TypeScript).unwrap();
        let root = tree.root_node();
        let node = find_function_node(root, "asyncGen", Language::TypeScript, source);
        assert!(
            node.is_some(),
            "Should find async generator function declaration"
        );
        let node = node.unwrap();
        assert_eq!(node.kind(), "generator_function_declaration");
        let name = get_function_name(node, Language::TypeScript, source);
        assert_eq!(name.as_deref(), Some("asyncGen"));
    }

    #[test]
    fn test_ts_generator_function_expression() {
        let source = r#"
const genArrow = function*(x: number): Generator<number> {
    yield x;
};
"#;
        let tree = parse(source, Language::TypeScript).unwrap();
        let root = tree.root_node();
        let node = find_function_node(root, "genArrow", Language::TypeScript, source);
        assert!(
            node.is_some(),
            "Should find generator function expression via const assignment"
        );
        let node = node.unwrap();
        assert_eq!(node.kind(), "generator_function");
        let body = get_function_body(node, Language::TypeScript);
        assert!(
            body.is_some(),
            "Should find body of generator function expression"
        );
    }

    // -- JavaScript generator function tests --

    #[test]
    fn test_js_generator_function_declaration() {
        let source = r#"
function* genNumbers() {
    yield 1;
    yield 2;
}
"#;
        let tree = parse(source, Language::JavaScript).unwrap();
        let root = tree.root_node();
        let node = find_function_node(root, "genNumbers", Language::JavaScript, source);
        assert!(
            node.is_some(),
            "Should find JS generator function declaration"
        );
        let node = node.unwrap();
        assert_eq!(node.kind(), "generator_function_declaration");
    }

    #[test]
    fn test_js_async_generator_function() {
        let source = r#"
async function* asyncIter() {
    yield "a";
    yield "b";
}
"#;
        let tree = parse(source, Language::JavaScript).unwrap();
        let root = tree.root_node();
        let node = find_function_node(root, "asyncIter", Language::JavaScript, source);
        assert!(node.is_some(), "Should find JS async generator function");
    }

    // -- TypeScript regular function tests (regression) --

    #[test]
    fn test_ts_regular_function() {
        let source = r#"
function greet(name: string): string {
    const greeting = "Hello, " + name;
    return greeting;
}
"#;
        let tree = parse(source, Language::TypeScript).unwrap();
        let root = tree.root_node();
        let node = find_function_node(root, "greet", Language::TypeScript, source);
        assert!(node.is_some(), "Should find regular function declaration");
        assert_eq!(node.unwrap().kind(), "function_declaration");
    }

    #[test]
    fn test_ts_arrow_function() {
        let source = r#"
const add = (a: number, b: number): number => {
    return a + b;
};
"#;
        let tree = parse(source, Language::TypeScript).unwrap();
        let root = tree.root_node();
        let node = find_function_node(root, "add", Language::TypeScript, source);
        assert!(node.is_some(), "Should find arrow function via const");
        assert_eq!(node.unwrap().kind(), "arrow_function");
    }

    #[test]
    fn test_ts_class_method() {
        let source = r#"
class MyClass {
    myMethod(x: number): number {
        return x * 2;
    }
}
"#;
        let tree = parse(source, Language::TypeScript).unwrap();
        let root = tree.root_node();
        let node = find_function_node(root, "myMethod", Language::TypeScript, source);
        assert!(node.is_some(), "Should find class method");
        assert_eq!(node.unwrap().kind(), "method_definition");
    }

    #[test]
    fn test_ts_exported_function() {
        let source = r#"
export function fetchData(url: string): Promise<string> {
    return fetch(url).then(r => r.text());
}
"#;
        let tree = parse(source, Language::TypeScript).unwrap();
        let root = tree.root_node();
        let node = find_function_node(root, "fetchData", Language::TypeScript, source);
        assert!(node.is_some(), "Should find exported function");
    }

    #[test]
    fn test_ts_exported_generator() {
        let source = r#"
export function* items(): Generator<number> {
    yield 1;
    yield 2;
}
"#;
        let tree = parse(source, Language::TypeScript).unwrap();
        let root = tree.root_node();
        let node = find_function_node(root, "items", Language::TypeScript, source);
        assert!(node.is_some(), "Should find exported generator function");
    }

    // -- DFG integration tests for generator functions --

    #[test]
    fn test_ts_generator_dfg() {
        use crate::dfg::get_dfg_context;
        let source = r#"
function* genNumbers(): Generator<number> {
    const x = 1;
    yield x;
    const y = 2;
    yield y;
}
"#;
        let result = get_dfg_context(source, "genNumbers", Language::TypeScript);
        assert!(
            result.is_ok(),
            "DFG should succeed for generator functions, got: {:?}",
            result.err()
        );
        let dfg = result.unwrap();
        assert_eq!(dfg.function, "genNumbers");
        assert!(
            !dfg.variables.is_empty(),
            "Should find variables in generator function"
        );
    }

    #[test]
    fn test_ts_async_generator_dfg() {
        use crate::dfg::get_dfg_context;
        let source = r#"
async function* asyncGen(): AsyncGenerator<string> {
    const msg = "hello";
    yield msg;
}
"#;
        let result = get_dfg_context(source, "asyncGen", Language::TypeScript);
        assert!(
            result.is_ok(),
            "DFG should succeed for async generator functions, got: {:?}",
            result.err()
        );
    }

    // -- CFG integration tests for generator functions --

    #[test]
    fn test_ts_generator_cfg() {
        use crate::cfg::get_cfg_context;
        let source = r#"
function* genNumbers(): Generator<number> {
    const x = 1;
    yield x;
}
"#;
        let result = get_cfg_context(source, "genNumbers", Language::TypeScript);
        assert!(result.is_ok());
        let cfg = result.unwrap();
        assert_eq!(cfg.function, "genNumbers");
        assert!(
            !cfg.blocks.is_empty(),
            "CFG should have blocks for generator function"
        );
    }

    // -- get_function_node_kinds tests --

    #[test]
    fn test_ts_node_kinds_include_generators() {
        let kinds = get_function_node_kinds(Language::TypeScript);
        assert!(
            kinds.contains(&"generator_function_declaration"),
            "TypeScript node kinds should include generator_function_declaration"
        );
        assert!(
            kinds.contains(&"generator_function"),
            "TypeScript node kinds should include generator_function"
        );
    }

    #[test]
    fn test_js_node_kinds_include_generators() {
        let kinds = get_function_node_kinds(Language::JavaScript);
        assert!(
            kinds.contains(&"generator_function_declaration"),
            "JavaScript node kinds should include generator_function_declaration"
        );
        assert!(
            kinds.contains(&"generator_function"),
            "JavaScript node kinds should include generator_function"
        );
    }

    // -- C pointer-returning function tests --

    #[test]
    fn test_c_pointer_returning_function() {
        let source = r#"
typedef struct { int x; } MyStruct;

MyStruct *createStruct(void) {
    int y = 1;
    return (MyStruct*)0;
}
"#;
        let tree = parse(source, Language::C).unwrap();
        let root = tree.root_node();
        let node = find_function_node(root, "createStruct", Language::C, source);
        assert!(
            node.is_some(),
            "Should find C function with pointer return type"
        );
        let node = node.unwrap();
        assert_eq!(node.kind(), "function_definition");
        let name = get_function_name(node, Language::C, source);
        assert_eq!(name.as_deref(), Some("createStruct"));
    }

    #[test]
    fn test_c_pointer_returning_function_dfg() {
        use crate::dfg::get_dfg_context;
        let source = r#"
typedef struct { int x; } MyStruct;

MyStruct *createStruct(int val) {
    int y = val + 1;
    return (MyStruct*)0;
}
"#;
        let result = get_dfg_context(source, "createStruct", Language::C);
        assert!(
            result.is_ok(),
            "DFG should succeed for C pointer-returning function, got: {:?}",
            result.err()
        );
        let dfg = result.unwrap();
        assert_eq!(dfg.function, "createStruct");
    }

    #[test]
    fn test_cpp_pointer_returning_function() {
        let source = r#"
struct Node { int val; };

Node *createNode(int x) {
    int temp = x * 2;
    return nullptr;
}
"#;
        let tree = parse(source, Language::Cpp).unwrap();
        let root = tree.root_node();
        let node = find_function_node(root, "createNode", Language::Cpp, source);
        assert!(
            node.is_some(),
            "Should find C++ function with pointer return type"
        );
        let name = get_function_name(node.unwrap(), Language::Cpp, source);
        assert_eq!(name.as_deref(), Some("createNode"));
    }

    // -- Swift init_declaration tests --

    #[test]
    fn test_swift_init_declaration() {
        let source = r#"
class App {
    init(port: Int) {
        let x = port + 1
    }
}
"#;
        let tree = parse(source, Language::Swift).unwrap();
        let root = tree.root_node();
        let node = find_function_node(root, "init", Language::Swift, source);
        assert!(node.is_some(), "Should find Swift init declaration");
    }

    #[test]
    fn test_swift_init_dfg() {
        use crate::dfg::get_dfg_context;
        let source = r#"
class Server {
    init(port: Int) {
        let addr = port + 1000
    }
}
"#;
        let result = get_dfg_context(source, "init", Language::Swift);
        assert!(
            result.is_ok(),
            "DFG should succeed for Swift init, got: {:?}",
            result.err()
        );
    }

    #[test]
    fn test_swift_node_kinds_include_init() {
        let kinds = get_function_node_kinds(Language::Swift);
        assert!(
            kinds.contains(&"init_declaration"),
            "Swift node kinds should include init_declaration"
        );
    }

    // -- Lua dot-indexed function name tests --

    #[test]
    fn test_lua_dot_indexed_function_short_name() {
        let source = r#"
function Kong.init()
    local x = 1
    return x
end
"#;
        let tree = parse(source, Language::Lua).unwrap();
        let root = tree.root_node();
        // Should find "init" when searching by short name
        let node = find_function_node(root, "init", Language::Lua, source);
        assert!(
            node.is_some(),
            "Should find Lua dot-indexed function by short name 'init'"
        );
    }

    #[test]
    fn test_lua_dot_indexed_function_full_name() {
        let source = r#"
function Kong.init()
    local x = 1
    return x
end
"#;
        let tree = parse(source, Language::Lua).unwrap();
        let root = tree.root_node();
        // Should also find by full qualified name
        let node = find_function_node(root, "Kong.init", Language::Lua, source);
        assert!(
            node.is_some(),
            "Should find Lua dot-indexed function by full name 'Kong.init'"
        );
    }

    #[test]
    fn test_lua_dot_indexed_function_dfg() {
        use crate::dfg::get_dfg_context;
        let source = r#"
function M.request(url)
    local result = url .. "/api"
    return result
end
"#;
        let result = get_dfg_context(source, "request", Language::Lua);
        assert!(
            result.is_ok(),
            "DFG should succeed for Lua dot-indexed function by short name, got: {:?}",
            result.err()
        );
    }

    #[test]
    fn test_luau_dot_indexed_function_short_name() {
        let source = r#"
function Module.process(data)
    local x = data + 1
    return x
end
"#;
        let tree = parse(source, Language::Luau).unwrap();
        let root = tree.root_node();
        let node = find_function_node(root, "process", Language::Luau, source);
        assert!(
            node.is_some(),
            "Should find Luau dot-indexed function by short name 'process'"
        );
    }

    // =========================================================================
    // C++ qualified method definition tests
    // =========================================================================

    #[test]
    fn test_cpp_qualified_method_definition() {
        // C++ method defined outside class body with ClassName::method syntax
        let source = r#"
class MyClass {
public:
    void externalMethod();
};

void MyClass::externalMethod() {
    int x = 1;
}
"#;
        let tree = parse(source, Language::Cpp).unwrap();
        let root = tree.root_node();
        let node = find_function_node(root, "externalMethod", Language::Cpp, source);
        assert!(
            node.is_some(),
            "Should find C++ qualified method definition (ClassName::method)"
        );
        let node = node.unwrap();
        assert_eq!(node.kind(), "function_definition");
        let name = get_function_name(node, Language::Cpp, source);
        assert_eq!(
            name.as_deref(),
            Some("externalMethod"),
            "get_function_name should extract bare name from qualified C++ method"
        );
    }

    #[test]
    fn test_cpp_qualified_method_dfg() {
        use crate::dfg::get_dfg_context;
        let source = r#"
void MyClass::compute(int a) {
    int b = a + 1;
    int c = b * 2;
}
"#;
        let result = get_dfg_context(source, "compute", Language::Cpp);
        assert!(
            result.is_ok(),
            "DFG should succeed for C++ qualified method, got: {:?}",
            result.err()
        );
        let dfg = result.unwrap();
        assert_eq!(dfg.function, "compute");
    }

    #[test]
    fn test_cpp_inline_class_method() {
        // C++ method defined inline inside class body
        let source = r#"
class MyClass {
public:
    void myMethod() {
        int x = 1;
    }
};
"#;
        let tree = parse(source, Language::Cpp).unwrap();
        let root = tree.root_node();
        let node = find_function_node(root, "myMethod", Language::Cpp, source);
        assert!(
            node.is_some(),
            "Should find C++ inline class method (field_identifier)"
        );
        let node = node.unwrap();
        assert_eq!(node.kind(), "function_definition");
        let name = get_function_name(node, Language::Cpp, source);
        assert_eq!(
            name.as_deref(),
            Some("myMethod"),
            "get_function_name should extract name from inline C++ class method"
        );
    }

    #[test]
    fn test_cpp_inline_class_method_dfg() {
        use crate::dfg::get_dfg_context;
        let source = r#"
class Widget {
public:
    int calculate(int a, int b) {
        int sum = a + b;
        int product = a * b;
        return sum;
    }
};
"#;
        let result = get_dfg_context(source, "calculate", Language::Cpp);
        assert!(
            result.is_ok(),
            "DFG should succeed for C++ inline class method, got: {:?}",
            result.err()
        );
        let dfg = result.unwrap();
        assert_eq!(dfg.function, "calculate");
        assert!(
            !dfg.variables.is_empty(),
            "Should find variables in inline class method"
        );
    }

    #[test]
    fn test_cpp_namespace_function() {
        // C++ function inside a namespace
        let source = r#"
namespace Foo {
    void bar() {
        int x = 1;
    }
}
"#;
        let tree = parse(source, Language::Cpp).unwrap();
        let root = tree.root_node();
        let node = find_function_node(root, "bar", Language::Cpp, source);
        assert!(node.is_some(), "Should find C++ function inside namespace");
    }

    #[test]
    fn test_cpp_const_qualified_method() {
        // C++ const method defined outside class
        let source = r#"
bool NormalizedStringType::isNever() const {
    return !isCofinite && singletons.empty();
}
"#;
        let tree = parse(source, Language::Cpp).unwrap();
        let root = tree.root_node();
        let node = find_function_node(root, "isNever", Language::Cpp, source);
        assert!(node.is_some(), "Should find C++ const qualified method");
        let name = get_function_name(node.unwrap(), Language::Cpp, source);
        assert_eq!(name.as_deref(), Some("isNever"));
    }

    #[test]
    fn test_cpp_nested_namespace_qualified_method() {
        // C++ method with deeply nested namespace::class::method
        let source = r#"
void Luau::Analysis::Normalizer::normalize() {
    int x = 1;
}
"#;
        let tree = parse(source, Language::Cpp).unwrap();
        let root = tree.root_node();
        let node = find_function_node(root, "normalize", Language::Cpp, source);
        assert!(node.is_some(), "Should find deeply qualified C++ method");
        let name = get_function_name(node.unwrap(), Language::Cpp, source);
        assert_eq!(name.as_deref(), Some("normalize"));
    }

    // =========================================================================
    // C qualified function tests (same issues apply to C with qualified names)
    // =========================================================================

    #[test]
    fn test_c_inline_struct_method_like() {
        // C doesn't have classes but let's ensure function_definition inside
        // struct declarations or other nesting still works
        let source = r#"
void process(int x) {
    int y = x + 1;
}
"#;
        let tree = parse(source, Language::C).unwrap();
        let root = tree.root_node();
        let node = find_function_node(root, "process", Language::C, source);
        assert!(node.is_some(), "Should find simple C function");
    }

    // =========================================================================
    // Lua/Luau local_function tests
    // =========================================================================

    #[test]
    fn test_lua_local_function_node_kinds() {
        // Verify that local_function is included in node kinds for Lua
        // This ensures consistency with ast_utils::function_node_kinds
        let kinds = get_function_node_kinds(Language::Lua);
        // Lua tree-sitter may or may not use "local_function" - but if ast_utils
        // includes it, function_finder should too for consistency
        let ast_kinds = crate::security::ast_utils::function_node_kinds(Language::Lua);
        for kind in ast_kinds {
            assert!(
                kinds.contains(kind),
                "function_finder should include '{}' which ast_utils includes for Lua",
                kind
            );
        }
    }

    #[test]
    fn test_luau_local_function_node_kinds() {
        let kinds = get_function_node_kinds(Language::Luau);
        let ast_kinds = crate::security::ast_utils::function_node_kinds(Language::Luau);
        for kind in ast_kinds {
            assert!(
                kinds.contains(kind),
                "function_finder should include '{}' which ast_utils includes for Luau",
                kind
            );
        }
    }
}