Skip to main content

fallow_graph/graph/
types.rs

1//! Shared graph types: module nodes, re-export edges, export symbols, and references.
2
3use std::ops::Range;
4use std::path::PathBuf;
5
6use fallow_types::discover::FileId;
7use fallow_types::extract::ExportName;
8
9/// A single module in the graph.
10#[derive(Debug)]
11pub struct ModuleNode {
12    /// Unique identifier for this module.
13    pub file_id: FileId,
14    /// Absolute path to the module file.
15    pub path: PathBuf,
16    /// Range into the flat `edges` array.
17    pub edge_range: Range<usize>,
18    /// Exports declared by this module.
19    pub exports: Vec<ExportSymbol>,
20    /// Re-exports from this module (export { x } from './y', export * from './z').
21    pub re_exports: Vec<ReExportEdge>,
22    /// Whether this module is an entry point.
23    pub is_entry_point: bool,
24    /// Whether this module is reachable from any entry point.
25    pub is_reachable: bool,
26    /// Whether this module has CJS exports (module.exports / exports.*).
27    pub has_cjs_exports: bool,
28}
29
30/// A re-export edge, tracking which exports are forwarded from which module.
31#[derive(Debug)]
32pub struct ReExportEdge {
33    /// The module being re-exported from.
34    pub source_file: FileId,
35    /// The name imported from the source (or "*" for star re-exports).
36    pub imported_name: String,
37    /// The name exported from this module.
38    pub exported_name: String,
39    /// Whether this is a type-only re-export.
40    pub is_type_only: bool,
41}
42
43/// An export with reference tracking.
44#[derive(Debug)]
45pub struct ExportSymbol {
46    /// The exported name (named or default).
47    pub name: ExportName,
48    /// Whether this is a type-only export.
49    pub is_type_only: bool,
50    /// Source span of the export declaration.
51    pub span: oxc_span::Span,
52    /// Which files reference this export.
53    pub references: Vec<SymbolReference>,
54    /// Members of this export (enum members, class members).
55    pub members: Vec<fallow_types::extract::MemberInfo>,
56}
57
58/// A reference to an export from another file.
59#[derive(Debug, Clone)]
60pub struct SymbolReference {
61    /// The file that references this export.
62    pub from_file: FileId,
63    /// How the export is referenced.
64    pub kind: ReferenceKind,
65    /// Byte span of the import statement in the referencing file.
66    /// Used by the LSP to locate references for Code Lens navigation.
67    pub import_span: oxc_span::Span,
68}
69
70/// How an export is referenced.
71#[derive(Debug, Clone, PartialEq, Eq)]
72pub enum ReferenceKind {
73    /// A named import (`import { foo }`).
74    NamedImport,
75    /// A default import (`import Foo`).
76    DefaultImport,
77    /// A namespace import (`import * as ns`).
78    NamespaceImport,
79    /// A re-export (`export { foo } from './bar'`).
80    ReExport,
81    /// A dynamic import (`import('./foo')`).
82    DynamicImport,
83    /// A side-effect import (`import './styles'`).
84    SideEffectImport,
85}
86
87// Size assertions for types defined in this module.
88// `ExportSymbol` and `SymbolReference` are stored in Vecs per module node.
89// `ReExportEdge` is stored in a Vec per module for re-export chain resolution.
90#[cfg(target_pointer_width = "64")]
91const _: () = assert!(std::mem::size_of::<ExportSymbol>() == 88);
92#[cfg(target_pointer_width = "64")]
93const _: () = assert!(std::mem::size_of::<SymbolReference>() == 16);
94#[cfg(target_pointer_width = "64")]
95const _: () = assert!(std::mem::size_of::<ReExportEdge>() == 56);
96// `ModuleNode` is stored in a Vec — one per discovered file.
97#[cfg(target_pointer_width = "64")]
98const _: () = assert!(std::mem::size_of::<ModuleNode>() == 96);