code-graph-cli 3.0.3

Code intelligence engine for TypeScript/JavaScript/Rust/Python/Go — query the dependency graph instead of reading source files.
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
use std::sync::OnceLock;

use tree_sitter::{Language, Node, Query, QueryCursor, StreamingIterator, Tree};

// ---------------------------------------------------------------------------
// Data structures
// ---------------------------------------------------------------------------

/// The kind of import statement.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ImportKind {
    /// ESM static import: `import { X } from './module'`
    Esm,
    /// CommonJS require: `const X = require('./module')`
    Cjs,
    /// Dynamic import: `import('./module')`
    DynamicImport,
    /// Python absolute import: `import os` or `from pkg import name`
    PythonAbsolute,
    /// Python relative import: `from . import X` (level=1) or `from ..pkg import Y` (level=2)
    PythonRelative { level: usize },
    /// Python conditional absolute import (inside try/except block): `try: from fast import X`
    PythonConditionalAbsolute,
    /// Python conditional relative import (inside try/except block): `try: from . import X`
    PythonConditionalRelative { level: usize },
    /// Go absolute import: `import "pkg"`.
    GoAbsolute,
    /// Go blank import: `import _ "pkg"` — side-effect only.
    GoBlank,
    /// Go dot import: `import . "pkg"` — all names imported.
    GoDot,
}

/// A single imported name from a module.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ImportSpecifier {
    /// The local name used in this file.
    pub name: String,
    /// The alias (original name) when using `import { original as alias }`.
    pub alias: Option<String>,
    /// True for `import React from 'react'` (default import).
    pub is_default: bool,
    /// True for `import * as ns from 'module'` (namespace import).
    pub is_namespace: bool,
}

/// An import extracted from a source file.
#[derive(Debug, Clone)]
pub struct ImportInfo {
    /// Kind of import (ESM / CJS / dynamic / Python).
    pub kind: ImportKind,
    /// The raw module specifier string, e.g. `"react"` or `"./utils"`.
    pub module_path: String,
    /// The names imported from the module.
    pub specifiers: Vec<ImportSpecifier>,
    /// 1-based line number where the import statement begins.
    /// 0 for imports extracted from older code paths that do not set this field.
    /// Used by Python import extraction (Plan 17-02); consumed by pipeline in Plan 17-03.
    #[allow(dead_code)]
    pub line: usize,
}

/// The kind of export statement.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExportKind {
    /// `export { Foo, Bar }`
    Named,
    /// `export default X`
    Default,
    /// `export { X } from './module'`
    ReExport,
    /// `export * from './module'`
    ReExportAll,
}

/// An export extracted from a source file.
#[derive(Debug, Clone)]
pub struct ExportInfo {
    /// Kind of export.
    pub kind: ExportKind,
    /// The names being exported (empty for Default and ReExportAll).
    pub names: Vec<String>,
    /// The source module for re-exports.
    pub source: Option<String>,
}

// ---------------------------------------------------------------------------
// Query strings
// ---------------------------------------------------------------------------

/// Tree-sitter query for ESM static imports.
/// Matches `import { X } from 'module'`, `import X from 'module'`, `import * as X from 'module'`.
const IMPORT_QUERY_TS: &str = r#"
    (import_statement
      source: (string (string_fragment) @module_path)) @import
"#;

/// Tree-sitter query for CJS require calls.
/// Note: we do not use #eq? predicate here because tree-sitter 0.26 StreamingIterator
/// does not auto-filter custom predicates. We filter for "require" in code instead.
const REQUIRE_QUERY: &str = r#"
    (call_expression
      function: (identifier) @fn
      arguments: (arguments (string (string_fragment) @module_path)))
"#;

/// Tree-sitter query for dynamic import() calls.
const DYNAMIC_IMPORT_QUERY: &str = r#"
    (call_expression
      function: (import)
      arguments: (arguments (string (string_fragment) @module_path))) @dynamic_import
