cha-parser 0.1.1

Tree-sitter based AST parser for Cha (TypeScript, Rust)
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
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

use cha_core::{ClassInfo, FunctionInfo, ImportInfo, SourceFile, SourceModel};
use tree_sitter::{Node, Parser};

use crate::LanguageParser;

pub struct TypeScriptParser;

impl LanguageParser for TypeScriptParser {
    fn language_name(&self) -> &str {
        "typescript"
    }

    fn parse(&self, file: &SourceFile) -> Option<SourceModel> {
        let mut parser = Parser::new();
        parser
            .set_language(&tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into())
            .ok()?;
        let tree = parser.parse(&file.content, None)?;
        let root = tree.root_node();
        let src = file.content.as_bytes();

        let mut ctx = ParseContext::new(src);
        ctx.collect_nodes(root, false);

        Some(SourceModel {
            language: "typescript".into(),
            total_lines: file.line_count(),
            functions: ctx.col.functions,
            classes: ctx.col.classes,
            imports: ctx.col.imports,
        })
    }
}

/// Accumulator for collected AST items.
struct Collector {
    functions: Vec<FunctionInfo>,
    classes: Vec<ClassInfo>,
    imports: Vec<ImportInfo>,
}

/// Bundles source bytes and collector to eliminate repeated parameter passing.
struct ParseContext<'a> {
    src: &'a [u8],
    col: Collector,
}

impl<'a> ParseContext<'a> {
    fn new(src: &'a [u8]) -> Self {
        Self {
            src,
            col: Collector {
                functions: Vec::new(),
                classes: Vec::new(),
                imports: Vec::new(),
            },
        }
    }

    fn collect_nodes(&mut self, node: Node, exported: bool) {
        let mut cursor = node.walk();
        for child in node.children(&mut cursor) {
            self.collect_single_node(child, exported);
        }
    }

    fn collect_single_node(&mut self, child: Node, exported: bool) {
        match child.kind() {
            "export_statement" => self.collect_nodes(child, true),
            "function_declaration" | "method_definition" => self.push_function(child, exported),
            "lexical_declaration" | "variable_declaration" => {
                extract_arrow_functions(child, self.src, exported, &mut self.col.functions);
                self.collect_nodes(child, exported);
            }
            "class_declaration" => self.push_class(child, exported),
            "import_statement" => self.push_import(child),
            _ => self.collect_nodes(child, false),
        }
    }

    fn push_function(&mut self, node: Node, exported: bool) {
        if let Some(mut f) = extract_function(node, self.src) {
            f.is_exported = exported;
            self.col.functions.push(f);
        }
    }

    fn push_class(&mut self, node: Node, exported: bool) {
        if let Some(mut c) = extract_class(node, self.src) {
            c.is_exported = exported;
            self.col.classes.push(c);
        }
    }

    fn push_import(&mut self, node: Node) {
        if let Some(i) = extract_import(node, self.src) {
            self.col.imports.push(i);
        }
    }
}

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

/// Hash the AST structure of a node (kind + children structure, ignoring names).
fn hash_ast_structure(node: Node) -> u64 {
    let mut hasher = DefaultHasher::new();
    walk_hash(node, &mut hasher);
    hasher.finish()
}

fn walk_hash(node: Node, hasher: &mut DefaultHasher) {
    node.kind().hash(hasher);
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        walk_hash(child, hasher);
    }
}

