tldr-core 0.1.4

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
//! Multi-language interface extraction tests
//!
//! These tests validate that tree-sitter correctly parses different languages
//! and that our node kind assumptions are correct.

use tldr_core::ast::ParserPool;
use tldr_core::types::Language;

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

// =============================================================================
// Python
// =============================================================================

#[test]
fn test_python_function_definition_node_kinds() {
    let source = r#"
def public_func():
    """A public function."""
    pass

def _private_func():
    pass
"#;
    let pool = ParserPool::new();
    let tree = pool.parse(source, Language::Python).unwrap();
    let root = tree.root_node();

    let mut func_count = 0;
    let mut cursor = root.walk();
    for child in root.children(&mut cursor) {
        if child.kind() == "function_definition" {
            func_count += 1;
            let name = child
                .child_by_field_name("name")
                .map(|n| node_text(n, source.as_bytes()))
                .unwrap_or("");
            assert!(!name.is_empty(), "Function should have a name");
        }
    }
    assert_eq!(func_count, 2, "Should find 2 functions");
}

#[test]
fn test_python_class_definition_node_kinds() {
    let source = r#"
class PublicClass:
    def public_method(self):
        pass

    def _private_method(self):
        pass
"#;
    let pool = ParserPool::new();
    let tree = pool.parse(source, Language::Python).unwrap();
    let root = tree.root_node();

    let mut class_count = 0;
    let mut cursor = root.walk();
    for child in root.children(&mut cursor) {
        if child.kind() == "class_definition" {
            class_count += 1;
            let name = child
                .child_by_field_name("name")
                .map(|n| node_text(n, source.as_bytes()))
                .unwrap_or("");
            assert_eq!(name, "PublicClass");

            // Check body for methods
            let body = child.child_by_field_name("body").unwrap();
            let mut method_count = 0;
            let mut body_cursor = body.walk();
            for body_child in body.children(&mut body_cursor) {
                if body_child.kind() == "function_definition" {
                    method_count += 1;
                }
            }
            assert_eq!(method_count, 2, "Should find 2 methods in class body");
        }
    }
    assert_eq!(class_count, 1, "Should find 1 class");
}

// =============================================================================
// Rust
// =============================================================================

#[test]
fn test_rust_function_item_node_kinds() {
    let source = r#"
/// Adds two numbers.
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

fn private_helper() -> bool {
    true
}

pub async fn async_fetch() -> String {
    String::new()
}
"#;
    let pool = ParserPool::new();
    let tree = pool.parse(source, Language::Rust).unwrap();
    let root = tree.root_node();

    let mut pub_funcs = Vec::new();
    let mut priv_funcs = Vec::new();
    let mut cursor = root.walk();

    for child in root.children(&mut cursor) {
        if child.kind() == "function_item" {
            let name = child
                .child_by_field_name("name")
                .map(|n| node_text(n, source.as_bytes()).to_string())
                .unwrap_or_default();

            // Check for visibility_modifier
            let mut is_pub = false;
            for i in 0..child.child_count() {
                if let Some(gc) = child.child(i) {
                    if gc.kind() == "visibility_modifier" {
                        is_pub = true;
                        break;
                    }
                }
            }

            if is_pub {
                pub_funcs.push(name);
            } else {
                priv_funcs.push(name);
            }
        }
    }

    assert_eq!(
        pub_funcs.len(),
        2,
        "Should find 2 pub functions: {:?}",
        pub_funcs
    );
    assert!(pub_funcs.contains(&"add".to_string()));
    assert!(pub_funcs.contains(&"async_fetch".to_string()));
    assert_eq!(priv_funcs.len(), 1, "Should find 1 private function");
    assert!(priv_funcs.contains(&"private_helper".to_string()));
}