"#;

/// Tree-sitter query for export statements.
const EXPORT_QUERY: &str = r#"
    (export_statement) @export_stmt
"#;

// ---------------------------------------------------------------------------
// Query cache — one set of statics per grammar (TS / TSX / JS).
// Queries are grammar-specific: a Query compiled for one grammar cannot be
// used against a tree parsed with a different grammar.
// ---------------------------------------------------------------------------

// TypeScript (.ts)
static TS_IMPORT_QUERY: OnceLock<Query> = OnceLock::new();
static TS_REQUIRE_QUERY: OnceLock<Query> = OnceLock::new();
static TS_DYNAMIC_QUERY: OnceLock<Query> = OnceLock::new();
static TS_EXPORT_QUERY: OnceLock<Query> = OnceLock::new();

// TypeScript-TSX (.tsx / .jsx)
static TSX_IMPORT_QUERY: OnceLock<Query> = OnceLock::new();
static TSX_REQUIRE_QUERY: OnceLock<Query> = OnceLock::new();
static TSX_DYNAMIC_QUERY: OnceLock<Query> = OnceLock::new();
static TSX_EXPORT_QUERY: OnceLock<Query> = OnceLock::new();

// JavaScript (.js)
static JS_IMPORT_QUERY: OnceLock<Query> = OnceLock::new();
static JS_REQUIRE_QUERY: OnceLock<Query> = OnceLock::new();
static JS_DYNAMIC_QUERY: OnceLock<Query> = OnceLock::new();
static JS_EXPORT_QUERY: OnceLock<Query> = OnceLock::new();

/// Which language group a file falls into.
/// Note: `Language::name()` returns `None` for TypeScript/TSX grammars in tree-sitter 0.26.
/// We therefore use `is_tsx` (derived from file extension) for TS vs TSX discrimination,
/// and `language.name() == Some("javascript")` only for the JavaScript grammar check.
/// This mirrors the pattern established in symbols.rs.
enum LangGroup {
    TypeScript,
    Tsx,
    JavaScript,
}

fn lang_group(language: &Language, is_tsx: bool) -> LangGroup {
    // JavaScript grammar reliably returns Some("javascript") from name().
    // TypeScript and TSX grammars may return None; use is_tsx flag instead.
    match language.name().unwrap_or("") {
        "javascript" => LangGroup::JavaScript,
        _ => {
            if is_tsx {
                LangGroup::Tsx
            } else {
                LangGroup::TypeScript
            }
        }
    }
}

fn import_query(language: &Language, is_tsx: bool) -> &'static Query {
    match lang_group(language, is_tsx) {
        LangGroup::TypeScript => TS_IMPORT_QUERY.get_or_init(|| {
            Query::new(language, IMPORT_QUERY_TS).expect("invalid TS import query")
        }),
        LangGroup::Tsx => TSX_IMPORT_QUERY.get_or_init(|| {
            Query::new(language, IMPORT_QUERY_TS).expect("invalid TSX import query")
        }),
        LangGroup::JavaScript => JS_IMPORT_QUERY.get_or_init(|| {
            Query::new(language, IMPORT_QUERY_TS).expect("invalid JS import query")
        }),
    }
}

fn require_query(language: &Language, is_tsx: bool) -> &'static Query {
    match lang_group(language, is_tsx) {
        LangGroup::TypeScript => TS_REQUIRE_QUERY
            .get_or_init(|| Query::new(language, REQUIRE_QUERY).expect("invalid TS require query")),
        LangGroup::Tsx => TSX_REQUIRE_QUERY.get_or_init(|| {
            Query::new(language, REQUIRE_QUERY).expect("invalid TSX require query")
        }),
        LangGroup::JavaScript => JS_REQUIRE_QUERY
            .get_or_init(|| Query::new(language, REQUIRE_QUERY).expect("invalid JS require query")),
    }
}

