code-graph-cli 3.0.2

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
use std::collections::HashMap;
use std::path::{Path, PathBuf};

use petgraph::Direction;
use petgraph::visit::EdgeRef;

use crate::graph::{
    CodeGraph,
    edge::EdgeKind,
    node::{FileKind, GraphNode, SymbolKind, SymbolVisibility},
};
use crate::query::find::kind_to_str;

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

/// Dependency role of a file in the project.
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub enum FileRole {
    EntryPoint,
    LibraryRoot,
    Test,
    Config,
    Types,
    Utility,
}

/// Graph topology label for a file.
#[derive(Debug, Clone, PartialEq, serde::Serialize)]
pub enum GraphLabel {
    Hub,    // >= 5 importers
    Leaf,   // 0 importers
    Bridge, // 2+ importers AND 3+ imports
}

/// An exported symbol from a file.
#[derive(Debug, Clone, serde::Serialize)]
pub struct ExportedSymbol {
    pub name: String,
    pub kind: String, // "fn", "struct", etc.
}

/// Summary information for a single file.
#[derive(Debug, Clone, serde::Serialize)]
pub struct FileSummary {
    pub relative_path: String,
    pub role: FileRole,
    pub line_count: usize,
    pub symbol_count: usize,
    /// Breakdown of all symbols by kind string (e.g. "fn" -> 3, "struct" -> 1).
    pub symbol_kinds: HashMap<String, usize>,
    pub exports: Vec<ExportedSymbol>,
    pub import_count: usize,   // outgoing import edges
    pub importer_count: usize, // incoming import edges
    pub graph_label: Option<GraphLabel>,
}

// ---------------------------------------------------------------------------
// Role detection helpers
// ---------------------------------------------------------------------------

/// Detect the dependency role of a file based on its name, path, and symbols.
fn detect_role(
    file_info: &crate::graph::node::FileInfo,
    root: &Path,
    outgoing_reexport_count: usize,
    symbols: &[crate::graph::node::SymbolInfo],
) -> FileRole {
    let path = &file_info.path;

    // Config/Ci files
    match file_info.kind {
        FileKind::Config | FileKind::Ci => return FileRole::Config,
        _ => {}
    }

    let file_name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");

    let path_str = path.to_string_lossy();

    // Test file detection
    let is_test = file_name.contains("test")
        || file_name.contains("spec")
        || path_str.contains("/tests/")
        || path_str.contains("/__tests__/")
        || file_name.ends_with("_test.rs")
        || path_str.contains("\\tests\\")
        || path_str.contains("\\_tests_\\");

    if is_test {
        return FileRole::Test;
    }

    // Entry point detection: common entry point file names near the root
    let entry_point_names = [
        "main.rs", "main.ts", "main.js", "index.ts", "index.js", "app.ts", "app.js",
    ];
    if entry_point_names.contains(&file_name) {
        // Check depth from root: count components between root and the file's parent dir
        if let Ok(rel) = path.strip_prefix(root) {
            let depth = rel.components().count();
            // depth=1 means directly in root, depth=2 means one level deep (e.g. src/main.rs)
            if depth <= 2 {
                return FileRole::EntryPoint;
            }
        }
    }

    // Library root detection
    let lib_root_names = ["lib.rs", "mod.rs"];
    if lib_root_names.contains(&file_name) {
        return FileRole::LibraryRoot;
    }

    // Many re-exports => library root
    if outgoing_reexport_count >= 3 {
        return FileRole::LibraryRoot;
    }

    // Types file detection: >= 60% of symbols are type-defining kinds
    if !symbols.is_empty() {
        let type_kinds = [
            SymbolKind::TypeAlias,
            SymbolKind::Interface,
            SymbolKind::Struct,
            SymbolKind::Enum,
            SymbolKind::Trait,
        ];
        let type_count = symbols
            .iter()
            .filter(|s| type_kinds.contains(&s.kind))
            .count();
        let fn_kinds = [
            SymbolKind::Function,
            SymbolKind::ImplMethod,
            SymbolKind::Method,
        ];
        let fn_count = symbols
            .iter()
            .filter(|s| fn_kinds.contains(&s.kind))
            .count();
        if type_count > 0 && fn_count == 0 && type_count * 100 / symbols.len() >= 60 {
            return FileRole::Types;
        }
    }

    FileRole::Utility
}

