git-prism 0.8.0

Agent-optimized git data MCP server — structured change manifests and full file snapshots for LLM agents
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
use super::{CallSite, Function, LanguageAnalyzer, MAX_RECURSION_DEPTH, body_hash_for_node};
use tree_sitter::Parser;

#[derive(Debug, Clone, Copy)]
enum JsDialect {
    TypeScript,
    Tsx,
    JavaScript,
}

pub struct TypeScriptAnalyzer {
    dialect: JsDialect,
}

impl TypeScriptAnalyzer {
    pub fn typescript() -> Self {
        Self {
            dialect: JsDialect::TypeScript,
        }
    }

    pub fn tsx() -> Self {
        Self {
            dialect: JsDialect::Tsx,
        }
    }

    pub fn javascript() -> Self {
        Self {
            dialect: JsDialect::JavaScript,
        }
    }

    fn create_parser(&self) -> Parser {
        let mut parser = Parser::new();
        let language = match self.dialect {
            JsDialect::TypeScript => tree_sitter_typescript::LANGUAGE_TYPESCRIPT.into(),
            JsDialect::Tsx => tree_sitter_typescript::LANGUAGE_TSX.into(),
            JsDialect::JavaScript => tree_sitter_javascript::LANGUAGE.into(),
        };
        parser
            .set_language(&language)
            .expect("Error loading grammar");
        parser
    }
}

fn signature_text(source: &[u8], node: &tree_sitter::Node) -> String {
    let start = node.start_byte();
    let body = node.child_by_field_name("body");
    let end = body.map_or(node.end_byte(), |b| b.start_byte());
    let raw = &source[start..end];
    String::from_utf8_lossy(raw).trim().to_string()
}

fn extract_functions_from_node(
    source: &[u8],
    node: &tree_sitter::Node,
    class_name: Option<&str>,
    functions: &mut Vec<Function>,
    depth: usize,
    language: &str,
) {
    if depth >= MAX_RECURSION_DEPTH {
        // defense-in-depth: unreachable with current tree-sitter-typescript grammar
        // (stacked export_statement nodes collapse during error recovery), retained
        // for future grammar changes
        tracing::warn!(
            depth_limit = MAX_RECURSION_DEPTH,
            language = language,
            operation = "functions",
            "tree-sitter depth guard fired: recursive walk truncated; some functions may be missing"
        );
        return;
    }
    let mut cursor = node.walk();
    for child in node.children(&mut cursor) {
        match child.kind() {
            "function_declaration" => {
                let name = child
                    .child_by_field_name("name")
                    .and_then(|n| n.utf8_text(source).ok())
                    .unwrap_or("")
                    .to_string();
                let signature = signature_text(source, &child);
                let body_hash = body_hash_for_node(source, child);
                functions.push(Function {
                    name,
                    signature,
                    start_line: child.start_position().row + 1,
                    end_line: child.end_position().row + 1,
                    body_hash,
                });
            }
            "method_definition" => {
                let method_name = child
                    .child_by_field_name("name")
                    .and_then(|n| n.utf8_text(source).ok())
                    .unwrap_or("");
                let name = match class_name {
                    Some(cls) => format!("{cls}.{method_name}"),
                    None => method_name.to_string(),
                };
                let signature = signature_text(source, &child);
                let body_hash = body_hash_for_node(source, child);
                functions.push(Function {
                    name,
                    signature,
                    start_line: child.start_position().row + 1,
                    end_line: child.end_position().row + 1,
                    body_hash,
                });
            }
            "class_declaration" => {
                let cls_name = child
                    .child_by_field_name("name")
                    .and_then(|n| n.utf8_text(source).ok())
                    .unwrap_or("");
                if let Some(body) = child.child_by_field_name("body") {
                    // cargo-mutants: skip -- equivalent mutant: `depth + 1` → `depth * 1`
                    // is observably identical here. Per the file-level comment on the
                    // depth guard, tree-sitter-typescript error-recovers nested classes
                    // to ERROR nodes rather than nested `class_declaration`, so the
                    // recursion depth here cannot reach MAX_RECURSION_DEPTH for any
                    // valid or malformed input. The depth guard exists for defense-in-depth
                    // against future grammar changes; this is the corresponding mutant.
                    extract_functions_from_node(
                        source,
                        &body,
                        Some(cls_name),
                        functions,
                        depth + 1,
                        language,
                    );
                }
            }
            "lexical_declaration" => {
                // Handle named arrow functions: const foo = () => {}
                let mut decl_cursor = child.walk();
                for decl_child in child.children(&mut decl_cursor) {
                    if decl_child.kind() == "variable_declarator"
                        && let Some(arrow_node) = decl_child
                            .child_by_field_name("value")
                            .filter(|v| v.kind() == "arrow_function")
                    {
                        let fn_name = decl_child
                            .child_by_field_name("name")
                            .and_then(|n| n.utf8_text(source).ok())
                            .unwrap_or("");
                        let signature = signature_text(source, &child);
                        let body_hash = body_hash_for_node(source, arrow_node);
                        functions.push(Function {
                            name: fn_name.to_string(),
                            signature,
                            start_line: child.start_position().row + 1,
                            end_line: arrow_node.end_position().row + 1,
                            body_hash,
                        });
                    }
                }
            }
            "export_statement" => {
                // cargo-mutants: skip -- equivalent mutant: `depth + 1` → `depth * 1`
                // is observably identical here. Per the file-level comment on the
                // depth guard, stacked `export` keywords error-recover to a single
                // `export_statement`, not to recursively nested ones. The depth
                // counter cannot reach MAX_RECURSION_DEPTH from this arm for any
                // valid or malformed input.
                extract_functions_from_node(
                    source,
                    &child,
                    class_name,
                    functions,
                    depth + 1,
                    language,
                );
            }
            _ => {}
        }
    }
}