fn dynamic_import_query(language: &Language, is_tsx: bool) -> &'static Query {
    match lang_group(language, is_tsx) {
        LangGroup::TypeScript => TS_DYNAMIC_QUERY.get_or_init(|| {
            Query::new(language, DYNAMIC_IMPORT_QUERY).expect("invalid TS dynamic import query")
        }),
        LangGroup::Tsx => TSX_DYNAMIC_QUERY.get_or_init(|| {
            Query::new(language, DYNAMIC_IMPORT_QUERY).expect("invalid TSX dynamic import query")
        }),
        LangGroup::JavaScript => JS_DYNAMIC_QUERY.get_or_init(|| {
            Query::new(language, DYNAMIC_IMPORT_QUERY).expect("invalid JS dynamic import query")
        }),
    }
}

fn export_query(language: &Language, is_tsx: bool) -> &'static Query {
    match lang_group(language, is_tsx) {
        LangGroup::TypeScript => TS_EXPORT_QUERY
            .get_or_init(|| Query::new(language, EXPORT_QUERY).expect("invalid TS export query")),
        LangGroup::Tsx => TSX_EXPORT_QUERY
            .get_or_init(|| Query::new(language, EXPORT_QUERY).expect("invalid TSX export query")),
        LangGroup::JavaScript => JS_EXPORT_QUERY
            .get_or_init(|| Query::new(language, EXPORT_QUERY).expect("invalid JS export query")),
    }
}

// ---------------------------------------------------------------------------
// Helper utilities
// ---------------------------------------------------------------------------

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

// ---------------------------------------------------------------------------
// Import extraction
// ---------------------------------------------------------------------------

/// Extract all ESM specifiers from an import_statement node.
///
/// Handles:
/// - Named: `import { useState, useEffect } from 'react'`
/// - Default: `import React from 'react'`
/// - Namespace: `import * as path from 'path'`
/// - Combined: `import React, { useState } from 'react'`
fn extract_esm_specifiers(import_node: Node, source: &[u8]) -> Vec<ImportSpecifier> {
    let mut specifiers = Vec::new();

    // Walk direct children of the import_statement to find import clauses.
    let mut cursor = import_node.walk();
    for child in import_node.children(&mut cursor) {
        match child.kind() {
            "import_clause" => {
                extract_import_clause(child, source, &mut specifiers);
            }
            "namespace_import" => {
                // `import * as ns from 'module'` (direct child, rare but handle it)
                if let Some(name) = extract_namespace_import_name(child, source) {
                    specifiers.push(ImportSpecifier {
                        name,
                        alias: None,
                        is_default: false,
                        is_namespace: true,
                    });
                }
            }
            _ => {}
        }
    }

    specifiers
}

/// Recursively extract specifiers from an `import_clause` node.
fn extract_import_clause(clause_node: Node, source: &[u8], specifiers: &mut Vec<ImportSpecifier>) {
    let mut cursor = clause_node.walk();
    for child in clause_node.children(&mut cursor) {
        match child.kind() {
            "identifier" => {
                // Default import: `import React from ...`
                specifiers.push(ImportSpecifier {
                    name: node_text(child, source).to_owned(),
                    alias: None,
                    is_default: true,
                    is_namespace: false,
                });
            }
            "named_imports" => {
                // Named: `{ useState, useEffect as UE }`
                extract_named_imports(child, source, specifiers);
            }
            "namespace_import" => {
                // `* as ns` — the identifier has no field name, just find the identifier child
                if let Some(name) = extract_namespace_import_name(child, source) {
                    specifiers.push(ImportSpecifier {
                        name,
                        alias: None,
                        is_default: false,
                        is_namespace: true,
                    });
                }
            }
            _ => {}
        }
    }
}

/// Extract the identifier name from a `namespace_import` node (`* as identifier`).
/// The identifier is not assigned a field name in the grammar — find it by kind.
fn extract_namespace_import_name(ns_node: Node, source: &[u8]) -> Option<String> {
    let mut cursor = ns_node.walk();
    for child in ns_node.children(&mut cursor) {
        if child.kind() == "identifier" {
            return Some(node_text(child, source).to_owned());
        }
    }
    None
}

