Skip to main content

fallow_extract/cache/
types.rs

1//! Serialization types for the incremental parse cache.
2//!
3//! All types use bitcode `Encode`/`Decode` for fast binary serialization.
4
5use bitcode::{Decode, Encode};
6
7use crate::MemberKind;
8
9/// Cache version — bump when the cache format or cached extraction semantics change.
10pub(super) const CACHE_VERSION: u32 = 42;
11
12/// Maximum cache file size to deserialize (256 MB).
13pub(super) const MAX_CACHE_SIZE: usize = 256 * 1024 * 1024;
14
15/// Import kind discriminant for `CachedImport`:
16/// 0 = Named, 1 = Default, 2 = Namespace, 3 = `SideEffect`.
17pub(super) const IMPORT_KIND_NAMED: u8 = 0;
18pub(super) const IMPORT_KIND_DEFAULT: u8 = 1;
19pub(super) const IMPORT_KIND_NAMESPACE: u8 = 2;
20pub(super) const IMPORT_KIND_SIDE_EFFECT: u8 = 3;
21
22/// Cached data for a single module.
23#[derive(Debug, Clone, Encode, Decode)]
24pub struct CachedModule {
25    /// xxh3 hash of the file content.
26    pub content_hash: u64,
27    /// File modification time (seconds since epoch) for fast cache validation.
28    /// When mtime+size match the on-disk file, we skip reading file content entirely.
29    pub mtime_secs: u64,
30    /// File size in bytes for fast cache validation.
31    pub file_size: u64,
32    /// Exported symbols.
33    pub exports: Vec<CachedExport>,
34    /// Import specifiers.
35    pub imports: Vec<CachedImport>,
36    /// Re-export specifiers.
37    pub re_exports: Vec<CachedReExport>,
38    /// Dynamic import specifiers.
39    pub dynamic_imports: Vec<CachedDynamicImport>,
40    /// `require()` specifiers.
41    pub require_calls: Vec<CachedRequireCall>,
42    /// Static member accesses (e.g., `Status.Active`).
43    pub member_accesses: Vec<crate::MemberAccess>,
44    /// Identifiers used as whole objects (Object.values, for..in, spread, etc.).
45    pub whole_object_uses: Vec<String>,
46    /// Dynamic import patterns with partial static resolution.
47    pub dynamic_import_patterns: Vec<CachedDynamicImportPattern>,
48    /// Whether this module uses CJS exports.
49    pub has_cjs_exports: bool,
50    /// Local names of import bindings that are never referenced in this file.
51    pub unused_import_bindings: Vec<String>,
52    /// Inline suppression directives.
53    pub suppressions: Vec<CachedSuppression>,
54    /// Pre-computed line-start byte offsets for O(log N) byte-to-line/col conversion.
55    pub line_offsets: Vec<u32>,
56    /// Per-function complexity metrics.
57    pub complexity: Vec<fallow_types::extract::FunctionComplexity>,
58    /// Feature flag use sites.
59    pub flag_uses: Vec<fallow_types::extract::FlagUse>,
60    /// Heritage metadata for exported classes.
61    pub class_heritage: Vec<fallow_types::extract::ClassHeritageInfo>,
62}
63
64/// Cached suppression directive.
65#[derive(Debug, Clone, Encode, Decode)]
66pub struct CachedSuppression {
67    /// 1-based line this suppression applies to. 0 = file-wide.
68    pub line: u32,
69    /// 1-based line where the comment itself appears.
70    pub comment_line: u32,
71    /// 0 = suppress all, 1-19 = `IssueKind` discriminant.
72    pub kind: u8,
73}
74
75/// Cached export data for a single export declaration.
76#[derive(Debug, Clone, Encode, Decode)]
77pub struct CachedExport {
78    /// Export name (or "default" for default exports).
79    pub name: String,
80    /// Whether this is a default export.
81    pub is_default: bool,
82    /// Whether this is a type-only export.
83    pub is_type_only: bool,
84    /// Visibility tag discriminant (0=None, 1=Public, 2=Internal, 3=Beta, 4=Alpha).
85    pub visibility: u8,
86    /// The local binding name, if different.
87    pub local_name: Option<String>,
88    /// Byte offset of the export span start.
89    pub span_start: u32,
90    /// Byte offset of the export span end.
91    pub span_end: u32,
92    /// Members of this export (for enums and classes).
93    pub members: Vec<CachedMember>,
94    /// The local name of the parent class from `extends` clause, if any.
95    pub super_class: Option<String>,
96}
97
98/// Cached import data for a single import declaration.
99#[derive(Debug, Clone, Encode, Decode)]
100pub struct CachedImport {
101    /// The import specifier.
102    pub source: String,
103    /// For Named imports, the imported symbol name. Empty for other kinds.
104    pub imported_name: String,
105    /// The local binding name.
106    pub local_name: String,
107    /// Whether this is a type-only import.
108    pub is_type_only: bool,
109    /// Import kind: 0=Named, 1=Default, 2=Namespace, 3=SideEffect.
110    pub kind: u8,
111    /// Byte offset of the import span start.
112    pub span_start: u32,
113    /// Byte offset of the import span end.
114    pub span_end: u32,
115    /// Byte offset of the source string literal span start.
116    pub source_span_start: u32,
117    /// Byte offset of the source string literal span end.
118    pub source_span_end: u32,
119}
120
121/// Cached dynamic import data.
122#[derive(Debug, Clone, Encode, Decode)]
123pub struct CachedDynamicImport {
124    /// The import specifier.
125    pub source: String,
126    /// Byte offset of the span start.
127    pub span_start: u32,
128    /// Byte offset of the span end.
129    pub span_end: u32,
130    /// Names destructured from the import result.
131    pub destructured_names: Vec<String>,
132    /// Local variable name for namespace imports.
133    pub local_name: Option<String>,
134}
135
136/// Cached `require()` call data.
137#[derive(Debug, Clone, Encode, Decode)]
138pub struct CachedRequireCall {
139    /// The require specifier.
140    pub source: String,
141    /// Byte offset of the span start.
142    pub span_start: u32,
143    /// Byte offset of the span end.
144    pub span_end: u32,
145    /// Names destructured from the require result.
146    pub destructured_names: Vec<String>,
147    /// Local variable name for namespace requires.
148    pub local_name: Option<String>,
149}
150
151/// Cached re-export data.
152#[derive(Debug, Clone, Encode, Decode)]
153pub struct CachedReExport {
154    /// The module being re-exported from.
155    pub source: String,
156    /// Name imported from the source.
157    pub imported_name: String,
158    /// Name exported from this module.
159    pub exported_name: String,
160    /// Whether this is a type-only re-export.
161    pub is_type_only: bool,
162    /// Byte offset of the re-export span start (for line-number reporting).
163    pub span_start: u32,
164    /// Byte offset of the re-export span end.
165    pub span_end: u32,
166}
167
168/// Cached enum or class member data.
169#[derive(Debug, Clone, Encode, Decode)]
170pub struct CachedMember {
171    /// Member name.
172    pub name: String,
173    /// Member kind (enum, method, or property).
174    pub kind: MemberKind,
175    /// Byte offset of the span start.
176    pub span_start: u32,
177    /// Byte offset of the span end.
178    pub span_end: u32,
179    /// Whether this member has decorators.
180    pub has_decorator: bool,
181}
182
183/// Cached dynamic import pattern data (template literals, `import.meta.glob`).
184#[derive(Debug, Clone, Encode, Decode)]
185pub struct CachedDynamicImportPattern {
186    /// Static prefix of the import path.
187    pub prefix: String,
188    /// Static suffix, if any.
189    pub suffix: Option<String>,
190    /// Byte offset of the span start.
191    pub span_start: u32,
192    /// Byte offset of the span end.
193    pub span_end: u32,
194}