#[test]
fn test_rust_struct_impl_trait_node_kinds() {
    let source = r#"
pub struct Point {
    pub x: f64,
    pub y: f64,
}

impl Point {
    pub fn new(x: f64, y: f64) -> Self {
        Point { x, y }
    }

    fn internal(&self) {}
}

pub trait Drawable {
    fn draw(&self);
}
"#;
    let pool = ParserPool::new();
    let tree = pool.parse(source, Language::Rust).unwrap();
    let root = tree.root_node();

    let mut struct_count = 0;
    let mut impl_count = 0;
    let mut trait_count = 0;
    let mut cursor = root.walk();

    for child in root.children(&mut cursor) {
        match child.kind() {
            "struct_item" => struct_count += 1,
            "impl_item" => {
                impl_count += 1;
                // Check that impl has declaration_list body
                let mut has_decl_list = false;
                let mut inner_cursor = child.walk();
                for gc in child.children(&mut inner_cursor) {
                    if gc.kind() == "declaration_list" {
                        has_decl_list = true;
                        // Count pub methods
                        let mut pub_methods = 0;
                        let mut priv_methods = 0;
                        let mut decl_cursor = gc.walk();
                        for method in gc.children(&mut decl_cursor) {
                            if method.kind() == "function_item" {
                                let mut is_pub = false;
                                for k in 0..method.child_count() {
                                    if let Some(mk) = method.child(k) {
                                        if mk.kind() == "visibility_modifier" {
                                            is_pub = true;
                                            break;
                                        }
                                    }
                                }
                                if is_pub {
                                    pub_methods += 1;
                                } else {
                                    priv_methods += 1;
                                }
                            }
                        }
                        assert_eq!(pub_methods, 1, "Should find 1 pub method in impl");
                        assert_eq!(priv_methods, 1, "Should find 1 private method in impl");
                    }
                }
                assert!(has_decl_list, "impl should have declaration_list");
            }
            "trait_item" => trait_count += 1,
            _ => {}
        }
    }

    assert_eq!(struct_count, 1, "Should find 1 struct");
    assert_eq!(impl_count, 1, "Should find 1 impl");
    assert_eq!(trait_count, 1, "Should find 1 trait");
}

#[test]
fn test_rust_doc_comments() {
    let source = r#"
/// Adds two numbers.
/// This is a multi-line doc comment.
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}
"#;
    let pool = ParserPool::new();
    let tree = pool.parse(source, Language::Rust).unwrap();
    let root = tree.root_node();

    let mut cursor = root.walk();
    for child in root.children(&mut cursor) {
        if child.kind() == "function_item" {
            // Check for preceding doc comments
            let mut comments = Vec::new();
            let mut prev = child.prev_sibling();
            while let Some(sib) = prev {
                if sib.kind() == "line_comment" {
                    let text = node_text(sib, source.as_bytes());
                    if text.starts_with("///") {
                        comments.push(text.to_string());
                    }
                } else {
                    break;
                }
                prev = sib.prev_sibling();
            }
            assert!(
                !comments.is_empty(),
                "Should find doc comments before function"
            );
        }
    }
}

// =============================================================================
// Go
// =============================================================================

#[test]
fn test_go_function_declaration_node_kinds() {
    let source = r#"
package main

// ProcessData handles data processing.
func ProcessData(input string) (string, error) {
    return input, nil
}

func internalHelper() bool {
    return true
}
"#;
    let pool = ParserPool::new();
    let tree = pool.parse(source, Language::Go).unwrap();
    let root = tree.root_node();

    let mut funcs = Vec::new();
    let mut cursor = root.walk();
    for child in root.children(&mut cursor) {
        if child.kind() == "function_declaration" {
            let name = child
                .child_by_field_name("name")
                .map(|n| node_text(n, source.as_bytes()).to_string())
                .unwrap_or_default();
            funcs.push(name);
        }
    }

    assert_eq!(funcs.len(), 2, "Should find 2 functions: {:?}", funcs);
    assert!(funcs.contains(&"ProcessData".to_string()));
    assert!(funcs.contains(&"internalHelper".to_string()));
}

#[test]
fn test_go_exported_vs_unexported() {
    let source = r#"
package main

func Exported() {}
func unexported() {}
"#;
    let pool = ParserPool::new();
    let tree = pool.parse(source, Language::Go).unwrap();
    let root = tree.root_node();

    let mut cursor = root.walk();
    for child in root.children(&mut cursor) {
        if child.kind() == "function_declaration" {
            let name = child
                .child_by_field_name("name")
                .map(|n| node_text(n, source.as_bytes()))
                .unwrap_or("");
            let first_char = name.chars().next().unwrap();
            if name == "Exported" {
                assert!(first_char.is_uppercase());
            } else {
                assert!(first_char.is_lowercase());
            }
        }
    }
}

// =============================================================================
// TypeScript
// =============================================================================