/// Determine graph label based on importer and import counts.
fn compute_graph_label(importer_count: usize, import_count: usize) -> Option<GraphLabel> {
    if importer_count >= 5 {
        Some(GraphLabel::Hub)
    } else if importer_count == 0 {
        Some(GraphLabel::Leaf)
    } else if importer_count >= 2 && import_count >= 3 {
        Some(GraphLabel::Bridge)
    } else {
        None
    }
}

/// Count lines in a file by counting `\n` bytes.
fn count_lines(path: &Path) -> usize {
    match std::fs::read(path) {
        Ok(bytes) => bytes.iter().filter(|&&b| b == b'\n').count(),
        Err(_) => 0,
    }
}

// ---------------------------------------------------------------------------
// Main query function
// ---------------------------------------------------------------------------

/// Build a summary for a single file in the graph.
///
/// Returns `Err` if the file path is not found in the graph.
pub fn file_summary(
    graph: &CodeGraph,
    root: &Path,
    file_path: &Path,
) -> Result<FileSummary, String> {
    // Resolve path: relative paths are joined to root.
    let abs_path: PathBuf = if file_path.is_absolute() {
        file_path.to_path_buf()
    } else {
        root.join(file_path)
    };

    let file_idx = graph
        .file_index
        .get(&abs_path)
        .copied()
        .ok_or_else(|| format!("File not found: {}", file_path.display()))?;

    // Get FileInfo
    let file_info = match &graph.graph[file_idx] {
        GraphNode::File(fi) => fi.clone(),
        _ => {
            return Err(format!(
                "Node at path is not a File: {}",
                file_path.display()
            ));
        }
    };

    // Collect all symbols via Contains edges (top-level symbols only)
    let all_symbols: Vec<crate::graph::node::SymbolInfo> = graph
        .graph
        .edges(file_idx)
        .filter_map(|edge_ref| {
            if let EdgeKind::Contains = edge_ref.weight()
                && let GraphNode::Symbol(ref sym) = graph.graph[edge_ref.target()]
            {
                return Some(sym.clone());
            }
            None
        })
        .collect();

    let symbol_count = all_symbols.len();

    // Build symbol kind breakdown map
    let mut symbol_kinds: HashMap<String, usize> = HashMap::new();
    for sym in &all_symbols {
        *symbol_kinds
            .entry(kind_to_str(&sym.kind).to_string())
            .or_insert(0) += 1;
    }

    // Filter exported symbols:
    // - For TS/JS: is_exported == true
    // - For Rust: visibility is Pub or PubCrate
    let is_rust = file_info.language == "rust";
    let exports: Vec<ExportedSymbol> = all_symbols
        .iter()
        .filter(|sym| {
            if is_rust {
                sym.visibility == SymbolVisibility::Pub
                    || sym.visibility == SymbolVisibility::PubCrate
            } else {
                sym.is_exported
            }
        })
        .map(|sym| ExportedSymbol {
            name: sym.name.clone(),
            kind: kind_to_str(&sym.kind).to_string(),
        })
        .collect();

    // Count outgoing import edges (ResolvedImport, RustImport, ReExport, BarrelReExportAll)
    let mut import_count: usize = 0;
    let mut reexport_count: usize = 0;
    for edge_ref in graph.graph.edges(file_idx) {
        match edge_ref.weight() {
            EdgeKind::ResolvedImport { .. } | EdgeKind::RustImport { .. } => {
                import_count += 1;
            }
            EdgeKind::ReExport { .. } => {
                import_count += 1;
                reexport_count += 1;
            }
            EdgeKind::BarrelReExportAll => {
                import_count += 1;
                reexport_count += 1;
            }
            _ => {}
        }
    }

    // Count incoming import edges (files that import this file)
    let importer_count: usize = graph
        .graph
        .edges_directed(file_idx, Direction::Incoming)
        .filter(|edge_ref| {
            matches!(
                edge_ref.weight(),
                EdgeKind::ResolvedImport { .. } | EdgeKind::BarrelReExportAll
            )
        })
        .count();

    // Graph label
    let graph_label = compute_graph_label(importer_count, import_count);

    // Role detection
    let role = detect_role(&file_info, root, reexport_count, &all_symbols);

    // Line count
    let line_count = count_lines(&abs_path);

    // Compute relative path for display
    let relative_path = abs_path
        .strip_prefix(root)
        .map(|p| p.to_string_lossy().into_owned())
        .unwrap_or_else(|_| abs_path.to_string_lossy().into_owned());

    Ok(FileSummary {
        relative_path,
        role,
        line_count,
        symbol_count,
        symbol_kinds,
        exports,
        import_count,
        importer_count,
        graph_label,
    })
}

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

