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 = 46;
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    /// Local import bindings referenced from type positions.
53    pub type_referenced_import_bindings: Vec<String>,
54    /// Local import bindings referenced from value positions.
55    pub value_referenced_import_bindings: Vec<String>,
56    /// Inline suppression directives.
57    pub suppressions: Vec<CachedSuppression>,
58    /// Pre-computed line-start byte offsets for O(log N) byte-to-line/col conversion.
59    pub line_offsets: Vec<u32>,
60    /// Per-function complexity metrics.
61    pub complexity: Vec<fallow_types::extract::FunctionComplexity>,
62    /// Feature flag use sites.
63    pub flag_uses: Vec<fallow_types::extract::FlagUse>,
64    /// Heritage metadata for exported classes.
65    pub class_heritage: Vec<fallow_types::extract::ClassHeritageInfo>,
66}
67
68/// Cached suppression directive.
69#[derive(Debug, Clone, Encode, Decode)]
70pub struct CachedSuppression {
71    /// 1-based line this suppression applies to. 0 = file-wide.
72    pub line: u32,
73    /// 1-based line where the comment itself appears.
74    pub comment_line: u32,
75    /// 0 = suppress all, 1-19 = `IssueKind` discriminant.
76    pub kind: u8,
77}
78
79/// Cached export data for a single export declaration.
80#[derive(Debug, Clone, Encode, Decode)]
81pub struct CachedExport {
82    /// Export name (or "default" for default exports).
83    pub name: String,
84    /// Whether this is a default export.
85    pub is_default: bool,
86    /// Whether this is a type-only export.
87    pub is_type_only: bool,
88    /// Visibility tag discriminant (0=None, 1=Public, 2=Internal, 3=Beta, 4=Alpha).
89    pub visibility: u8,
90    /// The local binding name, if different.
91    pub local_name: Option<String>,
92    /// Byte offset of the export span start.
93    pub span_start: u32,
94    /// Byte offset of the export span end.
95    pub span_end: u32,
96    /// Members of this export (for enums and classes).
97    pub members: Vec<CachedMember>,
98    /// The local name of the parent class from `extends` clause, if any.
99    pub super_class: Option<String>,
100}
101
102/// Cached import data for a single import declaration.
103#[derive(Debug, Clone, Encode, Decode)]
104pub struct CachedImport {
105    /// The import specifier.
106    pub source: String,
107    /// For Named imports, the imported symbol name. Empty for other kinds.
108    pub imported_name: String,
109    /// The local binding name.
110    pub local_name: String,
111    /// Whether this is a type-only import.
112    pub is_type_only: bool,
113    /// Import kind: 0=Named, 1=Default, 2=Namespace, 3=SideEffect.
114    pub kind: u8,
115    /// Byte offset of the import span start.
116    pub span_start: u32,
117    /// Byte offset of the import span end.
118    pub span_end: u32,
119    /// Byte offset of the source string literal span start.
120    pub source_span_start: u32,
121    /// Byte offset of the source string literal span end.
122    pub source_span_end: u32,
123}
124
125/// Cached dynamic import data.
126#[derive(Debug, Clone, Encode, Decode)]
127pub struct CachedDynamicImport {
128    /// The import specifier.
129    pub source: String,
130    /// Byte offset of the span start.
131    pub span_start: u32,
132    /// Byte offset of the span end.
133    pub span_end: u32,
134    /// Names destructured from the import result.
135    pub destructured_names: Vec<String>,
136    /// Local variable name for namespace imports.
137    pub local_name: Option<String>,
138}
139
140/// Cached `require()` call data.
141#[derive(Debug, Clone, Encode, Decode)]
142pub struct CachedRequireCall {
143    /// The require specifier.
144    pub source: String,
145    /// Byte offset of the span start.
146    pub span_start: u32,
147    /// Byte offset of the span end.
148    pub span_end: u32,
149    /// Names destructured from the require result.
150    pub destructured_names: Vec<String>,
151    /// Local variable name for namespace requires.
152    pub local_name: Option<String>,
153}
154
155/// Cached re-export data.
156#[derive(Debug, Clone, Encode, Decode)]
157pub struct CachedReExport {
158    /// The module being re-exported from.
159    pub source: String,
160    /// Name imported from the source.
161    pub imported_name: String,
162    /// Name exported from this module.
163    pub exported_name: String,
164    /// Whether this is a type-only re-export.
165    pub is_type_only: bool,
166    /// Byte offset of the re-export span start (for line-number reporting).
167    pub span_start: u32,
168    /// Byte offset of the re-export span end.
169    pub span_end: u32,
170}
171
172/// Cached enum or class member data.
173#[derive(Debug, Clone, Encode, Decode)]
174pub struct CachedMember {
175    /// Member name.
176    pub name: String,
177    /// Member kind (enum, method, or property).
178    pub kind: MemberKind,
179    /// Byte offset of the span start.
180    pub span_start: u32,
181    /// Byte offset of the span end.
182    pub span_end: u32,
183    /// Whether this member has decorators.
184    pub has_decorator: bool,
185}
186
187/// Cached dynamic import pattern data (template literals, `import.meta.glob`).
188#[derive(Debug, Clone, Encode, Decode)]
189pub struct CachedDynamicImportPattern {
190    /// Static prefix of the import path.
191    pub prefix: String,
192    /// Static suffix, if any.
193    pub suffix: Option<String>,
194    /// Byte offset of the span start.
195    pub span_start: u32,
196    /// Byte offset of the span end.
197    pub span_end: u32,
198}