#[test]
fn test_typescript_class_and_function_node_kinds() {
    let source = r#"
class UserService {
    async fetchUser(id: string): Promise<User> {
        return {} as User;
    }
}

function processData(input: string): number {
    return input.length;
}

interface User {
    id: string;
    name: string;
}

type Status = "active" | "inactive";
"#;
    let pool = ParserPool::new();
    let tree = pool.parse(source, Language::TypeScript).unwrap();
    let root = tree.root_node();

    let mut class_count = 0;
    let mut func_count = 0;
    let mut interface_count = 0;
    let mut type_alias_count = 0;
    let mut cursor = root.walk();

    for child in root.children(&mut cursor) {
        match child.kind() {
            "class_declaration" => class_count += 1,
            "function_declaration" => func_count += 1,
            "interface_declaration" => interface_count += 1,
            "type_alias_declaration" => type_alias_count += 1,
            _ => {}
        }
    }

    assert_eq!(class_count, 1, "Should find 1 class");
    assert_eq!(func_count, 1, "Should find 1 function");
    assert_eq!(interface_count, 1, "Should find 1 interface");
    assert_eq!(type_alias_count, 1, "Should find 1 type alias");
}

// =============================================================================
// Java
// =============================================================================

#[test]
fn test_java_class_declaration_node_kinds() {
    let source = r#"
public class UserService {
    public String getUser(String id) {
        return id;
    }

    private void internalCleanup() {}
}
"#;
    let pool = ParserPool::new();
    let tree = pool.parse(source, Language::Java).unwrap();
    let root = tree.root_node();

    let mut class_count = 0;
    let mut cursor = root.walk();
    for child in root.children(&mut cursor) {
        if child.kind() == "class_declaration" {
            class_count += 1;
            let name = child
                .child_by_field_name("name")
                .map(|n| node_text(n, source.as_bytes()))
                .unwrap_or("");
            assert_eq!(name, "UserService");

            // Check modifiers
            if let Some(mods) = child.child_by_field_name("modifiers") {
                assert!(
                    node_text(mods, source.as_bytes()).contains("public"),
                    "Class should have public modifier"
                );
            }

            // Check body - Java uses "class_body" not "body" field
            // Try both "body" field and explicit class_body search
            let mut c2 = child.walk();
            let class_body = child
                .children(&mut c2)
                .find(|gc| gc.kind() == "class_body");
            let body = child.child_by_field_name("body").or(class_body);
            assert!(
                body.is_some(),
                "Should find class body. Children: {:?}",
                (0..child.child_count())
                    .map(|i| child.child(i).map(|c| c.kind().to_string()))
                    .collect::<Vec<_>>()
            );
            let body = body.unwrap();
            let mut pub_methods = 0;
            let mut priv_methods = 0;
            let mut body_cursor = body.walk();
            for bc in body.children(&mut body_cursor) {
                if bc.kind() == "method_declaration" {
                    // Check modifiers - try field name and direct child search
                    let mod_text = bc
                        .child_by_field_name("modifiers")
                        .or_else(|| {
                            for i in 0..bc.child_count() {
                                if let Some(gc) = bc.child(i) {
                                    if gc.kind() == "modifiers" {
                                        return Some(gc);
                                    }
                                }
                            }
                            None
                        })
                        .map(|m| node_text(m, source.as_bytes()).to_string())
                        .unwrap_or_default();

                    if mod_text.contains("public") {
                        pub_methods += 1;
                    } else if mod_text.contains("private") {
                        priv_methods += 1;
                    }
                }
            }
            assert_eq!(pub_methods, 1, "Should find 1 public method");
            assert_eq!(priv_methods, 1, "Should find 1 private method");
        }
    }
    assert_eq!(class_count, 1, "Should find 1 class");
}

// =============================================================================
// C
// =============================================================================