#[cfg(test)]
mod tests {
    use std::io::Write;
    use std::path::PathBuf;

    use super::*;
    use crate::graph::{
        CodeGraph,
        edge::EdgeKind,
        node::{SymbolInfo, SymbolKind, SymbolVisibility},
    };

    fn make_symbol(
        name: &str,
        kind: SymbolKind,
        vis: SymbolVisibility,
        exported: bool,
    ) -> SymbolInfo {
        SymbolInfo {
            name: name.into(),
            kind,
            line: 1,
            is_exported: exported,
            visibility: vis,
            ..Default::default()
        }
    }

    #[test]
    fn test_entry_point_detection() {
        let mut graph = CodeGraph::new();
        let root = PathBuf::from("/tmp/test_project");
        // main.rs at depth 2 (src/main.rs)
        let file_path = root.join("src/main.rs");
        graph.add_file(file_path.clone(), "rust");

        let summary = file_summary(&graph, &root, &file_path).unwrap();
        assert_eq!(
            summary.role,
            FileRole::EntryPoint,
            "main.rs at src/ depth should be EntryPoint"
        );
    }

    #[test]
    fn test_test_file_detection() {
        let mut graph = CodeGraph::new();
        let root = PathBuf::from("/tmp/test_project");

        // File in tests/ directory
        let file_path = root.join("tests/integration_test.rs");
        graph.add_file(file_path.clone(), "rust");
        let summary = file_summary(&graph, &root, &file_path).unwrap();
        assert_eq!(
            summary.role,
            FileRole::Test,
            "File in tests/ should be Test"
        );
    }

    #[test]
    fn test_test_file_detection_by_name() {
        let mut graph = CodeGraph::new();
        let root = PathBuf::from("/tmp/test_project");

        // File with "test" in name
        let file_path = root.join("src/test_utils.rs");
        graph.add_file(file_path.clone(), "rust");
        let summary = file_summary(&graph, &root, &file_path).unwrap();
        assert_eq!(
            summary.role,
            FileRole::Test,
            "File with 'test' in name should be Test"
        );
    }

    #[test]
    fn test_library_root_detection() {
        let mut graph = CodeGraph::new();
        let root = PathBuf::from("/tmp/test_project");

        let file_path = root.join("src/lib.rs");
        graph.add_file(file_path.clone(), "rust");
        let summary = file_summary(&graph, &root, &file_path).unwrap();
        assert_eq!(
            summary.role,
            FileRole::LibraryRoot,
            "lib.rs should be LibraryRoot"
        );
    }

    #[test]
    fn test_types_file_detection() {
        let mut graph = CodeGraph::new();
        let root = PathBuf::from("/tmp/test_project");

        let file_path = root.join("src/types.rs");
        let file_idx = graph.add_file(file_path.clone(), "rust");

        // Add 3 struct symbols (all type-defining) and 0 functions
        graph.add_symbol(
            file_idx,
            make_symbol("TypeA", SymbolKind::Struct, SymbolVisibility::Pub, false),
        );
        graph.add_symbol(
            file_idx,
            make_symbol("TypeB", SymbolKind::Struct, SymbolVisibility::Pub, false),
        );
        graph.add_symbol(
            file_idx,
            make_symbol("TypeC", SymbolKind::Enum, SymbolVisibility::Pub, false),
        );

        let summary = file_summary(&graph, &root, &file_path).unwrap();
        assert_eq!(
            summary.role,
            FileRole::Types,
            "File with 100% type symbols and 0 functions should be Types"
        );
    }

    #[test]
    fn test_utility_default() {
        let mut graph = CodeGraph::new();
        let root = PathBuf::from("/tmp/test_project");

        // A regular file that doesn't match any specific role
        let file_path = root.join("src/helpers.rs");
        let file_idx = graph.add_file(file_path.clone(), "rust");
        // Add some function symbols (not all types)
        graph.add_symbol(
            file_idx,
            make_symbol(
                "helper_fn",
                SymbolKind::Function,
                SymbolVisibility::Pub,
                false,
            ),
        );

        let summary = file_summary(&graph, &root, &file_path).unwrap();
        assert_eq!(
            summary.role,
            FileRole::Utility,
            "Regular file with function symbols should default to Utility"
        );
    }