/// Extract individual import_specifier nodes from a named_imports node.
fn extract_named_imports(
    named_imports_node: Node,
    source: &[u8],
    specifiers: &mut Vec<ImportSpecifier>,
) {
    let mut cursor = named_imports_node.walk();
    for child in named_imports_node.children(&mut cursor) {
        if child.kind() == "import_specifier" {
            // `name` field: the local binding name
            // `alias` field in tree-sitter is actually the "name" field (what was exported)
            // In `import { foo as bar }`: name="foo", alias="bar"
            // tree-sitter field: name -> original, alias -> local
            let name_node = child.child_by_field_name("name");
            let alias_node = child.child_by_field_name("alias");

            match (name_node, alias_node) {
                (Some(n), Some(a)) => {
                    // `import { foo as bar }` — n="foo", a="bar"
                    specifiers.push(ImportSpecifier {
                        name: node_text(a, source).to_owned(),        // local binding
                        alias: Some(node_text(n, source).to_owned()), // original name
                        is_default: false,
                        is_namespace: false,
                    });
                }
                (Some(n), None) => {
                    specifiers.push(ImportSpecifier {
                        name: node_text(n, source).to_owned(),
                        alias: None,
                        is_default: false,
                        is_namespace: false,
                    });
                }
                _ => {}
            }
        }
    }
}

/// Find the variable name from a CJS require statement's parent variable_declarator.
/// e.g. `const fs = require('fs')` → "fs"
fn find_require_binding(call_node: Node, source: &[u8]) -> Option<String> {
    // Walk up to variable_declarator
    let mut current = call_node.parent();
    while let Some(n) = current {
        if n.kind() == "variable_declarator" {
            if let Some(name_node) = n.child_by_field_name("name") {
                return Some(node_text(name_node, source).to_owned());
            }
            break;
        }
        current = n.parent();
    }
    None
}