#[test]
fn test_c_function_definition_node_kinds() {
    let source = r#"
int add(int a, int b) {
    return a + b;
}

static int internal_helper(void) {
    return 42;
}
"#;
    let pool = ParserPool::new();
    let tree = pool.parse(source, Language::C).unwrap();
    let root = tree.root_node();

    let mut funcs = Vec::new();
    let mut cursor = root.walk();
    for child in root.children(&mut cursor) {
        if child.kind() == "function_definition" {
            // Get name from declarator
            let mut is_static = false;
            for i in 0..child.child_count() {
                if let Some(gc) = child.child(i) {
                    if gc.kind() == "storage_class_specifier"
                        && node_text(gc, source.as_bytes()) == "static"
                    {
                        is_static = true;
                    }
                }
            }

            // Get function name
            let name = if let Some(declarator) = child.child_by_field_name("declarator") {
                get_c_func_name(declarator, source.as_bytes())
            } else {
                String::new()
            };

            funcs.push((name, is_static));
        }
    }

    assert_eq!(funcs.len(), 2, "Should find 2 functions: {:?}", funcs);
    assert!(funcs.iter().any(|(n, s)| n == "add" && !s));
    assert!(funcs.iter().any(|(n, s)| n == "internal_helper" && *s));
}

fn get_c_func_name(declarator: tree_sitter::Node, source: &[u8]) -> String {
    if declarator.kind() == "identifier" {
        return node_text(declarator, source).to_string();
    }
    if let Some(inner) = declarator.child_by_field_name("declarator") {
        return get_c_func_name(inner, source);
    }
    if let Some(first) = declarator.child(0) {
        if first.kind() == "identifier" {
            return node_text(first, source).to_string();
        }
    }
    String::new()
}

// =============================================================================
// Ruby
// =============================================================================

#[test]
fn test_ruby_class_and_method_node_kinds() {
    let source = r#"
class UserManager
  def find_user(id)
    # find user
  end

  def _private_method
    # private
  end
end
"#;
    let pool = ParserPool::new();
    let tree = pool.parse(source, Language::Ruby).unwrap();
    let root = tree.root_node();

    let mut class_count = 0;
    let mut cursor = root.walk();
    for child in root.children(&mut cursor) {
        if child.kind() == "class" {
            class_count += 1;
            let name = child
                .child_by_field_name("name")
                .map(|n| node_text(n, source.as_bytes()))
                .unwrap_or("");
            assert_eq!(name, "UserManager");

            // Ruby class body: methods may be in a body_statement child or direct
            let mut method_count = 0;
            // First print all children to understand structure
            let child_kinds: Vec<String> = (0..child.child_count())
                .filter_map(|i| child.child(i).map(|c| c.kind().to_string()))
                .collect();
            // Try direct children first
            let mut inner_cursor = child.walk();
            for gc in child.children(&mut inner_cursor) {
                if gc.kind() == "method" {
                    method_count += 1;
                }
                // Also check inside body_statement
                if gc.kind() == "body_statement" {
                    let mut body_cursor = gc.walk();
                    for bc in gc.children(&mut body_cursor) {
                        if bc.kind() == "method" {
                            method_count += 1;
                        }
                    }
                }
            }
            assert_eq!(
                method_count, 2,
                "Should find 2 methods in class body. Children: {:?}",
                child_kinds
            );
        }
    }
    assert_eq!(class_count, 1, "Should find 1 class");
}

// =============================================================================
// C#
// =============================================================================

#[test]
fn test_csharp_class_node_kinds() {
    let source = r#"
public class UserService {
    public string GetUser(string id) {
        return id;
    }

    private void InternalCleanup() {}
}
"#;
    let pool = ParserPool::new();
    let tree = pool.parse(source, Language::CSharp).unwrap();
    let root = tree.root_node();

    let mut class_count = 0;
    let mut cursor = root.walk();
    for child in root.children(&mut cursor) {
        if child.kind() == "class_declaration" {
            class_count += 1;
        }
    }
    assert_eq!(class_count, 1, "Should find 1 C# class");
}

// =============================================================================
// Scala
// =============================================================================

#[test]
fn test_scala_class_node_kinds() {
    let source = r#"
class UserService {
  def getUser(id: String): String = {
    id
  }
}

object Config {
  val name = "test"
}
"#;
    let pool = ParserPool::new();
    let tree = pool.parse(source, Language::Scala).unwrap();
    let root = tree.root_node();

    let mut class_count = 0;
    let mut object_count = 0;
    let mut cursor = root.walk();
    for child in root.children(&mut cursor) {
        match child.kind() {
            "class_definition" => class_count += 1,
            "object_definition" => object_count += 1,
            _ => {}
        }
    }
    assert_eq!(class_count, 1, "Should find 1 Scala class");
    assert_eq!(object_count, 1, "Should find 1 Scala object");
}