fn extract_function(node: Node, src: &[u8]) -> Option<FunctionInfo> {
    let name_node = node.child_by_field_name("name")?;
    let name = node_text(name_node, src).to_string();
    let start_line = node.start_position().row + 1;
    let end_line = node.end_position().row + 1;
    let body = node.child_by_field_name("body");
    let body_hash = body.map(hash_ast_structure);
    let parameter_count = count_parameters(node);
    let parameter_types = extract_param_types(node, src);
    let chain_depth = body.map(max_chain_depth).unwrap_or(0);
    let switch_arms = body.map(count_switch_arms).unwrap_or(0);
    let external_refs = body
        .map(|b| collect_external_refs(b, src))
        .unwrap_or_default();
    let is_delegating = body.map(|b| check_delegating(b, src)).unwrap_or(false);
    Some(FunctionInfo {
        name,
        start_line,
        end_line,
        line_count: end_line - start_line + 1,
        complexity: count_complexity(node),
        body_hash,
        is_exported: false,
        parameter_count,
        parameter_types,
        chain_depth,
        switch_arms,
        external_refs,
        is_delegating,
        comment_lines: count_comment_lines(node),
        referenced_fields: collect_this_fields(body, src),
        null_check_fields: collect_null_checks_ts(body, src),
        switch_dispatch_target: extract_switch_target_ts(body, src),
        optional_param_count: count_optional_params_ts(node, src),
    })
}

fn extract_arrow_functions(
    node: Node,
    src: &[u8],
    exported: bool,
    functions: &mut Vec<FunctionInfo>,
) {
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if child.kind() == "variable_declarator"
            && let Some(f) = try_extract_arrow(child, node, src, exported)
        {
            functions.push(f);
        }
    }
}

// Try to extract an arrow function from a variable declarator.
fn try_extract_arrow(child: Node, decl: Node, src: &[u8], exported: bool) -> Option<FunctionInfo> {
    let name = child
        .child_by_field_name("name")
        .map(|n| node_text(n, src).to_string())?;
    let value = child.child_by_field_name("value")?;
    if value.kind() != "arrow_function" {
        return None;
    }
    let start_line = decl.start_position().row + 1;
    let end_line = decl.end_position().row + 1;
    let body = value.child_by_field_name("body");
    let body_hash = body.map(hash_ast_structure);
    Some(FunctionInfo {
        name,
        start_line,
        end_line,
        line_count: end_line - start_line + 1,
        complexity: count_complexity(value),
        body_hash,
        is_exported: exported,
        parameter_count: count_parameters(value),
        parameter_types: extract_param_types(value, src),
        chain_depth: body.map(max_chain_depth).unwrap_or(0),
        switch_arms: body.map(count_switch_arms).unwrap_or(0),
        external_refs: body
            .map(|b| collect_external_refs(b, src))
            .unwrap_or_default(),
        is_delegating: body.map(|b| check_delegating(b, src)).unwrap_or(false),
        comment_lines: count_comment_lines(value),
        referenced_fields: collect_this_fields(body, src),
        null_check_fields: collect_null_checks_ts(body, src),
        switch_dispatch_target: extract_switch_target_ts(body, src),
        optional_param_count: count_optional_params_ts(value, src),
    })
}

fn extract_class(node: Node, src: &[u8]) -> Option<ClassInfo> {
    let name_node = node.child_by_field_name("name")?;
    let name = node_text(name_node, src).to_string();
    let start_line = node.start_position().row + 1;
    let end_line = node.end_position().row + 1;
    let body = node.child_by_field_name("body")?;
    let (methods, delegating, fields, has_behavior, cb_fields) = scan_class_body(body, src);
    let is_interface =
        node.kind() == "interface_declaration" || node.kind() == "abstract_class_declaration";
    let has_listener_field = !cb_fields.is_empty();
    let has_notify_method = has_iterate_and_call_ts(body, src, &cb_fields);

    Some(ClassInfo {
        name,
        start_line,
        end_line,
        method_count: methods,
        line_count: end_line - start_line + 1,
        is_exported: false,
        delegating_method_count: delegating,
        field_count: fields.len(),
        field_names: fields,
        has_behavior,
        is_interface,
        parent_name: extract_parent_name(node, src),
        override_count: 0,
        self_call_count: 0,
        has_listener_field,
        has_notify_method,
    })
}