/// Extract all imports (ESM, CJS, dynamic) from a parsed syntax tree.
///
/// `is_tsx` must be `true` for `.tsx` and `.jsx` files — used to select the correct
/// per-grammar query cache. This mirrors the `is_tsx` convention from `extract_symbols`.
pub fn extract_imports(
    tree: &Tree,
    source: &[u8],
    language: &Language,
    is_tsx: bool,
) -> Vec<ImportInfo> {
    let mut imports = Vec::new();

    // --- ESM static imports ---
    {
        let query = import_query(language, is_tsx);
        let module_path_idx = query
            .capture_index_for_name("module_path")
            .expect("import query must have @module_path");
        let import_idx = query
            .capture_index_for_name("import")
            .expect("import query must have @import");

        let mut cursor = QueryCursor::new();
        let mut matches = cursor.matches(query, tree.root_node(), source);

        while let Some(m) = matches.next() {
            let mut import_node: Option<Node> = None;
            let mut module_path: Option<String> = None;

            for capture in m.captures {
                if capture.index == import_idx {
                    import_node = Some(capture.node);
                } else if capture.index == module_path_idx {
                    module_path = Some(node_text(capture.node, source).to_owned());
                }
            }

            if let (Some(imp_node), Some(path)) = (import_node, module_path) {
                let specifiers = extract_esm_specifiers(imp_node, source);
                imports.push(ImportInfo {
                    kind: ImportKind::Esm,
                    module_path: path,
                    specifiers,
                    line: 0,
                });
            }
        }
    }

    // --- CJS require() calls ---
    {
        // The require query matches ALL call_expression(identifier, ...) patterns.
        // We filter for "require" in code (tree-sitter 0.26 StreamingIterator does not
        // auto-apply #eq? predicates).
        let query = require_query(language, is_tsx);
        let module_path_idx = match query.capture_index_for_name("module_path") {
            Some(idx) => idx,
            None => {
                // query compiled without expected captures — skip CJS
                return imports;
            }
        };
        let fn_idx = query.capture_index_for_name("fn");

        let mut cursor = QueryCursor::new();
        let mut matches = cursor.matches(query, tree.root_node(), source);

        while let Some(m) = matches.next() {
            let mut module_path: Option<String> = None;
            let mut call_node: Option<Node> = None;
            let mut fn_name: Option<String> = None;

            for capture in m.captures {
                if capture.index == module_path_idx {
                    module_path = Some(node_text(capture.node, source).to_owned());
                    call_node = Some(capture.node);
                } else if fn_idx == Some(capture.index) {
                    fn_name = Some(node_text(capture.node, source).to_owned());
                }
            }

            // Only process calls to `require(...)`, not arbitrary identifier calls.
            if fn_name.as_deref() != Some("require") {
                continue;
            }

            if let Some(path) = module_path {
                // Walk up to find the call_expression node for binding detection.
                let call_expr = call_node.and_then(|n| {
                    let mut c = Some(n);
                    while let Some(node) = c {
                        if node.kind() == "call_expression" {
                            return Some(node);
                        }
                        c = node.parent();
                    }
                    None
                });

                let mut specifiers = Vec::new();
                if let Some(call) = call_expr
                    && let Some(binding) = find_require_binding(call, source)
                {
                    specifiers.push(ImportSpecifier {
                        name: binding,
                        alias: None,
                        is_default: false,
                        is_namespace: false,
                    });
                }

                imports.push(ImportInfo {
                    kind: ImportKind::Cjs,
                    module_path: path,
                    specifiers,
                    line: 0,
                });
            }
        }
    }

    // --- Dynamic import() calls ---
    {
        let query = dynamic_import_query(language, is_tsx);
        let module_path_idx = query
            .capture_index_for_name("module_path")
            .expect("dynamic import query must have @module_path");

        let mut cursor = QueryCursor::new();
        let mut matches = cursor.matches(query, tree.root_node(), source);

        while let Some(m) = matches.next() {
            let mut module_path: Option<String> = None;

            for capture in m.captures {
                if capture.index == module_path_idx {
                    module_path = Some(node_text(capture.node, source).to_owned());
                }
            }

            if let Some(path) = module_path {
                imports.push(ImportInfo {
                    kind: ImportKind::DynamicImport,
                    module_path: path,
                    specifiers: Vec::new(),
                    line: 0,
                });
            }
        }
    }

    imports
}

// ---------------------------------------------------------------------------
// Export extraction
// ---------------------------------------------------------------------------

// ---------------------------------------------------------------------------
// Rust use declaration extraction
// ---------------------------------------------------------------------------

/// Extract all Rust `use` and `pub use` declarations from a parsed syntax tree.
///
/// Returns a `Vec<RustUseInfo>` with the raw use path string and `is_pub_use` flag.
/// Phase 8 stores raw source text; Phase 9 handles use-tree expansion.
pub fn extract_rust_use(tree: &Tree, source: &[u8]) -> Vec<crate::parser::RustUseInfo> {
    let mut results = Vec::new();
    let root = tree.root_node();

    let mut cursor = root.walk();
    for child in root.children(&mut cursor) {
        if child.kind() != "use_declaration" {
            continue;
        }

        // Check for pub visibility modifier
        let is_pub_use = {
            let mut c = child.walk();
            child
                .children(&mut c)
                .any(|n| n.kind() == "visibility_modifier")
        };

        // Extract the use path from the "argument" field
        let path = match child.child_by_field_name("argument") {
            Some(arg_node) => node_text(arg_node, source).to_owned(),
            None => continue,
        };

        results.push(crate::parser::RustUseInfo { path, is_pub_use });
    }

    results
}