    #[test]
    fn test_hub_label() {
        let mut graph = CodeGraph::new();
        let root = PathBuf::from("/tmp/test_project");

        let file_path = root.join("src/central.rs");
        let hub_idx = graph.add_file(file_path.clone(), "rust");

        // Add 5 files that import from hub (incoming ResolvedImport edges)
        for i in 0..5 {
            let importer_path = root.join(format!("src/importer{}.rs", i));
            let importer_idx = graph.add_file(importer_path, "rust");
            graph.graph.add_edge(
                importer_idx,
                hub_idx,
                EdgeKind::ResolvedImport {
                    specifier: "./central".into(),
                },
            );
        }

        let summary = file_summary(&graph, &root, &file_path).unwrap();
        assert_eq!(
            summary.graph_label,
            Some(GraphLabel::Hub),
            "File with 5+ importers should be Hub"
        );
    }

    #[test]
    fn test_leaf_label() {
        let mut graph = CodeGraph::new();
        let root = PathBuf::from("/tmp/test_project");

        // A file with no incoming imports
        let file_path = root.join("src/leaf.rs");
        graph.add_file(file_path.clone(), "rust");

        let summary = file_summary(&graph, &root, &file_path).unwrap();
        assert_eq!(
            summary.graph_label,
            Some(GraphLabel::Leaf),
            "File with 0 importers should be Leaf"
        );
    }

    #[test]
    fn test_bridge_label() {
        let mut graph = CodeGraph::new();
        let root = PathBuf::from("/tmp/test_project");

        let file_path = root.join("src/bridge.rs");
        let bridge_idx = graph.add_file(file_path.clone(), "rust");

        // 2 incoming importers
        for i in 0..2 {
            let importer_path = root.join(format!("src/importer{}.rs", i));
            let importer_idx = graph.add_file(importer_path, "rust");
            graph.graph.add_edge(
                importer_idx,
                bridge_idx,
                EdgeKind::ResolvedImport {
                    specifier: "./bridge".into(),
                },
            );
        }

        // 3 outgoing imports
        for i in 0..3 {
            let dep_path = root.join(format!("src/dep{}.rs", i));
            let dep_idx = graph.add_file(dep_path, "rust");
            graph.graph.add_edge(
                bridge_idx,
                dep_idx,
                EdgeKind::ResolvedImport {
                    specifier: format!("./dep{}", i),
                },
            );
        }

        let summary = file_summary(&graph, &root, &file_path).unwrap();
        assert_eq!(
            summary.graph_label,
            Some(GraphLabel::Bridge),
            "File with 2 importers and 3 imports should be Bridge"
        );
    }

    #[test]
    fn test_exports_not_truncated() {
        let mut graph = CodeGraph::new();
        let root = PathBuf::from("/tmp/test_project");

        let file_path = root.join("src/big_exports.ts");
        let file_idx = graph.add_file(file_path.clone(), "typescript");

        // Add 20 exported symbols
        for i in 0..20 {
            graph.add_symbol(
                file_idx,
                make_symbol(
                    &format!("ExportedFn{}", i),
                    SymbolKind::Function,
                    SymbolVisibility::Private, // TS uses is_exported
                    true,                      // is_exported = true
                ),
            );
        }

        let summary = file_summary(&graph, &root, &file_path).unwrap();
        assert_eq!(
            summary.exports.len(),
            20,
            "All 20 exports should be listed (no truncation)"
        );
    }

    #[test]
    fn test_line_count() {
        // Create a temp file with known number of lines
        let mut tmp = tempfile::NamedTempFile::new().expect("tempfile");
        // Write 10 lines
        for i in 0..10 {
            writeln!(tmp, "line {}", i).unwrap();
        }
        let tmp_path = tmp.path().to_path_buf();

        let mut graph = CodeGraph::new();
        // Use the temp dir as "root" — but we'll call file_summary with absolute path
        let root = PathBuf::from("/tmp");
        graph.add_file(tmp_path.clone(), "rust");

        let summary = file_summary(&graph, &root, &tmp_path).unwrap();
        assert_eq!(
            summary.line_count, 10,
            "Should count 10 lines in the temp file"
        );
    }
}