/// Scan class body and return (method_count, delegating_count, field_names, has_behavior, callback_fields).
fn scan_class_body(body: Node, src: &[u8]) -> (usize, usize, Vec<String>, bool, Vec<String>) {
    let mut methods = 0;
    let mut delegating = 0;
    let mut fields = Vec::new();
    let mut callback_fields = Vec::new();
    let mut has_behavior = false;
    let mut cursor = body.walk();
    for child in body.children(&mut cursor) {
        match child.kind() {
            "method_definition" => {
                let (is_behavior, is_delegating) = classify_method(child, src);
                methods += 1;
                has_behavior |= is_behavior;
                delegating += usize::from(is_delegating);
            }
            "public_field_definition" | "property_definition" => {
                if let Some(n) = child.child_by_field_name("name") {
                    let name = node_text(n, src).to_string();
                    if is_callback_collection_type_ts(child, src) {
                        callback_fields.push(name.clone());
                    }
                    fields.push(name);
                }
            }
            _ => {}
        }
    }
    (methods, delegating, fields, has_behavior, callback_fields)
}

/// Classify a method: returns (is_behavior, is_delegating).
fn classify_method(node: Node, src: &[u8]) -> (bool, bool) {
    let mname = node
        .child_by_field_name("name")
        .map(|n| node_text(n, src))
        .unwrap_or("");
    let is_behavior = !is_accessor_name(mname) && mname != "constructor";
    let is_delegating = node
        .child_by_field_name("body")
        .is_some_and(|b| check_delegating(b, src));
    (is_behavior, is_delegating)
}

fn count_parameters(node: Node) -> usize {
    let params = match node.child_by_field_name("parameters") {
        Some(p) => p,
        None => return 0,
    };
    let mut cursor = params.walk();
    params
        .children(&mut cursor)
        .filter(|c| {
            matches!(
                c.kind(),
                "required_parameter" | "optional_parameter" | "rest_parameter"
            )
        })
        .count()
}

fn extract_param_types(node: Node, src: &[u8]) -> Vec<String> {
    let params = match node.child_by_field_name("parameters") {
        Some(p) => p,
        None => return vec![],
    };
    let mut types = Vec::new();
    let mut cursor = params.walk();
    for child in params.children(&mut cursor) {
        if let Some(ann) = child.child_by_field_name("type") {
            types.push(node_text(ann, src).to_string());
        }
    }
    types.sort();
    types
}

fn max_chain_depth(node: Node) -> usize {
    let mut max = 0;
    walk_chain_depth(node, &mut max);
    max
}

fn walk_chain_depth(node: Node, max: &mut usize) {
    if node.kind() == "member_expression" {
        let depth = measure_chain(node);
        if depth > *max {
            *max = depth;
        }
    }
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        walk_chain_depth(child, max);
    }
}

/// Count consecutive property accesses (a.b.c.d), skipping method calls.
fn measure_chain(node: Node) -> usize {
    let mut depth = 0;
    let mut current = node;
    while current.kind() == "member_expression" {
        depth += 1;
        if let Some(obj) = current.child_by_field_name("object") {
            current = obj;
        } else {
            break;
        }
    }
    depth
}

fn count_switch_arms(node: Node) -> usize {
    let mut count = 0;
    walk_switch_arms(node, &mut count);
    count
}

fn walk_switch_arms(node: Node, count: &mut usize) {
    if node.kind() == "switch_case" || node.kind() == "switch_default" {
        *count += 1;
    }
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        walk_switch_arms(child, count);
    }
}

fn collect_external_refs(node: Node, src: &[u8]) -> Vec<String> {
    let mut refs = Vec::new();
    walk_external_refs(node, src, &mut refs);
    refs.sort();
    refs.dedup();
    refs
}

fn member_chain_root(node: Node) -> Node {
    let mut current = node;
    while current.kind() == "member_expression" {
        match current.child_by_field_name("object") {
            Some(child) => current = child,
            None => break,
        }
    }
    current
}

fn walk_external_refs(node: Node, src: &[u8], refs: &mut Vec<String>) {
    if node.kind() == "member_expression" {
        let root = member_chain_root(node);
        let text = node_text(root, src);
        if text != "this" && text != "self" && !text.is_empty() {
            refs.push(text.to_string());
        }
    }
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        walk_external_refs(child, src, refs);
    }
}

fn single_stmt(body: Node) -> Option<Node> {
    let mut cursor = body.walk();
    let stmts: Vec<_> = body
        .children(&mut cursor)
        .filter(|c| c.kind() != "{" && c.kind() != "}")
        .collect();
    (stmts.len() == 1).then(|| stmts[0])
}