impl LanguageAnalyzer for TypeScriptAnalyzer {
    fn extract_functions(&self, source: &[u8]) -> anyhow::Result<Vec<Function>> {
        let mut parser = self.create_parser();
        let tree = parser
            .parse(source, None)
            .ok_or_else(|| anyhow::anyhow!("Failed to parse source"))?;
        let root = tree.root_node();
        let mut functions = Vec::new();
        let language = match self.dialect {
            JsDialect::TypeScript => "typescript",
            JsDialect::Tsx => "tsx",
            JsDialect::JavaScript => "javascript",
        };
        extract_functions_from_node(source, &root, None, &mut functions, 0, language);
        Ok(functions)
    }

    fn extract_calls(&self, source: &[u8]) -> anyhow::Result<Vec<CallSite>> {
        let mut parser = self.create_parser();
        let tree = parser
            .parse(source, None)
            .ok_or_else(|| anyhow::anyhow!("Failed to parse source"))?;

        let mut calls = Vec::new();
        let mut stack = vec![tree.root_node()];
        while let Some(node) = stack.pop() {
            if node.kind() == "call_expression"
                && let Some(func) = node.child_by_field_name("function")
            {
                let callee = func.utf8_text(source).unwrap_or("").to_string();
                let (is_method_call, receiver) = match func.kind() {
                    "member_expression" => {
                        let recv = func
                            .child_by_field_name("object")
                            .and_then(|n| n.utf8_text(source).ok())
                            .map(|s| s.to_string());
                        (true, recv)
                    }
                    _ => (false, None),
                };
                calls.push(CallSite {
                    callee,
                    line: node.start_position().row + 1,
                    is_method_call,
                    receiver,
                });
            }
            for i in (0..node.child_count()).rev() {
                if let Some(child) = node.child(i as u32) {
                    stack.push(child);
                }
            }
        }
        calls.sort_by_key(|c| c.line);
        Ok(calls)
    }