/// Extract all exports from a parsed syntax tree.
///
/// `is_tsx` must be `true` for `.tsx` and `.jsx` files — used to select the correct
/// per-grammar query cache. This mirrors the `is_tsx` convention from `extract_symbols`.
pub fn extract_exports(
    tree: &Tree,
    source: &[u8],
    language: &Language,
    is_tsx: bool,
) -> Vec<ExportInfo> {
    let mut exports = Vec::new();

    let query = export_query(language, is_tsx);
    let export_stmt_idx = query
        .capture_index_for_name("export_stmt")
        .expect("export query must have @export_stmt");

    let mut cursor = QueryCursor::new();
    let mut matches = cursor.matches(query, tree.root_node(), source);

    while let Some(m) = matches.next() {
        let mut export_node: Option<Node> = None;

        for capture in m.captures {
            if capture.index == export_stmt_idx {
                export_node = Some(capture.node);
            }
        }

        if let Some(node) = export_node
            && let Some(info) = classify_export(node, source)
        {
            exports.push(info);
        }
    }

    exports
}

/// Classify a single export_statement node.
fn classify_export(node: Node, source: &[u8]) -> Option<ExportInfo> {
    // Check if this is a re-export (has a `source` field).
    let source_str = find_export_source(node, source);

    // Check for wildcard export: `export * from './module'`
    let has_star = (0..node.child_count()).any(|i| {
        node.child(i as u32)
            .map(|c| c.kind() == "*")
            .unwrap_or(false)
    });

    if has_star {
        // `export * from './module'`
        return Some(ExportInfo {
            kind: ExportKind::ReExportAll,
            names: Vec::new(),
            source: source_str,
        });
    }

    // Check for export_clause (named or re-export with names).
    let export_clause = find_child_of_kind(node, "export_clause");

    if let Some(clause) = export_clause {
        let names = extract_export_clause_names(clause, source);
        if source_str.is_some() {
            // `export { X } from './module'`
            return Some(ExportInfo {
                kind: ExportKind::ReExport,
                names,
                source: source_str,
            });
        } else {
            // `export { X, Y }`
            return Some(ExportInfo {
                kind: ExportKind::Named,
                names,
                source: None,
            });
        }
    }

    // Check for `default` keyword — `export default X`
    let has_default = (0..node.child_count()).any(|i| {
        node.child(i as u32)
            .map(|c| node_text(c, source) == "default")
            .unwrap_or(false)
    });

    if has_default {
        return Some(ExportInfo {
            kind: ExportKind::Default,
            names: Vec::new(),
            source: None,
        });
    }

    // Inline export (export function/class/etc.) — skip here; symbols.rs captures these.
    None
}

/// Find the source module string from a re-export statement.
/// e.g. `export { X } from './utils'` → Some("./utils")
fn find_export_source(export_node: Node, source: &[u8]) -> Option<String> {
    let mut cursor = export_node.walk();
    for child in export_node.children(&mut cursor) {
        if child.kind() == "string" {
            // The string's first named child is string_fragment
            if let Some(frag) = child.named_child(0) {
                return Some(node_text(frag, source).to_owned());
            }
        }
    }
    None
}

/// Find the first direct child of `node` with the given kind.
fn find_child_of_kind<'a>(node: Node<'a>, kind: &str) -> Option<Node<'a>> {
    let mut cursor = node.walk();
    node.children(&mut cursor)
        .find(|child| child.kind() == kind)
}