fn is_external_call(node: Node, src: &[u8]) -> bool {
    node.kind() == "call_expression"
        && node.child_by_field_name("function").is_some_and(|func| {
            func.kind() == "member_expression"
                && func
                    .child_by_field_name("object")
                    .is_some_and(|obj| node_text(obj, src) != "this")
        })
}

fn check_delegating(body: Node, src: &[u8]) -> bool {
    let Some(stmt) = single_stmt(body) else {
        return false;
    };
    let expr = match stmt.kind() {
        "return_statement" => stmt.child(1).unwrap_or(stmt),
        "expression_statement" => stmt.child(0).unwrap_or(stmt),
        _ => stmt,
    };
    is_external_call(expr, src)
}

fn count_complexity(node: Node) -> usize {
    let mut complexity = 1;
    walk_complexity(node, &mut complexity);
    complexity
}

fn walk_complexity(node: Node, count: &mut usize) {
    match node.kind() {
        "if_statement" | "else_clause" | "for_statement" | "for_in_statement"
        | "while_statement" | "do_statement" | "switch_case" | "catch_clause"
        | "ternary_expression" => {
            *count += 1;
        }
        "binary_expression" => {
            let mut cursor = node.walk();
            for child in node.children(&mut cursor) {
                if child.kind() == "&&" || child.kind() == "||" {
                    *count += 1;
                }
            }
        }
        _ => {}
    }
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        walk_complexity(child, count);
    }
}

fn extract_import(node: Node, src: &[u8]) -> Option<ImportInfo> {
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if child.kind() == "string" {
            let raw = node_text(child, src);
            let source = raw.trim_matches(|c| c == '\'' || c == '"').to_string();
            return Some(ImportInfo {
                source,
                line: node.start_position().row + 1,
            });
        }
    }
    None
}

/// Count comment lines inside a node (recursive).
fn count_comment_lines(node: Node) -> usize {
    let mut count = 0;
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if child.kind() == "comment" {
            count += child.end_position().row - child.start_position().row + 1;
        } else if child.child_count() > 0 {
            count += count_comment_lines(child);
        }
    }
    count
}

/// Collect `this.xxx` field references from a function body.
fn collect_this_fields(body: Option<Node>, src: &[u8]) -> Vec<String> {
    let Some(body) = body else { return vec![] };
    let mut refs = Vec::new();
    collect_this_refs(body, src, &mut refs);
    refs.sort();
    refs.dedup();
    refs
}

fn collect_this_refs(node: Node, src: &[u8], refs: &mut Vec<String>) {
    if node.kind() == "member_expression"
        && let Some(obj) = node.child_by_field_name("object")
        && node_text(obj, src) == "this"
        && let Some(prop) = node.child_by_field_name("property")
    {
        refs.push(node_text(prop, src).to_string());
    }
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        collect_this_refs(child, src, refs);
    }
}

/// Check if a method name looks like a getter/setter.
fn is_accessor_name(name: &str) -> bool {
    let lower = name.to_lowercase();
    lower.starts_with("get") || lower.starts_with("set") || lower.starts_with("is")
}

/// Extract parent class name from `extends` clause.
fn extract_parent_name(node: Node, src: &[u8]) -> Option<String> {
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if child.kind() == "class_heritage" {
            let mut inner = child.walk();
            for c in child.children(&mut inner) {
                if c.kind() == "extends_clause" {
                    // First identifier child is the parent name
                    let mut ec = c.walk();
                    for e in c.children(&mut ec) {
                        if e.kind() == "identifier" || e.kind() == "type_identifier" {
                            return Some(node_text(e, src).to_string());
                        }
                    }
                }
            }
        }
    }
    None
}

/// Collect field names checked for null/undefined in TS.
fn collect_null_checks_ts(body: Option<Node>, src: &[u8]) -> Vec<String> {
    let Some(body) = body else { return vec![] };
    let mut fields = Vec::new();
    walk_null_checks_ts(body, src, &mut fields);
    fields.sort();
    fields.dedup();
    fields
}