    fn extract_imports(&self, source: &[u8]) -> anyhow::Result<Vec<String>> {
        let mut parser = self.create_parser();
        let tree = parser
            .parse(source, None)
            .ok_or_else(|| anyhow::anyhow!("Failed to parse source"))?;
        let root = tree.root_node();
        let mut imports = Vec::new();

        let mut cursor = root.walk();
        for child in root.children(&mut cursor) {
            if child.kind() == "import_statement" {
                let text = child.utf8_text(source).unwrap_or("").trim().to_string();
                imports.push(text);
            }
        }

        Ok(imports)
    }
}

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

    #[test]
    fn extracts_simple_function() {
        let source = br#"function greet(name: string): void {
    console.log(name);
}
"#;
        let analyzer = TypeScriptAnalyzer::typescript();
        let functions = analyzer.extract_functions(source).unwrap();
        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, "greet");
        assert_eq!(functions[0].signature, "function greet(name: string): void");
        assert_eq!(functions[0].start_line, 1);
        assert_eq!(functions[0].end_line, 3);
    }

    #[test]
    fn extracts_class_methods() {
        let source = br#"class Greeter {
    greet(name: string): void {
        console.log(name);
    }

    farewell(): void {
        console.log("bye");
    }
}
"#;
        let analyzer = TypeScriptAnalyzer::typescript();
        let functions = analyzer.extract_functions(source).unwrap();
        assert_eq!(functions.len(), 2);
        assert_eq!(functions[0].name, "Greeter.greet");
        assert_eq!(functions[0].signature, "greet(name: string): void");
        assert_eq!(functions[1].name, "Greeter.farewell");
    }

    #[test]
    fn extracts_named_arrow_function() {
        let source = br#"const add = (a: number, b: number): number => {
    return a + b;
};
"#;
        let analyzer = TypeScriptAnalyzer::typescript();
        let functions = analyzer.extract_functions(source).unwrap();
        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, "add");
        assert_eq!(functions[0].start_line, 1);
        assert_eq!(functions[0].end_line, 3);
    }

    #[test]
    fn empty_file_returns_no_functions() {
        let source = b"";
        let analyzer = TypeScriptAnalyzer::typescript();
        let functions = analyzer.extract_functions(source).unwrap();
        assert!(functions.is_empty());
    }

    #[test]
    fn extracts_es_imports() {
        let source = br#"import { foo, bar } from './utils';
import * as path from 'path';
"#;
        let analyzer = TypeScriptAnalyzer::typescript();
        let imports = analyzer.extract_imports(source).unwrap();
        assert_eq!(imports.len(), 2);
        assert_eq!(imports[0], "import { foo, bar } from './utils';");
        assert_eq!(imports[1], "import * as path from 'path';");
    }

    #[test]
    fn no_imports_returns_empty() {
        let source = br#"function hello() {
    return 1;
}
"#;
        let analyzer = TypeScriptAnalyzer::typescript();
        let imports = analyzer.extract_imports(source).unwrap();
        assert!(imports.is_empty());
    }

    #[test]
    fn handles_javascript_syntax() {
        let source = br#"function greet(name) {
    console.log(name);
}
"#;
        let analyzer = TypeScriptAnalyzer::javascript();
        let functions = analyzer.extract_functions(source).unwrap();
        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, "greet");
        assert_eq!(functions[0].signature, "function greet(name)");
    }

    #[test]
    fn javascript_dialect_parses_commonjs_require() {
        let source = br#"const fs = require('fs');
const path = require('path');

function readFile(name) {
    return fs.readFileSync(name);
}
"#;
        let analyzer = TypeScriptAnalyzer::javascript();
        let functions = analyzer.extract_functions(source).unwrap();
        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, "readFile");
    }

    #[test]
    fn tsx_dialect_parses_jsx_component() {
        let source = br#"import React from 'react';

function App(): JSX.Element {
    return <div>Hello</div>;
}
"#;
        let analyzer = TypeScriptAnalyzer::tsx();
        let functions = analyzer.extract_functions(source).unwrap();
        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, "App");
    }

    #[test]
    fn javascript_dialect_extracts_function_expression() {
        let source = br#"var greet = function(name) {
    console.log(name);
};
"#;
        let analyzer = TypeScriptAnalyzer::javascript();
        let functions = analyzer.extract_functions(source).unwrap();
        // var declarations with function expressions are not named arrow functions,
        // so they won't be extracted (consistent with current behavior)
        assert!(functions.is_empty());
    }

    #[test]
    fn tsx_dialect_extracts_imports() {
        let source = br#"import React from 'react';
import { useState } from 'react';
"#;
        let analyzer = TypeScriptAnalyzer::tsx();
        let imports = analyzer.extract_imports(source).unwrap();
        assert_eq!(imports.len(), 2);
        assert_eq!(imports[0], "import React from 'react';");
        assert_eq!(imports[1], "import { useState } from 'react';");
    }

    #[test]
    fn typescript_dialect_parses_typed_code() {
        let source = br#"function add(a: number, b: number): number {
    return a + b;
}
"#;
        let analyzer = TypeScriptAnalyzer::typescript();
        let functions = analyzer.extract_functions(source).unwrap();
        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, "add");
        assert_eq!(
            functions[0].signature,
            "function add(a: number, b: number): number"
        );
    }

    // Kill mutants: replace + with * or - in method_definition line number arithmetic (row + 1).
    // Method at row > 0 ensures row+1 != row*1 and row+1 != row-1.
    #[test]
    fn it_reports_correct_line_numbers_for_method_definition() {
        let source = b"// comment line 1
// comment line 2

class Greeter {
    greet(name: string): void {
        console.log(name);
    }
}
";
        let analyzer = TypeScriptAnalyzer::typescript();
        let functions = analyzer.extract_functions(source).unwrap();
        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, "Greeter.greet");
        assert_eq!(functions[0].start_line, 5);
        assert_eq!(functions[0].end_line, 7);
    }

    #[test]
    fn extracts_simple_calls() {
        let source = br#"function main() {
    const x = foo();
    const y = bar(x);
    baz(x, y);
}
"#;
        let analyzer = TypeScriptAnalyzer::typescript();
        let calls = analyzer.extract_calls(source).unwrap();
        let callees: Vec<&str> = calls.iter().map(|c| c.callee.as_str()).collect();
        assert_eq!(callees, vec!["foo", "bar", "baz"]);
        assert!(calls.iter().all(|c| !c.is_method_call));
    }

    #[test]
    fn extracts_method_calls() {
        let source = br#"function process(server: Server) {
    server.start();
    server.handleRequest();
}
"#;
        let analyzer = TypeScriptAnalyzer::typescript();
        let calls = analyzer.extract_calls(source).unwrap();
        let callees: Vec<&str> = calls.iter().map(|c| c.callee.as_str()).collect();
        assert_eq!(callees, vec!["server.start", "server.handleRequest"]);
        assert!(calls.iter().all(|c| c.is_method_call));
        assert_eq!(calls[0].receiver.as_deref(), Some("server"));
    }

    #[test]
    fn extracts_console_log() {
        let source = br#"function example() {
    console.log("hello");
    Array.from([1, 2]);
}
"#;
        let analyzer = TypeScriptAnalyzer::typescript();
        let calls = analyzer.extract_calls(source).unwrap();
        let callees: Vec<&str> = calls.iter().map(|c| c.callee.as_str()).collect();
        assert!(callees.contains(&"console.log"));
        assert!(callees.contains(&"Array.from"));
    }

    #[test]
    fn extracts_calls_inside_callbacks() {
        let source = br#"function example() {
    setTimeout(() => {
        doWork();
    }, 1000);
}
"#;
        let analyzer = TypeScriptAnalyzer::typescript();
        let calls = analyzer.extract_calls(source).unwrap();
        let callees: Vec<&str> = calls.iter().map(|c| c.callee.as_str()).collect();
        assert!(callees.contains(&"setTimeout"));
        assert!(callees.contains(&"doWork"));
    }

    #[test]
    fn javascript_extracts_calls() {
        let source = br#"function main() {
    const result = calculate(input);
    console.log(result);
}
"#;
        let analyzer = TypeScriptAnalyzer::javascript();
        let calls = analyzer.extract_calls(source).unwrap();
        let callees: Vec<&str> = calls.iter().map(|c| c.callee.as_str()).collect();
        assert!(callees.contains(&"calculate"));
        assert!(callees.contains(&"console.log"));
    }

    #[test]
    fn empty_file_returns_no_calls() {
        let source = b"";
        let analyzer = TypeScriptAnalyzer::typescript();
        let calls = analyzer.extract_calls(source).unwrap();
        assert!(calls.is_empty());
    }

    // Kill extract_calls line-offset mutants (+ with - or *) at line 212.
    // Calls on lines 2, 3, 4 distinguish `row + 1` from `row * 1` and `row - 1`.
    #[test]
    fn it_reports_call_sites_on_correct_lines() {
        let source = b"function run() {
    foo();
    bar();
    baz();
}
";
        let analyzer = TypeScriptAnalyzer::typescript();
        let calls = analyzer.extract_calls(source).unwrap();
        assert_eq!(calls.len(), 3);
        assert_eq!(calls[0].callee, "foo");
        assert_eq!(calls[0].line, 2);
        assert_eq!(calls[1].callee, "bar");
        assert_eq!(calls[1].line, 3);
        assert_eq!(calls[2].callee, "baz");
        assert_eq!(calls[2].line, 4);
    }

    /// Depth-guard warning: the `tracing::warn!` in `extract_functions_from_node` is
    /// defense-in-depth for future grammar changes. With the current tree-sitter TypeScript
    /// grammar, the guard is not reachable via any syntactically meaningful input:
    ///
    /// - `class C0 { class C1 { ... } }` → tree-sitter error-recovers inner classes to
    ///   ERROR nodes; the `class_declaration` arm is never matched inside a class body.
    /// - `export export export class C {}` → extra `export` keywords become a single
    ///   ERROR node; nested `export_statement` nodes are never produced.
    /// - TypeScript namespaces parse as `internal_module > statement_block`; the `_` arm
    ///   does not recurse, so namespace depth never increments the recursion counter.
    ///
    /// The warn! is present for when a future grammar version produces such nesting.
    /// The triangulation test below confirms shallow input never emits the warning.
    /// Triangulation: shallow input must NOT emit the depth-guard warning.
    #[test]
    #[traced_test]
    fn it_does_not_emit_depth_guard_warning_on_shallow_input() {
        let source = b"export class Foo { bar(): void {} }\n";
        let analyzer = TypeScriptAnalyzer::typescript();
        let _ = analyzer.extract_functions(source);
        assert!(!logs_contain("depth guard fired"));
    }

    /// Verifies that the `language` parameter is correctly threaded through to all three
    /// JS dialects. The TSX and JavaScript analyzers share the same `extract_functions_from_node`
    /// free function as TypeScript — this test confirms the `language` parameter wiring
    /// compiles and that each dialect passes a distinct string. Since the depth guard is
    /// unreachable via the current grammar for all three dialects, we test the negative
    /// case (no warning emitted for shallow input) for the tsx dialect here.
    #[test]
    #[traced_test]
    fn tsx_dialect_does_not_emit_depth_guard_warning_on_shallow_input() {
        let source = b"export function App(): JSX.Element { return null; }\n";
        let analyzer = TypeScriptAnalyzer::tsx();
        let _ = analyzer.extract_functions(source);
        assert!(!logs_contain("depth guard fired"));
    }

    /// Defense-in-depth: deeply-nested TypeScript export_statement and class_declaration
    /// bodies are guarded by `MAX_RECURSION_DEPTH`. The two recursion sites in
    /// `extract_functions_from_node` are `class_declaration` body and `export_statement`.
    ///
    /// Investigation per the plan: valid TypeScript syntax does not allow
    /// `class A { class B {} }` at the declaration level, and the tree-sitter
    /// TypeScript grammar's error recovery produces ERROR nodes (not nested
    /// `class_declaration` nodes) for malformed input. Similarly, stacked `export`
    /// keywords are parse errors and error-recover to a single `export_statement`
    /// wrapping the inner tokens, not to recursively nested `export_statement` nodes.
    /// Because the walker's match is explicit (`"export_statement"` and `"class_declaration"`),
    /// ERROR-kind nodes are invisible to the recursion path.
    ///
    /// Guard added for defense-in-depth and consistency. This test verifies the
    /// walker completes without crashing on a deeply-nested (but grammar-limited)
    /// export chain, and that extraction still works correctly.
    #[test]
    fn it_completes_without_overflow_on_deeply_stacked_export_keywords() {
        const GENERATED_NESTING_LEVELS: usize = 5000;
        const CONSTRAINED_THREAD_STACK_BYTES: usize = 2 * 1024 * 1024;

        // Stacked `export` keywords — tree-sitter error-recovers these, so
        // they don't produce nested export_statement nodes in practice.
        // The test still validates the analyzer handles the input safely.
        let mut source = String::new();
        for _ in 0..GENERATED_NESTING_LEVELS {
            source.push_str("export ");
        }
        source.push_str("class C {}\n");

        let handle = std::thread::Builder::new()
            .stack_size(CONSTRAINED_THREAD_STACK_BYTES)
            .spawn(move || {
                let analyzer = TypeScriptAnalyzer::typescript();
                analyzer.extract_functions(source.as_bytes())
            })
            .expect("spawn analyzer thread");

        let result = handle
            .join()
            .expect("analyzer thread must not stack-overflow on deeply-nested input");
        result.expect("analyzer must return Ok on deeply-nested input");
        // No assertion on function count — the parse tree shape is grammar-dependent.
    }

    /// Triangulation: sequential exported classes with methods must all extract.
    /// This confirms the guard does not fire on legitimate export_statement usage.
    /// NOTE: These are sequential (not nested) exports — each recurses at depth 1,
    /// not depth 255. This tests non-regression of the export_statement arm, not
    /// the depth-boundary property (tree-sitter error-recovers stacked `export`
    /// keywords to a single export_statement, so true depth-255 nesting via exports
    /// is not achievable with valid or error-recovered TypeScript syntax).
    #[test]
    fn it_extracts_methods_from_exported_classes() {
        const CLASS_COUNT: usize = 255;

        // Build 255 sequential (not nested) exported classes each with a method,
        // to confirm the export_statement arm still works at high volume.
        let mut source = String::new();
        for i in 0..CLASS_COUNT {
            source.push_str(&format!("export class C{i} {{ method{i}(): void {{}} }}\n"));
        }

        let analyzer = TypeScriptAnalyzer::typescript();
        let functions = analyzer.extract_functions(source.as_bytes()).unwrap();
        assert_eq!(
            functions.len(),
            CLASS_COUNT,
            "all {CLASS_COUNT} methods must be extracted"
        );
    }

    #[test]
    fn extracts_export_function() {
        let source = br#"export function greet(name: string): string {
    return `Hello, ${name}!`;
}
"#;
        let analyzer = TypeScriptAnalyzer::typescript();
        let functions = analyzer.extract_functions(source).unwrap();
        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, "greet");
    }

    #[test]
    fn extracts_export_default_function() {
        let source = br#"export default function handler(req: any): any {
    return { status: 200 };
}
"#;
        let analyzer = TypeScriptAnalyzer::typescript();
        let functions = analyzer.extract_functions(source).unwrap();
        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, "handler");
    }

    #[test]
    fn extracts_export_class_methods() {
        let source = br#"export class Calculator {
    add(a: number, b: number): number {
        return a + b;
    }
}
"#;
        let analyzer = TypeScriptAnalyzer::typescript();
        let functions = analyzer.extract_functions(source).unwrap();
        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, "Calculator.add");
    }

    #[test]
    fn extracts_export_const_arrow_function() {
        let source = br#"export const add = (a: number, b: number): number => {
    return a + b;
};
"#;
        let analyzer = TypeScriptAnalyzer::typescript();
        let functions = analyzer.extract_functions(source).unwrap();
        assert_eq!(functions.len(), 1);
        assert_eq!(functions[0].name, "add");
    }
}