/// Extract the exported names from an export_clause node.
fn extract_export_clause_names(clause_node: Node, source: &[u8]) -> Vec<String> {
    let mut names = Vec::new();
    let mut cursor = clause_node.walk();
    for child in clause_node.children(&mut cursor) {
        if child.kind() == "export_specifier" {
            // The `name` field holds the original name being exported.
            if let Some(name_node) = child.child_by_field_name("name") {
                names.push(node_text(name_node, source).to_owned());
            }
        }
    }
    names
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn parse_ts(source: &str) -> (tree_sitter::Tree, Language) {
        let lang = language_for_extension("ts").unwrap();
        let mut parser = tree_sitter::Parser::new();
        parser.set_language(&lang).unwrap();
        let tree = parser.parse(source.as_bytes(), None).unwrap();
        (tree, lang)
    }

    fn parse_js(source: &str) -> (tree_sitter::Tree, Language) {
        let lang = language_for_extension("js").unwrap();
        let mut parser = tree_sitter::Parser::new();
        parser.set_language(&lang).unwrap();
        let tree = parser.parse(source.as_bytes(), None).unwrap();
        (tree, lang)
    }

    fn parse_tsx(source: &str) -> (tree_sitter::Tree, Language) {
        let lang = language_for_extension("tsx").unwrap();
        let mut parser = tree_sitter::Parser::new();
        parser.set_language(&lang).unwrap();
        let tree = parser.parse(source.as_bytes(), None).unwrap();
        (tree, lang)
    }

    // Test 1: ESM named imports
    #[test]
    fn test_esm_named_imports() {
        let src = "import { useState, useEffect } from 'react';";
        let (tree, lang) = parse_ts(src);
        let imports = extract_imports(&tree, src.as_bytes(), &lang, false);
        assert_eq!(imports.len(), 1, "should find 1 import");
        let imp = &imports[0];
        assert_eq!(imp.kind, ImportKind::Esm);
        assert_eq!(imp.module_path, "react");
        assert_eq!(imp.specifiers.len(), 2, "should have 2 specifiers");
        let names: Vec<_> = imp.specifiers.iter().map(|s| s.name.as_str()).collect();
        assert!(names.contains(&"useState"), "missing useState");
        assert!(names.contains(&"useEffect"), "missing useEffect");
        assert!(
            imp.specifiers
                .iter()
                .all(|s| !s.is_default && !s.is_namespace)
        );
    }

    // Test 2: ESM default import
    #[test]
    fn test_esm_default_import() {
        let src = "import React from 'react';";
        let (tree, lang) = parse_ts(src);
        let imports = extract_imports(&tree, src.as_bytes(), &lang, false);
        assert_eq!(imports.len(), 1);
        let imp = &imports[0];
        assert_eq!(imp.kind, ImportKind::Esm);
        assert_eq!(imp.module_path, "react");
        assert_eq!(imp.specifiers.len(), 1);
        assert_eq!(imp.specifiers[0].name, "React");
        assert!(imp.specifiers[0].is_default);
        assert!(!imp.specifiers[0].is_namespace);
    }

    // Test 3: ESM namespace import
    #[test]
    fn test_esm_namespace_import() {
        let src = "import * as path from 'path';";
        let (tree, lang) = parse_ts(src);
        let imports = extract_imports(&tree, src.as_bytes(), &lang, false);
        assert_eq!(imports.len(), 1);
        let imp = &imports[0];
        assert_eq!(imp.kind, ImportKind::Esm);
        assert_eq!(imp.module_path, "path");
        assert_eq!(imp.specifiers.len(), 1);
        assert_eq!(imp.specifiers[0].name, "path");
        assert!(imp.specifiers[0].is_namespace);
        assert!(!imp.specifiers[0].is_default);
    }

    // Test 4: CJS require
    #[test]
    fn test_cjs_require() {
        let src = "const fs = require('fs');";
        let (tree, lang) = parse_js(src);
        let imports = extract_imports(&tree, src.as_bytes(), &lang, false);
        assert_eq!(imports.len(), 1, "should find 1 import");
        let imp = &imports[0];
        assert_eq!(imp.kind, ImportKind::Cjs);
        assert_eq!(imp.module_path, "fs");
    }

    // Test 5: Dynamic import
    #[test]
    fn test_dynamic_import() {
        let src = "const mod = await import('./lazy');";
        let (tree, lang) = parse_ts(src);
        let imports = extract_imports(&tree, src.as_bytes(), &lang, false);
        assert_eq!(imports.len(), 1, "should find 1 dynamic import");
        let imp = &imports[0];
        assert_eq!(imp.kind, ImportKind::DynamicImport);
        assert_eq!(imp.module_path, "./lazy");
    }

    // Test 6: Named export
    #[test]
    fn test_named_export() {
        let src = "export { foo, bar };";
        let (tree, lang) = parse_ts(src);
        let exports = extract_exports(&tree, src.as_bytes(), &lang, false);
        assert_eq!(exports.len(), 1, "should find 1 export");
        let exp = &exports[0];
        assert_eq!(exp.kind, ExportKind::Named);
        assert_eq!(exp.names.len(), 2, "should have 2 names");
        assert!(exp.names.contains(&"foo".to_string()));
        assert!(exp.names.contains(&"bar".to_string()));
        assert!(exp.source.is_none());
    }

    // Test 7: Default export
    #[test]
    fn test_default_export() {
        let src = "export default MyComponent;";
        let (tree, lang) = parse_ts(src);
        let exports = extract_exports(&tree, src.as_bytes(), &lang, false);
        assert_eq!(exports.len(), 1, "should find 1 export");
        let exp = &exports[0];
        assert_eq!(exp.kind, ExportKind::Default);
        assert!(exp.names.is_empty());
        assert!(exp.source.is_none());
    }

    // Test 8: Re-export
    #[test]
    fn test_reexport() {
        let src = "export { helper } from './utils';";
        let (tree, lang) = parse_ts(src);
        let exports = extract_exports(&tree, src.as_bytes(), &lang, false);
        assert_eq!(exports.len(), 1, "should find 1 re-export");
        let exp = &exports[0];
        assert_eq!(exp.kind, ExportKind::ReExport);
        assert!(exp.names.contains(&"helper".to_string()));
        assert_eq!(exp.source.as_deref(), Some("./utils"));
    }

    // Test 9: Re-export all
    #[test]
    fn test_reexport_all() {
        let src = "export * from './types';";
        let (tree, lang) = parse_ts(src);
        let exports = extract_exports(&tree, src.as_bytes(), &lang, false);
        assert_eq!(exports.len(), 1, "should find 1 re-export-all");
        let exp = &exports[0];
        assert_eq!(exp.kind, ExportKind::ReExportAll);
        assert!(exp.names.is_empty());
        assert_eq!(exp.source.as_deref(), Some("./types"));
    }

    #[test]
    fn test_appfile_imports() {
        let src = "import { useState } from 'react';\nimport * as path from 'path';\nconst fs = require('fs');";
        let (tree, lang) = parse_ts(src);
        let imports = extract_imports(&tree, src.as_bytes(), &lang, false);
        let summary = imports
            .iter()
            .map(|i| format!("{:?}:{}", i.kind, i.module_path))
            .collect::<Vec<_>>()
            .join(", ");
        assert_eq!(
            imports.len(),
            3,
            "Expected 3 imports, got {}: [{}]",
            imports.len(),
            summary
        );
    }

    // This test verifies that TSX processing does not contaminate TS import statics.
    #[test]
    fn test_tsx_then_ts_imports() {
        // Process TSX file first (initializes TSX statics)
        let tsx_src = "export const b = 2;";
        let (tsx_tree, tsx_lang) = parse_tsx(tsx_src);
        let tsx_imports = extract_imports(&tsx_tree, tsx_src.as_bytes(), &tsx_lang, true);
        assert_eq!(tsx_imports.len(), 0, "TSX file should have 0 imports");

        // Now process TS file — must use its own TS statics, not TSX statics
        let ts_src = "import { useState } from 'react';";
        let (ts_tree, ts_lang) = parse_ts(ts_src);
        let ts_imports = extract_imports(&ts_tree, ts_src.as_bytes(), &ts_lang, false);
        assert_eq!(
            ts_imports.len(),
            1,
            "TS file after TSX should still find 1 import"
        );
    }
}