fn walk_null_checks_ts(node: Node, src: &[u8], fields: &mut Vec<String>) {
    if node.kind() == "binary_expression"
        && let text = node_text(node, src)
        && (text.contains("null") || text.contains("undefined"))
        && let Some(left) = node.child_by_field_name("left")
        && let ltext = node_text(left, src)
        && let Some(f) = ltext.strip_prefix("this.")
    {
        fields.push(f.to_string());
    }
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        walk_null_checks_ts(child, src, fields);
    }
}

/// Extract switch dispatch target in TS.
fn extract_switch_target_ts(body: Option<Node>, src: &[u8]) -> Option<String> {
    let body = body?;
    find_switch_target_ts(body, src)
}

fn find_switch_target_ts(node: Node, src: &[u8]) -> Option<String> {
    if node.kind() == "switch_statement"
        && let Some(value) = node.child_by_field_name("value")
    {
        return Some(node_text(value, src).to_string());
    }
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if let Some(t) = find_switch_target_ts(child, src) {
            return Some(t);
        }
    }
    None
}

/// Count optional parameters in TS (those with ? or default value).
fn count_optional_params_ts(node: Node, src: &[u8]) -> usize {
    let Some(params) = node.child_by_field_name("parameters") else {
        return 0;
    };
    let mut count = 0;
    let mut cursor = params.walk();
    for child in params.children(&mut cursor) {
        let text = node_text(child, src);
        if text.contains('?') || child.child_by_field_name("value").is_some() {
            count += 1;
        }
    }
    count
}

/// Check if a field's type annotation is a callback collection.
/// Matches: `Function[]`, `Array<Function>`, `(() => void)[]`, `((x: T) => R)[]`
fn is_callback_collection_type_ts(field_node: Node, src: &[u8]) -> bool {
    let Some(ty) = field_node.child_by_field_name("type") else {
        // No type annotation — check initializer for array literal
        if let Some(init) = field_node.child_by_field_name("value") {
            let text = node_text(init, src);
            return text == "[]" || text.contains("new Array");
        }
        return false;
    };
    let text = node_text(ty, src);
    // Function[] or Array<Function> or (() => void)[] or ((...) => ...)[]
    (text.contains("Function") && (text.contains("[]") || text.contains("Array<")))
        || (text.contains("=>") && text.contains("[]"))
        || text.contains("Array<(")
}

/// Structural Observer detection for TS: method iterates a callback field and calls elements.
/// Pattern: `this.field.forEach(cb => cb(...))` or `for (const cb of this.field) { cb(...) }`
fn has_iterate_and_call_ts(body: Node, src: &[u8], cb_fields: &[String]) -> bool {
    if cb_fields.is_empty() {
        return false;
    }
    let mut cursor = body.walk();
    for child in body.children(&mut cursor) {
        if child.kind() == "method_definition"
            && let Some(fn_body) = child.child_by_field_name("body")
        {
            for field in cb_fields {
                let this_field = format!("this.{field}");
                if walk_for_iterate_call_ts(fn_body, src, &this_field) {
                    return true;
                }
            }
        }
    }
    false
}

fn walk_for_iterate_call_ts(node: Node, src: &[u8], this_field: &str) -> bool {
    // for (const x of this.field) { x(...) }
    if node.kind() == "for_in_statement"
        && node_text(node, src).contains(this_field)
        && let Some(loop_body) = node.child_by_field_name("body")
        && has_call_expression_ts(loop_body)
    {
        return true;
    }
    // this.field.forEach(cb => cb(...))
    if node.kind() == "call_expression" || node.kind() == "expression_statement" {
        let text = node_text(node, src);
        if text.contains(this_field) && text.contains("forEach") {
            return true;
        }
    }
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if walk_for_iterate_call_ts(child, src, this_field) {
            return true;
        }
    }
    false
}

fn has_call_expression_ts(node: Node) -> bool {
    if node.kind() == "call_expression" {
        return true;
    }
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        if has_call_expression_ts(child) {
            return true;
        }
    }
    false
}