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 = 51;
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    /// Whether this import originated from an SFC `<style>` block / `<style src>` (CSS context).
114    pub from_style: bool,
115    /// Import kind: 0=Named, 1=Default, 2=Namespace, 3=SideEffect.
116    pub kind: u8,
117    /// Byte offset of the import span start.
118    pub span_start: u32,
119    /// Byte offset of the import span end.
120    pub span_end: u32,
121    /// Byte offset of the source string literal span start.
122    pub source_span_start: u32,
123    /// Byte offset of the source string literal span end.
124    pub source_span_end: u32,
125}
126
127/// Cached dynamic import data.
128#[derive(Debug, Clone, Encode, Decode)]
129pub struct CachedDynamicImport {
130    /// The import specifier.
131    pub source: String,
132    /// Byte offset of the span start.
133    pub span_start: u32,
134    /// Byte offset of the span end.
135    pub span_end: u32,
136    /// Names destructured from the import result.
137    pub destructured_names: Vec<String>,
138    /// Local variable name for namespace imports.
139    pub local_name: Option<String>,
140}
141
142/// Cached `require()` call data.
143#[derive(Debug, Clone, Encode, Decode)]
144pub struct CachedRequireCall {
145    /// The require specifier.
146    pub source: String,
147    /// Byte offset of the span start.
148    pub span_start: u32,
149    /// Byte offset of the span end.
150    pub span_end: u32,
151    /// Names destructured from the require result.
152    pub destructured_names: Vec<String>,
153    /// Local variable name for namespace requires.
154    pub local_name: Option<String>,
155}
156
157/// Cached re-export data.
158#[derive(Debug, Clone, Encode, Decode)]
159pub struct CachedReExport {
160    /// The module being re-exported from.
161    pub source: String,
162    /// Name imported from the source.
163    pub imported_name: String,
164    /// Name exported from this module.
165    pub exported_name: String,
166    /// Whether this is a type-only re-export.
167    pub is_type_only: bool,
168    /// Byte offset of the re-export span start (for line-number reporting).
169    pub span_start: u32,
170    /// Byte offset of the re-export span end.
171    pub span_end: u32,
172}
173
174/// Cached enum or class member data.
175#[derive(Debug, Clone, Encode, Decode)]
176pub struct CachedMember {
177    /// Member name.
178    pub name: String,
179    /// Member kind (enum, method, or property).
180    pub kind: MemberKind,
181    /// Byte offset of the span start.
182    pub span_start: u32,
183    /// Byte offset of the span end.
184    pub span_end: u32,
185    /// Whether this member has decorators.
186    pub has_decorator: bool,
187}
188
189/// Cached dynamic import pattern data (template literals, `import.meta.glob`).
190#[derive(Debug, Clone, Encode, Decode)]
191pub struct CachedDynamicImportPattern {
192    /// Static prefix of the import path.
193    pub prefix: String,
194    /// Static suffix, if any.
195    pub suffix: Option<String>,
196    /// Byte offset of the span start.
197    pub span_start: u32,
198    /// Byte offset of the span end.
199    pub span_end: u32,
200}